semi-idle-arpg/scripts/NPCEnemy.gd

50 lines
1.3 KiB
GDScript3
Raw Normal View History

# base NPCEnemy class
2024-10-15 08:37:50 -06:00
class_name NPCEnemy
extends NPC
@export var killType: WorldArea.KillTypes = WorldArea.KillTypes.NATURAL
var target
var speed := 300
var prevCollisions := 0
func _init() -> void:
self.npcDifficulty = NPCDifficulties.NORMAL
self.npcTier = NPCTiers.I
func _ready() -> void:
# add to enemies
add_to_group("enemies")
# set health
_random_mod_health()
# set health bar
%Healthbar.max_value = maxHealth
%Healthbar.value = maxHealth
func _process(_delta: float) -> void:
%Healthbar.value = maxHealth - damageTaken
if damageTaken >= maxHealth:
# save vars to emit
var enemyDifficulty = self.npcDifficulty
var enemyKillType = self.killType
# delete node
self.remove_from_group("enemies")
self.queue_free()
# send signal that I died to be counted, etc.
SignalBus.enemy_died.emit(enemyDifficulty, enemyKillType)
func _physics_process(_delta):
velocity = position.direction_to(target) * speed
# look_at(target)
# FIXME: want enemy to stop when touching something, to prevent vibrating
if position.distance_to(target) > 10 and get_slide_collision_count() == prevCollisions:
move_and_slide()
prevCollisions = get_slide_collision_count()