Smooth cell to cell player movement
parent
169fec61af
commit
395e16374a
|
@ -1,28 +1,55 @@
|
||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
@export var speed: float = 400
|
@export var speed: float = 40
|
||||||
|
|
||||||
@onready var raycast: RayCast2D = $RayCast2D
|
@onready var raycast: RayCast2D = $RayCast2D
|
||||||
|
|
||||||
|
const TILE_SIZE = 8
|
||||||
|
|
||||||
|
var is_moving = false
|
||||||
|
var input_dir: Vector2
|
||||||
|
var prev_input: Vector2
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func _process_movement():
|
func _process(delta):
|
||||||
velocity = Vector2.ZERO
|
_try_interact()
|
||||||
if Input.is_action_pressed("move_up"):
|
pass
|
||||||
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 _physics_process(delta):
|
||||||
|
_try_move()
|
||||||
|
|
||||||
|
|
||||||
|
func _try_move():
|
||||||
|
input_dir = Vector2.ZERO
|
||||||
|
if Input.is_action_pressed("move_up"):
|
||||||
|
input_dir = Vector2.UP
|
||||||
|
elif Input.is_action_pressed("move_right"):
|
||||||
|
input_dir = Vector2.RIGHT
|
||||||
|
elif Input.is_action_pressed("move_down"):
|
||||||
|
input_dir = Vector2.DOWN
|
||||||
|
elif Input.is_action_pressed("move_left"):
|
||||||
|
input_dir = Vector2.LEFT
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
func _try_interact():
|
func _try_interact():
|
||||||
if Input.is_action_just_pressed("attack") and raycast.is_colliding():
|
if Input.is_action_just_pressed("attack") and raycast.is_colliding():
|
||||||
|
@ -31,10 +58,3 @@ func _try_interact():
|
||||||
(collision as PuzzleElement).interact()
|
(collision as PuzzleElement).interact()
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func _process(delta):
|
|
||||||
_try_interact()
|
|
||||||
pass
|
|
||||||
|
|
||||||
func _physics_process(delta):
|
|
||||||
_process_movement()
|
|
||||||
|
|
|
@ -48,7 +48,6 @@ size = Vector2(8, 8)
|
||||||
z_index = 1
|
z_index = 1
|
||||||
collision_mask = 3
|
collision_mask = 3
|
||||||
script = ExtResource("1_jrd75")
|
script = ExtResource("1_jrd75")
|
||||||
speed = 40.0
|
|
||||||
|
|
||||||
[node name="Camera2D" type="Camera2D" parent="."]
|
[node name="Camera2D" type="Camera2D" parent="."]
|
||||||
|
|
||||||
|
@ -68,4 +67,4 @@ shape = SubResource("RectangleShape2D_l7fhd")
|
||||||
|
|
||||||
[node name="RayCast2D" type="RayCast2D" parent="."]
|
[node name="RayCast2D" type="RayCast2D" parent="."]
|
||||||
target_position = Vector2(0, 8)
|
target_position = Vector2(0, 8)
|
||||||
collision_mask = 2
|
collision_mask = 3
|
||||||
|
|
Reference in New Issue