2021-04-24 21:35:17 +02:00
|
|
|
tool
|
|
|
|
extends Area2D
|
|
|
|
|
|
|
|
|
2021-04-24 22:28:55 +02:00
|
|
|
export var speed : float = 1
|
2021-04-25 16:16:48 +02:00
|
|
|
export var speed_dash : float = 3
|
2021-04-24 22:28:55 +02:00
|
|
|
export var rest : float = 1
|
2021-04-25 16:29:54 +02:00
|
|
|
export var dashes : int = 1
|
2021-04-24 21:35:17 +02:00
|
|
|
|
|
|
|
export var tile_position := Vector2(0,0)
|
2021-04-25 14:03:43 +02:00
|
|
|
export(NodePath) var level_tilemap
|
2021-04-24 21:35:17 +02:00
|
|
|
|
2021-04-25 14:03:43 +02:00
|
|
|
onready var level = get_node(level_tilemap) as Level
|
2021-04-24 21:35:17 +02:00
|
|
|
|
2021-04-24 22:28:55 +02:00
|
|
|
var last_move_time := 0.0
|
2021-04-25 02:22:07 +02:00
|
|
|
var step_count := 0
|
|
|
|
var stepped := false
|
2021-04-25 16:16:48 +02:00
|
|
|
var dashing := false
|
2021-04-24 22:28:55 +02:00
|
|
|
|
2021-04-24 21:35:17 +02:00
|
|
|
class Movement:
|
|
|
|
var action: String
|
|
|
|
var direction: Vector2
|
|
|
|
var animation: String
|
|
|
|
|
|
|
|
func _init(_action, _direction, _animation):
|
|
|
|
action = _action
|
|
|
|
direction = _direction
|
|
|
|
animation = _animation
|
|
|
|
|
|
|
|
var movements = [
|
|
|
|
Movement.new("ui_right", Vector2.RIGHT, "right"),
|
|
|
|
Movement.new("ui_left", Vector2.LEFT, "left"),
|
|
|
|
Movement.new("ui_up", Vector2.UP, "back"),
|
|
|
|
Movement.new("ui_down", Vector2.DOWN, "front")
|
|
|
|
]
|
|
|
|
|
|
|
|
var movement_queue = []
|
|
|
|
var current_target = null
|
2021-04-25 16:16:48 +02:00
|
|
|
var current_direction : Movement = movements.back()
|
2021-04-24 21:35:17 +02:00
|
|
|
onready var sprite = $AnimatedSprite as AnimatedSprite
|
|
|
|
|
|
|
|
func _process(delta):
|
|
|
|
|
2021-04-25 01:55:01 +02:00
|
|
|
|
2021-04-24 21:35:17 +02:00
|
|
|
|
|
|
|
if Engine.editor_hint:
|
|
|
|
|
|
|
|
|
2021-04-25 14:03:43 +02:00
|
|
|
if level == null and not level_tilemap == "":
|
2021-04-24 21:35:17 +02:00
|
|
|
|
2021-04-25 14:03:43 +02:00
|
|
|
level = get_node(level_tilemap) as Level
|
2021-04-24 21:35:17 +02:00
|
|
|
|
2021-04-25 14:03:43 +02:00
|
|
|
if level != null:
|
2021-04-24 21:35:17 +02:00
|
|
|
|
|
|
|
tile_position.x = round(tile_position.x)
|
|
|
|
tile_position.y = round(tile_position.y)
|
|
|
|
|
|
|
|
update_position()
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
2021-04-24 22:28:55 +02:00
|
|
|
if (last_move_time + rest) * 1000 < OS.get_ticks_msec():
|
|
|
|
for movement in movements:
|
|
|
|
if Input.is_action_pressed(movement.action):
|
2021-04-25 16:16:48 +02:00
|
|
|
current_direction = movement
|
|
|
|
if not Input.is_action_pressed("lock_move"):
|
2021-04-25 14:03:43 +02:00
|
|
|
var target = level.get_tile_v(tile_position + movement.direction)
|
2021-04-25 14:53:16 +02:00
|
|
|
if target.can_walk() or target.can_push(movement.direction):
|
2021-04-25 02:37:12 +02:00
|
|
|
movement_queue.push_back(movement)
|
2021-04-25 03:13:21 +02:00
|
|
|
|
2021-04-24 22:28:55 +02:00
|
|
|
|
2021-04-24 21:35:17 +02:00
|
|
|
update_animation()
|
|
|
|
|
|
|
|
if not movement_queue.empty():
|
|
|
|
|
|
|
|
var current_movement : Movement = movement_queue.front()
|
2021-04-25 14:53:16 +02:00
|
|
|
|
2021-04-24 21:35:17 +02:00
|
|
|
if current_target == null:
|
|
|
|
current_target = tile_position + current_movement.direction
|
2021-04-25 14:53:16 +02:00
|
|
|
var tile = level.get_tile_v(current_target)
|
|
|
|
if not tile.can_walk() and not tile.can_push(current_movement.direction):
|
|
|
|
current_target = null
|
|
|
|
movement_queue.pop_front()
|
|
|
|
else:
|
|
|
|
tile.push(current_movement.direction)
|
2021-04-24 21:35:17 +02:00
|
|
|
|
2021-04-25 16:16:48 +02:00
|
|
|
if current_target != null:
|
|
|
|
tile_position += current_movement.direction * delta * (speed_dash if dashing else speed)
|
2021-04-25 14:53:16 +02:00
|
|
|
|
|
|
|
var remaining : Vector2 = current_target - tile_position
|
|
|
|
|
|
|
|
if remaining.dot(current_movement.direction) <= 0:
|
|
|
|
tile_position = current_target
|
|
|
|
current_target = null
|
|
|
|
movement_queue.pop_front()
|
|
|
|
stepped = false
|
2021-04-25 16:16:48 +02:00
|
|
|
dashing = false
|
2021-04-25 14:53:16 +02:00
|
|
|
elif remaining.length() < 0.5 and not stepped:
|
|
|
|
step_count += 1
|
|
|
|
stepped = true
|
|
|
|
|
|
|
|
update_position()
|
|
|
|
last_move_time = OS.get_ticks_msec() / 1000.0
|
2021-04-24 21:35:17 +02:00
|
|
|
|
2021-04-25 16:29:54 +02:00
|
|
|
if dashes > 0 and Input.is_action_just_pressed("dash"):
|
2021-04-25 16:16:48 +02:00
|
|
|
|
|
|
|
if not current_target == null:
|
|
|
|
tile_position = current_target
|
|
|
|
|
|
|
|
var dash_end :Level.Tile = level.get_tile_v(tile_position).dash(current_direction.direction)
|
|
|
|
if dash_end:
|
|
|
|
movement_queue = [current_direction]
|
|
|
|
current_target = Vector2 (dash_end.x, dash_end.y)
|
|
|
|
dashing = true
|
2021-04-25 16:29:54 +02:00
|
|
|
dashes = dashes - 1
|
2021-04-24 21:35:17 +02:00
|
|
|
|
|
|
|
func update_position():
|
2021-04-25 14:03:43 +02:00
|
|
|
position = level.cell_custom_transform * (tile_position + Vector2.ONE / 2 )
|
2021-04-24 21:35:17 +02:00
|
|
|
|
|
|
|
func update_animation():
|
2021-04-25 16:16:48 +02:00
|
|
|
sprite.animation = current_direction.animation
|
2021-04-25 16:29:54 +02:00
|
|
|
sprite.frame = step_count % 2 + (0 if dashes > 0 else 2)
|