Compare commits

..

2 commits

2 changed files with 28 additions and 37 deletions

View file

@ -10,21 +10,9 @@ script = ExtResource("1_44r7b")
texture = ExtResource("1_5amrr") texture = ExtResource("1_5amrr")
centered = false centered = false
[node name="UpCast" type="RayCast2D" parent="."] [node name="MoveRay" type="RayCast2D" parent="."]
position = Vector2(8, 8) position = Vector2(8, 8)
target_position = Vector2(0, -16) target_position = Vector2(0, 0)
[node name="LeftCast" type="RayCast2D" parent="."]
position = Vector2(8, 8)
target_position = Vector2(-16, 0)
[node name="DownCast" type="RayCast2D" parent="."]
position = Vector2(8, 8)
target_position = Vector2(0, 16)
[node name="RightCast" type="RayCast2D" parent="."]
position = Vector2(8, 8)
target_position = Vector2(16, 0)
[node name="MoveTimer" type="Timer" parent="."] [node name="MoveTimer" type="Timer" parent="."]
one_shot = true one_shot = true

View file

@ -4,13 +4,13 @@ const TILE_SIZE : int = 16
const MOVE_TIME : float = 0.2 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 @onready var MoveTimer : Timer = $MoveTimer
var direction : int @onready var MoveRay : RayCast2D = $MoveRay
var direction : Vector2i
var last_direction : Vector2i
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
@ -19,40 +19,43 @@ func _ready() -> void:
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void: func _process(delta: float) -> void:
direction = NONE direction = Vector2i(0,0)
if Input.is_action_pressed("ui_left"): if Input.is_action_pressed("ui_left"):
direction = LEFT direction.x -= 1
if Input.is_action_just_pressed("ui_left"): if Input.is_action_just_pressed("ui_left"):
last_direction = Vector2i(-1,0)
move() move()
if Input.is_action_pressed("ui_right"): if Input.is_action_pressed("ui_right"):
direction = RIGHT direction.x += 1
if Input.is_action_just_pressed("ui_right"): if Input.is_action_just_pressed("ui_right"):
last_direction = Vector2i(1,0)
move() move()
if Input.is_action_pressed("ui_up"): if Input.is_action_pressed("ui_up"):
direction = UP direction.y -= 1
if Input.is_action_just_pressed("ui_up"): if Input.is_action_just_pressed("ui_up"):
last_direction = Vector2i(0,-1)
move() move()
if Input.is_action_pressed("ui_down"): if Input.is_action_pressed("ui_down"):
direction = DOWN direction.y += 1
if Input.is_action_just_pressed("ui_down"): if Input.is_action_just_pressed("ui_down"):
last_direction = Vector2i(0,1)
move() move()
if MoveTimer.is_stopped(): if MoveTimer.is_stopped():
move() move()
func move() -> void: func move() -> void:
if direction == LEFT and not DirectionCast[LEFT].is_colliding(): if last_direction.x:
position.x -= TILE_SIZE direction.y = 0
if direction == RIGHT and not DirectionCast[RIGHT].is_colliding(): if last_direction.y:
position.x += TILE_SIZE direction.x = 0
if direction == UP and not DirectionCast[UP].is_colliding():
position.y -= TILE_SIZE MoveRay.target_position = Vector2(TILE_SIZE * direction)
if direction == DOWN and not DirectionCast[DOWN].is_colliding(): MoveRay.force_raycast_update()
position.y += TILE_SIZE
if not MoveRay.is_colliding():
position += Vector2(TILE_SIZE * direction)
MoveTimer.start(MOVE_TIME) MoveTimer.start(MOVE_TIME)