Skip to content

Commit 84f3e65

Browse files
committed
feat(ECS模式): finish update
1 parent 609c29b commit 84f3e65

File tree

14 files changed

+413
-311
lines changed

14 files changed

+413
-311
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
import { createDataOrientedComponent1, createGameObject, createOtherComponent1, setDataOrientedComponent1, setOtherComponent1 } from "./world/SceneAPI";
2-
import { createState, init, loop, action1 } from "./world/World";
1+
import * as WorldSceneAPI from "./world/SceneAPI";
2+
import * as World from "./world/World";
33
import { state as worldState } from "./world/WorldStateType";
44

55
let _createScene = (worldState: worldState): worldState => {
6-
let gameObject1Data = createGameObject(worldState)
6+
//创建gameObject1
7+
let gameObject1Data = WorldSceneAPI.createGameObject(worldState)
78
worldState = gameObject1Data[0]
89
let gameObject1 = gameObject1Data[1]
910

10-
let dataOrientedComponent1Data = createDataOrientedComponent1(worldState)
11+
//创建组件
12+
let dataOrientedComponent1Data = WorldSceneAPI.createDataOrientedComponent1(worldState)
1113
worldState = dataOrientedComponent1Data[0]
1214
let dataOrientedComponent1 = dataOrientedComponent1Data[1]
13-
let otherComponent1Data = createOtherComponent1(worldState)
15+
let otherComponent1Data = WorldSceneAPI.createOtherComponent1(worldState)
1416
let otherComponent1 = otherComponent1Data[1]
1517
worldState = otherComponent1Data[0]
1618

17-
worldState = setDataOrientedComponent1(worldState, gameObject1, dataOrientedComponent1)
18-
worldState = setOtherComponent1(worldState, gameObject1, otherComponent1)
19-
20-
worldState = action1(worldState, gameObject1, dataOrientedComponent1, otherComponent1)
19+
//挂载组件
20+
worldState = WorldSceneAPI.setDataOrientedComponent1(worldState, gameObject1, dataOrientedComponent1)
21+
worldState = WorldSceneAPI.setOtherComponent1(worldState, gameObject1, otherComponent1)
2122

23+
//触发gameObject1的行为
24+
worldState = World.action1(worldState, gameObject1, dataOrientedComponent1, otherComponent1)
2225

2326
创建更多的GameObjects...
2427

25-
2628
return worldState
2729
}
2830

29-
let worldState = createState({ dataOrientedComponent1Count: xx })
31+
let worldState = World.createState({ dataOrientedComponent1Count: xx })
3032

3133
worldState = _createScene(worldState)
3234

33-
worldState = init(worldState)
35+
worldState = World.init(worldState)
3436

35-
loop(worldState)
37+
World.loop(worldState)

ECS模式代码/ecs_pattern_role_abstract/src/manager/dataoriented_component1/ManagerStateType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ export type TypeArrayType = Float32Array | Uint8Array | Uint16Array | Uint32Arra
66

