Skip to content

Commit e1d85ea

Browse files
committed
ecs-refactor
1 parent 8c61459 commit e1d85ea

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

src/BasicSystem.tsx

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,54 @@ import {
55
defineState,
66
dispatchAction,
77
getMutableState,
8-
receiveActions,
9-
useHookstate,
108
none,
11-
getState
9+
useHookstate
1210
} from '@etherealengine/hyperflux'
13-
import { defineSystem } from '@etherealengine/engine/src/ecs/functions/SystemFunctions'
1411

1512
import { EntityUUID } from '@etherealengine/common/src/interfaces/EntityUUID'
16-
import { isClient } from '@etherealengine/engine/src/common/functions/getEnvironment'
1713

18-
import { matches, string } from '@etherealengine/engine/src/common/functions/MatchesUtils'
1914
import { NetworkTopics } from '@etherealengine/engine/src/networking/classes/Network'
2015
import { WorldNetworkAction } from '@etherealengine/engine/src/networking/functions/WorldNetworkAction'
2116

2217
import { PhysicsSystem } from '@etherealengine/engine/src/physics/PhysicsModule'
23-
import { Physics } from '@etherealengine/engine/src/physics/classes/Physics'
2418

25-
import { Vector3 } from 'three'
26-
import { UUIDComponent } from '@etherealengine/engine/src/scene/components/UUIDComponent'
27-
import { getComponent, setComponent } from '@etherealengine/engine/src/ecs/functions/ComponentFunctions'
28-
import { TransformComponent } from '@etherealengine/engine/src/transform/components/TransformComponent'
29-
import { VisibleComponent } from '@etherealengine/engine/src/scene/components/VisibleComponent'
19+
import { isClient } from '@etherealengine/common/src/utils/getEnvironment'
20+
import { defineSystem, getComponent, setComponent } from '@etherealengine/ecs'
3021
import { NameComponent } from '@etherealengine/engine/src/scene/components/NameComponent'
3122
import { PrimitiveGeometryComponent } from '@etherealengine/engine/src/scene/components/PrimitiveGeometryComponent'
32-
import { ColliderComponent } from '@etherealengine/engine/src/scene/components/ColliderComponent'
33-
import { ColliderDesc, RigidBodyDesc } from '@dimforge/rapier3d-compat'
34-
import { PhysicsState } from '@etherealengine/engine/src/physics/state/PhysicsState'
35-
import { RigidBodyComponent } from '@etherealengine/engine/src/physics/components/RigidBodyComponent'
36-
import { getInteractionGroups } from '@etherealengine/engine/src/physics/functions/getInteractionGroups'
37-
import { CollisionGroups, DefaultCollisionMask } from '@etherealengine/engine/src/physics/enums/CollisionGroups'
23+
import { UUIDComponent } from '@etherealengine/engine/src/scene/components/UUIDComponent'
24+
import { VisibleComponent } from '@etherealengine/engine/src/scene/components/VisibleComponent'
25+
import { TransformComponent } from '@etherealengine/engine/src/transform/components/TransformComponent'
3826

3927
//
4028
// Description of the format of a spawn action to create a artifact
4129
//
4230

43-
const spawnAction = defineAction({
44-
...WorldNetworkAction.spawnObject.actionShape,
45-
prefab: 'ee.basic.ball',
46-
$topic: NetworkTopics.world
47-
})
31+
class BasicActions {
32+
static spawnAction = defineAction({
33+
...WorldNetworkAction.spawnObject.actionShape,
34+
prefab: 'ee.basic.ball',
35+
$topic: NetworkTopics.world
36+
})
37+
}
4838

4939
//
5040
// Global state that tracks locally spawned or destroyed artifacts by using action receptors
5141
//
5242

5343
export const BasicState = defineState({
5444
name: 'ee.basic.BasicState',
55-
initial: {} as Record< EntityUUID, {} >,
56-
receptors: [
57-
[
58-
spawnAction,
59-
(state, action: typeof spawnAction.matches._TYPE) => {
60-
state[action.entityUUID].merge({})
61-
}
62-
],
63-
[
64-
WorldNetworkAction.destroyObject,
65-
(state, action: typeof WorldNetworkAction.destroyObject.matches._TYPE) => {
66-
state[action.entityUUID].set(none)
67-
}
68-
]
69-
]
45+
initial: {} as Record<EntityUUID, {}>,
46+
receptors: {
47+
onSpawnAction: BasicActions.spawnAction.receive((action) => {
48+
const state = getMutableState(BasicState)
49+
state[action.entityUUID].merge({})
50+
}),
51+
onDestroyObject: WorldNetworkAction.destroyObject.receive((action) => {
52+
const state = getMutableState(BasicState)
53+
state[action.entityUUID].set(none)
54+
})
55+
}
7056
})
7157

7258
//
@@ -79,7 +65,7 @@ const ArtifactReactor = ({ entityUUID }: { entityUUID: EntityUUID }) => {
7965
const entity = UUIDComponent.getEntityByUUID(entityUUID)
8066
setComponent(entity, TransformComponent)
8167
setComponent(entity, VisibleComponent)
82-
setComponent(entity, NameComponent,'hello')
68+
setComponent(entity, NameComponent, 'hello')
8369
setComponent(entity, PrimitiveGeometryComponent, { geometryType: 1 })
8470

8571
/*
@@ -104,23 +90,22 @@ const ArtifactReactor = ({ entityUUID }: { entityUUID: EntityUUID }) => {
10490
10591
*/
10692

107-
if(isClient) return
93+
if (isClient) return
10894

10995
// positions are networked intrinsically
11096

111-
const x = Math.random()*10
97+
const x = Math.random() * 10
11298
const y = 0
113-
const z = Math.random()*10
114-
const transform = getComponent(entity,TransformComponent)
115-
transform.position.set(x,y,z)
99+
const z = Math.random() * 10
100+
const transform = getComponent(entity, TransformComponent)
101+
transform.position.set(x, y, z)
116102

117103
// forces are networked intrinsically
118104

119105
//const angle = Math.random()*Math.PI*2
120106
//const direction = new Vector3( Math.sin(angle),0,Math.cos(angle))
121107
//const velocity = 0.025 + Math.random()*0.01
122108
//rigidBody.body.applyImpulse(direction.multiplyScalar(velocity), true)
123-
124109
}, [])
125110
return null
126111
}
@@ -148,15 +133,13 @@ let counter = 0
148133
//
149134

150135
const execute = () => {
151-
receiveActions(BasicState)
152-
153-
if(isClient) return
136+
if (isClient) return
154137
counter++
155-
if(counter&255) return
138+
if (counter & 255) return
156139

157140
const entityUUID = `basic-${counter}` as EntityUUID
158141
const prefab = 'ee.basic.ball'
159-
const action = spawnAction({ entityUUID, prefab })
142+
const action = BasicActions.spawnAction({ entityUUID, prefab })
160143
dispatchAction(action)
161144
}
162145

0 commit comments

Comments
 (0)