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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
import { Bodies, Constraint } from 'matter-js';
import { Entity } from '@serpentity/serpentity';
// Components
import AngleComponent from '../components/angle';
import BodyComponent from '../components/body';
import ControlMapComponent from '../components/control_map';
import CoupledEntitiesComponent from '../components/coupled_entities';
import DashComponent from '../components/dash';
import ElasticComponent from '../components/elastic';
import ForceComponent from '../components/force';
import MaxVelocityComponent from '../components/max_velocity';
import PositionComponent from '@serpentity/components.position';
import PixiContainerComponent from '../components/pixi_container';
import PixiFactory from '../factories/pixi';
import Config from '../config';
const internals = {
kNoEntityError: 'Entity Not Found: This method requires entityA and entityB to be set in the config.',
kEntityHasNoBodyError: 'Entity Has No Body: This method requires entities have a BodyComponent.'
};
/**
* Factory object that contains many methods to create prefab entities.
*
* @type object
* @name SumoFactory
*/
export default {
/**
* Creates a sumo entity and adds it to the engine. Can override
* position in the config object
*
* @function createSumo
* @memberof SumoFactory
* @param {external:Serpentity} [engine] the serpentity engine to attach
* to. If not sent, it will not be attached.
* @param {object} [config] the config to override the entity, accepts
* the key `position` as an object with an x and y property.
* @return {external:Serpentity.Entity} the created entity
*/
createSumo(engine, config = {}) {
const entity = new Entity();
// POSITION
entity.addComponent(new PositionComponent(config.position));
const position = entity.getComponent(PositionComponent);
entity.addComponent(new AngleComponent(config.angle));
const angle = entity.getComponent(AngleComponent);
entity.addComponent(new ForceComponent(config.force));
config.maxVelocity = {
maxVelocity: 12
};
entity.addComponent(new MaxVelocityComponent(config.maxVelocity));
// CONTROLS & ABILITIES
entity.addComponent(new DashComponent(config.dash));
// RENDERING
const radius = 25;
const container = config.container || {
container: PixiFactory.createSumo({ radius })
};
container.container.position.x = position.x;
container.container.position.y = position.y;
container.container.rotation = angle.angle;
entity.addComponent(new PixiContainerComponent(container));
// PHYSICS
const frictionAir = 0.02;
const friction = 1;
const frictionStatic = 1;
const restitution = 1;
const density = 1.5;
const body = Bodies.circle(position.x / Config.meterSize, position.y / Config.meterSize, radius / Config.meterSize, {
label: 'Sumo body',
angle: angle.angle,
density,
frictionAir,
frictionStatic,
friction,
restitution
});
entity.addComponent(new BodyComponent({ body }));
if (engine) {
engine.addEntity(entity);
}
return entity;
},
/**
* Creates a rubber band entity and adds it to the engine.
*
* @function createRubberBand
* @memberof SumoFactory
* @param {external:Serpentity} [engine] the serpentity engine to attach
* to. If not sent, it will not be attached.
* @param {object} [config] the config to override the entity, it
* must include entityA and entityB which will be tied by it. If they
* are not sent or don't have a physics body, this will throw.
* @return {external:Serpentity.Entity} the created entity
*/
createRubberBand(engine, config = {}) {
const entity = new Entity();
if (!config.entityA || !config.entityB) {
throw new Error(internals.kNoEntityError);
}
if (!config.entityA.hasComponent(BodyComponent) || !config.entityB.hasComponent(BodyComponent)) {
throw new Error(internals.kEntityHasNoBodyError);
}
// RENDERING
const container = config.container || {
container: PixiFactory.createEmptyGraphic()
};
entity.addComponent(new PixiContainerComponent(container));
// PHYSICS
const bodyA = config.entityA.getComponent(BodyComponent).body;
const bodyB = config.entityB.getComponent(BodyComponent).body;
const damping = 0;
const length = 100 / Config.meterSize;
const stiffness = 0.001;
const body = Constraint.create({
bodyA,
bodyB,
damping,
length,
stiffness
});
entity.addComponent(new BodyComponent({ body }));
entity.addComponent(new CoupledEntitiesComponent({
coupledEntities: [config.entityA, config.entityB]
}));
entity.addComponent(new ElasticComponent());
if (engine) {
engine.addEntity(entity);
}
return entity;
},
/**
* Creates a controllable sumo entity and adds it to the engine. Can override
* position in the config object
*
* @function createControllableSumo
* @memberof SumoFactory
* @param {external:Serpentity} [engine] the serpentity engine to attach
* to. If not sent, it will not be attached.
* @param {object} [config] the config to override the entity, accepts
* the key `position` as an object with an x and y property.
* @return {external:Serpentity.Entity} the created entity
*/
createControllableSumo(engine, config = {}) {
const entity = this.createSumo(null, config);
entity.addComponent(new ControlMapComponent({
map: [
{
source: {
type: 'keyboard',
index: 37 // left arrow
},
target: {
component: ForceComponent,
property: 'x',
value: (value) => -Number(value)
}
},
{
source: {
type: 'keyboard',
index: 39 // right arrow
},
target: {
component: ForceComponent,
property: 'x',
value: (value) => Number(value)
}
},
{
source: {
type: 'keyboard',
index: 38 // up arrow
},
target: {
component: ForceComponent,
property: 'y',
value: (value) => -Number(value)
}
},
{
source: {
type: 'keyboard',
index: 40 // down arrow
},
target: {
component: ForceComponent,
property: 'y',
value: (value) => Number(value)
}
},
{
source: {
type: 'keyboard',
index: 90 // Z
},
target: {
component: DashComponent,
property: 'dashing'
}
}
]
}));
if (engine) {
engine.addEntity(entity);
}
return entity;
},
/**
* Creates a static harness entity
*
* @function createHarness
* @memberof SumoFactory
* @param {external:Serpentity} [engine] the serpentity engine to attach
* to. If not sent, it will not be attached.
* @param {object} [config] the config to override the entity, accepts
* the key `position` as an object with an x and y property.
* @return {external:Serpentity.Entity} the created entity
*/
createHarness(engine, config = {}) {
const entity = new Entity();
// POSITION
entity.addComponent(new PositionComponent(config.position));
const position = entity.getComponent(PositionComponent);
// RENDERING
const radius = 15;
const container = config.container || {
container: PixiFactory.createHarness({ radius })
};
container.container.position.x = position.x;
container.container.position.y = position.y;
entity.addComponent(new PixiContainerComponent(container));
// PHYSICS
const friction = 0;
const frictionStatic = 0;
const restitution = 1;
const body = Bodies.circle(position.x / Config.meterSize, position.y / Config.meterSize, radius / Config.meterSize, {
isStatic: true,
label: 'Harness body',
friction,
restitution,
frictionStatic
});
entity.addComponent(new BodyComponent({ body }));
if (engine) {
engine.addEntity(entity);
}
return entity;
},
/**
* Creates a static arena entity
*
* @function createArena
* @memberof SumoFactory
* @param {external:Serpentity} [engine] the serpentity engine to attach
* to. If not sent, it will not be attached.
* @param {object} [config] the config to override the entity, accepts
* the key `position` as an object with an x and y property.
* @return {external:Serpentity.Entity} the created entity
*/
createArena(engine, config = {}) {
const entity = new Entity();
// POSITION
entity.addComponent(new PositionComponent(config.position));
const position = entity.getComponent(PositionComponent);
// RENDERING
const radius = 300;
const container = config.container || {
container: PixiFactory.createArena({ radius })
};
container.container.position.x = position.x;
container.container.position.y = position.y;
entity.addComponent(new PixiContainerComponent(container));
if (engine) {
engine.addEntity(entity);
}
return entity;
}
};
|