feat: 添加玩家和敌人血条
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
extends Node
|
||||
class_name HealthComponent
|
||||
|
||||
signal on_unit_hit
|
||||
signal on_unit_died
|
||||
signal on_health_changed(current: float, max: float)
|
||||
|
||||
var max_health := 1.0
|
||||
var current_health := 1.0
|
||||
|
||||
func setup(stats: UnitStats) -> void:
|
||||
max_health = stats.health
|
||||
current_health = max_health
|
||||
on_health_changed.emit(current_health, max_health)
|
||||
|
||||
func take_damage(value: float) -> void:
|
||||
if current_health <=0:
|
||||
return
|
||||
|
||||
current_health -= value
|
||||
current_health = max(0, current_health)
|
||||
|
||||
on_unit_hit.emit()
|
||||
on_health_changed.emit(current_health, max_health)
|
||||
|
||||
if current_health <= 0:
|
||||
current_health = 0
|
||||
on_unit_died.emit()
|
||||
die()
|
||||
|
||||
func heal(amount: float) -> void:
|
||||
if current_health <= 0:
|
||||
return
|
||||
|
||||
current_health += amount
|
||||
current_health = min(max_health, current_health)
|
||||
on_health_changed.emit(current_health, max_health)
|
||||
|
||||
|
||||
func die() -> void:
|
||||
owner.queue_free()
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://c2hqb7ixwow4h
|
||||
@@ -0,0 +1,6 @@
|
||||
[gd_scene format=3 uid="uid://2ew7i6g3p0v8"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c2hqb7ixwow4h" path="res://scenes/components/health_component.gd" id="1_2wcl1"]
|
||||
|
||||
[node name="HealthComponent" type="Node" unique_id=455886217]
|
||||
script = ExtResource("1_2wcl1")
|
||||
Reference in New Issue
Block a user