Skip to content

Commit baed4bf

Browse files
committed
revamp project
1 parent 9fe7b91 commit baed4bf

File tree

2 files changed

+71
-82
lines changed

2 files changed

+71
-82
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"version": "1.5.0"
88
},
99
"scripts": {
10-
"test": "exit 0",
10+
"check-errors": "tsc --noemit",
11+
"test": "mocha --config .mocharc.js",
1112
"format": "prettier --write \"**/*.{ts,tsx}\"",
1213
"format-scss": "stylelint \"**/*.scss\" --fix",
1314
"format-staged": "lint-staged",

src/BasicSystem.tsx

Lines changed: 69 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
defineState,
66
dispatchAction,
77
getMutableState,
8+
getState,
89
none,
910
useHookstate
1011
} from '@etherealengine/hyperflux'
@@ -14,35 +15,43 @@ import { EntityUUID } from '@etherealengine/common/src/interfaces/EntityUUID'
1415
import { NetworkTopics } from '@etherealengine/spatial/src/networking/classes/Network'
1516
import { WorldNetworkAction } from '@etherealengine/spatial/src/networking/functions/WorldNetworkAction'
1617

17-
import { PhysicsSystem } from '@etherealengine/spatial/src/physics/PhysicsModule'
18-
1918
import { isClient } from '@etherealengine/common/src/utils/getEnvironment'
20-
import { defineSystem, getComponent, setComponent } from '@etherealengine/ecs'
19+
import { PresentationSystemGroup, defineSystem, getComponent, setComponent } from '@etherealengine/ecs'
20+
import { ECSState } from '@etherealengine/ecs/src/ECSState'
2121
import { PrimitiveGeometryComponent } from '@etherealengine/engine/src/scene/components/PrimitiveGeometryComponent'
22-
import { TransformComponent } from '@etherealengine/spatial/src/transform/components/TransformComponent'
22+
import { GeometryTypeEnum } from '@etherealengine/engine/src/scene/constants/GeometryTypeEnum'
2323
import { NameComponent } from '@etherealengine/spatial/src/common/NameComponent'
2424
import { UUIDComponent } from '@etherealengine/spatial/src/common/UUIDComponent'
25+
import { NetworkState } from '@etherealengine/spatial/src/networking/NetworkState'
26+
import { ColliderComponent } from '@etherealengine/spatial/src/physics/components/ColliderComponent'
27+
import { RigidBodyComponent } from '@etherealengine/spatial/src/physics/components/RigidBodyComponent'
2528
import { VisibleComponent } from '@etherealengine/spatial/src/renderer/components/VisibleComponent'
26-
27-
//
28-
// Description of the format of a spawn action to create a artifact
29-
//
30-
31-
class BasicActions {
32-
static spawnAction = defineAction({
33-
...WorldNetworkAction.spawnObject.actionShape,
34-
prefab: 'ee.basic.ball',
35-
$topic: NetworkTopics.world
36-
})
29+
import { TransformComponent } from '@etherealengine/spatial/src/transform/components/TransformComponent'
30+
import { Vector3 } from 'three'
31+
32+
/**
33+
* Basic actions to spawn and destroy objects
34+
* This extends and naturally utilizes the functionality in EntityNetworkState
35+
*/
36+
37+
const BasicActions = {
38+
spawnAction: defineAction(
39+
WorldNetworkAction.spawnObject.extend({
40+
type: 'ee.basic.SPAWN_BALL',
41+
$topic: NetworkTopics.world
42+
})
43+
)
3744
}
3845

39-
//
40-
// Global state that tracks locally spawned or destroyed artifacts by using action receptors
41-
//
46+
/**
47+
* Global state that tracks locally spawned or destroyed artifacts by using action receptors
48+
*/
4249

43-
export const BasicState = defineState({
50+
const BasicState = defineState({
4451
name: 'ee.basic.BasicState',
52+
4553
initial: {} as Record<EntityUUID, {}>,
54+
4655
receptors: {
4756
onSpawnAction: BasicActions.spawnAction.receive((action) => {
4857
const state = getMutableState(BasicState)
@@ -55,101 +64,80 @@ export const BasicState = defineState({
5564
}
5665
})
5766

58-
//
59-
// A reactor such that each basic state record has an associated a visual artifact
60-
//
67+
/**
68+
* A reactor such that each basic state record has an associated a visual artifact
69+
*/
6170

6271
const ArtifactReactor = ({ entityUUID }: { entityUUID: EntityUUID }) => {
63-
const basicState = useHookstate(getMutableState(BasicState)[entityUUID])
64-
useEffect(() => {
65-
const entity = UUIDComponent.getEntityByUUID(entityUUID)
66-
setComponent(entity, TransformComponent)
67-
setComponent(entity, VisibleComponent)
68-
setComponent(entity, NameComponent, 'hello')
69-
setComponent(entity, PrimitiveGeometryComponent, { geometryType: 1 })
70-
71-
/*
72+
/** Entity creation and destruction is handled by EntityNetworkState */
73+
const entity = UUIDComponent.useEntityByUUID(entityUUID)
7274

73-
setComponent(entity, ColliderComponent, {
74-
bodyType: 0, // dynamic
75-
shapeType: 1, // sphere
76-
collisionMask: 1,
77-
restitution: 0.5
78-
})
75+
useEffect(() => {
76+
if (!entity) return
7977

80-
const rigidBodyDesc = RigidBodyDesc.dynamic()
81-
Physics.createRigidBody(entity, getState(PhysicsState).physicsWorld, rigidBodyDesc, [])
82-
83-
const rigidBody = getComponent(entity, RigidBodyComponent)
84-
85-
const interactionGroups = getInteractionGroups(CollisionGroups.Default, DefaultCollisionMask)
86-
const colliderDesc = ColliderDesc.ball(0.1).setCollisionGroups(interactionGroups)
87-
colliderDesc.setRestitution(1)
88-
89-
Physics.createColliderAndAttachToRigidBody(getState(PhysicsState).physicsWorld, colliderDesc, rigidBody.body)
90-
91-
*/
78+
setComponent(entity, TransformComponent, { scale: new Vector3(0.1, 0.1, 0.1) })
79+
setComponent(entity, VisibleComponent)
80+
setComponent(entity, NameComponent, entityUUID)
81+
setComponent(entity, PrimitiveGeometryComponent, { geometryType: GeometryTypeEnum.SphereGeometry })
82+
setComponent(entity, RigidBodyComponent, { type: 'dynamic' })
83+
setComponent(entity, ColliderComponent, { shape: 'sphere' })
9284

9385
if (isClient) return
9486

95-
// positions are networked intrinsically
87+
const angle = Math.random() * Math.PI * 2
88+
const direction = new Vector3(Math.sin(angle), 0, Math.cos(angle))
89+
const velocity = 0.025 + Math.random() * 0.01
90+
getComponent(entity, RigidBodyComponent).body.applyImpulse(direction.multiplyScalar(velocity), true)
91+
}, [entity])
9692

97-
const x = Math.random() * 10
98-
const y = 0
99-
const z = Math.random() * 10
100-
const transform = getComponent(entity, TransformComponent)
101-
transform.position.set(x, y, z)
102-
103-
// forces are networked intrinsically
104-
105-
//const angle = Math.random()*Math.PI*2
106-
//const direction = new Vector3( Math.sin(angle),0,Math.cos(angle))
107-
//const velocity = 0.025 + Math.random()*0.01
108-
//rigidBody.body.applyImpulse(direction.multiplyScalar(velocity), true)
109-
}, [])
11093
return null
11194
}
11295

113-
//
114-
// Observe spawn events and make sure there are sub reactors that reflect them
115-
// Make sub-reactors for each entry
116-
//
96+
/**
97+
* Observe spawn events and create a sub-reactor for each entry in the basic state
98+
*/
11799

118100
const reactor = () => {
119101
const basicState = useHookstate(getMutableState(BasicState))
120102
return (
121103
<>
122104
{basicState.keys.map((entityUUID: EntityUUID) => (
123-
<ArtifactReactor entityUUID={entityUUID} />
105+
<ArtifactReactor key={entityUUID} entityUUID={entityUUID} />
124106
))}
125107
</>
126108
)
127109
}
128110

129111
let counter = 0
112+
const spawnRate = 3
130113

131-
//
132-
// Periodically change the basic state
133-
//
114+
/**
115+
* Spawn a new basic entity every 3 seconds
116+
*/
134117

135118
const execute = () => {
136-
if (isClient) return
137-
counter++
138-
if (counter & 255) return
119+
/** Only run this on the server */
120+
if (isClient || !NetworkState.worldNetwork) return
121+
122+
const { deltaSeconds, elapsedSeconds } = getState(ECSState)
123+
124+
counter += deltaSeconds
125+
126+
if (counter < spawnRate) return
127+
counter = 0
139128

140-
const entityUUID = `basic-${counter}` as EntityUUID
141-
const prefab = 'ee.basic.ball'
142-
const action = BasicActions.spawnAction({ entityUUID, prefab })
129+
const entityUUID = `basic-${elapsedSeconds}` as EntityUUID
130+
const action = BasicActions.spawnAction({ entityUUID, position: new Vector3(Math.random(), 1, Math.random()) })
143131
dispatchAction(action)
144132
}
145133

146-
//
147-
// System
148-
//
134+
/**
135+
* System to register the execute function and reactor
136+
*/
149137

150138
export const BasicSystem = defineSystem({
151139
uuid: 'basic.system',
152140
reactor,
153141
execute,
154-
insert: { after: PhysicsSystem }
142+
insert: { after: PresentationSystemGroup }
155143
})

0 commit comments

Comments
 (0)