extends CharacterBody2D var speed = 40 var player_chase = false var player = null var health = 100 var player_inattack_zone = false var can_take_damage = true func _ready(): add_to_group("enemies") func _physics_process(delta): deal_with_damage() update_health() if player_chase: position += (player.position - position)/speed $AnimatedSprite2D.play("walk") if(player.position.x - position.x) < 0: $AnimatedSprite2D.flip_h = true else: $AnimatedSprite2D.flip_h = false else: $AnimatedSprite2D.play("idle") func _on_detection_area_body_entered(body: Node2D) -> void: player = body player_chase = true func _on_detection_area_area_shape_exited(area_rid: RID, area: Area2D, area_shape_index: int, local_shape_index: int) -> void: player = null player_chase = false func enemy(): pass func _on_enemy_hitbox_body_entered(body: Node2D) -> void: if body.has_method("player"): player_inattack_zone = true func _on_enemy_hitbox_body_exited(body: Node2D) -> void: if body.has_method("player"): player_inattack_zone = false func deal_with_damage(): if player_inattack_zone and Global.player_current_attack == true: if can_take_damage == true: health = health - 20 $take_damage_cooldown.start() can_take_damage = false if health <= 0: self.queue_free() func _on_take_damage_cooldown_timeout() -> void: can_take_damage = true func update_health(): var healthbar = $healthbar healthbar.value = health if health >= 100: healthbar.visible = false else: healthbar.visible = true func take_damage(amount): health -= amount print("Enemy took ", amount, " damage. Health: ", health) # For debugging if health <= 0: queue_free()