-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
265 lines (232 loc) · 6.97 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* This is an implementation of the Game of Life using NECS.
* https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
*
* It may be poorly written and unoptimized, it is a quick demonstration for
* fun purposes. An ECS is not the best paradigm for the Game Of Life.
*/
const TerminalCanvas = require('terminal-canvas');
const Entity = require('../../entity');
const AComponent = require('../../acomponent');
const ASystem = require('../../asystem');
const SystemComponent = require('../../system_component');
const uniqWith = require('lodash/uniqWith');
const isEqual = require('lodash/isEqual');
const uniqueId = require('lodash/uniqueId');
/**
* Represent a living cell in the world
* It have a position and we save if on the next state it will still be alive
*/
class CellComponent extends AComponent
{
constructor(parentEntity, position)
{
super(parentEntity);
if (typeof position !== 'object'
|| typeof position.x !== 'number' || typeof position.y !== 'number')
{
throw new TypeError('position is not properly set');
}
this._position = Object.assign({}, position);
this._willBeAlive = true;
}
// Both identity() and static identity() are mandatory. Represent the name
// of the component for easy access (eg. `component.cell`).
get identity()
{
return CellComponent.identity;
}
static get identity()
{
return 'cell';
}
set willBeAlive(v)
{
this._willBeAlive = v;
}
get willBeAlive()
{
return this._willBeAlive;
}
get position()
{
return this._position;
}
}
/**
* A system that enforce Game of Life rules.
* See https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life for rules
*/
class CellsRulesSystem extends ASystem
{
constructor(parent)
{
super(parent, [CellComponent]);
}
earlyUpdate(entities)
{
const positionsToCreate = [];
// forEach on all entities that comply to the requirements (eg. [CellComponent]: which have a CellComponent).
entities.forEach(entity =>
{
const adjacentCount = this._countAdjacentEntity(entity, entities);
this._findNewCells(entity, entities, positionsToCreate);
// Mark cells to die according to rules
if (adjacentCount <= 1)
{
entity.cell.willBeAlive = false;
}
else if (adjacentCount > 3)
{
entity.cell.willBeAlive = false;
}
});
// Create new cells according to rules
const uniquePositionsToCreate = uniqWith(positionsToCreate, isEqual);
uniquePositionsToCreate.forEach(position =>
{
const child = this.parent.createChild(uniqueId('cell_'));
child.add(CellComponent, position);
});
}
_findNewCells(entity1, entities, positionsToCreate)
{
const positions =
[
{x: entity1.cell.position.x - 1, y: entity1.cell.position.y - 1},
{x: entity1.cell.position.x - 1, y: entity1.cell.position.y + 0},
{x: entity1.cell.position.x - 1, y: entity1.cell.position.y + 1},
{x: entity1.cell.position.x + 0, y: entity1.cell.position.y - 1},
{x: entity1.cell.position.x + 0, y: entity1.cell.position.y + 1},
{x: entity1.cell.position.x + 1, y: entity1.cell.position.y - 1},
{x: entity1.cell.position.x + 1, y: entity1.cell.position.y + 0},
{x: entity1.cell.position.x + 1, y: entity1.cell.position.y + 1},
];
positions.forEach(position =>
{
const count = this._countAdjacentPosition(position, entities);
if (count === 3)
{
if (!this._isEntitiesOnPosition(position, entities))
{
positionsToCreate.push(position);
}
}
});
}
_isEntitiesOnPosition(position, entities)
{
return entities.some(entity =>
{
if (position.x === entity.cell.position.x &&
position.y === entity.cell.position.y)
{
return true;
}
return false;
});
}
_countAdjacentPosition(position1, entities)
{
let count = 0;
entities.forEach(entity2 =>
{
if (this._isAdjacent(position1, entity2.cell.position))
{
++count;
}
});
return count;
}
_countAdjacentEntity(entity1, entities)
{
let count = 0;
entities.forEach(entity2 =>
{
if (entity1 !== entity2
&& this._isAdjacent(entity1.cell.position, entity2.cell.position))
{
++count;
}
});
return count;
}
_isAdjacent(position1, position2)
{
if (Math.abs(position1.x - position2.x) <= 1
&& Math.abs(position1.y - position2.y) <= 1)
{
return true;
}
return false;
}
}
/**
* System that delete cells marked as dead.
*/
class DeleteDeadCellsSystem extends ASystem
{
constructor(parent)
{
super(parent, [CellComponent]);
}
earlyUpdate(entities)
{
entities.forEach(entity =>
{
if (!entity.cell.willBeAlive)
{
entity.deleteThis();
}
});
}
}
/**
* System that draw cell on the terminal using `TerminalCanvas`.
*/
class DrawSystem extends ASystem
{
constructor(parent)
{
super(parent, [CellComponent]);
this._canvas = new TerminalCanvas();
this._BLOCK_CHARACTER = '\u2588';
this._canvas.reset();
}
earlyUpdate(entities)
{
this._canvas.eraseScreen();
entities.forEach(entity =>
{
const position = entity.cell.position;
this._canvas.moveTo(position.x, position.y);
this._canvas.write(this._BLOCK_CHARACTER);
this._canvas.flush();
});
this._canvas.moveTo(200, 200);
this._canvas.write(this._BLOCK_CHARACTER);
this._canvas.flush();
}
}
// Create the world and add the systems.
// Systems are always executed in order of adding
const world = Entity.createWorld([SystemComponent]);
world.systems.add(CellsRulesSystem);
world.systems.add(DeleteDeadCellsSystem);
world.systems.add(DrawSystem);
// Initial cells
const child1 = world.createChild('child1');
child1.add(CellComponent, {x: 15, y: 15});
const child2 = world.createChild('child2');
child2.add(CellComponent, {x: 16, y: 15});
const child3 = world.createChild('child3');
child3.add(CellComponent, {x: 17, y: 15});
const child4 = world.createChild('child4');
child4.add(CellComponent, {x: 17, y: 14});
const child5 = world.createChild('child5');
child5.add(CellComponent, {x: 16, y: 13});
// Then update every N ms, cells will live and move thanks to systems.
setInterval(() =>
{
world.update();
}, 50);