Hello everyone im having some trouble to make the player gets dead. After reaches 0 health points he still alive and dont play animation for death. any idea what is missing?
extends CharacterBody2D
var speed = 100
var player_state
var health = Player_data.player_health
@export var inv: Inv
var gun_equiped = false
var gun_cooldown = true
var bullet = preload(“res://Scenes/bullet.tscn”)
var mouse_loc_from_player = null
var is_dead = false
func _ready():
$CollisionShape2D.disabled = false
$hitbox/CollisionShape2D.disabled = false
print(health)
func _physics_process(_delta):
if is_dead:
return
mouse_loc_from_player = get_global_mouse_position() - self.position
var direction = Input.get_vector("left", "right", "up", "down")
if direction.x == 0 and direction.y == 0:
player_state = "idle"
elif direction.x != 0 or direction.y != 0:
player_state = "walking"
velocity = direction * speed
move_and_slide()
if Input.is_action_just_pressed("1"):
gun_equiped = !gun_equiped
var mouse_pos = get_global_mouse_position()
$Marker2D.look_at(mouse_pos)
if Input.is_action_just_pressed("left_mouse") and gun_equiped and gun_cooldown:
gun_cooldown = false
var bullet_instance = bullet.instantiate()
bullet_instance.rotation = $Marker2D.rotation
bullet_instance.global_position = $Marker2D.global_position
add_child(bullet_instance)
await get_tree().create_timer(1.5).timeout
gun_cooldown = true
play_anim(direction)
func play_anim(dir):
if player_state == “dead”:
$AnimatedSprite2D.play(“Dying”)
return
if !gun_equiped:
if player_state == "idle":
$AnimatedSprite2D.play("Idle")
elif player_state == "walking":
if dir.x == 1:
$AnimatedSprite2D.flip_h = false
elif dir.x == -1:
$AnimatedSprite2D.flip_h = true
$AnimatedSprite2D.play("Walking")
else:
if player_state == "idle":
$AnimatedSprite2D.play("Idle_Armed")
elif player_state == "walking":
if dir.x == 1:
$AnimatedSprite2D.flip_h = false
elif dir.x == -1:
$AnimatedSprite2D.flip_h = true
$AnimatedSprite2D.play("Walking_Armed")
# Rotate sprite based on mouse position (if gun equipped)
if gun_equiped:
if mouse_loc_from_player.x > 0:
$AnimatedSprite2D.flip_h = false
else:
$AnimatedSprite2D.flip_h = true
func player():
pass # Placeholder for character-specific functionality
func collect(item):
inv.insert(item) # Assuming inv
is an inventory object
func flash():
$Sprite2D.material.set_shader_parameter(“flash_modifier”, 1)
await get_tree().create_timer(0.3).timeout
$Sprite2D.material.set_shader_parameter(“flash_modifier”, 0)
func dead():
print(“Player is dead”) # Debugging line
is_dead = true
player_state = “dead”
$AnimatedSprite2D.play(“Dying”)
$CollisionShape2D.disabled = true
$hitbox/CollisionShape2D.disabled = true
velocity = Vector2.ZERO # Stop movement
set_process(false) # Stop processing
set_physics_process(false) # Stop physics processing
# Optionally:
# - Play death sound effect
# - Trigger respawn logic
# - Display game over screen
func _on_hitbox_area_entered(area):
if area.is_in_group(“hazard”):
health -= 1 # Adjust damage amount as needed
flash()
print("Health: ", health) # Debugging line
if health <= 0 and not is_dead:
dead()