-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (52 loc) · 1.42 KB
/
index.js
File metadata and controls
63 lines (52 loc) · 1.42 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const Entity = require('../../entity');
const AComponent = require('../../acomponent');
const SystemComponent = require('../../system_component');
const ASystem = require('../../asystem');
// Simple component which only hold `payload`.
class DataComponent extends AComponent
{
constructor(parent)
{
super(parent);
this.payload = null;
}
// Allow easy access: `entity.data` will return the component
static get identity()
{
return 'data';
}
get identity()
{
return DataComponent.identity;
}
}
class DataSystem extends ASystem
{
constructor(parent)
{
// This sytem works on entities which have the component DataComponent
super(parent, [DataComponent]);
}
earlyUpdate(entities)
{
entities.forEach(entity =>
{
console.log(`Payload of entity '${entity.name}' is ${entity.data.payload}`);
});
}
}
// Create a new world with the capability of running systems
const world = Entity.createWorld([SystemComponent]);
world.systems.add(DataSystem);
// create a child to world and add DataComponent
const child1 = world.createChild('child1', [DataComponent]);
child1.data.payload = 42;
const child2 = world.createChild('child2');
child2.add(DataComponent); // Also works
child2.data.payload = 43;
// Run the update() procedure which call update() on components and systems
console.log('Update...');
world.update();
child1.data.payload = -1;
console.log('Update...');
world.update();