semi-idle-arpg/scripts/NPC.gd
2024-11-05 09:29:20 -07:00

61 lines
1.7 KiB
GDScript

# base NPC class
class_name NPC
extends CharacterBody2D
enum NPCDifficulty {MINION, NORMAL, ELITE, BOSS, ELITEBOSS, BBEG}
signal npc_died(npcTier: Globals.World.Tier)
@export var charName := "Character"
@export var maxHealth := 10
@export var damageTaken := 0
@export var npcDifficulty: NPCDifficulty
# TODO: remove npc/enemy Tier and just tie it all to world tier
@export var npcTier: Globals.World.Tier
@export var faction: Globals.World.Faction
func _random_mod_health() -> void:
randomize()
# noise factor
var noise_factor = randf_range(7, 10)
self.maxHealth = round(noise_factor * self.maxHealth)
# difficulty factor
match self.npcDifficulty:
NPCDifficulty.MINION:
self.maxHealth /= 2
NPCDifficulty.ELITE:
self.maxHealth *= 2
NPCDifficulty.BOSS:
self.maxHealth *= 4
NPCDifficulty.ELITEBOSS:
self.maxHealth *= 8
NPCDifficulty.BBEG:
self.maxHealth *= 16
# npcTier factor
self.maxHealth = round(exp(self.npcTier) * self.maxHealth)
# fun factor (just additional factor to tweak)
self.maxHealth *= 10
# func _init() -> void:
# self.npcDifficulty = NPCDifficulty.NORMAL
# self.npcTier = Globals.Tier.I
func _ready() -> void:
# modify max health value based on the various factors
_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:
# send signal that I died to be counted, gen loot, etc.
npc_died.emit(self.npcTier)
# delete node
self.free()