add movement deadzone constant and prepare player script for hitting state

This commit is contained in:
yuki 2025-11-14 22:06:58 -03:00
parent 1f5b08e39f
commit 8379f06e24
2 changed files with 3 additions and 34 deletions

View file

@ -1,13 +1,13 @@
class_name Player extends CharacterBody2D
const DEADZONE: float = 0.1
@export var id: int = 1
@export var max_speed: float = 90
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var state_machine: StateMachine = $StateMachine
var is_hitting: bool = false
var was_moving: bool = false
var anim_dir: String
var def_dir: String
@ -20,34 +20,3 @@ 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)
func hit(dir: String) -> void:
if not is_hitting:
sprite.play('up' if id == 1 else 'down')
sprite.frame = 1
sprite.pause()
var sprite_texture: Texture2D = sprite.sprite_frames.get_frame_texture('up', 1)
var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
# flip entire node horizontally if spawning left
# flip vertically if player 2
hit_node.scale = Vector2(
-1 if dir == "left" else 1,
1 if id == 1 else -1
)
add_child(hit_node)
# set position according to frame width and height
@warning_ignore("integer_division")
hit_node.global_position = global_position + Vector2(
(sprite_texture.get_width()/2)*(-1 if dir == "left" else 1),
(sprite_texture.get_height()*-1) if id == 1 else 4
)
hit_node.timer.connect("timeout", _on_hit_end)
is_hitting = true
func _on_hit_end() -> void:
is_hitting = false

View file

@ -16,7 +16,7 @@ func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
func _state_physics_update(_delta: float) -> void:
var movement_vector: Vector2 = player.get_movement_vector()
if movement_vector.length() >= 1:
if movement_vector.length() > player.DEADZONE:
finished.emit(RUNNING)
return
elif Input.is_action_pressed("hit_left"):