62 lines
1.4 KiB
GDScript
62 lines
1.4 KiB
GDScript
extends Node2D
|
|
|
|
const TILE_SIZE : int = 16
|
|
|
|
const MOVE_TIME : float = 0.2
|
|
|
|
@onready var MoveTimer : Timer = $MoveTimer
|
|
|
|
@onready var MoveRay : RayCast2D = $MoveRay
|
|
|
|
var direction : Vector2i
|
|
|
|
var last_direction : Vector2i
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
pass # Replace with function body.
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
direction = Vector2i(0,0)
|
|
|
|
if Input.is_action_pressed("ui_left"):
|
|
direction.x -= 1
|
|
if Input.is_action_just_pressed("ui_left"):
|
|
last_direction = Vector2i(-1,0)
|
|
move()
|
|
if Input.is_action_pressed("ui_right"):
|
|
direction.x += 1
|
|
if Input.is_action_just_pressed("ui_right"):
|
|
last_direction = Vector2i(1,0)
|
|
move()
|
|
if Input.is_action_pressed("ui_up"):
|
|
direction.y -= 1
|
|
if Input.is_action_just_pressed("ui_up"):
|
|
last_direction = Vector2i(0,-1)
|
|
move()
|
|
if Input.is_action_pressed("ui_down"):
|
|
direction.y += 1
|
|
if Input.is_action_just_pressed("ui_down"):
|
|
last_direction = Vector2i(0,1)
|
|
move()
|
|
|
|
if MoveTimer.is_stopped():
|
|
move()
|
|
|
|
func move() -> void:
|
|
if last_direction.x:
|
|
direction.y = 0
|
|
if last_direction.y:
|
|
direction.x = 0
|
|
|
|
MoveRay.target_position = Vector2(TILE_SIZE * direction)
|
|
MoveRay.force_raycast_update()
|
|
|
|
if not MoveRay.is_colliding():
|
|
position += Vector2(TILE_SIZE * direction)
|
|
|
|
|
|
MoveTimer.start(MOVE_TIME)
|