65 lines
1.7 KiB
GDScript
65 lines
1.7 KiB
GDScript
class_name Enemy extends Entity
|
|
|
|
@export var attack_charge_time: float = 0.7
|
|
@export var flee_range: float = 20.0
|
|
@export var approach_range: float = 100.0
|
|
|
|
@onready var nav_agent: NavigationAgent2D = $NavigationAgent2D
|
|
@onready var sprite = $Anim
|
|
|
|
var current_charge: float = 0.0
|
|
var target_node: Node2D
|
|
|
|
func _ready() -> void:
|
|
add_to_group("enemy")
|
|
target_node = get_tree().get_first_node_in_group("player")
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not target_node: return
|
|
|
|
nav_agent.target_position = target_node.position
|
|
var dist = position.distance_to(target_node.position)
|
|
var next_path_pos = nav_agent.get_next_path_position()
|
|
var dir = position.direction_to(next_path_pos)
|
|
|
|
if dist > approach_range:
|
|
velocity += dir * move_speed
|
|
current_charge = 0
|
|
elif dist < flee_range:
|
|
velocity -= dir * move_speed
|
|
current_charge = 0
|
|
else:
|
|
_handle_attack_charge(delta)
|
|
|
|
sprite.flip_h = target_node.position.x > position.x
|
|
scale = Vector2.ONE * (1 + (current_charge * 0.2))
|
|
|
|
super._physics_process(delta)
|
|
|
|
func _handle_attack_charge(delta):
|
|
current_charge += delta
|
|
|
|
if current_charge >= attack_charge_time:
|
|
use_mask(target_node.global_position)
|
|
current_charge = 0
|
|
|
|
func _on_velocity_computed(safe_vel: Vector2):
|
|
velocity = safe_vel
|
|
|
|
func damage(amount: int = 1) -> void:
|
|
super.damage(amount)
|
|
SoundManager.play_sfx(SoundManager.SFX_DEATH)
|
|
|
|
func knockback():
|
|
velocity += global_position.direction_to(nav_agent.target_position).normalized() * -2000
|
|
|
|
func die():
|
|
var drop_scene := load("res://scenes/mask_drop.tscn")
|
|
var drop : MaskDrop = drop_scene.instantiate()
|
|
drop.mask_type = current_mask_data
|
|
get_parent().add_child(drop)
|
|
drop.global_position = global_position
|
|
|
|
EventBus.screenshake.emit(10)
|
|
super.die()
|
|
queue_free()
|