适用版本:重构后的 WeaponBullet / BulletEffects 架构(2025-07-01)
+-------------------+
| weapons.json |
+-------------------+
|
| 解析后生成 WeaponConfig
v
+-------------------+
| WeaponBullet | << 场景节点挂载脚本 (入口)
+-------------------+
| | | |
| | | +--------------------+ ④ 生命周期
| | +-----------------------+ ③ 命中效果
| +--------------------------+ ② 弹道
+-----------------------------+ ① 数量/生成
weapons.json,实例化子弹并把四大维度配置分发给各模块。{
"weapons": [
{
"id": "pea_shooter",
"stats": { "damage": 20, "bulletSpeed": 30, ... },
"bulletConfig": {
"count": { /* ① BulletCount 配置 */ },
"trajectory": { /* ② BulletTrajectory 配置 */ },
"hitEffects": [ /* ③ BulletHitEffect 列表 */ ],
"lifecycle": { /* ④ BulletLifecycle 配置 */ },
"visual": { /* 资源路径:子弹预制、特效等 */ }
}
}
]
}
所有视觉资源路径现已统一为
Animation/WeaponTx/txXXXX/txXXXX,可直接被 resources.load 成功解析。
bulletLifecycle.init(cfg, startPos)。maxLifetime / maxRange / 越界 等条件。state.shouldDestroy 后调用 this.node.destroy() —— *场景中唯一允许直接销毁子弹的位置*。WeaponBullet.onHit 只负责:
bulletHitEffect.processHit()(产生伤害, 可能更改弹道)。bulletLifecycle.onHit(otherNode) 更新内部计数/阶段。destroy() 结点。因此 子弹是否消失 完全取决于 BulletLifecycle 的配置与状态。
旧版代码中,WeaponBullet.onHit 和其他脚本曾直接 this.node.destroy(),与 BulletLifecycle 产生并行决策。
已做的调整:
WeaponBullet.onHit 不再调用 this.node.destroy()。WeaponBullet.forceDestroy 仅调用 bulletLifecycle.forceDestroy()。若仍有异常消失,请检查:
lifecycle.type 是否设置为 hit_destroy / range_limit 等会触发销毁。maxLifetime、maxRange 或 penetration = 0 等。checkOutOfBounds() 自毁。BulletHitEffect.processEffect() 中实现,并在 weapons.json -> hitEffects 列表添加即可。BulletLifecycleConfig.type 联合类型加入枚举 + 对应处理函数。BulletTrajectory 中扩展 _update*() 逻辑并在 trajectory.type 中配置。| 模块 | 文件 | 说明 |
|---|---|---|
| 数量 & 生成 | BulletEffects/BulletCount.ts |
计算子弹生成阵列、偏移、延迟 |
| 弹道 | BulletEffects/BulletTrajectory.ts |
速度、重力、追踪、弧线、方向变化 |
| 命中 | BulletEffects/BulletHitEffect.ts |
叠加型命中效果栈、特效播放 |
| 生命周期 | BulletEffects/BulletLifecycle.ts |
子弹存亡唯一裁决者 |
| 入口 | WeaponBullet.ts |
读取配置 → 初始化上述 4 组件 |
结论:现在所有子弹的生成、运动、特效与销毁都遵循统一配置与单一生命周期管理。请将旧逻辑移除后再行测试,如仍有问题,可通过
weaponBullet.getBulletStatus()输出实时状态进行排查。