-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
46 lines (41 loc) · 1.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Import flock
import * as flock from 'flock-ecs';
// Create a World
// A world stores components and componentValues,
// Creates new entities,
// And performs queries for systems
const world = new flock.World();
// Create a Component
// Components are how entities describe the data they need
const Position = new flock.Component(
() => ({
x: 0,
y: 0,
})
);
// Register the component with your world
world.registerComponent(Position);
// Create a System
// This sytem logs out each entity's position to the console
const logSystem = new flock.System(
(entities) => {
entities.forEach(entity => {
// Entity.getComponent gets a ComponentValue from an entity
const position = entity.getComponent(Position)!;
// Make sure you use the value property to access your data
console.log(`{ x: ${position.value.x}, y: ${position.value.y} }`);
});
},
// Filter the entities by which components the entities contain
[ Position ],
);
// Create Entities
for (let i=0; i<10; i++) {
const entity = world.createEntity();
// Provide a different position value than the default one when creating the entity
entity.addComponent(Position, { x: Math.random() * 100, y: Math.random() * 100 });
}
// Run the system on the world
logSystem.run(world);
// Tell the world to clean up any removed entities
world.maintain();