41 lines
891 B
GDScript
41 lines
891 B
GDScript
|
extends CharacterBody2D
|
||
|
|
||
|
@export var speed: float = 400
|
||
|
|
||
|
@onready var raycast: RayCast2D = $RayCast2D
|
||
|
|
||
|
func _ready():
|
||
|
pass
|
||
|
|
||
|
func _process_movement():
|
||
|
velocity = Vector2.ZERO
|
||
|
if Input.is_action_pressed("move_up"):
|
||
|
velocity.y -= 1
|
||
|
elif Input.is_action_pressed("move_right"):
|
||
|
velocity.x += 1
|
||
|
elif Input.is_action_pressed("move_down"):
|
||
|
velocity.y += 1
|
||
|
elif Input.is_action_pressed("move_left"):
|
||
|
velocity.x -= 1
|
||
|
|
||
|
if velocity.length() > 0:
|
||
|
velocity = velocity.normalized() * speed
|
||
|
raycast.target_position = velocity.normalized() * 8
|
||
|
|
||
|
move_and_slide()
|
||
|
|
||
|
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
|
||
|
|
||
|
func _process(delta):
|
||
|
_try_interact()
|
||
|
pass
|
||
|
|
||
|
func _physics_process(delta):
|
||
|
_process_movement()
|