2024-09-15 23:25:12 +03:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
2024-09-16 00:32:56 +03:00
|
|
|
@export var speed: float = 40
|
2024-09-15 23:25:12 +03:00
|
|
|
|
|
|
|
@onready var raycast: RayCast2D = $RayCast2D
|
|
|
|
|
2024-09-16 00:32:56 +03:00
|
|
|
const TILE_SIZE = 8
|
|
|
|
|
|
|
|
var is_moving = false
|
|
|
|
var input_dir: Vector2
|
|
|
|
var prev_input: Vector2
|
|
|
|
|
2024-09-15 23:25:12 +03:00
|
|
|
func _ready():
|
|
|
|
pass
|
|
|
|
|
2024-09-16 00:32:56 +03:00
|
|
|
func _process(delta):
|
|
|
|
_try_interact()
|
|
|
|
pass
|
|
|
|
|
|
|
|
func _physics_process(delta):
|
|
|
|
_try_move()
|
|
|
|
|
|
|
|
|
|
|
|
func _try_move():
|
|
|
|
input_dir = Vector2.ZERO
|
2024-09-15 23:25:12 +03:00
|
|
|
if Input.is_action_pressed("move_up"):
|
2024-09-16 00:32:56 +03:00
|
|
|
input_dir = Vector2.UP
|
2024-09-15 23:25:12 +03:00
|
|
|
elif Input.is_action_pressed("move_right"):
|
2024-09-16 00:32:56 +03:00
|
|
|
input_dir = Vector2.RIGHT
|
2024-09-15 23:25:12 +03:00
|
|
|
elif Input.is_action_pressed("move_down"):
|
2024-09-16 00:32:56 +03:00
|
|
|
input_dir = Vector2.DOWN
|
2024-09-15 23:25:12 +03:00
|
|
|
elif Input.is_action_pressed("move_left"):
|
2024-09-16 00:32:56 +03:00
|
|
|
input_dir = Vector2.LEFT
|
2024-09-15 23:25:12 +03:00
|
|
|
|
2024-09-16 00:32:56 +03:00
|
|
|
if input_dir and !is_moving:
|
|
|
|
raycast.target_position = input_dir * 8
|
|
|
|
|
|
|
|
if input_dir != prev_input:
|
|
|
|
prev_input = input_dir
|
|
|
|
return
|
|
|
|
|
|
|
|
var collision = raycast.get_collider()
|
|
|
|
if raycast.is_colliding():
|
|
|
|
return
|
|
|
|
|
|
|
|
is_moving = true
|
|
|
|
prev_input = input_dir
|
|
|
|
var tween = create_tween()
|
|
|
|
tween.tween_property(self, "position", position + input_dir * TILE_SIZE, 0.25)
|
|
|
|
tween.tween_callback(func():
|
|
|
|
is_moving = false
|
|
|
|
)
|
2024-09-15 23:25:12 +03:00
|
|
|
|
|
|
|
func _try_interact():
|
|
|
|
if Input.is_action_just_pressed("attack") and raycast.is_colliding():
|
|
|
|
var collision = raycast.get_collider()
|
|
|
|
if collision is PuzzleElement:
|
|
|
|
(collision as PuzzleElement).interact()
|
|
|
|
pass
|
|
|
|
pass
|