59 lines
1.4 KiB
GDScript
59 lines
1.4 KiB
GDScript
extends Node2D
|
|
|
|
const TILE_SIZE : int = 16
|
|
|
|
const MOVE_TIME : float = 0.2
|
|
|
|
enum {UP,LEFT,DOWN,RIGHT,NONE = -1}
|
|
|
|
@onready var DirectionCast : Array[RayCast2D] = [$UpCast,$LeftCast,$DownCast,$RightCast]
|
|
|
|
@onready var MoveTimer : Timer = $MoveTimer
|
|
|
|
var direction : int
|
|
|
|
# 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 = NONE
|
|
|
|
if Input.is_action_pressed("ui_left"):
|
|
direction = LEFT
|
|
if Input.is_action_just_pressed("ui_left"):
|
|
move()
|
|
|
|
if Input.is_action_pressed("ui_right"):
|
|
direction = RIGHT
|
|
if Input.is_action_just_pressed("ui_right"):
|
|
move()
|
|
|
|
if Input.is_action_pressed("ui_up"):
|
|
direction = UP
|
|
if Input.is_action_just_pressed("ui_up"):
|
|
move()
|
|
|
|
if Input.is_action_pressed("ui_down"):
|
|
direction = DOWN
|
|
if Input.is_action_just_pressed("ui_down"):
|
|
move()
|
|
|
|
if MoveTimer.is_stopped():
|
|
move()
|
|
|
|
|
|
func move() -> void:
|
|
if direction == LEFT and not DirectionCast[LEFT].is_colliding():
|
|
position.x -= TILE_SIZE
|
|
if direction == RIGHT and not DirectionCast[RIGHT].is_colliding():
|
|
position.x += TILE_SIZE
|
|
if direction == UP and not DirectionCast[UP].is_colliding():
|
|
position.y -= TILE_SIZE
|
|
if direction == DOWN and not DirectionCast[DOWN].is_colliding():
|
|
position.y += TILE_SIZE
|
|
|
|
MoveTimer.start(MOVE_TIME)
|