last init
This commit is contained in:
33
scripts/components/health.gd
Normal file
33
scripts/components/health.gd
Normal file
@@ -0,0 +1,33 @@
|
||||
extends Node
|
||||
|
||||
@export var max_health := 100.0
|
||||
const HEALTH_REGEN := 1.0
|
||||
var current_health: float
|
||||
|
||||
func _ready() -> void:
|
||||
current_health = max_health
|
||||
EventBus.damage_requested.connect(_on_damage_requested)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if current_health > 0 and current_health < max_health:
|
||||
current_health = min(current_health + HEALTH_REGEN * delta, max_health)
|
||||
EventBus.health_changed.emit(get_parent(), current_health, max_health)
|
||||
|
||||
func _on_damage_requested(attacker: Node, target: Node, amount: float) -> void:
|
||||
if target != get_parent():
|
||||
return
|
||||
var remaining: float = amount
|
||||
var shield: Node = get_parent().get_node_or_null("Shield")
|
||||
if shield:
|
||||
remaining = shield.absorb(remaining)
|
||||
EventBus.damage_dealt.emit(attacker, get_parent(), amount)
|
||||
if remaining > 0:
|
||||
take_damage(remaining, attacker)
|
||||
|
||||
func take_damage(amount: float, attacker: Node) -> void:
|
||||
current_health -= amount
|
||||
if current_health <= 0:
|
||||
current_health = 0
|
||||
EventBus.health_changed.emit(get_parent(), current_health, max_health)
|
||||
if current_health <= 0:
|
||||
EventBus.entity_died.emit(get_parent())
|
||||
1
scripts/components/health.gd.uid
Normal file
1
scripts/components/health.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b053b4fkkeaod
|
||||
32
scripts/components/shield.gd
Normal file
32
scripts/components/shield.gd
Normal file
@@ -0,0 +1,32 @@
|
||||
extends Node
|
||||
|
||||
@export var max_shield := 50.0
|
||||
const REGEN_DELAY := 3.0
|
||||
const REGEN_TIME := 5.0
|
||||
var current_shield: float
|
||||
var regen_timer := 0.0
|
||||
|
||||
func _ready() -> void:
|
||||
current_shield = max_shield
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if current_shield < max_shield:
|
||||
regen_timer += delta
|
||||
if regen_timer >= REGEN_DELAY:
|
||||
current_shield += (max_shield / REGEN_TIME) * delta
|
||||
if current_shield >= max_shield:
|
||||
current_shield = max_shield
|
||||
EventBus.shield_regenerated.emit(get_parent())
|
||||
EventBus.shield_changed.emit(get_parent(), current_shield, max_shield)
|
||||
|
||||
func absorb(amount: float) -> float:
|
||||
if current_shield <= 0:
|
||||
return amount
|
||||
regen_timer = 0.0
|
||||
var absorbed: float = min(amount, current_shield)
|
||||
current_shield -= absorbed
|
||||
print("%s Schild: %s/%s (-%s)" % [get_parent().name, current_shield, max_shield, absorbed])
|
||||
if current_shield <= 0:
|
||||
EventBus.shield_broken.emit(get_parent())
|
||||
EventBus.shield_changed.emit(get_parent(), current_shield, max_shield)
|
||||
return amount - absorbed
|
||||
1
scripts/components/shield.gd.uid
Normal file
1
scripts/components/shield.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bpfw71oprcvou
|
||||
Reference in New Issue
Block a user