refactor(game): 移除已弃用的事件常量
- 删除 UpdateHero 和 UpdateFightHero 事件 - 移除 MISSION_UPDATE 事件常量 - 优化游戏事件枚举定义
This commit is contained in:
270
.qoder/repowiki/zh/content/地图系统/怪物系统/怪物实体/怪物实体组件系统.md
Normal file
270
.qoder/repowiki/zh/content/地图系统/怪物系统/怪物实体/怪物实体组件系统.md
Normal file
@@ -0,0 +1,270 @@
|
||||
# 怪物实体组件系统
|
||||
|
||||
<cite>
|
||||
**本文档引用的文件**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
- [MonModelComp.ts](file://assets/script/game/hero/MonModelComp.ts)
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
- [BoxSet.ts](file://assets/script/game/common/config/BoxSet.ts)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts)
|
||||
- [Main.ts](file://assets/script/Main.ts)
|
||||
</cite>
|
||||
|
||||
## 目录
|
||||
1. [引言](#引言)
|
||||
2. [项目结构](#项目结构)
|
||||
3. [核心组件](#核心组件)
|
||||
4. [架构概述](#架构概述)
|
||||
5. [详细组件分析](#详细组件分析)
|
||||
6. [依赖分析](#依赖分析)
|
||||
7. [性能考虑](#性能考虑)
|
||||
8. [故障排除指南](#故障排除指南)
|
||||
9. [结论](#结论)
|
||||
|
||||
## 引言
|
||||
本文档详细说明了Cocos游戏项目中Monster实体的ECS(实体-组件-系统)机制,重点分析了在init方法中注册BattleMoveComp和MonModelComp组件的实现。文档深入解析了BattleMoveComp如何驱动怪物向左移动至目标X坐标(-800)的逻辑,结合BoxSet.MONSTER碰撞分组常量说明其在物理系统中的角色定义。同时阐述了MonModelComp作为数据容器的职责,包括属性重置机制与扩展字段设计原则,并提供开发示例和典型问题排查方法。
|
||||
|
||||
## 项目结构
|
||||
本项目采用基于功能模块的文件组织方式,核心游戏逻辑位于`assets/script/game`目录下,分为多个子模块如`hero`、`common`、`map`等。ECS相关组件和系统被组织在`common/ecs/position`路径下,体现了清晰的架构分层。
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "Assets"
|
||||
subgraph "Script"
|
||||
subgraph "Game"
|
||||
Hero[hero模块]
|
||||
Common[common模块]
|
||||
Map[map模块]
|
||||
Initialize[initialize模块]
|
||||
end
|
||||
Main[Main.ts]
|
||||
end
|
||||
Resources[resources]
|
||||
end
|
||||
Hero --> |包含| Monster[Monster实体]
|
||||
Common --> |包含| ECS[ECS系统]
|
||||
Common --> |包含| Config[配置文件]
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
- [BoxSet.ts](file://assets/script/game/common/config/BoxSet.ts)
|
||||
|
||||
**Section sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
- [Main.ts](file://assets/script/Main.ts)
|
||||
|
||||
## 核心组件
|
||||
Monster实体通过ECS架构实现了行为与数据的分离,其核心由BattleMoveComp(移动行为)和MonModelComp(数据模型)两个组件构成。这种设计模式使得组件可以独立开发、测试和复用,同时通过系统(System)统一管理组件间的交互逻辑。
|
||||
|
||||
**Section sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts)
|
||||
- [MonModelComp.ts](file://assets/script/game/hero/MonModelComp.ts)
|
||||
|
||||
## 架构概述
|
||||
游戏采用ECS(Entity-Component-System)架构模式,将游戏对象分解为实体(Entity)、组件(Component)和系统(System)三个核心概念。实体作为容器,持有多个组件;组件作为纯数据结构,存储特定功能的数据;系统则负责处理具有特定组件组合的实体,实现游戏逻辑。
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class Entity {
|
||||
+addComponent(comp)
|
||||
+removeComponent(comp)
|
||||
+getComponent(type)
|
||||
}
|
||||
class Component {
|
||||
<<abstract>>
|
||||
+reset()
|
||||
}
|
||||
class System {
|
||||
<<abstract>>
|
||||
+filter()
|
||||
+update(entity)
|
||||
}
|
||||
class Monster {
|
||||
-BattleMoveComp
|
||||
-MonModelComp
|
||||
-TalComp
|
||||
}
|
||||
class BattleMoveComp {
|
||||
+direction : number
|
||||
+targetX : number
|
||||
+moving : boolean
|
||||
}
|
||||
class MonModelComp {
|
||||
<<data container>>
|
||||
}
|
||||
class BattleMoveSystem {
|
||||
+filter()
|
||||
+update(entity)
|
||||
}
|
||||
Entity <|-- Monster
|
||||
Component <|-- BattleMoveComp
|
||||
Component <|-- MonModelComp
|
||||
System <|-- BattleMoveSystem
|
||||
Monster --> BattleMoveComp
|
||||
Monster --> MonModelComp
|
||||
BattleMoveSystem ..> BattleMoveComp : processes
|
||||
BattleMoveSystem ..> HeroViewComp : interacts with
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
|
||||
## 详细组件分析
|
||||
|
||||
### Monster实体初始化机制
|
||||
Monster实体在初始化时通过`addComponents`方法注册了BattleMoveComp、MonModelComp和TalComp三个组件。这一过程遵循ECS框架的组件注册规范,确保实体具备必要的数据和行为能力。
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant M as Monster
|
||||
participant ECS as ECS框架
|
||||
participant BMS as BattleMoveSystem
|
||||
M->>ECS : init()
|
||||
M->>ECS : addComponents(BattleMoveComp, MonModelComp, TalComp)
|
||||
ECS->>M : 返回组件实例引用
|
||||
M->>M : 设置HeroModel、HeroView、BattleMove成员变量
|
||||
M->>BMS : 系统检测到新实体
|
||||
BMS->>BMS : 将实体加入处理队列
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
|
||||
**Section sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L15-L30)
|
||||
|
||||
### BattleMoveComp移动逻辑解析
|
||||
BattleMoveComp组件负责管理怪物的移动行为,其核心属性包括`direction`(移动方向)、`targetX`(目标X坐标)和`moving`(移动状态)。在Monster的`load`方法中,这些属性被初始化为向左移动至-800坐标。
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([开始移动]) --> CheckState["检查游戏状态<br/>(play && !pause)"]
|
||||
CheckState --> |否| End([停止移动])
|
||||
CheckState --> |是| CheckStop["检查停止条件<br/>(is_stop, is_dead, isStun等)"]
|
||||
CheckStop --> |是| SetIdle["设置idle状态"] --> End
|
||||
CheckStop --> |否| CalculateDelta["计算位移增量<br/>delta = (SPEED/3) * dt * direction"]
|
||||
CalculateDelta --> CalculateNewX["计算新X坐标<br/>newX = currentX + delta"]
|
||||
CalculateNewX --> ValidatePos["验证位置有效性"]
|
||||
ValidatePos --> |有效| UpdatePos["更新节点位置"] --> SetMove["设置move状态"] --> End
|
||||
ValidatePos --> |无效| StopMoving["设置moving=false"] --> SetIdle --> End
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts#L1-L15)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L50-L270)
|
||||
|
||||
**Section sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L80-L90)
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
|
||||
### BoxSet.MONSTER碰撞分组角色
|
||||
BoxSet.MONSTER常量定义了怪物实体在物理系统中的碰撞分组标识,值为2。该常量不仅用于物理碰撞检测,还作为阵营标识参与游戏逻辑判断,如敌我识别、技能作用范围等。
|
||||
|
||||
```mermaid
|
||||
erDiagram
|
||||
BOXSET ||--o{ MONSTER : "包含"
|
||||
BOXSET ||--o{ HERO : "包含"
|
||||
BOXSET ||--o{ SKILL : "包含"
|
||||
BOXSET {
|
||||
int MONSTER
|
||||
int HERO
|
||||
int SKILL_TAG
|
||||
int ATK_RANGE
|
||||
}
|
||||
MONSTER {
|
||||
string name
|
||||
int group = 2
|
||||
}
|
||||
HERO {
|
||||
string name
|
||||
int group = 4
|
||||
}
|
||||
SKILL {
|
||||
string name
|
||||
int tag = 8
|
||||
}
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [BoxSet.ts](file://assets/script/game/common/config/BoxSet.ts#L5-L15)
|
||||
|
||||
**Section sources**
|
||||
- [BoxSet.ts](file://assets/script/game/common/config/BoxSet.ts)
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L75-L80)
|
||||
|
||||
### MonModelComp数据容器职责
|
||||
MonModelComp作为纯粹的数据容器组件,遵循ECS框架的组件设计原则。其主要职责是存储怪物的持久化数据,通过reset方法实现组件回收时的状态重置,保证组件在对象池中的可重用性。
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class MonModelComp {
|
||||
<<data container>>
|
||||
+reset()
|
||||
}
|
||||
note right of MonModelComp
|
||||
作为数据容器,不包含业务逻辑
|
||||
所有数据操作由系统或其他组件完成
|
||||
reset方法确保组件回收时状态清零
|
||||
end note
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [MonModelComp.ts](file://assets/script/game/hero/MonModelComp.ts#L1-L20)
|
||||
|
||||
**Section sources**
|
||||
- [MonModelComp.ts](file://assets/script/game/hero/MonModelComp.ts)
|
||||
|
||||
## 依赖分析
|
||||
Monster实体的组件系统存在明确的依赖关系,BattleMoveComp依赖于HeroViewComp的存在,因为移动逻辑需要操作视图节点的位置和状态。这种依赖关系通过BattleMoveSystem的filter方法声明,确保只有同时拥有这两个组件的实体才会被系统处理。
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
A[BattleMoveSystem] --> B[BattleMoveComp]
|
||||
A --> C[HeroViewComp]
|
||||
D[Monster] --> B
|
||||
D --> E[MonModelComp]
|
||||
D --> F[TalComp]
|
||||
D --> C
|
||||
style A fill:#f9f,stroke:#333
|
||||
style B fill:#bbf,stroke:#333
|
||||
style C fill:#bbf,stroke:#333
|
||||
style D fill:#9f9,stroke:#333
|
||||
style E fill:#f96,stroke:#333
|
||||
style F fill:#f96,stroke:#333
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L10-L15)
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
|
||||
**Section sources**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts)
|
||||
|
||||
## 性能考虑
|
||||
ECS架构通过组件对象池机制有效减少了运行时的内存分配和垃圾回收压力。BattleMoveComp和MonModelComp的reset方法确保了组件在销毁时能够正确重置状态,使其可以安全地返回对象池供后续复用,从而提升整体性能表现。
|
||||
|
||||
## 故障排除指南
|
||||
### 组件注册遗漏
|
||||
当Monster实体缺少必要组件时,可能导致移动功能失效或数据丢失。检查`init`方法中的`addComponents`调用,确保所有必需组件都被正确注册。
|
||||
|
||||
**Section sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L15-L30)
|
||||
|
||||
### 依赖顺序错误
|
||||
BattleMoveSystem要求实体同时拥有BattleMoveComp和HeroViewComp才能正常工作。如果组件添加顺序不当或遗漏HeroViewComp,系统将无法处理该实体。确保在调用`hero_init`方法前已完成所有必要组件的注册。
|
||||
|
||||
**Section sources**
|
||||
- [Mon.ts](file://assets/script/game/hero/Mon.ts#L100-L120)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L10-L15)
|
||||
|
||||
## 结论
|
||||
Monster实体的ECS实现展示了良好的架构设计,通过组件化分离了数据与行为,提高了代码的可维护性和可扩展性。BattleMoveComp的移动逻辑与BoxSet.MONSTER的碰撞分组协同工作,确保了怪物在游戏世界中的正确行为。开发者在扩展功能时应遵循相同的组件设计原则,确保系统的稳定性和一致性。
|
||||
Reference in New Issue
Block a user