82 lines
1.8 KiB
GDScript
82 lines
1.8 KiB
GDScript
tool
|
|
extends Area2D
|
|
|
|
|
|
export var speed = 1
|
|
|
|
export var tile_position := Vector2(0,0)
|
|
export(NodePath) var grid_path
|
|
|
|
onready var grid = get_node(grid_path) as TileMap
|
|
|
|
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
|
|
onready var sprite = $AnimatedSprite as AnimatedSprite
|
|
|
|
func _process(delta):
|
|
|
|
|
|
if Engine.editor_hint:
|
|
|
|
|
|
if grid == null and not grid_path == "":
|
|
|
|
grid = get_node(grid_path) as TileMap
|
|
|
|
if grid != null:
|
|
|
|
tile_position.x = round(tile_position.x)
|
|
tile_position.y = round(tile_position.y)
|
|
|
|
update_position()
|
|
|
|
else:
|
|
|
|
for movement in movements:
|
|
if Input.is_action_just_pressed(movement.action):
|
|
movement_queue.push_back(movement)
|
|
|
|
update_animation()
|
|
|
|
if not movement_queue.empty():
|
|
|
|
var current_movement : Movement = movement_queue.front()
|
|
|
|
if current_target == null:
|
|
current_target = tile_position + current_movement.direction
|
|
|
|
tile_position += current_movement.direction * delta * speed
|
|
|
|
var remaining = current_target - tile_position
|
|
|
|
if remaining.dot(current_movement.direction) <= 0:
|
|
tile_position = current_target
|
|
current_target = null
|
|
movement_queue.pop_front()
|
|
|
|
update_position()
|
|
|
|
|
|
func update_position():
|
|
position = grid.cell_custom_transform * (tile_position + Vector2.ONE / 2 )
|
|
|
|
func update_animation():
|
|
sprite.animation = sprite.animation if movement_queue.empty() else movement_queue.front().animation
|