semi-idle-arpg/scripts/NPCEnemy.gd

51 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.Faction = WorldArea.Faction.CREATURE
var target
2024-10-18 09:11:15 -06:00
var speed := 200
var prevCollisions := 0
func _init() -> void:
# TODO: randomize within area restrictions? or put this in area?
self.npcDifficulty = NPCDifficulty.NORMAL
self.npcTier = NPCTier.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()