extends CharacterBody2D
# Punching Variables
enum State {IDLE, MOVING, PUNCHING}
var state = State.IDLE
var punch_key = ""
var punch_damage = 10
var stamina_cost = 5
var bodies_in_hitbox = []
var hitbox_collision_shape
#UI
var healthbar: TextureProgressBar
var staminabar: TextureProgressBar
#run
var stamina_drain_rate = 10
# Movement
const walk_speed = 80
const run_speed = 200
var current_speed = walk_speed
var current_dir = "none"
var last_dir = "down"
var animated_sprite
var running = false
var hitbox
# Attributes
var maxHealth = 100
var currentHealth = maxHealth
var minHealth = 0
var maxStamina = 100
var currentStamina = maxStamina
var minStamina = 0
func _ready():
animated_sprite = get_node("AnimatedSprite2D")
hitbox = get_node("player_hitbox")
hitbox_collision_shape = hitbox.get_node("CollisionShape2D")
$healthbar.max_value = maxHealth
currentHealth = maxHealth
$healthbar.value = currentHealth
$staminabar.max_value = maxStamina
currentStamina = maxStamina
$staminabar.value = currentStamina
func _physics_process(delta):
stamina_drain(delta)
update_running_state() # Move this line here
if state != State.PUNCHING:
player_movement(delta)
move_and_slide()
else:
velocity = Vector2.ZERO
# Punching controls
if Input.is_action_just_pressed("left_punch") and state != State.PUNCHING:
state = State.PUNCHING
punch_key = "lpunch"
perform_punch()
if Input.is_action_just_pressed("right_punch") and state != State.PUNCHING:
state = State.PUNCHING
punch_key = "rpunch"
perform_punch()
func _input(event):
if event.is_action_pressed("run"):
running = !running
update_running_state()
func player_movement(_delta):
velocity.x = 0
velocity.y = 0
if running:
current_speed = run_speed
else:
current_speed = walk_speed
if Input.is_action_pressed("ui_right"):
current_dir = "right"
velocity.x += current_speed
if Input.is_action_pressed("ui_left"):
current_dir = "left"
velocity.x -= current_speed
if Input.is_action_pressed("ui_up"):
current_dir = "up"
velocity.y -= current_speed
if Input.is_action_pressed("ui_down"):
current_dir = "down"
velocity.y += current_speed
if current_dir != "none":
last_dir = current_dir
velocity = velocity.normalized() * current_speed
play_anim(velocity)
func update_running_state():
if running and currentStamina > 20:
current_speed = run_speed
else:
running = false
current_speed = walk_speed
func stamina_drain(delta):
if running and not velocity.is_equal_approx(Vector2.ZERO):
currentStamina = max(minStamina, currentStamina - stamina_drain_rate * delta)
else:
currentStamina = min(maxStamina, currentStamina + stamina_drain_rate * delta)
update_stamina_bar()
func update_health_bar():
$healthbar.value = currentHealth
func update_stamina_bar():
$staminabar.value = currentStamina
func update_hitbox_position(direction):
match direction:
"right":
hitbox_collision_shape.position = Vector2(6.25, -14)
"left":
hitbox_collision_shape.position = Vector2(-6, -14)
"up":
hitbox_collision_shape.position = Vector2(0, -22)
"down":
hitbox_collision_shape.position = Vector2(0, -9)
func perform_punch():
if currentStamina > 0:
play_punch_anim()
await get_tree().create_timer(0.5).timeout # Adjust the duration of the punch animation as needed
for body in bodies_in_hitbox:
if body.has_method("take_damage"):
body.take_damage(punch_damage, global_position) # Pass the player's global_position
currentStamina -= stamina_cost
state = State.IDLE
func play_punch_anim():
animated_sprite.play(punch_key + "_" + last_dir)
func play_anim(movement):
var dir = last_dir
var anim_name = ""
var state = "walk"
if current_speed == run_speed:
state = "run"
if movement.is_equal_approx(Vector2.ZERO):
anim_name = "idle_" + dir
else:
if abs(velocity.x) >= abs(velocity.y):
if velocity.x > 0:
dir = "right"
else:
dir = "left"
else:
if velocity.y > 0:
dir = "down"
else:
dir = "up"
anim_name = state + "_" + dir
last_dir = dir
if animated_sprite.animation != anim_name:
animated_sprite.play(anim_name)
update_hitbox_position(last_dir)
if animated_sprite.animation != anim_name:
animated_sprite.play(anim_name)
func _on_player_hitbox_body_entered(body):
if body.is_in_group("npc"):
bodies_in_hitbox.append(body)
print("entered hitbox")
func _on_player_hitbox_body_exited(body):
if body.is_in_group("npc"):
bodies_in_hitbox.erase(body)
print("exited hitbox")
func player():
pass