89 lines
2.0 KiB
GDScript
89 lines
2.0 KiB
GDScript
class_name Enemy
|
|
extends CharacterBody2D
|
|
|
|
@export var target: CollisionObject2D
|
|
@export var default_move_speed: float = 100
|
|
@export var default_max_health: float = 10.0
|
|
@export var default_contact_damage: float = 5.0
|
|
|
|
@onready var contact_damage_cd: Timer = $ContactDamageCD
|
|
@onready var animation_player: AnimationPlayer = $AnimationPlayer
|
|
|
|
var move_speed: float
|
|
var health: float
|
|
var max_health: float
|
|
var god_mode: bool = false
|
|
var is_dead: bool = false
|
|
|
|
var _xp_orb = preload("res://scenes/xp_orb.tscn")
|
|
|
|
|
|
func _ready() -> void:
|
|
move_speed = default_move_speed
|
|
max_health = default_max_health
|
|
health = max_health
|
|
GlobalConst.sig_debug_enemy_god_mode.connect(enemy_god_mode_toggle)
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if target:
|
|
var direction = global_position.direction_to(target.global_position)
|
|
var distance = global_position.distance_to(target.global_position)
|
|
if distance > 4:
|
|
velocity = direction * move_speed
|
|
move_and_slide()
|
|
else:
|
|
deal_damage()
|
|
|
|
|
|
func take_damage(value: float, is_crit: bool = false):
|
|
if god_mode:
|
|
return
|
|
health -= value
|
|
var dm = preload("res://scenes/damage_numbers.tscn").instantiate()
|
|
dm.damage_taken = value
|
|
dm.critical_damage = is_crit
|
|
animation_player.play("take_damage")
|
|
add_child(dm)
|
|
if health <= 0:
|
|
die()
|
|
|
|
|
|
func deal_damage():
|
|
if target.is_in_group("damagable"):
|
|
if contact_damage_cd.is_stopped():
|
|
target.take_damage(default_contact_damage)
|
|
contact_damage_cd.start()
|
|
|
|
|
|
func die():
|
|
if is_dead:
|
|
return
|
|
is_dead = true
|
|
drop_xp_orb()
|
|
target = null
|
|
velocity = Vector2.ZERO
|
|
animation_player.play("die")
|
|
animation_player.animation_finished.connect(_on_die_anim_finished)
|
|
|
|
|
|
func drop_xp_orb() -> void:
|
|
var orb: XPOrb = _xp_orb.instantiate()
|
|
orb.value = 5
|
|
orb.position = position
|
|
get_parent().add_child(orb)
|
|
|
|
|
|
func cheer_anim():
|
|
if not animation_player.is_playing():
|
|
animation_player.play("jump")
|
|
|
|
|
|
func enemy_god_mode_toggle(toggle_on: bool) -> void:
|
|
god_mode = toggle_on
|
|
|
|
|
|
func _on_die_anim_finished(name: String):
|
|
visible = false
|
|
queue_free()
|