crabs-game/scripts/game1/Game1Script.gd

187 lines
4.5 KiB
GDScript3
Raw Normal View History

2020-08-17 12:15:41 +02:00
extends Node
2020-08-17 17:27:28 +02:00
var playing = false
onready var player = preload('res://scenes/game1/Player.tscn').instance()
onready var player_camera = player.get_node("Camera2D")
onready var levels = [
$Game/Level1,
$Game/Level2,
]
var current_level = 0
2020-08-17 17:27:28 +02:00
2020-08-17 12:15:41 +02:00
func _ready():
launch_game()
2020-08-17 17:27:28 +02:00
$Opening1.play('default')
yield($Opening1, 'animation_finished')
$Opening1.hide()
func launch_game():
# Prepare levels
spawn_entities()
# Hide opening
2020-08-17 17:27:28 +02:00
$Opening2.hide()
# Prepare player
var level = levels[current_level]
2020-08-18 17:45:22 +02:00
level.add_child_below_node(level.get_node("Entities"), player)
2020-08-17 17:27:28 +02:00
playing = true
player_camera.make_current()
player.connect('frame_changed', self, 'update_camera')
# Spawn player
var spawn_pos = Vector2()
#todo: get spawn pos from map
player.position = spawn_pos + Vector2(8, 8)
func spawn_entities():
for node in levels:
2020-08-18 17:45:22 +02:00
var entities = Node2D.new()
entities.name = 'Entities'
node.add_child_below_node(node.get_node('Props'), entities)
for layer in node.get_children():
2020-08-18 17:45:22 +02:00
if !(layer is TileMap): continue
var tm = layer as TileMap
var tileset = tm.tile_set
# Flatfish cells
var cells = []
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))
for cell in cells:
2020-08-18 17:45:22 +02:00
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)
var y = tm.is_cell_y_flipped(cell.x, cell.y)
if t && x:
fish.rotation_degrees = 90
elif x && y:
fish.rotation_degrees = 180
elif t && y:
fish.rotation_degrees = 270
entities.add_child(fish)
tm.set_cell(cell.x, cell.y, -1)
var go_right = false
var go_left = false
2020-08-18 17:45:22 +02:00
var do_interact = false
2020-08-17 17:27:28 +02:00
func _input(event):
if playing:
2020-08-17 17:27:28 +02:00
if event.is_action_pressed('right'):
go_right = true
2020-08-17 17:27:28 +02:00
if event.is_action_pressed('left'):
go_left = true
2020-08-18 17:45:22 +02:00
if event.is_action_pressed('interact'):
do_interact = true
if event.is_action_released('right'):
go_right = false
if event.is_action_released('left'):
go_left = false
2020-08-18 17:45:22 +02:00
if event.is_action_released('interact'):
do_interact = false
2020-08-18 18:49:16 +02:00
var trying_to_move = false
func move():
2020-08-18 18:49:16 +02:00
if !playing || trying_to_move || is_interacting || is_camera_moving: return
trying_to_move = true
yield(get_tree(), "physics_frame")
yield(get_tree(), "physics_frame")
if go_right && !go_left && can_move_towards('Right'):
move_right()
elif go_left && !go_right && can_move_towards('Left'):
move_left()
trying_to_move = false
func _process(delta):
interact()
move()
func can_move_towards(sensorName : String) -> bool:
var sensor = player.get_node("Sensors/" + sensorName) as Area2D
return sensor.get_overlapping_bodies().size() == 0 && sensor.get_overlapping_areas().size() == 0
2020-08-17 17:27:28 +02:00
func move_right():
is_camera_moving = true
player.play('moving')
update_camera()
yield(player, 'animation_finished')
player.play('default')
player.frame = 0
2020-08-18 17:45:22 +02:00
offset_player(16)
is_camera_moving = false
move()
2020-08-17 17:27:28 +02:00
func move_left():
is_camera_moving = true
2020-08-18 17:45:22 +02:00
offset_player(-16)
2020-08-17 17:27:28 +02:00
player.play('moving', true)
update_camera()
yield(player, 'animation_finished')
player.play('default')
player.frame = 0
is_camera_moving = false
move()
2020-08-17 17:27:28 +02:00
2020-08-18 17:45:22 +02:00
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
2020-08-18 18:49:16 +02:00
print((player.position - Vector2(8, 8)) / 16)
2020-08-18 17:45:22 +02:00
2020-08-17 17:27:28 +02:00
const animation_steps = [3, 6, 10, 13]
var is_camera_moving = false
func update_camera():
2020-08-18 17:45:22 +02:00
if is_camera_moving && !is_interacting:
2020-08-17 17:27:28 +02:00
player_camera.position = Vector2(animation_steps[player.frame], 0)
else:
player_camera.position = Vector2()
2020-08-18 17:45:22 +02:00
var is_interacting = false
func interact():
2020-08-18 18:49:16 +02:00
if !playing || !do_interact || is_interacting || trying_to_move || is_camera_moving: return
2020-08-18 17:45:22 +02:00
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()