game1: make flat fishes work

This commit is contained in:
Alice Gaudon 2020-08-18 17:45:22 +02:00
parent d58119d498
commit 862907f15b
3 changed files with 94 additions and 19 deletions

View File

@ -99,6 +99,13 @@ down={
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
]
}
interact={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777221,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
]
}
[rendering]

View File

@ -7,14 +7,34 @@ const s_h = preload('res://images/game1/flatfish_s_h.tres')
const t = preload('res://images/game1/flatfish_t.tres')
const t_h = preload('res://images/game1/flatfish_t_h.tres')
var type: int
var appeared = false
var half_rotation = false
func _init(_type : int):
type = _type
if type == FlatfishType.S:
texture = s_h
else:
texture = t_h
func _init():
update_texture()
enum FlatfishType {S, T}
func update_texture():
if appeared:
if half_rotation:
texture = t
else:
texture = s
else:
if half_rotation:
texture = t_h
else:
texture = s_h
func appear():
appeared = true
update_texture()
func do_rotation():
half_rotation = !half_rotation
if half_rotation:
rotation_degrees += 90
update_texture()
func disappear():
appeared = false
update_texture()

View File

@ -28,7 +28,7 @@ func launch_game():
# Prepare player
var level = levels[current_level]
level.add_child_below_node(level.get_node("Props"), player)
level.add_child_below_node(level.get_node("Entities"), player)
playing = true
player_camera.make_current()
player.connect('frame_changed', self, 'update_camera')
@ -40,7 +40,12 @@ func launch_game():
func spawn_entities():
for node in levels:
var entities = Node2D.new()
entities.name = 'Entities'
node.add_child_below_node(node.get_node('Props'), entities)
for layer in node.get_children():
if !(layer is TileMap): continue
var tm = layer as TileMap
var tileset = tm.tile_set
@ -49,11 +54,8 @@ func spawn_entities():
for tile in ['flatfish_s', 'flatfish_s_hidden', 'flatfish_t', 'flatfish_t_hidden']:
cells += tm.get_used_cells_by_id(tileset.find_tile_by_name(tile))
var entities = Node.new()
node.add_child_below_node(node, entities)
for cell in cells:
var fish = Flatfish.new(Flatfish.FlatfishType.S)
var fish = Flatfish.new()
fish.position = cell * 16 + Vector2(8, 8)
var t = tm.is_cell_transposed(cell.x, cell.y)
var x = tm.is_cell_x_flipped(cell.x, cell.y)
@ -70,6 +72,7 @@ func spawn_entities():
var go_right = false
var go_left = false
var do_interact = false
func _input(event):
if playing:
@ -77,15 +80,20 @@ func _input(event):
go_right = true
if event.is_action_pressed('left'):
go_left = true
move()
if event.is_action_pressed('interact'):
do_interact = true
move()
interact()
if event.is_action_released('right'):
go_right = false
if event.is_action_released('left'):
go_left = false
if event.is_action_released('interact'):
do_interact = false
func move():
if playing && !is_camera_moving:
if playing && !is_camera_moving && !is_interacting:
if go_right && !go_left:
move_right()
elif go_left && !go_right:
@ -100,14 +108,14 @@ func move_right():
player.play('default')
player.frame = 0
player.position.x += 16
offset_player(16)
is_camera_moving = false
move()
func move_left():
is_camera_moving = true
player.position.x -= 16
offset_player(-16)
player.play('moving', true)
update_camera()
@ -119,10 +127,50 @@ func move_left():
move()
func offset_player(offset):
var r = int(ceil(player.rotation_degrees / 90.0)) % 4
if r == 0: player.position.x += offset
elif r == 1: player.position.y += offset
elif r == 2: player.position.x -= offset
elif r == 3: player.position.y -= offset
print(player.position / 16)
const animation_steps = [3, 6, 10, 13]
var is_camera_moving = false
func update_camera():
if is_camera_moving:
if is_camera_moving && !is_interacting:
player_camera.position = Vector2(animation_steps[player.frame], 0)
else:
player_camera.position = Vector2()
var is_interacting = false
func interact():
if !playing || is_camera_moving || is_interacting || !do_interact: return
is_interacting = true
var level = levels[current_level]
var entities = level.get_node('Entities').get_children()
var fish = null
for e in entities:
if e is Flatfish && e.position == player.position:
fish = e
break
if fish != null:
fish.appear()
yield(get_tree().create_timer(0.2), "timeout")
fish.do_rotation()
player.rotation_degrees += 45
yield(get_tree().create_timer(0.2), "timeout")
fish.do_rotation()
player.rotation_degrees += 45
yield(get_tree().create_timer(0.2), "timeout")
fish.disappear()
is_interacting = false
if fish != null: interact()