77
export type state = {
88
maxIndex: number,
9+
//buffer保存了该种组件所有的value1、value2、...、valueX数据
910
buffer: ArrayBuffer,
11+
//该种组件所有的value1数据的视图
1012
value1s: TypeArrayType,
13+
//该种组件所有的value2数据的视图
1114
value2s: TypeArrayType,
1215
更多valueXs...,
1316

ECS模式代码/ecs_pattern_role_abstract/src/manager/other_component1/ManagerStateType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { gameObject } from "../../gameObject/GameObjectType";
44

55
export type state = {
66
maxUID: number,
7+
//value1Map是Hash Map,用来保存该种组件所有的value1数据
78
value1Map: Map<component, value1 type>,
89
更多valueXMap...,
910

ECS模式代码/ecs_pattern_role_abstract/src/system/CreateStateSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export let createState = ({ dataOrientedComponent1Count }): worldState => {
99
dataOrientedComponent1ManagerState: DataOrientedComponent1Manager.createState(dataOrientedComponent1Count),
1010
otherComponent1ManagerState: OtherComponent1Manager.createState(),
1111

12-
更多的DataOrientedManagerState和OtherComponentManagerState...
12+
创建更多的DataOrientedManagerState和OtherComponentManagerState...
1313
}
1414
}

ECS模式代码/ecs_pattern_role_abstract/src/world/World.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ let requestAnimationFrame = (func) => {
1919

2020

2121
export let loop = (worldState: worldState) => {
22-
调用System来update
22+
调用OtherSystem来update
2323

24-
调用System来render
24+
调用OtherSystem来render
2525

2626
requestAnimationFrame(
2727
(time) => {

ECS模式代码/story_after/src/component/FlyComponent.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { getExnFromStrictNull } from "commonlib-ts/src/NullableUtils";
22
import { state as flyComponentState } from "./FlyComponentStateType"
3-
import { getPositionComponentExn, getVelocityComponentExn } from "../gameObject/GameObject";
3+
import * as GameObject from "../gameObject/GameObject";
44
import { getGameObjectStateExn, setPositionComponentState } from "../utils/WorldUtils";
55
import { state as worldState } from "../world/WorldStateType";
6-
import { getVelocity } from "./VelocityComponent";
7-
import { getPosition, setPosition } from "./PositionComponent";
6+
import * as VelocityComponent from "./VelocityComponent";
7+
import * as PositionComponent from "./PositionComponent";
88

99
export let create = (): flyComponentState => {
1010
let flyComponentState: flyComponentState = {
@@ -27,20 +27,21 @@ export let setMaxVelocity = (flyComponentState: flyComponentState, maxVelocity)
2727
}
2828

2929
export let fly = (worldState: worldState, flyComponentState: flyComponentState): worldState => {
30-
let maxVelocity = flyComponentState.maxVelocity
31-
30+
//获得该组件的maxVelocity、gameObject
31+
let maxVelocity = getMaxVelocity(flyComponentState)
3232
let gameObject = getExnFromStrictNull(flyComponentState.gameObject)
33-
let gameObjectState = getGameObjectStateExn(worldState, gameObject)
3433

35-
//通过gameObject获得positionComponent,获得它的position
36-
let [x, y, z] = getPosition(getPositionComponentExn(gameObjectState))
34+
//通过该组件的gameObject,获得挂载到该gameObject的positionComponent组件
35+
//获得它的position
36+
let [x, y, z] = PositionComponent.getPosition(GameObject.getPositionComponentExn(getGameObjectStateExn(worldState, gameObject)))
3737

38-
//通过gameObject获得velocityComponent,获得它的velocity
39-
let velocity = getVelocity(getVelocityComponentExn(gameObjectState))
38+
//通过该组件的gameObject,获得挂载到该gameObject的velocityComponent组件
39+
//获得它的velocity
40+
let velocity = VelocityComponent.getVelocity(GameObject.getVelocityComponentExn(getGameObjectStateExn(worldState, gameObject)))
4041

42+
//根据maxVelocity、velocity,更新positionComponent组件的position
4143
velocity = velocity < maxVelocity ? (velocity * 2.0) : maxVelocity
42-
43-
let positionComponentState = setPosition(getPositionComponentExn(gameObjectState), [x + velocity, y + velocity, z + velocity])
44+
let positionComponentState = PositionComponent.setPosition(GameObject.getPositionComponentExn(getGameObjectStateExn(worldState, gameObject)), [x + velocity, y + velocity, z + velocity])
4445

4546
return setPositionComponentState(worldState, gameObject, positionComponentState)
4647
}

ECS模式代码/story_after/src/component/PositionComponent.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { getExnFromStrictNull } from "commonlib-ts/src/NullableUtils";
22
import { state as positionComponentState } from "../component/PositionComponentStateType"
3-
import { getVelocityComponentExn } from "../gameObject/GameObject";
3+
import * as GameObject from "../gameObject/GameObject";
44
import { getGameObjectStateExn, setPositionComponentState } from "../utils/WorldUtils";
55
import { state as worldState } from "../world/WorldStateType";
6-
import { getVelocity } from "./VelocityComponent";
6+
import * as VelocityComponent from "./VelocityComponent";
77

88
export let create = (): positionComponentState => {
99
let positionComponentState: positionComponentState = {
@@ -41,13 +41,15 @@ export let update = (positionComponentState: positionComponentState): positionCo
4141
}
4242

4343
export let move = (worldState: worldState, positionComponentState: positionComponentState): worldState => {
44-
let [x, y, z] = positionComponentState.position
44+
//获得该组件的position、gameObject
45+
let [x, y, z] = getPosition(positionComponentState)
4546

47+
//通过该组件的gameObject,获得挂载到该gameObject的velocityComponent组件
48+
//获得它的velocity
4649
let gameObject = getExnFromStrictNull(positionComponentState.gameObject)
50+
let velocity = VelocityComponent.getVelocity(GameObject.getVelocityComponentExn(getGameObjectStateExn(worldState, gameObject)))
4751

48-
//通过gameObject获得velocityComponent,获得它的velocity
49-
let velocity = getVelocity(getVelocityComponentExn(getGameObjectStateExn(worldState, gameObject)))
50-
52+
//根据velocity,更新该组件的position
5153
positionComponentState = setPosition(positionComponentState, [x + velocity, y + velocity, z + velocity])
5254

5355
return setPositionComponentState(worldState, gameObject, positionComponentState)

ECS模式代码/story_improve/src/Client.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { init, loop } from "ecs_pattern_utils/src/World";
2-
import * as UpdateSystem from "./system/UpdateSystem"
3-
import * as RenderOneByOneSystem from "./system/RenderOneByOneSystem"
4-
import * as RenderInstancesSystem from "./system/RenderInstancesSystem"
52
import { createFlyComponent, createGameObject, createInstanceComponent, createPositionComponent, createVelocityComponent, setFlyComponent, setInstanceComponent, setPositionComponent, setVelocityComponent } from "./world/SceneAPI";
6-
import { createState, move, fly } from "./world/World";
3+
import { createState, move, fly, update, renderOneByOne, renderInstances } from "./world/World";
74
import { state as worldState } from "./world/WorldStateType";
85

96
let _createScene = (worldState: worldState): worldState => {
@@ -106,4 +103,4 @@ worldState = _createScene(worldState)
106103

107104
worldState = init(worldState)
108105

109-
loop(worldState, [UpdateSystem.update, RenderOneByOneSystem.render, RenderInstancesSystem.render])
106+
loop(worldState, [update, renderOneByOne, renderInstances])

ECS模式代码/story_improve/src/system/FlySystem.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { getMaxVelocity } from "../manager/fly_component/Manager";
2-
import { getPosition, setPosition } from "../manager/position_component/Manager";
3-
import { getVelocity } from "../manager/velocity_component/Manager";
1+
import * as FlyComponentManager from "../manager/fly_component/Manager";
2+
import * as PositionComponentManager from "../manager/position_component/Manager";
3+
import * as VelocityComponentManager from "../manager/velocity_component/Manager";
44
import { state as worldState } from "../world/WorldStateType";
55

66
export let fly = (worldState: worldState, positionComponent, velocityComponent, flyComponent): worldState => {
7-
let [x, y, z] = getPosition(worldState.positionComponentManagerState, positionComponent)
7+
let [x, y, z] = PositionComponentManager.getPosition(worldState.positionComponentManagerState, positionComponent)
88

9-
let velocity = getVelocity(worldState.velocityComponentManagerState, velocityComponent)
9+
let velocity = VelocityComponentManager.getVelocity(worldState.velocityComponentManagerState, velocityComponent)
1010

11-
let maxVelocity = getMaxVelocity(worldState.flyComponentManagerState, flyComponent)
11+
let maxVelocity = FlyComponentManager.getMaxVelocity(worldState.flyComponentManagerState, flyComponent)
1212

13+
//根据maxVelociy、velocity,更新positionComponent的position
1314
velocity = velocity < maxVelocity ? (velocity * 2.0) : maxVelocity
14-
15-
let positionComponentManagerState = setPosition(worldState.positionComponentManagerState, positionComponent, [x + velocity, y + velocity, z + velocity])
15+
let positionComponentManagerState = PositionComponentManager.setPosition(worldState.positionComponentManagerState, positionComponent, [x + velocity, y + velocity, z + velocity])
1616

1717
return {
1818
...worldState,

ECS模式代码/story_improve/src/system/MoveSystem.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { getPosition, setPosition } from "../manager/position_component/Manager";
2-
import { getVelocity } from "../manager/velocity_component/Manager";
1+
import * as PositionComponentManager from "../manager/position_component/Manager";
2+
import * as VelocityComponentManager from "../manager/velocity_component/Manager";
33
import { state as worldState } from "../world/WorldStateType";
44

55
export let move = (worldState: worldState, positionComponent, velocityComponent): worldState => {
6-
let [x, y, z] = getPosition(worldState.positionComponentManagerState, positionComponent)
6+
let [x, y, z] = PositionComponentManager.getPosition(worldState.positionComponentManagerState, positionComponent)
77

8-
let velocity = getVelocity(worldState.velocityComponentManagerState, velocityComponent)
8+
let velocity = VelocityComponentManager.getVelocity(worldState.velocityComponentManagerState, velocityComponent)
99

10-
let positionComponentManagerState = setPosition(worldState.positionComponentManagerState, positionComponent, [x + velocity, y + velocity, z + velocity])
10+
let positionComponentManagerState = PositionComponentManager.setPosition(worldState.positionComponentManagerState, positionComponent, [x + velocity, y + velocity, z + velocity])
1111

1212
return {
1313
...worldState,

0 commit comments

Comments
 (0)