88 lines
2.2 KiB
GDScript
88 lines
2.2 KiB
GDScript
class_name Player extends CharacterBody2D
|
|
|
|
const DIR_4 = [
|
|
Vector2.RIGHT,
|
|
Vector2.DOWN,
|
|
Vector2.LEFT,
|
|
Vector2.UP,
|
|
]
|
|
|
|
signal direction_change(new_direction: Vector2)
|
|
signal player_damaged(hurtbox: Hurtbox)
|
|
|
|
@onready var animation_player : AnimationPlayer = $AnimationPlayer
|
|
@onready var sprite : Sprite2D = $Sprite2D
|
|
@onready var state_machine: PlayerStateMachine = $state_machine
|
|
@onready var hitbox: Hitbox = $Hitbox
|
|
@onready var effect_animation: AnimationPlayer = $effect_animation
|
|
|
|
@export_group("Stats")
|
|
@export var hp: int = 6
|
|
@export var maxHp: int = 6
|
|
|
|
var cardinal_direction : Vector2 = Vector2.DOWN
|
|
var direction : Vector2 = Vector2.ZERO
|
|
var invlunerable: bool = false
|
|
|
|
func _ready() -> void:
|
|
PlayerManager.player = self
|
|
state_machine.initialize(self)
|
|
hitbox.damaged.connect(on_damage)
|
|
update_hp(99)
|
|
|
|
func _process(_delta: float) -> void:
|
|
direction = Vector2(
|
|
Input.get_axis("left", "right"),
|
|
Input.get_axis("up", "down"),
|
|
).normalized()
|
|
|
|
func _physics_process(_delta: float) -> void:
|
|
move_and_slide()
|
|
|
|
func set_direction() -> bool:
|
|
if direction == Vector2.ZERO:
|
|
return false
|
|
|
|
var new_direction = DIR_4[int(round((direction + cardinal_direction * 0.1).angle() / TAU * DIR_4.size()))]
|
|
|
|
if new_direction == cardinal_direction:
|
|
return false
|
|
cardinal_direction = new_direction
|
|
direction_change.emit(new_direction)
|
|
sprite.scale.x = -1 if cardinal_direction == Vector2.LEFT else 1
|
|
return true
|
|
|
|
func update_animation(state: String) -> void:
|
|
animation_player.play(state + "_" + anim_direction())
|
|
|
|
func anim_direction() -> String:
|
|
var direction_result : String = "side"
|
|
if cardinal_direction == Vector2.DOWN:
|
|
direction_result = "down"
|
|
elif cardinal_direction == Vector2.UP:
|
|
direction_result = "up"
|
|
return direction_result
|
|
|
|
func on_damage(hurtbox: Hurtbox) -> void:
|
|
if invlunerable:
|
|
return
|
|
update_hp(-hurtbox.damage)
|
|
if hp > 0:
|
|
player_damaged.emit(hurtbox)
|
|
else:
|
|
player_damaged.emit(hurtbox)
|
|
update_hp(99)
|
|
|
|
func update_hp(more_hp: int) -> void:
|
|
hp = clampi(hp + more_hp, 0, maxHp)
|
|
Hud.update_hp(hp, maxHp)
|
|
|
|
func make_invulnerable(duration: float = 1.0) -> void:
|
|
invlunerable = true
|
|
hitbox.monitoring = false
|
|
|
|
await get_tree().create_timer(duration).timeout
|
|
invlunerable = false
|
|
hitbox.monitoring = true
|
|
|