semi-idle-arpg/scripts/NPCEnemy.gd
2024-10-18 16:17:39 -06:00

59 lines
1.5 KiB
GDScript

# base NPCEnemy class
class_name NPCEnemy
extends NPC
@export var killType: WorldArea.Faction = WorldArea.Faction.CREATURE
var target
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()
func _deal_damage() -> void:
PlayerState.player_currenthealth -= 1
SignalBus.enemy_damaged_player.emit()
func _on_attack_timer_timeout() -> void:
_deal_damage()