This is the code I have for the player:
extends CharacterBody2D
signal health_depleted
var health = 100.0
const damage_rate = 10.0
var attacking: bool = false
func _ready():
%AnimationPlayer.animation_finished.connect(_on_animation_finished)
func _physics_process(delta):
var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
if attacking:
velocity = Vector2.ZERO
else:
velocity = direction * 400
move_and_slide()
if not attacking:
if velocity.length() > 0.0:
%Bluwee.play_run()
else:
%Bluwee.play_idle()
if Input.is_action_just_pressed("attack") and not attacking:
attacking = true
handle_attack_animation()
var overlapping_mobs = %HurtBox.get_overlapping_bodies()
if overlapping_mobs.size() > 0:
health -= damage_sheep * overlapping_mobs.size() * delta
%ProgressBar.value = health
if health <= 0.0:
health_depleted.emit()
func handle_attack_animation():
var mouse_position = get_global_mouse_position()
var vector_to_mouse = (mouse_position - global_position).normalized()
var angle_to_mouse = vector_to_mouse.angle() # Angle in radians
# Convert angle to degrees for easier comparisons
var angle_in_degrees = rad_to_deg(angle_to_mouse)
# Adjust for top-down orientation (0 degrees = up)
if angle_in_degrees < 0:
angle_in_degrees += 360
# Determine animation based on the angle
if angle_in_degrees >= 315 or angle_in_degrees < 45:
%Bluwee.play_attack() # Mouse is above the player
elif angle_in_degrees >= 45 and angle_in_degrees < 135:
%Bluwee.play_down_attack() # Mouse is to the right of the player
elif angle_in_degrees >= 225 and angle_in_degrees < 315:
%Bluwee.play_up_attack() # Mouse is below the player
elif angle_in_degrees >= 135 and angle_in_degrees < 225:
%Bluwee.play_left_attack() # Placeholder for left attacks if needed
func _on_animation_finished(animation: String):
# Reset attacking flag when the attack animation is done
if animation in ["right_attack", "back_attack", "front_attack", "left_attack"]:
attacking = false
%WarriorBlue.scale.x = 1
%Bluwee.play_idle()
This is the code I have for the sheep:
extends StaticBody2D
var health = 50.0
var damage_sheep := 1.0
func _ready():
var sheep_anim = $AnimationPlayer
sheep_anim.play("idle")
I am trying to make the player take damage whenever he touches the sheep. But for that I’d need to get the variable from the sheep scene to use it in the player scene