change movement to 4 directional

This commit is contained in:
yuki 2025-11-22 15:53:59 -03:00
parent a939ea4837
commit ef7df7ea89
3 changed files with 33 additions and 9 deletions

View file

@ -8,8 +8,31 @@ const DEADZONE = 0.1
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var state_machine: StateMachine = $StateMachine
var h_press_tick: int = 0 ## last time horizontal axis was pressed
var v_press_tick: int = 0 ## last time vertical axis was pressed
func _input(event: InputEvent) -> void:
if event.is_action_pressed("move_left") or event.is_action_pressed("move_right"):
h_press_tick = Time.get_ticks_msec()
if event.is_action_pressed("move_up") or event.is_action_pressed("move_down"):
v_press_tick = Time.get_ticks_msec()
func get_movement_vector() -> Vector2:
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
return Vector2(x_mov, y_mov)
var x_strength: float = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
var y_strength: float = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
if abs(x_strength) < DEADZONE and abs(y_strength) < DEADZONE:
return Vector2.ZERO
if abs(x_strength) < DEADZONE:
return Vector2(0, signf(y_strength))
if abs(y_strength) < DEADZONE:
return Vector2(signf(x_strength), 0)
# use most recent axis press
if h_press_tick > v_press_tick:
return Vector2(signf(x_strength), 0)
else:
return Vector2(0, signf(y_strength))

View file

@ -6,11 +6,10 @@ var next_state: StringName
func _enter(_previous_state_path: String, _data: Dictionary[StringName, Variant] = {}) -> void:
print('entering idle')
player.velocity = Vector2.ZERO
player.move_and_slide()
# TODO: animations
player.sprite.play("idle")
print('entered')

View file

@ -10,13 +10,15 @@ func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
func _state_physics_update(_delta: float) -> void:
var movement_vector: Vector2 = player.get_movement_vector()
var direction: Vector2 = movement_vector.normalized()
var direction: Vector2 = player.get_movement_vector()
direction = direction.normalized()
player.velocity = direction * player.max_speed
player.move_and_slide()
if watch_state([WALKING]): return
# TODO: animations
#player.sprite.play(player.anim_dir)
if direction.y > 0:
player.sprite.play("walk_down")
if direction.y < 0:
player.sprite.play("walk_up")