smashball/scenes/player/player.gd
2025-11-13 23:28:32 -03:00

60 lines
1.5 KiB
GDScript

extends CharacterBody2D
@export var player: int = 1
@export var max_speed: float = 150
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
var is_hitting: bool = false
var was_moving: bool = false
var anim_dir: String = 'down'
func _ready() -> void:
sprite.play('down')
sprite.frame = 1
sprite.pause()
func _physics_process(_delta: float) -> void:
var movement_vector: Vector2 = get_movement_vector()
var direction: Vector2 = movement_vector.normalized() if not is_hitting else Vector2.ZERO
velocity = direction * max_speed
move_and_slide()
var is_moving: bool = velocity.length() > 10
if Input.is_action_pressed("hit_left") and not is_hitting:
if not is_hitting:
sprite.play('up')
sprite.frame = 1
sprite.pause()
var hit: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
add_child(hit)
hit.timer.connect("timeout", _on_hit_end)
is_hitting = true
if is_moving:
if abs(velocity.x) > abs(velocity.y):
anim_dir = 'right' if velocity.x > 0 else 'left'
else:
anim_dir = 'down' if velocity.y > 0 else 'up'
sprite.play(anim_dir)
else:
if was_moving and not is_hitting:
sprite.play('down')
sprite.frame = 1
sprite.pause()
was_moving = is_moving
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 _on_hit_end() -> void:
is_hitting = false