merged with entity
This commit is contained in:
commit
91f939d9fc
2 changed files with 89 additions and 0 deletions
88
base/scripts/entity.gd
Normal file
88
base/scripts/entity.gd
Normal 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
|
1
base/scripts/entity.gd.uid
Normal file
1
base/scripts/entity.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://7572s220f0rv
|
Loading…
Add table
Add a link
Reference in a new issue