113 lines
2.8 KiB
GDScript
113 lines
2.8 KiB
GDScript
extends Entity
|
|
|
|
@export var GRAVITY = 30
|
|
@export var JUMP_FORCE = 500
|
|
|
|
@onready var animated_sprite = $AnimatedSprite2D
|
|
@onready var attack_timer = $AttackTimer
|
|
@onready var collider = $CollisionShape2D
|
|
@onready var punch_hitbox = $PunchHitbox
|
|
@onready var death_timer = $DeathTimer
|
|
|
|
var jumping = false
|
|
var facing_right = true
|
|
var right_punch = false
|
|
var hurting = false
|
|
var is_dying = false
|
|
|
|
const ATTACK_DAMAGE = 25
|
|
const INITIAL_HEALTH = 100
|
|
const SPEED_MULTIPLIER = 300
|
|
const ATTACK_KNOCKBACK = 5000
|
|
|
|
func _init() -> void:
|
|
super._init(INITIAL_HEALTH, SPEED_MULTIPLIER, ATTACK_DAMAGE)
|
|
|
|
func _ready():
|
|
animated_sprite.play("idle")
|
|
punch_hitbox.body_entered.connect(punch_connect)
|
|
punch_hitbox.visible = false
|
|
|
|
func _physics_process(delta):
|
|
if is_on_floor():
|
|
# allow the player to jump
|
|
if jumping:
|
|
jumping = false
|
|
|
|
if Input.is_action_just_pressed("jump"):
|
|
velocity.y = -JUMP_FORCE
|
|
jumping = true
|
|
animated_sprite.play("jump")
|
|
else:
|
|
# apply gravity
|
|
velocity.y += GRAVITY
|
|
if velocity.y > 1000:
|
|
velocity.y = 1000
|
|
|
|
# perform horizontal movement
|
|
var horizontal_direction = Input.get_axis("move_left", "move_right")
|
|
velocity.x = speed * horizontal_direction
|
|
move_and_slide()
|
|
|
|
# ensure the player faces the correct direction
|
|
if horizontal_direction > 0 && !facing_right:
|
|
animated_sprite.flip_h = false
|
|
punch_hitbox.rotate(PI)
|
|
elif horizontal_direction < 0 && facing_right:
|
|
animated_sprite.flip_h = true
|
|
punch_hitbox.rotate(PI)
|
|
|
|
if horizontal_direction != 0:
|
|
facing_right = horizontal_direction > 0
|
|
|
|
# figure out which animation should be played
|
|
# respecting the jump animation above all
|
|
if !jumping and !hurting and attack_timer.is_stopped():
|
|
if is_on_floor() && horizontal_direction != 0:
|
|
animated_sprite.play("walk")
|
|
else:
|
|
animated_sprite.play("idle")
|
|
|
|
super._physics_process(delta)
|
|
|
|
|
|
punch_hitbox.visible = true if !attack_timer.is_stopped() else false
|
|
|
|
func punch_connect(node: Node):
|
|
if node is Entity and node != self and !attack_timer.is_stopped():
|
|
node.health -= damage
|
|
node.take_knockback(ATTACK_KNOCKBACK)
|
|
if node.has_method("hurt_anim"):
|
|
node.call("hurt_anim")
|
|
|
|
func hurt_anim():
|
|
hurting = true
|
|
animated_sprite.play("hurt")
|
|
animated_sprite.animation_finished.connect(disable_hurt)
|
|
|
|
func disable_hurt():
|
|
hurting = false
|
|
animated_sprite.animation_finished.disconnect(disable_hurt)
|
|
|
|
func _process(delta: float) -> void:
|
|
if health <= 0:
|
|
is_dying = true
|
|
death_timer.start()
|
|
health = 99999
|
|
|
|
if is_dying:
|
|
animated_sprite.play("death")
|
|
punch_hitbox.visible = false
|
|
speed = 0
|
|
|
|
if death_timer.is_stopped():
|
|
queue_free()
|
|
|
|
if attack_timer.is_stopped() and Input.is_action_just_pressed("attack"):
|
|
attack_timer.start()
|
|
|
|
if is_on_floor():
|
|
animated_sprite.play("right" if right_punch else "left")
|
|
right_punch = !right_punch
|
|
else:
|
|
animated_sprite.play("slam")
|