组件化设计:生命、属性、技能与效果系统
敌人脚本很容易越写越大:移动、血量、受击、护甲、燃烧、中毒、掉落、技能冷却全在一个文件里。写到几百行后,你再想复用“能被点燃”这件事,只能复制代码。
一句话先记住:节点组件管运行时行为,Resource 管数值配置。
一、别把角色写成大脚本
反例:
gdscript
extends CharacterBody2D
var hp := 100
var armor := 3
var burn_timer := 0.0
var poison_stacks := 0
var skill_cooldown := 0.0
func take_damage(amount: int) -> void:
...
func _physics_process(delta: float) -> void:
# 移动、buff、AI、技能、受击全在这里问题不是“代码长”,而是能力混在一起。下一只敌人想有血但没有技能,或者有技能但不会中毒,你很难拆。
二、推荐角色结构
text
Goblin (CharacterBody2D)
├── Sprite2D
├── CollisionShape2D
├── HealthComponent
├── HurtboxComponent
├── HitboxComponent
├── StatusEffectHolder
├── SkillRunner
└── EnemyAI分工:
text
HealthComponent 血量、死亡
HurtboxComponent 接收命中
HitboxComponent 造成命中
StatusEffectHolder 中毒、燃烧、减速
SkillRunner 技能冷却和释放
EnemyAI 巡逻、追击、攻击决策角色主体只负责移动和接线。
三、StatsResource
数值放资源:
gdscript
# res://combat/stats_resource.gd
class_name StatsResource
extends Resource
@export var max_hp: int = 100
@export var armor: int = 0
@export var move_speed: float = 120.0
@export var attack_power: int = 10
@export var knockback_resistance: float = 0.0给不同敌人做不同 .tres:
text
goblin_stats.tres
slime_stats.tres
boss_stats.tres同一个组件脚本,换配置就变成不同角色。
四、HealthComponent
gdscript
# res://combat/health_component.gd
class_name HealthComponent
extends Node
signal hp_changed(current: int, maximum: int)
signal died
@export var stats: StatsResource
var hp: int
func _ready() -> void:
assert(stats != null, "stats must be assigned")
hp = stats.max_hp
hp_changed.emit(hp, stats.max_hp)
func take_damage(amount: int) -> void:
var final_damage := maxi(amount - stats.armor, 1)
hp = maxi(hp - final_damage, 0)
hp_changed.emit(hp, stats.max_hp)
if hp == 0:
died.emit()
func heal(amount: int) -> void:
hp = mini(hp + amount, stats.max_hp)
hp_changed.emit(hp, stats.max_hp)这个组件不关心谁打的,也不关心动画怎么播。它只管血量。
五、Hurtbox 接收命中
gdscript
# res://combat/hurtbox_component.gd
class_name HurtboxComponent
extends Area2D
signal hit_received(amount: int, direction: Vector2)
func receive_hit(amount: int, direction: Vector2) -> void:
hit_received.emit(amount, direction)角色根节点接线:
gdscript
@onready var _health: HealthComponent = %HealthComponent
@onready var _hurtbox: HurtboxComponent = %HurtboxComponent
func _ready() -> void:
_hurtbox.hit_received.connect(_on_hit_received)
_health.died.connect(_on_died)
func _on_hit_received(amount: int, direction: Vector2) -> void:
_health.take_damage(amount)
# 击退、闪白、音效可以发给其它组件不要让 HurtboxComponent 直接找 HealthComponent。通过根节点接线更清楚。
六、Hitbox 造成命中
gdscript
# res://combat/hitbox_component.gd
class_name HitboxComponent
extends Area2D
@export var damage: int = 10
@export var owner_body: Node2D
func _ready() -> void:
area_entered.connect(_on_area_entered)
func _on_area_entered(area: Area2D) -> void:
if area == owner_body:
return
if area.has_method("receive_hit"):
var direction := owner_body.global_position.direction_to(area.global_position)
area.receive_hit(damage, direction)动画时间轴只负责开关 monitoring。命中逻辑在 hitbox。
七、状态效果
先不要写复杂 buff 框架。一个够用的 holder:
gdscript
# res://combat/status_effect_holder.gd
class_name StatusEffectHolder
extends Node
signal effect_started(id: StringName)
signal effect_ended(id: StringName)
var _effects: Dictionary = {}
func apply_effect(id: StringName, duration: float) -> void:
var is_new := not _effects.has(id)
_effects[id] = maxf(float(_effects.get(id, 0.0)), duration)
if is_new:
effect_started.emit(id)
func _process(delta: float) -> void:
for id in _effects.keys():
_effects[id] -= delta
if _effects[id] <= 0.0:
_effects.erase(id)
effect_ended.emit(id)
func has_effect(id: StringName) -> bool:
return _effects.has(id)中毒每秒扣血、燃烧生成粒子、减速改速度,可以先由角色根节点监听效果开始/结束再分发。
八、技能用 Resource 描述
技能数据:
gdscript
# res://combat/skill_resource.gd
class_name SkillResource
extends Resource
@export var id: StringName
@export var cooldown: float = 1.0
@export var damage: int = 10
@export var scene: PackedScene运行器:
gdscript
class_name SkillRunner
extends Node
@export var skills: Array[SkillResource] = []
var _cooldowns: Dictionary = {}
func can_cast(id: StringName) -> bool:
return float(_cooldowns.get(id, 0.0)) <= 0.0
func cast(id: StringName, origin: Node2D) -> void:
var skill := _find_skill(id)
if skill == null or not can_cast(id):
return
_cooldowns[id] = skill.cooldown
if skill.scene != null:
var node := skill.scene.instantiate()
origin.get_tree().current_scene.add_child(node)
node.global_position = origin.global_position
func _process(delta: float) -> void:
for id in _cooldowns.keys():
_cooldowns[id] = maxf(float(_cooldowns[id]) - delta, 0.0)
func _find_skill(id: StringName) -> SkillResource:
for skill in skills:
if skill.id == id:
return skill
return null以后新增技能,先新增 .tres 和场景,不急着改 AI。
验收
- 血量、受击、攻击、状态效果不是全写在角色主体脚本里。
- 数值用
StatsResource或类似资源配置。 - hitbox/hurtbox 通过信号或方法边界通信。
- 动画只开关 hitbox,不直接扣血。
- 新敌人可以通过换组件和资源组装出来。
常见坑
坑 1:组件之间互相乱找。
HealthComponent 不应该自己 get_parent().get_node("Hurtbox")。让根节点接线。
坑 2:把所有组件都抽象成基类。
项目初期没必要。先用清楚的具体组件。
坑 3:Resource 里放运行时状态。
共享资源会让多个实例互相影响。运行时 HP 放组件字段,基础 max_hp 放 Resource。
坑 4:技能数据化过头。
数值和引用进 Resource,复杂流程仍然写代码。
下一篇讲道具、装备和关卡配置的数据驱动。