This commit is contained in:
Rendo 2025-07-15 22:37:23 +05:00
commit d02bb3df47
3 changed files with 90 additions and 1 deletions

View file

@ -15,7 +15,7 @@
[node name="RotationalSprite" type="Sprite3D" parent="."]
billboard = 2
texture_filter = 0
texture = ExtResource("1_7qny8")
texture = ExtResource("9_83myg")
script = ExtResource("2_hmq60")
rotations = Dictionary[float, Texture2D]({
0.0: ExtResource("6_d1vpv"),

88
base/scripts/entity.gd Normal file
View file

@ -0,0 +1,88 @@
extends Node3D
## Base class for all hp-based objects
class_name Entity
## Maximum health points of an entity. Any heal cannot exceed this value
@export var max_hp : float
## Current amount of health points of an entity. Cannot be below 0 or [code]max_hp[/code]
var hp : float = max_hp
## Emitted when damage is taken
signal damage_taken(context : DamageTakenContext)
## Emitted when entity is healed
signal healed(context : HealedContext)
## Emitted on every health points change
signal hp_changed(context : HPChangedContext)
## Emitted when kill is requested
signal killed(context : KilledContext)
## Properly deal damage to entity
func deal_damage(amount : float, source : Entity):
var context = DamageTakenContext.new()
context.source = source
context.target = self
context.amount = amount
damage_taken.emit(context)
var delta_context = HPChangedContext.new()
delta_context.source = source
delta_context.target = self
delta_context.delta = -amount
hp_changed.emit(delta_context)
hp -= amount
if hp <= 0:
hp = 0
kill(source)
## Properly heal entity
func heal(amount : float, source : Entity):
var context = HealedContext.new()
context.source = source
context.target = self
context.amount = amount
healed.emit(context)
var delta_context = HPChangedContext.new()
delta_context.source = source
delta_context.target = self
delta_context.delta = amount
hp_changed.emit(delta_context)
hp += amount
if hp > max_hp:
hp = max_hp
## Invoked when an entity is killed by damage.
func kill(source : Entity):
var context = KilledContext.new()
context.source = source
context.target = self
killed.emit(context)
deconstruct()
## Method used to properly deconstruct entity
func deconstruct():
queue_free()
class DamageTakenContext:
var target : Entity
var source : Entity
var amount : float
class HealedContext:
var target : Entity
var source : Entity
var amount : float
class KilledContext:
var target : Entity
var source : Entity
class HPChangedContext:
var target : Entity
var source : Entity
var delta : float

View file

@ -0,0 +1 @@
uid://7572s220f0rv