react-encompass-ecs
Sync data from encompass-ecs to React
Usage
Full example: react-encompass-ecs-example
This example uses encompass-ecs
and react-three-fiber
.
Use data inside components, and update data inside components
Draw something with React libraries, for example, react-three-fiber
:
import useComponent from 'react-encompass-ecs'; { const boxes = ; return <> boxes </> ;} { // Hook into the render loop and rotate the scene a bit return <> <ambientLight = /> <spotLight = = = = = /> <Plane /> </> ;}
Spawn entities:
; @ public { for let count = 0; count < 100; count += 1 const boxEntity = this; boxEntity; const position = boxEntity; positionx = 0; positiony = 0; const velocity = boxEntity; velocityx = 3 * Math; velocityy = 3 * Math; }
State management with Hyper-ECS (Entity Component System with message passing):
@@ protected { const positionComponent = entity; const velocityComponent = entity; const motionMessage = this; motionMessagex = velocityComponentx; motionMessagey = velocityComponenty; } @@ public { const motionMessages = this; for const message of motionMessages const positionComponent = this; positionComponentx += messagex * dt / 1000; positionComponenty += messagey * dt / 1000; }
Set up boilerplate
Init Encompass World:
; { const worldBuilder = ; worldBuilder; worldBuilder; worldBuilder; worldBuilder; const instantizationMessage = worldBuilder; instantizationMessagemapDefinition = ``; const entityStore = worldBuilder; const world = worldBuilder; return world entityStore ;}
Set up game loop:
;type GameState = IEntityMap;: [GameState, MainLoop] { const currentGameEntities setGameEntities = ; return currentGameEntities ;}
Use gameloop in React app:
import Provider as EntityProvider from 'react-encompass-ecs'; { const currentGameEntities = ; return <Container> <Canvas = = = > <EntityProvider => <Scene /> </EntityProvider> </Canvas> <StatsGraph /> </Container> ;}
Todo
- Trigger system from React component is not implemented