refactor(game): 移除已弃用的事件常量
- 删除 UpdateHero 和 UpdateFightHero 事件 - 移除 MISSION_UPDATE 事件常量 - 优化游戏事件枚举定义
This commit is contained in:
488
.qoder/repowiki/zh/content/技术架构/ECS架构.md
Normal file
488
.qoder/repowiki/zh/content/技术架构/ECS架构.md
Normal file
@@ -0,0 +1,488 @@
|
||||
# ECS架构深度解析
|
||||
|
||||
<cite>
|
||||
**本文档引用的文件**
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
- [EcsPositionSystem.ts](file://assets/script/game/common/ecs/position/EcsPositionSystem.ts)
|
||||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts)
|
||||
- [MapModelComp.ts](file://assets/script/game/map/model/MapModelComp.ts)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts)
|
||||
- [ecs.md](file://doc/ecs/ecs.md)
|
||||
- [Main.ts](file://assets/script/Main.ts)
|
||||
</cite>
|
||||
|
||||
## 目录
|
||||
1. [简介](#简介)
|
||||
2. [ECS架构概述](#ecs架构概述)
|
||||
3. [组件系统详解](#组件系统详解)
|
||||
4. [实体生命周期管理](#实体生命周期管理)
|
||||
5. [系统架构与执行机制](#系统架构与执行机制)
|
||||
6. [实际案例分析](#实际案例分析)
|
||||
7. [性能优化与最佳实践](#性能优化与最佳实践)
|
||||
8. [扩展开发指南](#扩展开发指南)
|
||||
9. [总结](#总结)
|
||||
|
||||
## 简介
|
||||
|
||||
ECS(Entity-Component-System)是一种流行的游戏架构模式,它通过将数据与行为分离来实现高度模块化和可扩展的系统设计。本项目采用TypeScript版本的ECS框架,实现了游戏逻辑的数据与行为解耦,为复杂的游戏系统提供了清晰的架构基础。
|
||||
|
||||
## ECS架构概述
|
||||
|
||||
### 核心概念
|
||||
|
||||
ECS架构由三个核心元素组成:
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "ECS架构核心"
|
||||
Entity["实体 (Entity)<br/>唯一标识符"]
|
||||
Component["组件 (Component)<br/>数据容器"]
|
||||
System["系统 (System)<br/>逻辑处理器"]
|
||||
end
|
||||
subgraph "交互关系"
|
||||
Entity --> Component
|
||||
System --> Entity
|
||||
System -.-> Component
|
||||
end
|
||||
subgraph "执行流程"
|
||||
System --> Filter["筛选机制"]
|
||||
Filter --> Entities["符合条件的实体"]
|
||||
Entities --> Update["系统更新"]
|
||||
end
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [ecs.md](file://doc/ecs/ecs.md#L1-L27)
|
||||
|
||||
### 架构优势
|
||||
|
||||
1. **数据与行为分离**:组件只存储数据,系统处理逻辑
|
||||
2. **高度模块化**:通过组合不同组件创建复杂实体
|
||||
3. **性能优化**:批量处理相同类型的操作
|
||||
4. **易于扩展**:新增功能只需添加新组件和系统
|
||||
|
||||
## 组件系统详解
|
||||
|
||||
### 组件注册机制
|
||||
|
||||
组件通过装饰器`@ecs.register`进行注册,框架自动管理组件的生命周期和内存回收。
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class Component {
|
||||
<<abstract>>
|
||||
+reset() void
|
||||
}
|
||||
class BattleMoveComp {
|
||||
+number direction
|
||||
+number targetX
|
||||
+boolean moving
|
||||
+reset() void
|
||||
}
|
||||
class HeroViewComp {
|
||||
+string hero_name
|
||||
+number fac
|
||||
+boolean is_dead
|
||||
+number[] Attrs
|
||||
+reset() void
|
||||
}
|
||||
class MapModelComp {
|
||||
+number id
|
||||
+string resPrefab
|
||||
+reset() void
|
||||
}
|
||||
Component <|-- BattleMoveComp
|
||||
Component <|-- HeroViewComp
|
||||
Component <|-- MapModelComp
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts#L1-L16)
|
||||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L50)
|
||||
- [MapModelComp.ts](file://assets/script/game/map/model/MapModelComp.ts#L1-L43)
|
||||
|
||||
### 组件添加与移除机制
|
||||
|
||||
组件的添加和移除遵循严格的生命周期管理:
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Entity as 实体
|
||||
participant ECS as ECS框架
|
||||
participant Pool as 组件池
|
||||
participant System as 系统
|
||||
Entity->>ECS : add(Component)
|
||||
ECS->>Pool : 获取可用组件
|
||||
alt 组件池中有可用组件
|
||||
Pool-->>ECS : 返回组件实例
|
||||
else 组件池为空
|
||||
ECS->>ECS : 创建新组件
|
||||
end
|
||||
ECS->>Entity : 绑定组件
|
||||
System->>Entity : 筛选实体
|
||||
Entity-->>System : 返回实体列表
|
||||
Entity->>ECS : remove(Component)
|
||||
ECS->>Component : reset()重置状态
|
||||
ECS->>Pool : 回收组件到池中
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [ecs.md](file://doc/ecs/ecs.md#L45-L87)
|
||||
|
||||
**章节来源**
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts#L1-L16)
|
||||
- [ecs.md](file://doc/ecs/ecs.md#L15-L45)
|
||||
|
||||
## 实体生命周期管理
|
||||
|
||||
### 实体创建与销毁
|
||||
|
||||
实体通过继承`ecs.Entity`创建,并支持父子实体关系管理:
|
||||
|
||||
```mermaid
|
||||
stateDiagram-v2
|
||||
[*] --> Created : createEntity()
|
||||
Created --> Active : addComponents()
|
||||
Active --> Updated : systemUpdate()
|
||||
Updated --> Active : continue
|
||||
Active --> Removed : removeComponents()
|
||||
Removed --> Destroyed : destroy()
|
||||
Destroyed --> [*]
|
||||
Active --> Paused : systemPause()
|
||||
Paused --> Active : systemResume()
|
||||
```
|
||||
|
||||
### 实体管理操作
|
||||
|
||||
| 操作 | 方法 | 描述 |
|
||||
|------|------|------|
|
||||
| 创建实体 | `ecs.getEntity<Entity>()` | 从实体池中获取或创建新实体 |
|
||||
| 添加组件 | `entity.add(Component)` | 优先从组件池获取,否则创建新实例 |
|
||||
| 移除组件 | `entity.remove(Component)` | 组件重置后放回组件池 |
|
||||
| 销毁实体 | `entity.destroy()` | 清理所有组件并回收实体 |
|
||||
| 子实体管理 | `addChild(Entity)` | 管理实体间的父子关系 |
|
||||
|
||||
**章节来源**
|
||||
- [ecs.md](file://doc/ecs/ecs.md#L29-L87)
|
||||
|
||||
## 系统架构与执行机制
|
||||
|
||||
### 系统层次结构
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class System {
|
||||
<<abstract>>
|
||||
+execute(dt) void
|
||||
}
|
||||
class ComblockSystem {
|
||||
+filter() IMatcher
|
||||
+update(entity) void
|
||||
+entityEnter(entity) void
|
||||
+entityRemove(entity) void
|
||||
}
|
||||
class BattleMoveSystem {
|
||||
+filter() IMatcher
|
||||
+update(entity) void
|
||||
-checkEnemiesExist(entity) boolean
|
||||
-findNearestEnemy(entity) HeroViewComp
|
||||
-validatePosition(x, move) boolean
|
||||
}
|
||||
class EcsPositionSystem {
|
||||
+constructor()
|
||||
}
|
||||
System <|-- ComblockSystem
|
||||
ComblockSystem <|-- BattleMoveSystem
|
||||
System <|-- EcsPositionSystem
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L1-L50)
|
||||
- [EcsPositionSystem.ts](file://assets/script/game/common/ecs/position/EcsPositionSystem.ts#L1-L9)
|
||||
|
||||
### 筛选机制
|
||||
|
||||
系统通过`filter()`方法筛选符合条件的实体:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([系统执行]) --> Filter["filter()筛选"]
|
||||
Filter --> Matcher{"IMatcher匹配"}
|
||||
Matcher --> |allOf| AllMatch["所有组件都存在"]
|
||||
Matcher --> |anyOf| AnyMatch["至少有一个组件存在"]
|
||||
Matcher --> |excludeOf| ExcludeMatch["不包含指定组件"]
|
||||
Matcher --> |onlyOf| OnlyMatch["只有指定组件"]
|
||||
AllMatch --> ProcessEntities["处理实体列表"]
|
||||
AnyMatch --> ProcessEntities
|
||||
ExcludeMatch --> ProcessEntities
|
||||
OnlyMatch --> ProcessEntities
|
||||
ProcessEntities --> Update["系统更新"]
|
||||
Update --> End([完成])
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [ecs.md](file://doc/ecs/ecs.md#L88-L128)
|
||||
|
||||
### 系统执行流程
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Root as RootSystem
|
||||
participant System as ComblockSystem
|
||||
participant Entity as Entity
|
||||
participant Component as Component
|
||||
Root->>System : execute(dt)
|
||||
System->>System : filter()筛选实体
|
||||
loop 遍历符合条件的实体
|
||||
System->>Entity : entityEnter()首次进入
|
||||
System->>Entity : update()每帧更新
|
||||
Entity->>Component : get(Component)
|
||||
Component-->>Entity : 返回组件实例
|
||||
System->>Entity : 处理业务逻辑
|
||||
end
|
||||
System->>Entity : entityRemove()移除处理
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L10-L30)
|
||||
|
||||
**章节来源**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L1-L272)
|
||||
- [EcsPositionSystem.ts](file://assets/script/game/common/ecs/position/EcsPositionSystem.ts#L1-L9)
|
||||
|
||||
## 实际案例分析
|
||||
|
||||
### BattleMoveSystem详细分析
|
||||
|
||||
BattleMoveSystem展示了ECS架构在游戏AI中的应用:
|
||||
|
||||
#### 组件依赖关系
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
Entity["游戏实体"] --> BattleMoveComp["BattleMoveComp<br/>移动控制"]
|
||||
Entity --> HeroViewComp["HeroViewComp<br/>视图控制"]
|
||||
BattleMoveComp --> Direction["direction<br/>移动方向"]
|
||||
BattleMoveComp --> TargetX["targetX<br/>目标位置"]
|
||||
BattleMoveComp --> Moving["moving<br/>移动状态"]
|
||||
HeroViewComp --> HP["hp/mp<br/>生命值"]
|
||||
HeroViewComp --> Status["status<br/>状态"]
|
||||
HeroViewComp --> Position["node.position<br/>位置信息"]
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L10-L20)
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts#L4-L12)
|
||||
|
||||
#### 系统更新逻辑
|
||||
|
||||
BattleMoveSystem的核心更新逻辑体现了ECS的优势:
|
||||
|
||||
1. **数据分离**:移动逻辑完全封装在系统中
|
||||
2. **条件筛选**:只处理同时拥有BattleMoveComp和HeroViewComp的实体
|
||||
3. **状态管理**:根据游戏状态动态调整行为
|
||||
4. **性能优化**:只处理活跃实体,避免不必要的计算
|
||||
|
||||
#### 关键算法实现
|
||||
|
||||
| 算法 | 功能 | 实现位置 |
|
||||
|------|------|----------|
|
||||
| 敌人检测 | 检查攻击范围内是否有敌人 | `checkEnemiesInRange()` |
|
||||
| 最近敌人查找 | 寻找最近的敌对单位 | `findNearestEnemy()` |
|
||||
| 位置验证 | 验证移动位置的有效性 | `validatePosition()` |
|
||||
| 渲染层级更新 | 根据位置更新渲染顺序 | `updateRenderOrder()` |
|
||||
|
||||
**章节来源**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L15-L272)
|
||||
|
||||
### HeroViewComp综合组件分析
|
||||
|
||||
HeroViewComp展示了组件如何存储复杂的游戏状态:
|
||||
|
||||
#### 属性管理系统
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class HeroViewComp {
|
||||
+Record~number,Array~ BUFFS
|
||||
+Record~number,Array~ BUFFS_TEMP
|
||||
+number[] Attrs
|
||||
+number[] NeAttrs
|
||||
+addBuff(conf) void
|
||||
+recalculateSingleAttr(attrIndex) void
|
||||
+updateTemporaryBuffsDebuffs(dt) void
|
||||
}
|
||||
class BuffInstance {
|
||||
+number value
|
||||
+BType BType
|
||||
+number remainTime
|
||||
}
|
||||
HeroViewComp --> BuffInstance : 管理
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L60-L120)
|
||||
|
||||
#### BUFF系统架构
|
||||
|
||||
HeroViewComp实现了复杂的BUFF/DEBUFF管理系统:
|
||||
|
||||
1. **持久BUFF**:存储在`BUFFS`数组中,永久存在直到手动移除
|
||||
2. **临时BUFF**:存储在`BUFFS_TEMP`数组中,带有剩余时间
|
||||
3. **属性计算**:根据数值型和百分比型BUFF计算最终属性值
|
||||
4. **自动更新**:每帧更新临时BUFF的剩余时间
|
||||
|
||||
**章节来源**
|
||||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L780)
|
||||
|
||||
## 性能优化与最佳实践
|
||||
|
||||
### 内存管理策略
|
||||
|
||||
ECS框架通过以下机制优化内存使用:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Create["创建组件"] --> Pool{"组件池检查"}
|
||||
Pool --> |有可用| Reuse["重用组件实例"]
|
||||
Pool --> |无可用| New["创建新实例"]
|
||||
Reuse --> Use["使用组件"]
|
||||
New --> Use
|
||||
Use --> Remove["移除组件"]
|
||||
Remove --> Reset["reset()重置"]
|
||||
Reset --> Pool
|
||||
Destroy["销毁实体"] --> Clear["清理所有组件"]
|
||||
Clear --> Recycle["回收到池中"]
|
||||
```
|
||||
|
||||
### 性能优化技巧
|
||||
|
||||
1. **组件池化**:避免频繁的垃圾回收
|
||||
2. **批量处理**:系统一次性处理所有符合条件的实体
|
||||
3. **条件筛选**:精确的过滤机制减少不必要的遍历
|
||||
4. **状态缓存**:避免重复计算相同的状态
|
||||
|
||||
### 开发最佳实践
|
||||
|
||||
| 实践 | 原则 | 示例 |
|
||||
|------|------|------|
|
||||
| 组件职责单一 | 一个组件只负责一种数据类型 | BattleMoveComp只处理移动相关数据 |
|
||||
| 系统逻辑集中 | 系统处理相关的业务逻辑 | BattleMoveSystem处理所有移动逻辑 |
|
||||
| 避免循环依赖 | 组件间不应有强耦合关系 | 通过系统协调组件交互 |
|
||||
| 合理使用筛选器 | 精确的过滤条件提高性能 | `ecs.allOf(BattleMoveComp, HeroViewComp)` |
|
||||
|
||||
## 扩展开发指南
|
||||
|
||||
### 定义新组件
|
||||
|
||||
创建新组件的基本步骤:
|
||||
|
||||
```typescript
|
||||
// 1. 继承ecs.Comp
|
||||
@ecs.register('NewComponent')
|
||||
export class NewComponent extends ecs.Comp {
|
||||
// 2. 定义组件数据
|
||||
public data: any;
|
||||
public enabled: boolean = true;
|
||||
|
||||
// 3. 实现reset方法
|
||||
reset() {
|
||||
this.data = null;
|
||||
this.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 在实体中声明
|
||||
export class NewEntity extends ecs.Entity {
|
||||
New: NewComponent;
|
||||
}
|
||||
```
|
||||
|
||||
### 创建新系统
|
||||
|
||||
系统开发的标准流程:
|
||||
|
||||
```typescript
|
||||
@ecs.register('NewSystem')
|
||||
export class NewSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
|
||||
// 1. 定义筛选条件
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(NewComponent, AnotherComponent);
|
||||
}
|
||||
|
||||
// 2. 实现更新逻辑
|
||||
update(e: ecs.Entity) {
|
||||
const comp = e.get(NewComponent);
|
||||
const another = e.get(AnotherComponent);
|
||||
|
||||
// 处理业务逻辑
|
||||
if (comp.enabled) {
|
||||
// ...具体逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 系统接口选择
|
||||
|
||||
| 接口 | 用途 | 使用场景 |
|
||||
|------|------|----------|
|
||||
| `ISystemUpdate` | 每帧更新 | 需要持续处理的逻辑 |
|
||||
| `IEntityEnterSystem` | 实体首次进入 | 初始化实体状态 |
|
||||
| `IEntityRemoveSystem` | 实体移除处理 | 清理资源和状态 |
|
||||
| `ISystemFirstUpdate` | 系统首次更新 | 系统初始化逻辑 |
|
||||
|
||||
### 扩展示例
|
||||
|
||||
基于现有架构扩展新功能:
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "扩展架构"
|
||||
NewComp["NewComponent<br/>新功能组件"]
|
||||
NewSys["NewSystem<br/>新功能系统"]
|
||||
ExistingComp["ExistingComponent<br/>现有组件"]
|
||||
end
|
||||
subgraph "系统集成"
|
||||
BattleMove["BattleMoveSystem"]
|
||||
NewSys --> BattleMove
|
||||
NewComp --> BattleMove
|
||||
end
|
||||
subgraph "实体组合"
|
||||
Entity["游戏实体"]
|
||||
Entity --> NewComp
|
||||
Entity --> ExistingComp
|
||||
end
|
||||
```
|
||||
|
||||
**章节来源**
|
||||
- [ecs.md](file://doc/ecs/ecs.md#L129-L272)
|
||||
|
||||
## 总结
|
||||
|
||||
本项目采用的ECS架构展现了现代游戏开发的最佳实践:
|
||||
|
||||
### 核心优势
|
||||
|
||||
1. **高度模块化**:通过组件组合创建复杂实体
|
||||
2. **清晰的职责分离**:数据存储在组件,逻辑处理在系统
|
||||
3. **优秀的性能表现**:批量处理和精确筛选机制
|
||||
4. **良好的可扩展性**:易于添加新功能和修改现有逻辑
|
||||
|
||||
### 架构特点
|
||||
|
||||
- **数据驱动**:系统通过筛选机制处理数据
|
||||
- **事件驱动**:支持实体进入和移除事件
|
||||
- **类型安全**:TypeScript提供完整的类型检查
|
||||
- **内存友好**:组件池化和自动回收机制
|
||||
|
||||
### 应用价值
|
||||
|
||||
ECS架构特别适合:
|
||||
- 复杂的游戏AI系统
|
||||
- 大量相似但行为不同的实体
|
||||
- 需要高性能处理的游戏逻辑
|
||||
- 需要频繁扩展和修改的系统
|
||||
|
||||
通过深入理解和正确应用ECS架构,开发者可以构建出既高效又易于维护的游戏系统,为玩家提供流畅的游戏体验。
|
||||
245
.qoder/repowiki/zh/content/技术架构/MVVM框架.md
Normal file
245
.qoder/repowiki/zh/content/技术架构/MVVM框架.md
Normal file
@@ -0,0 +1,245 @@
|
||||
# MVVM框架
|
||||
|
||||
<cite>
|
||||
**本文档引用的文件**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md)
|
||||
- [VMBase.md](file://doc/mvvm/VMBase.md)
|
||||
- [VMCustom.md](file://doc/mvvm/VMCustom.md)
|
||||
- [VMLabel.md](file://doc/mvvm/VMLabel.md)
|
||||
- [VMState.md](file://doc/mvvm/VMState.md)
|
||||
- [VMProgress.md](file://doc/mvvm/VMProgress.md)
|
||||
- [VMEvent.md](file://doc/mvvm/VMEvent.md)
|
||||
- [VMParent.md](file://doc/mvvm/VMParent.md)
|
||||
- [ViewModelScript.md](file://doc/mvvm/ViewModelScript.md)
|
||||
- [GameUIConfig.ts](file://assets/script/game/common/config/GameUIConfig.ts)
|
||||
- [HInfoComp.ts](file://assets/script/game/map/HInfoComp.ts)
|
||||
- [SIconComp.ts](file://assets/script/game/map/SIconComp.ts)
|
||||
</cite>
|
||||
|
||||
## 目录
|
||||
1. [简介](#简介)
|
||||
2. [核心组件](#核心组件)
|
||||
3. [数据模型注册与管理](#数据模型注册与管理)
|
||||
4. [UI组件与数据绑定机制](#ui组件与数据绑定机制)
|
||||
5. [UI自动更新机制](#ui自动更新机制)
|
||||
6. [编辑器配置与使用示例](#编辑器配置与使用示例)
|
||||
7. [高级功能与最佳实践](#高级功能与最佳实践)
|
||||
|
||||
## 简介
|
||||
|
||||
Oops Plugin Framework的MVVM实现机制旨在通过数据驱动的方式简化UI逻辑开发,实现无代码或低代码的UI开发模式。该框架借鉴了Vue的设计理念,通过观察者模式和事件系统,实现了数据与UI的双向绑定。开发者无需频繁使用`cc.find`、`getChildByName`、`getComponent`等繁琐的DOM操作,而是通过简单的配置即可实现复杂的UI逻辑。
|
||||
|
||||
该框架的核心优势在于其组件化的实现方式,提供了多个低耦合的VM组件,如VMLabel、VMState、VMProgress等,这些组件可以直接在Cocos Creator编辑器中挂载和配置,极大地提高了开发效率和可维护性。
|
||||
|
||||
**Section sources**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md#L1-L51)
|
||||
|
||||
## 核心组件
|
||||
|
||||
### ViewModel
|
||||
ViewModel是MVVM框架的核心模块,负责动态管理数据模型,并通过`cc.director.emit`通知游戏内的节点组件状态变化。它是数据模型的包装器,提供了数据绑定、监听和通知的功能。
|
||||
|
||||
### VMBase
|
||||
VMBase是所有VM组件的基类,实现了基础的数据绑定功能。其他VM组件如VMLabel、VMState等都继承自VMBase。它提供了路径监听、值变化回调等核心功能。
|
||||
|
||||
### VM组件类型
|
||||
- **VMCustom**:通用组件,可自定义绑定任意组件的属性
|
||||
- **VMLabel**:专门用于文本显示,支持模板语法和格式化
|
||||
- **VMState**:根据数据值控制节点状态(激活、显示、颜色等)
|
||||
- **VMProgress**:用于进度条显示,支持双向绑定
|
||||
- **VMEvent**:在值变化时触发事件,调用其他组件的方法
|
||||
- **VMParent**:用于定义局部范围的ViewModel数据
|
||||
|
||||
**Section sources**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md#L14-L29)
|
||||
- [VMBase.md](file://doc/mvvm/VMBase.md#L1-L38)
|
||||
|
||||
## 数据模型注册与管理
|
||||
|
||||
### 数据模型注册
|
||||
通过`VM.add(data, 'tag')`方法注册数据模型,其中`data`是要绑定的数据对象,`tag`是该数据模型的唯一标识标签。注册后,该数据模型将被VMManager管理,可以通过标签进行访问和操作。
|
||||
|
||||
```typescript
|
||||
// 构建数据对象
|
||||
let data = {
|
||||
name: 'user',
|
||||
gold: 12200,
|
||||
info: {
|
||||
id: 0
|
||||
}
|
||||
}
|
||||
// 创建VM对象并添加到VMManager进行管理,标记为'user'标签
|
||||
VM.add(data, 'user');
|
||||
```
|
||||
|
||||
### 数据模型管理API
|
||||
- `add(data, tag)`:创建并添加ViewModel对象
|
||||
- `get(tag)`:获取指定标签的ViewModel实例
|
||||
- `remove(tag)`:移除指定标签的ViewModel对象
|
||||
- `setValue(path, value)`:设置值(支持全局路径)
|
||||
- `addValue(path, value)`:累加值
|
||||
- `getValue(path, defaultValue)`:获取值(支持默认值)
|
||||
|
||||
**Section sources**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md#L30-L51)
|
||||
- [ViewModelScript.md](file://doc/mvvm/ViewModelScript.md#L20-L67)
|
||||
|
||||
## UI组件与数据绑定机制
|
||||
|
||||
### VMCustom组件
|
||||
VMCustom是通用的绑定组件,可以绑定任意组件的任意属性。在编辑器中挂载VMCustom组件后,需要设置以下属性:
|
||||
- **Controller**:启用后支持双向绑定
|
||||
- **Watch Path**:要监听的数据路径
|
||||
- **Component Name**:要绑定的组件名称
|
||||
- **Component Property**:要绑定的组件属性
|
||||
- **refreshRate**:刷新频率(仅在Controller启用时生效)
|
||||
|
||||
### VMLabel组件
|
||||
VMLabel专门用于文本显示,支持模板语法和多种格式化方式。其主要特性包括:
|
||||
- 支持单路径和多路径监听
|
||||
- 使用`{{0}}`、`{{1}}`等模板语法
|
||||
- 支持多种格式化:`int`(整数)、`fix(n)`(小数位数)、`kmbt`(千位分隔)、`per`(百分比)等
|
||||
|
||||
### VMState组件
|
||||
VMState根据数据值控制节点状态,可以实现条件显示、颜色变化等功能。主要配置包括:
|
||||
- **Watch Path**:监听的数据路径
|
||||
- **Condition**:判断条件(如等于、大于等)
|
||||
- **Value Action**:满足条件时执行的操作(如节点激活、可见性、不透明度、颜色等)
|
||||
|
||||
### VMProgress组件
|
||||
VMProgress用于进度条显示,需要设置两个监听路径(最小值和最大值)。其配置与VMCustom类似,但专门针对进度条组件。
|
||||
|
||||
**Section sources**
|
||||
- [VMCustom.md](file://doc/mvvm/VMCustom.md#L1-L17)
|
||||
- [VMLabel.md](file://doc/mvvm/VMLabel.md#L1-L33)
|
||||
- [VMState.md](file://doc/mvvm/VMState.md#L1-L41)
|
||||
- [VMProgress.md](file://doc/mvvm/VMProgress.md#L1-L14)
|
||||
|
||||
## UI自动更新机制
|
||||
|
||||
### 事件驱动更新
|
||||
当数据模型的值发生变化时,ViewModel会通过`cc.director.emit`触发事件,通知所有监听该路径的组件进行更新。这种事件驱动的机制确保了数据变化能够实时反映到UI上。
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[修改数据模型] --> B{数据变化}
|
||||
B --> C[ViewModel检测到变化]
|
||||
C --> D[cc.director.emit触发事件]
|
||||
D --> E[VM组件接收到通知]
|
||||
E --> F[更新UI显示]
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [ViewModelScript.md](file://doc/mvvm/ViewModelScript.md#L69-L107)
|
||||
|
||||
### 双向绑定
|
||||
通过启用VMCustom组件的Controller功能,可以实现UI到数据的反向绑定。例如,当用户拖动进度条时,不仅可以更新UI显示,还可以自动更新对应的数据模型值。
|
||||
|
||||
### 局部数据管理
|
||||
对于局部组件,可以继承VMParent组件来管理局部数据。这种方式适用于弹窗等需要独立数据管理的场景。VMParent会在`onLoad`时自动将`*.name`形式的路径中的`*`替换为实际的ViewModel标签。
|
||||
|
||||
**Section sources**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md#L30-L51)
|
||||
- [VMParent.md](file://doc/mvvm/VMParent.md#L1-L23)
|
||||
|
||||
## 编辑器配置与使用示例
|
||||
|
||||
### HInfoComp组件分析
|
||||
HInfoComp是英雄信息显示组件,展示了如何通过VM组件实现数据绑定:
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class HInfoComp {
|
||||
+h_uuid : number
|
||||
+heroNodes : Node[]
|
||||
+update_data(uuid : number)
|
||||
+load_all_hero(uuid : number)
|
||||
+next_hero()
|
||||
+prev_hero()
|
||||
}
|
||||
HInfoComp --> VMLabel : "使用"
|
||||
HInfoComp --> VMProgress : "使用"
|
||||
HInfoComp --> VMState : "使用"
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [HInfoComp.ts](file://assets/script/game/map/HInfoComp.ts#L1-L343)
|
||||
|
||||
### SIconComp组件分析
|
||||
SIconComp是技能图标组件,展示了简单的数据绑定:
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class SIconCompComp {
|
||||
+update_data(s_uuid : number)
|
||||
}
|
||||
SIconCompComp --> VMCustom : "使用"
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [SIconComp.ts](file://assets/script/game/map/SIconComp.ts#L1-L27)
|
||||
|
||||
### 编辑器配置步骤
|
||||
1. 在节点上添加VM组件(如VMLabel、VMState等)
|
||||
2. 设置Watch Path为对应的数据路径(如`user.gold`)
|
||||
3. 配置组件特定属性(如VMLabel的模板格式)
|
||||
4. 运行游戏,数据变化将自动反映到UI上
|
||||
|
||||
### 数值格式化示例
|
||||
```typescript
|
||||
// 在VMLabel中使用模板语法
|
||||
// {{0:int}} 显示整数
|
||||
// {{0:fix(2)}} 显示两位小数
|
||||
// {{0:kmbt}} 使用K/M/B/T单位缩写
|
||||
// {{0:per}} 显示百分比
|
||||
// {{0:sep}} 使用千位分隔符
|
||||
```
|
||||
|
||||
### 节点状态切换示例
|
||||
```typescript
|
||||
// 使用VMState控制节点状态
|
||||
// Condition: value >= 30
|
||||
// Value Action: NODE_VISIBLE
|
||||
// Watch Nodes: 指定要控制的节点
|
||||
```
|
||||
|
||||
### 进度条更新示例
|
||||
```typescript
|
||||
// 使用VMProgress更新进度条
|
||||
// Watch Path Arr: [currentValue, maxValue]
|
||||
// Component Name: ProgressBar
|
||||
// Component Property: progress
|
||||
```
|
||||
|
||||
**Section sources**
|
||||
- [HInfoComp.ts](file://assets/script/game/map/HInfoComp.ts#L1-L343)
|
||||
- [SIconComp.ts](file://assets/script/game/map/SIconComp.ts#L1-L27)
|
||||
|
||||
## 高级功能与最佳实践
|
||||
|
||||
### VMEvent组件
|
||||
VMEvent组件可以在值变化时触发事件,调用其他组件的方法。适用于需要自定义处理逻辑的场景,如数值变化时播放动画、播放音效等。
|
||||
|
||||
### VMModify组件
|
||||
VMModify组件提供了便捷的数据修改方法,可以通过事件调用实现数据的增减操作:
|
||||
- `vAddInt`:增加整数
|
||||
- `vSubInt`:减少整数
|
||||
- `vMulInt`:乘以整数
|
||||
- `vDivInt`:除以整数
|
||||
- `vString`:设置字符串
|
||||
- `vNumber`:设置数值
|
||||
|
||||
### 性能优化建议
|
||||
1. 合理使用局部数据管理(VMParent),避免全局数据的过度监听
|
||||
2. 控制刷新频率(refreshRate),避免不必要的频繁更新
|
||||
3. 及时移除不再使用的ViewModel,防止内存泄漏
|
||||
4. 避免过度嵌套使用VMParent,以免影响性能
|
||||
|
||||
### 调试技巧
|
||||
1. 使用`VM.get(tag)`获取ViewModel实例进行调试
|
||||
2. 通过`vm.active = false`临时关闭通知功能进行问题排查
|
||||
3. 利用编辑器的组件配置界面直观地查看和修改绑定关系
|
||||
|
||||
**Section sources**
|
||||
- [VMEvent.md](file://doc/mvvm/VMEvent.md#L1-L21)
|
||||
- [VMModify.md](file://doc/mvvm/VMModify.md#L13-L29)
|
||||
465
.qoder/repowiki/zh/content/技术架构/单例模式管理.md
Normal file
465
.qoder/repowiki/zh/content/技术架构/单例模式管理.md
Normal file
@@ -0,0 +1,465 @@
|
||||
# 单例模式管理
|
||||
|
||||
<cite>
|
||||
**本文档中引用的文件**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts)
|
||||
- [Main.ts](file://assets/script/Main.ts)
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts)
|
||||
- [GameMap.ts](file://assets/script/game/map/GameMap.ts)
|
||||
- [WxCloudApi.ts](file://assets/script/game/wx_clound_client_api/WxCloudApi.ts)
|
||||
- [GameEvent.ts](file://assets/script/game/common/config/GameEvent.ts)
|
||||
</cite>
|
||||
|
||||
## 目录
|
||||
1. [概述](#概述)
|
||||
2. [系统架构](#系统架构)
|
||||
3. [SingletonModuleComp核心设计](#singletonmodulecompcore设计)
|
||||
4. [初始化流程分析](#初始化流程分析)
|
||||
5. [MVVM数据绑定机制](#mvvm数据绑定机制)
|
||||
6. [云同步与数据管理](#云同步与数据管理)
|
||||
7. [单例模式实现](#单例模式实现)
|
||||
8. [数据操作方法](#数据操作方法)
|
||||
9. [性能优化与最佳实践](#性能优化与最佳实践)
|
||||
10. [总结](#总结)
|
||||
|
||||
## 概述
|
||||
|
||||
SingletonModuleComp是游戏核心数据容器,采用单例模式设计,作为整个游戏应用的全局状态管理中心。它负责集中管理initialize、map、vmdata、heros等关键对象和状态,通过MVVM数据绑定机制实现视图与数据的同步,支持微信云同步功能,确保游戏数据的一致性和可靠性。
|
||||
|
||||
## 系统架构
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "游戏启动流程"
|
||||
Main[Main.ts] --> Run[run方法]
|
||||
Run --> InitSMC[初始化SMC]
|
||||
InitSMC --> VMAdd[vmAdd绑定]
|
||||
end
|
||||
subgraph "SingletonModuleComp核心"
|
||||
SMC[SingletonModuleComp] --> VMData[vmdata数据层]
|
||||
SMC --> Initialize[initialize模块]
|
||||
SMC --> Map[GameMap]
|
||||
SMC --> Cloud[WxCloudApi]
|
||||
end
|
||||
subgraph "数据流"
|
||||
VMData --> MVVM[MVVM绑定]
|
||||
Cloud --> Sync[云同步]
|
||||
Sync --> Local[本地数据]
|
||||
end
|
||||
subgraph "外部依赖"
|
||||
ECS[ECS框架] --> SMC
|
||||
Oops[Oops框架] --> SMC
|
||||
WX[微信API] --> Cloud
|
||||
end
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L25-L30)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L20-L195)
|
||||
|
||||
**章节来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L1-L41)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L1-L195)
|
||||
|
||||
## SingletonModuleComp核心设计
|
||||
|
||||
### 类结构与职责
|
||||
|
||||
SingletonModuleComp继承自ecs.Comp,作为ECS框架中的核心组件,承担以下主要职责:
|
||||
|
||||
- **全局状态管理**:维护游戏的核心状态数据
|
||||
- **数据持久化**:处理本地存储和云端同步
|
||||
- **业务逻辑协调**:管理英雄、金币、任务等业务数据
|
||||
- **事件通信**:通过GameEvent分发状态变更事件
|
||||
|
||||
### 核心数据结构
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class SingletonModuleComp {
|
||||
+Initialize initialize
|
||||
+GameMap map
|
||||
+string openid
|
||||
+object mission
|
||||
+array guides
|
||||
+number current_guide
|
||||
+object data
|
||||
+number fight_hero
|
||||
+array heros
|
||||
+array monsters
|
||||
+object vmdata
|
||||
+vmAdd() void
|
||||
+reset() void
|
||||
+updateGold(gold) boolean
|
||||
+addHero(hero_uuid) boolean
|
||||
+updateFihgtHero(heroId) boolean
|
||||
+finishGuide(index) void
|
||||
+updateCloudData() boolean
|
||||
+getCloudData() void
|
||||
+overrideLocalDataWithRemote(CloudData) void
|
||||
}
|
||||
class VMData {
|
||||
+boolean game_over
|
||||
+boolean game_pause
|
||||
+object mission_data
|
||||
+number gold
|
||||
}
|
||||
class MissionData {
|
||||
+number mon_num
|
||||
+number hero_num
|
||||
+number wave_time_num
|
||||
+boolean in_fight
|
||||
+number fight_time
|
||||
+number level
|
||||
+number max_mission
|
||||
+number coin
|
||||
}
|
||||
SingletonModuleComp --> VMData : "包含"
|
||||
VMData --> MissionData : "包含"
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L20-L87)
|
||||
|
||||
### 数据层次结构
|
||||
|
||||
| 数据类型 | 字段名 | 描述 | 用途 |
|
||||
|---------|--------|------|------|
|
||||
| 基础状态 | `mission` | 游戏任务状态 | 控制游戏流程状态 |
|
||||
| 引导系统 | `guides` | 引导步骤记录 | 管理新手引导进度 |
|
||||
| 游戏数据 | `data` | 基础游戏属性 | 存储分数、钻石、肉等 |
|
||||
| 英雄管理 | `heros` | 英雄列表 | 管理解锁的英雄 |
|
||||
| 战斗配置 | `fight_hero` | 当前战斗英雄 | 指定出战英雄 |
|
||||
| 地图数据 | `monsters` | 怪物配置 | 关卡怪物数据 |
|
||||
|
||||
**章节来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L25-L87)
|
||||
|
||||
## 初始化流程分析
|
||||
|
||||
### Main.ts中的启动流程
|
||||
|
||||
游戏启动时,Main类的run方法负责初始化SingletonModuleComp实例:
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Main as Main.ts
|
||||
participant ECS as ECS框架
|
||||
participant SMC as SingletonModuleComp
|
||||
participant VM as ViewModel
|
||||
Main->>ECS : 获取Initialize实体
|
||||
Main->>SMC : 设置initialize属性
|
||||
Main->>SMC : 调用vmAdd()
|
||||
SMC->>VM : 注册vmdata到VMManager
|
||||
VM-->>SMC : 绑定完成
|
||||
SMC-->>Main : 初始化完成
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L25-L30)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L89-L91)
|
||||
|
||||
### Initialize.ts的初始化过程
|
||||
|
||||
Initialize类负责统一的游戏数据加载流程,根据运行环境选择不同的数据源:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([开始初始化]) --> CheckEnv{检查运行环境}
|
||||
CheckEnv --> |微信客户端| LoadCloud[加载云端数据]
|
||||
CheckEnv --> |非微信客户端| LoadLocal[加载本地调试数据]
|
||||
LoadCloud --> InitWX[初始化微信云环境]
|
||||
InitWX --> WXLogin[微信登录]
|
||||
WXLogin --> GetCloudData[获取云端数据]
|
||||
GetCloudData --> OverrideLocal[覆盖本地数据]
|
||||
LoadLocal --> LoadDebug[加载本地调试数据]
|
||||
OverrideLocal --> Complete([初始化完成])
|
||||
LoadDebug --> Complete
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L85-L120)
|
||||
|
||||
**章节来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L25-L30)
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L85-L120)
|
||||
|
||||
## MVVM数据绑定机制
|
||||
|
||||
### ViewModel集成
|
||||
|
||||
SingletonModuleComp通过VM.add方法将vmdata注册到ViewModel管理系统:
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
subgraph "MVVM架构"
|
||||
VMData[vmdata对象] --> VMManager[VMManager]
|
||||
VMManager --> VMBase[VMBase组件]
|
||||
VMBase --> UI[UI组件]
|
||||
end
|
||||
subgraph "绑定类型"
|
||||
VMCustom[VMCustom] --> Label[cc.Label]
|
||||
VMProgress[VMProgress] --> ProgressBar[cc.ProgressBar]
|
||||
VMState[VMState] --> NodeState[节点状态]
|
||||
end
|
||||
VMBase --> VMCustom
|
||||
VMBase --> VMProgress
|
||||
VMBase --> VMState
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L89-L91)
|
||||
|
||||
### 数据绑定示例
|
||||
|
||||
| 绑定类型 | 监听路径 | 绑定组件 | 更新时机 |
|
||||
|---------|----------|----------|----------|
|
||||
| 金币显示 | `vmdata.gold` | VMLabel | 每次金币变更 |
|
||||
| 任务进度 | `vmdata.mission_data.coin` | VMProgress | 金币变化时 |
|
||||
| 游戏状态 | `vmdata.game_over` | VMState | 状态切换时 |
|
||||
| 英雄数量 | `vmdata.mission_data.hero_num` | VMLabel | 英雄加入时 |
|
||||
|
||||
### 事件驱动更新
|
||||
|
||||
系统通过GameEvent实现数据变更的通知机制:
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant User as 用户操作
|
||||
participant SMC as SingletonModuleComp
|
||||
participant VM as ViewModel
|
||||
participant UI as UI组件
|
||||
User->>SMC : updateGold(100)
|
||||
SMC->>SMC : 修改vmdata.gold
|
||||
SMC->>VM : dispatchEvent(GOLD_UPDATE)
|
||||
VM->>UI : 更新绑定组件
|
||||
UI-->>User : 显示新数值
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L175-L185)
|
||||
- [GameEvent.ts](file://assets/script/game/common/config/GameEvent.ts#L65-L66)
|
||||
|
||||
**章节来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L89-L91)
|
||||
- [GameEvent.ts](file://assets/script/game/common/config/GameEvent.ts#L65-L66)
|
||||
|
||||
## 云同步与数据管理
|
||||
|
||||
### 微信云API集成
|
||||
|
||||
系统集成了WxCloudApi用于云端数据同步:
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class WxCloudApi {
|
||||
+init(env) void
|
||||
+login() Promise
|
||||
+save(gameData) Promise
|
||||
+get() Promise
|
||||
}
|
||||
class CloudData {
|
||||
+string openid
|
||||
+GameDate data
|
||||
}
|
||||
class GameDate {
|
||||
+number gold
|
||||
+any heros
|
||||
+number fight_hero
|
||||
}
|
||||
WxCloudApi --> CloudData : "处理"
|
||||
CloudData --> GameDate : "包含"
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [WxCloudApi.ts](file://assets/script/game/wx_clound_client_api/WxCloudApi.ts#L1-L94)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L11-L18)
|
||||
|
||||
### 数据同步流程
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant SMC as SingletonModuleComp
|
||||
participant WX as WxCloudApi
|
||||
participant Cloud as 微信云数据库
|
||||
SMC->>WX : updateCloudData()
|
||||
WX->>Cloud : save(gameData)
|
||||
Cloud-->>WX : 保存结果
|
||||
WX-->>SMC : 返回状态
|
||||
alt 同步成功
|
||||
SMC->>SMC : 继续业务逻辑
|
||||
else 同步失败
|
||||
SMC->>SMC : 回滚操作
|
||||
SMC->>SMC : 显示错误提示
|
||||
end
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L93-L118)
|
||||
|
||||
### 数据覆盖策略
|
||||
|
||||
系统实现了智能的数据覆盖机制:
|
||||
|
||||
| 数据类型 | 覆盖条件 | 回滚策略 |
|
||||
|---------|----------|----------|
|
||||
| openid | 存在时覆盖 | 无 |
|
||||
| gold | 存在时覆盖 | 撤销变更 |
|
||||
| heros | 存在时覆盖 | 删除新增英雄 |
|
||||
| fight_hero | 存在时覆盖 | 恢复原配置 |
|
||||
|
||||
**章节来源**
|
||||
- [WxCloudApi.ts](file://assets/script/game/wx_clound_client_api/WxCloudApi.ts#L1-L94)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L93-L118)
|
||||
|
||||
## 单例模式实现
|
||||
|
||||
### ECS框架集成
|
||||
|
||||
SingletonModuleComp通过ECS.register装饰器注册为单例组件:
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "ECS生命周期"
|
||||
Register[ecs.register] --> Create[创建实例]
|
||||
Create --> Get[ecs.getSingleton]
|
||||
Get --> Use[全局使用]
|
||||
end
|
||||
subgraph "单例保证"
|
||||
Instance[SingletonInstance] --> GlobalVar[smc全局变量]
|
||||
GlobalVar --> Consistency[状态一致性]
|
||||
end
|
||||
Register --> Instance
|
||||
Create --> Instance
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L19-L20)
|
||||
|
||||
### 全局访问机制
|
||||
|
||||
通过全局变量smc提供便捷的单例访问:
|
||||
|
||||
```typescript
|
||||
// 全局访问方式
|
||||
import { smc } from 'SingletonModuleComp';
|
||||
|
||||
// 使用示例
|
||||
smc.updateGold(100);
|
||||
smc.addHero(5003);
|
||||
```
|
||||
|
||||
### 状态一致性保障
|
||||
|
||||
单例模式确保:
|
||||
- **全局唯一性**:整个应用中只有一个实例
|
||||
- **状态同步**:所有组件访问相同的数据状态
|
||||
- **内存效率**:避免重复创建和销毁
|
||||
- **线程安全**:在单线程环境下天然安全
|
||||
|
||||
**章节来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L19-L20)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L195)
|
||||
|
||||
## 数据操作方法
|
||||
|
||||
### 安全的数据修改
|
||||
|
||||
系统提供了多层安全保障的数据操作方法:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Operation[数据操作请求] --> Validate{验证操作}
|
||||
Validate --> |有效| LocalUpdate[本地更新]
|
||||
Validate --> |无效| Reject[拒绝操作]
|
||||
LocalUpdate --> CheckWX{微信环境?}
|
||||
CheckWX --> |是| CloudSync[云端同步]
|
||||
CheckWX --> |否| DispatchEvent[分发事件]
|
||||
CloudSync --> SyncResult{同步结果}
|
||||
SyncResult --> |成功| DispatchEvent
|
||||
SyncResult --> |失败| Rollback[回滚操作]
|
||||
Rollback --> ErrorMsg[错误提示]
|
||||
DispatchEvent --> Complete[操作完成]
|
||||
Reject --> ErrorMsg
|
||||
ErrorMsg --> Complete
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L120-L194)
|
||||
|
||||
### 核心数据操作方法
|
||||
|
||||
| 方法名 | 参数 | 功能 | 安全特性 |
|
||||
|-------|------|------|----------|
|
||||
| `updateGold` | `gold: number` | 增减金币 | 自动同步失败回滚 |
|
||||
| `addHero` | `hero_uuid: number` | 添加英雄 | 检查重复,同步失败删除 |
|
||||
| `updateFihgtHero` | `heroId: number` | 设置战斗英雄 | 微信环境自动同步 |
|
||||
| `finishGuide` | `index: number` | 完成引导步骤 | 计划中云端存储 |
|
||||
|
||||
### 错误处理机制
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
subgraph "错误类型"
|
||||
Network[网络错误]
|
||||
Sync[同步错误]
|
||||
Data[数据错误]
|
||||
Security[安全错误]
|
||||
end
|
||||
subgraph "处理策略"
|
||||
Retry[重试机制]
|
||||
Rollback[数据回滚]
|
||||
Notify[用户通知]
|
||||
Log[错误日志]
|
||||
end
|
||||
Network --> Retry
|
||||
Sync --> Rollback
|
||||
Data --> Notify
|
||||
Security --> Log
|
||||
```
|
||||
|
||||
**章节来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L120-L194)
|
||||
|
||||
## 性能优化与最佳实践
|
||||
|
||||
### 内存管理
|
||||
|
||||
- **延迟初始化**:仅在需要时创建子组件
|
||||
- **及时清理**:通过reset方法清理不必要的数据
|
||||
- **弱引用**:避免循环引用导致的内存泄漏
|
||||
|
||||
### 数据同步优化
|
||||
|
||||
- **批量操作**:合并多个数据变更减少同步次数
|
||||
- **增量更新**:只传输变更的数据而非全量数据
|
||||
- **缓存策略**:本地缓存常用数据减少网络请求
|
||||
|
||||
### MVVM性能优化
|
||||
|
||||
- **路径监听**:精确监听必要的数据路径
|
||||
- **事件节流**:限制频繁的数据变更事件
|
||||
- **组件复用**:合理使用UI组件的复用机制
|
||||
|
||||
### 最佳实践建议
|
||||
|
||||
1. **数据验证**:在修改数据前进行有效性检查
|
||||
2. **错误恢复**:实现完善的错误恢复机制
|
||||
3. **日志记录**:记录关键操作的日志便于调试
|
||||
4. **版本兼容**:考虑数据结构的向前兼容性
|
||||
|
||||
## 总结
|
||||
|
||||
SingletonModuleComp作为游戏的核心数据容器,通过单例模式确保了全局状态的一致性,结合MVVM数据绑定机制实现了视图与数据的高效同步。其设计充分考虑了移动端游戏的特点,支持微信云同步功能,提供了安全可靠的数据操作方法。
|
||||
|
||||
### 核心优势
|
||||
|
||||
- **全局统一**:单例模式保证状态一致性
|
||||
- **数据绑定**:MVVM机制简化UI开发
|
||||
- **云端同步**:支持跨设备数据共享
|
||||
- **安全可靠**:多重验证和回滚机制
|
||||
- **易于扩展**:模块化设计便于功能扩展
|
||||
|
||||
### 应用价值
|
||||
|
||||
该设计模式为游戏开发提供了标准化的状态管理解决方案,特别适用于需要云端同步和复杂数据交互的移动端游戏项目。通过合理的架构设计和性能优化,确保了游戏在各种环境下的稳定运行。
|
||||
451
.qoder/repowiki/zh/content/技术架构/技术架构.md
Normal file
451
.qoder/repowiki/zh/content/技术架构/技术架构.md
Normal file
@@ -0,0 +1,451 @@
|
||||
# 技术架构
|
||||
|
||||
<cite>
|
||||
**本文档中引用的文件**
|
||||
- [Main.ts](file://assets/script/Main.ts)
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts)
|
||||
- [EcsPositionSystem.ts](file://assets/script/game/common/ecs/position/EcsPositionSystem.ts)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts)
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts)
|
||||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts)
|
||||
- [HeroModelComp.ts](file://assets/script/game/hero/HeroModelComp.ts)
|
||||
- [MapModelComp.ts](file://assets/script/game/map/model/MapModelComp.ts)
|
||||
- [MapViewComp.ts](file://assets/script/game/map/view/MapViewComp.ts)
|
||||
- [ecs.md](file://doc/ecs/ecs.md)
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md)
|
||||
</cite>
|
||||
|
||||
## 目录
|
||||
1. [引言](#引言)
|
||||
2. [项目架构概览](#项目架构概览)
|
||||
3. [ECS架构支柱](#ecs架构支柱)
|
||||
4. [MVVM架构支柱](#mvvm架构支柱)
|
||||
5. [单例模式架构支柱](#单例模式架构支柱)
|
||||
6. [核心组件深度分析](#核心组件深度分析)
|
||||
7. [架构协作机制](#架构协作机制)
|
||||
8. [生命周期管理](#生命周期管理)
|
||||
9. [性能优化策略](#性能优化策略)
|
||||
10. [总结](#总结)
|
||||
|
||||
## 引言
|
||||
|
||||
本项目采用了一套高度集成的技术架构,以ECS(实体-组件-系统)、MVVM(模型-视图-视图模型)和单例模式三大支柱为核心,构建了一个高度解耦、可扩展的游戏引擎。这种架构设计不仅提升了代码的可维护性,还为游戏逻辑的复杂性管理提供了强有力的支撑。
|
||||
|
||||
## 项目架构概览
|
||||
|
||||
项目采用了分层架构设计,每一层都有明确的职责分工:
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "表现层"
|
||||
UI[用户界面]
|
||||
GUI[图形用户界面]
|
||||
end
|
||||
subgraph "业务逻辑层"
|
||||
ECS[ECS系统]
|
||||
MVVM[MVVM框架]
|
||||
SM[单例模块]
|
||||
end
|
||||
subgraph "数据层"
|
||||
DATA[游戏数据]
|
||||
CONFIG[配置数据]
|
||||
RESOURCES[资源管理]
|
||||
end
|
||||
UI --> ECS
|
||||
GUI --> MVVM
|
||||
ECS --> DATA
|
||||
MVVM --> DATA
|
||||
SM --> CONFIG
|
||||
DATA --> RESOURCES
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L1-L41)
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L1-L207)
|
||||
|
||||
**章节来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L1-L41)
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L1-L207)
|
||||
|
||||
## ECS架构支柱
|
||||
|
||||
### ECS核心概念
|
||||
|
||||
ECS(Entity-Component-System)架构是一种面向组件的设计模式,将游戏对象分解为三个核心元素:
|
||||
|
||||
1. **实体(Entity)**:纯粹的标识符,用于组合组件
|
||||
2. **组件(Component)**:携带数据的简单容器
|
||||
3. **系统(System)**:处理具有特定组件组合的实体
|
||||
|
||||
### 组件系统设计
|
||||
|
||||
项目中的组件系统展现了高度的模块化和可复用性:
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class ecs_Comp {
|
||||
+reset() void
|
||||
+canRecycle boolean
|
||||
+ent Entity
|
||||
}
|
||||
class BattleMoveComp {
|
||||
+direction number
|
||||
+targetX number
|
||||
+moving boolean
|
||||
+reset() void
|
||||
}
|
||||
class HeroViewComp {
|
||||
+status string
|
||||
+hero_uuid number
|
||||
+hp number
|
||||
+mp number
|
||||
+Attrs any
|
||||
+BUFFS any
|
||||
+update(dt) void
|
||||
}
|
||||
class HeroModelComp {
|
||||
+reset() void
|
||||
}
|
||||
ecs_Comp <|-- BattleMoveComp
|
||||
ecs_Comp <|-- HeroViewComp
|
||||
ecs_Comp <|-- HeroModelComp
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [BattleMoveComp.ts](file://assets/script/game/common/ecs/position/BattleMoveComp.ts#L1-L16)
|
||||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L780)
|
||||
- [HeroModelComp.ts](file://assets/script/game/hero/HeroModelComp.ts#L1-L13)
|
||||
|
||||
### 系统协作机制
|
||||
|
||||
系统通过过滤器机制实现高效的实体管理:
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant RS as RootSystem
|
||||
participant BS as BattleMoveSystem
|
||||
participant ECS as ECS引擎
|
||||
participant Entity as 实体
|
||||
participant Comp as 组件
|
||||
RS->>ECS : 执行系统
|
||||
ECS->>BS : filter()匹配实体
|
||||
BS->>Entity : 获取BattleMoveComp
|
||||
BS->>Entity : 获取HeroViewComp
|
||||
BS->>Entity : update()处理逻辑
|
||||
Entity->>Comp : 更新状态
|
||||
Comp-->>Entity : 返回结果
|
||||
Entity-->>BS : 完成处理
|
||||
BS-->>RS : 系统执行完毕
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L1-L272)
|
||||
- [EcsPositionSystem.ts](file://assets/script/game/common/ecs/position/EcsPositionSystem.ts#L1-L9)
|
||||
|
||||
### ECS解耦优势
|
||||
|
||||
ECS架构在游戏逻辑中实现了以下解耦效果:
|
||||
|
||||
1. **数据与行为分离**:组件只负责数据存储,系统负责逻辑处理
|
||||
2. **灵活组合**:通过不同组件的组合实现多样化的游戏对象
|
||||
3. **高效处理**:系统可以批量处理具有相同组件组合的实体
|
||||
4. **易于扩展**:新增功能只需添加新组件和系统,无需修改现有代码
|
||||
|
||||
**章节来源**
|
||||
- [ecs.md](file://doc/ecs/ecs.md#L1-L357)
|
||||
- [BattleMoveSystem.ts](file://assets/script/game/common/ecs/position/BattleMoveSystem.ts#L1-L272)
|
||||
|
||||
## MVVM架构支柱
|
||||
|
||||
### Oops Plugin Framework概述
|
||||
|
||||
Oops Plugin Framework是一个专为Cocos Creator设计的MVVM框架,提供了完整的双向数据绑定解决方案:
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
subgraph "ViewModel层"
|
||||
VM[ViewModel]
|
||||
VMData[数据模型]
|
||||
end
|
||||
subgraph "View层"
|
||||
UI[用户界面]
|
||||
Components[UI组件]
|
||||
end
|
||||
subgraph "Binding层"
|
||||
VMCustom[VMCustom]
|
||||
VMLabel[VMLabel]
|
||||
VMState[VMState]
|
||||
VMProgress[VMProgress]
|
||||
end
|
||||
VM --> VMData
|
||||
VMData --> VMCustom
|
||||
VMCustom --> UI
|
||||
VMLabel --> UI
|
||||
VMState --> UI
|
||||
VMProgress --> UI
|
||||
Components --> VMCustom
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md#L1-L52)
|
||||
|
||||
### 双向绑定机制
|
||||
|
||||
MVVM框架通过观察者模式实现了数据的自动同步:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([数据变更]) --> Notify[通知ViewModel]
|
||||
Notify --> Emit[触发cc.director.emit]
|
||||
Emit --> Listen[UI组件监听]
|
||||
Listen --> Update[更新界面显示]
|
||||
Update --> End([界面刷新完成])
|
||||
Start2([用户交互]) --> Trigger[触发事件]
|
||||
Trigger --> Modify[修改数据模型]
|
||||
Modify --> Notify
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md#L30-L51)
|
||||
|
||||
### ViewModel数据管理
|
||||
|
||||
项目中的数据模型通过SingletonModuleComp进行统一管理:
|
||||
|
||||
| 数据类型 | 管理方式 | 生命周期 | 用途 |
|
||||
|---------|---------|---------|------|
|
||||
| 游戏状态数据 | VM.add() | 全局 | UI绑定、状态同步 |
|
||||
| 用户配置数据 | 直接访问 | 持久化 | 设置保存、偏好记录 |
|
||||
| 实时战斗数据 | 动态更新 | 帧级 | 战斗界面、技能显示 |
|
||||
| 网络同步数据 | 异步处理 | 会话级 | 云端保存、数据同步 |
|
||||
|
||||
**章节来源**
|
||||
- [MvvmInfo.md](file://doc/mvvm/MvvmInfo.md#L1-L52)
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L1-L195)
|
||||
|
||||
## 单例模式架构支柱
|
||||
|
||||
### 全局模块管理
|
||||
|
||||
SingletonModuleComp作为全局状态管理的核心,实现了以下功能:
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class SingletonModuleComp {
|
||||
+initialize Initialize
|
||||
+map GameMap
|
||||
+openid string
|
||||
+mission any
|
||||
+guides any
|
||||
+data any
|
||||
+fight_hero number
|
||||
+heros any
|
||||
+monsters any
|
||||
+vmdata any
|
||||
+vmAdd() void
|
||||
+reset() void
|
||||
+updateCloudData() boolean
|
||||
+overrideLocalDataWithRemote() void
|
||||
+addHero() boolean
|
||||
+updateFihgtHero() boolean
|
||||
+updateGold() boolean
|
||||
}
|
||||
class smc {
|
||||
<<singleton>>
|
||||
}
|
||||
SingletonModuleComp --> smc : 实例
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L1-L195)
|
||||
|
||||
### 单例模式优势
|
||||
|
||||
1. **全局访问**:任何地方都可以通过smc访问全局状态
|
||||
2. **状态一致性**:确保整个应用的状态统一
|
||||
3. **内存效率**:避免重复创建相同功能的对象
|
||||
4. **生命周期管理**:集中管理应用的初始化和销毁
|
||||
|
||||
### 数据同步机制
|
||||
|
||||
项目实现了云端与本地数据的双向同步:
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Client as 客户端
|
||||
participant SMC as SingletonModuleComp
|
||||
participant Cloud as 微信云API
|
||||
participant Local as 本地存储
|
||||
Client->>SMC : updateCloudData()
|
||||
SMC->>Cloud : save(gameData)
|
||||
Cloud-->>SMC : 保存结果
|
||||
SMC-->>Client : 返回状态
|
||||
Client->>SMC : getCloudData()
|
||||
SMC->>Cloud : get()
|
||||
Cloud-->>SMC : 云端数据
|
||||
SMC->>SMC : overrideLocalDataWithRemote()
|
||||
SMC-->>Client : 数据同步完成
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L80-L150)
|
||||
|
||||
**章节来源**
|
||||
- [SingletonModuleComp.ts](file://assets/script/game/common/SingletonModuleComp.ts#L1-L195)
|
||||
|
||||
## 核心组件深度分析
|
||||
|
||||
### 主入口组件分析
|
||||
|
||||
Main.ts作为应用的主入口,负责整体初始化流程:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([应用启动]) --> Run[run方法]
|
||||
Run --> InitECS[初始化ECS系统]
|
||||
InitECS --> InitGUI[初始化GUI]
|
||||
InitGUI --> InitSMC[初始化SingletonModuleComp]
|
||||
InitSMC --> Complete([初始化完成])
|
||||
InitECS --> CreateEntity[创建Initialize实体]
|
||||
CreateEntity --> AddToSMC[添加到SMC]
|
||||
InitGUI --> LoadConfig[加载UI配置]
|
||||
LoadConfig --> SetupGUI[设置GUI框架]
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L15-L41)
|
||||
|
||||
### 初始化流程详解
|
||||
|
||||
Initialize.ts实现了复杂的游戏初始化流程:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Init[Initialize实体创建] --> Queue[AsyncQueue队列]
|
||||
Queue --> Custom[加载自定义资源]
|
||||
Custom --> Lang[加载语言包]
|
||||
Lang --> Common[加载公共资源]
|
||||
Common --> Progress[加载进度界面]
|
||||
Progress --> Start[开始游戏]
|
||||
Custom --> Font[加载字体资源]
|
||||
Font --> Unified[统一数据加载]
|
||||
Unified --> Cloud{检测微信客户端}
|
||||
Cloud --> |是| CloudLoad[云端数据加载]
|
||||
Cloud --> |否| LocalLoad[本地数据加载]
|
||||
CloudLoad --> Login[微信登录]
|
||||
Login --> Sync[数据同步]
|
||||
LocalLoad --> Debug[调试数据]
|
||||
```
|
||||
|
||||
**图表来源**
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L20-L207)
|
||||
|
||||
### 游戏状态管理
|
||||
|
||||
HeroViewComp展示了复杂的游戏状态管理:
|
||||
|
||||
| 状态类型 | 管理方式 | 更新频率 | 用途 |
|
||||
|---------|---------|---------|------|
|
||||
| 基础属性 | 直接计算 | 帧级 | 生命值、魔法值、防御力 |
|
||||
| Buff效果 | 数组管理 | 帧级 | 增益效果、持续时间 |
|
||||
| 负面状态 | 对象管理 | 帧级 | 减益效果、眩晕、冰冻 |
|
||||
| 战斗状态 | 状态机 | 事件触发 | 移动、攻击、死亡 |
|
||||
| 技能效果 | 特殊处理 | 事件触发 | 技能释放、特效显示 |
|
||||
|
||||
**章节来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L1-L41)
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L1-L207)
|
||||
- [HeroViewComp.ts](file://assets/script/game/hero/HeroViewComp.ts#L1-L780)
|
||||
|
||||
## 架构协作机制
|
||||
|
||||
### 三大支柱的协同工作
|
||||
|
||||
三个架构支柱在项目中形成了紧密的协作关系:
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "ECS系统"
|
||||
ECS_Entities[实体管理]
|
||||
ECS_Components[组件系统]
|
||||
ECS_Systems[系统处理]
|
||||
end
|
||||
subgraph "MVVM框架"
|
||||
VM_Manager[ViewModel管理]
|
||||
UI_Binding[UI绑定]
|
||||
Event_Handler[事件处理]
|
||||
end
|
||||
subgraph "单例模式"
|
||||
Global_State[全局状态]
|
||||
Data_Sync[数据同步]
|
||||
Config_Manage[配置管理]
|
||||
end
|
||||
ECS_Entities --> VM_Manager
|
||||
ECS_Components --> UI_Binding
|
||||
ECS_Systems --> Event_Handler
|
||||
VM_Manager --> Global_State
|
||||
UI_Binding --> Data_Sync
|
||||
Event_Handler --> Config_Manage
|
||||
Global_State --> ECS_Entities
|
||||
Data_Sync --> ECS_Components
|
||||
Config_Manage --> ECS_Systems
|
||||
```
|
||||
|
||||
### 生命周期管理
|
||||
|
||||
项目实现了完整的组件生命周期管理:
|
||||
|
||||
```mermaid
|
||||
stateDiagram-v2
|
||||
[*] --> Created : 实体创建
|
||||
Created --> Initialized : 初始化
|
||||
Initialized --> Active : 激活状态
|
||||
Active --> Updating : 帧更新
|
||||
Updating --> Active : 继续运行
|
||||
Active --> Paused : 暂停状态
|
||||
Paused --> Active : 恢复运行
|
||||
Active --> Destroyed : 销毁
|
||||
Destroyed --> [*]
|
||||
Initialized --> Error : 初始化失败
|
||||
Error --> [*] : 清理资源
|
||||
```
|
||||
|
||||
**章节来源**
|
||||
- [Main.ts](file://assets/script/Main.ts#L15-L41)
|
||||
- [Initialize.ts](file://assets/script/game/initialize/Initialize.ts#L20-L50)
|
||||
|
||||
## 性能优化策略
|
||||
|
||||
### ECS性能优化
|
||||
|
||||
1. **组件缓存池**:重用组件对象,减少垃圾回收压力
|
||||
2. **系统批处理**:同一系统的实体批量处理,提高CPU缓存命中率
|
||||
3. **过滤器优化**:精确的实体筛选,避免不必要的遍历
|
||||
4. **延迟销毁**:延迟实体销毁,减少频繁的内存分配
|
||||
|
||||
### MVVM性能优化
|
||||
|
||||
1. **脏检查优化**:智能的变更检测,避免不必要的UI更新
|
||||
2. **事件节流**:高频事件的防抖处理
|
||||
3. **组件懒加载**:按需加载UI组件,减少内存占用
|
||||
4. **绑定优化**:选择性的数据绑定,避免过度监听
|
||||
|
||||
### 单例模式优化
|
||||
|
||||
1. **延迟初始化**:按需创建单例对象
|
||||
2. **弱引用管理**:避免循环引用导致的内存泄漏
|
||||
3. **状态压缩**:定期清理无效状态数据
|
||||
4. **异步处理**:耗时操作的异步化处理
|
||||
|
||||
## 总结
|
||||
|
||||
本项目的技术架构通过ECS、MVVM和单例模式三大支柱,构建了一个高度解耦、可扩展的游戏引擎。这种架构设计带来了以下显著优势:
|
||||
|
||||
1. **代码可维护性**:清晰的职责分离,便于团队协作和长期维护
|
||||
2. **系统可扩展性**:模块化设计支持快速功能迭代和扩展
|
||||
3. **性能优化空间**:多层次的优化策略确保良好的运行性能
|
||||
4. **开发效率提升**:标准化的开发模式降低学习成本
|
||||
5. **质量保证**:完善的测试和调试机制确保代码质量
|
||||
|
||||
通过这种架构设计,项目不仅能够应对当前的功能需求,还为未来的功能扩展和技术升级奠定了坚实的基础。三大支柱的协同工作使得整个系统既灵活又稳定,为游戏开发提供了强有力的技术支撑。
|
||||
Reference in New Issue
Block a user