aboutsummaryrefslogtreecommitdiff
path: root/lib/sumo.js
blob: f353fe08432e38ded2da4c63a2038267effcd55e (plain)
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
340
341
342
343
import 'babel-polyfill';

import Config from './config';

// Systems

import ApplyForceSystem from './systems/apply_force';
import CreateCouplingLineSystem from './systems/create_coupling_line';
import ControlMapperSystem from './systems/control_mapper';
import DashSystem from './systems/dash';
import DetectPointsCollisionSystem from './systems/detect_points_collision';
import DetectWinnerSystem from './systems/detect_winner';
import DrawDashSystem from './systems/draw_dash';
import DrawGrabSystem from './systems/draw_grab';
import ElasticSystem from './systems/elastic';
import GrabSystem from './systems/grab';
import PhysicsWorldControlSystem from './systems/physics_world_control';
import PhysicsToAttributesSystem from './systems/physics_to_attributes';
import RenderPointsSystem from './systems/render_points';
import RenderSystem from './systems/render';
import RenderWinnerSystem from './systems/render_winner';
import AttributesToRenderableSystem from './systems/attributes_to_renderable';

// Factories

import SumoFactory from './factories/sumo';

// External Dependencies

import Serpentity from '@serpentity/serpentity';
import { Application } from 'pixi.js';
import { Engine } from 'matter-js';

const internals = {
  kBackgroundColor: 0xd8c590,
  kNoElementError: 'No element found. Cannot render.',

  // Handler for the window load event. Initializes and runs the app.

  onLoad() {

    const sumo = new internals.Sumo(Object.assign({
      element: document.getElementById('sumo-app-entry-point')
    }, Config));

    sumo.startLoop();

    window.sumo = sumo;
  }
};

/**
 * Sumo - main entry point. Attached to window->load
 *
 * @class Sumo
 *
 * @param {object} config the configuration to extend the object
 *
 * @property {HTMLElement} [element=null] the element in which to render.
 * Required, will throw if not provided
 * @property {Number} [fps=60] the fps target to maintain
 * @property {Number} [verticalResolution=224] how many pixels to render in the vertical
 * axis (gets scaled if the canvas is larger)
 * @property {Array<Number>} [aspectRatio=[2.76, 1]] the aspect ratio experssed as
 * an array of two numbers, where aspect ratio x:y is [x, y] (eg. [16, 9])
 */

internals.Sumo = class Sumo {

  constructor(config) {

    // These defaults can get overridden by config
    this.fps = 60;
    this.aspectRatio = [2.76, 1];
    this.verticalResolution = 224;

    Object.assign(this, config);

    if (!this.element) {
      throw new Error(internals.kNoElementError);
    }

    this._engine = new Serpentity();

    this._previousTime = 0;
    this._looping = false;

    // Initialization functions
    this._initializeCanvas();
    this._initializeMatter();
    this._initializePixi();
    this._initializeSystems();
    this._initializeEntities();
  }

  /**
   * Starts the main loop. Resets the FPS (if you change it it won't go
   * live until after you stop and start the loop)
   *
   * @function startLoop
   * @instance
   * @memberof Sumo
   */
  startLoop() {

    this._looping = true;
    this._frameDuration = 1000 / this.fps;
    window.requestAnimationFrame(this._loop.bind(this));
  }

  /**
 * Pauses the loop
 *
 * @function pauseLoop
 * @instance
 * @memberof Sumo
 */
  pauseLoop() {

    this._looping = false;
  }

  // The main loop used above. Runs the serpentity update process and
  // attempts to maintain FPS. The rest is handled by the engine.

  _loop(currentTime) {

    if (!this._looping) {
      return;
    }

    window.requestAnimationFrame(this._loop.bind(this));

    const currentFrameDuration = currentTime - this._previousTime;

    if (currentFrameDuration > this._frameDuration) {

      // We're sending the currentTime since it gives better results for
      // this type of renderer, though usually we expect the delta
      this._engine.update(currentFrameDuration);
      this._previousTime = currentTime;
    }
  }

  // Creates a canvas for rendering

  _initializeCanvas() {

    this._canvas = document.createElement('canvas');
    this.element.appendChild(this._canvas);
    this._resizeCanvas();
    window.addEventListener('resize', this._resizeCanvas.bind(this));
  }

  // Initialize MatterJs

  _initializeMatter() {

    this._matterJs = Engine.create();

    this._matterJs.world.gravity.y = 0;
  }

  // Initialize Pixi

  _initializePixi() {

    this._pixi = new Application({
      backgroundColor: internals.kBackgroundColor,
      view: this._canvas,
      width: this._canvas.width,
      height: this._canvas.height
    });
  }

  // Resizes the canvas to a square the size of the smallest magnitude
  // of the window.

  _resizeCanvas() {

    let width = window.innerWidth;
    let height = Math.round(width * this.aspectRatio[1] / this.aspectRatio[0]);

    if (window.innerHeight < height) {
      height = window.innerHeight;
      width = Math.round(height * this.aspectRatio[0] / this.aspectRatio[1]);
    }

    this._canvas.style.width = `${width}px`;
    this._canvas.style.height = `${height}px`;

    this._canvas.width = Math.round(this.verticalResolution * this.aspectRatio[0] / this.aspectRatio[1]);
    this._canvas.height = this.verticalResolution;
  }

  // Initializes the serpentity systems

  _initializeSystems() {

    this._engine.addSystem(new ControlMapperSystem());

    this._engine.addSystem(new DashSystem());

    this._engine.addSystem(new GrabSystem({
      engine: this._matterJs
    }));

    this._engine.addSystem(new ApplyForceSystem());

    this._engine.addSystem(new PhysicsWorldControlSystem({
      engine: this._matterJs
    }));

    this._engine.addSystem(new DetectPointsCollisionSystem());

    this._engine.addSystem(new DetectWinnerSystem());

    this._engine.addSystem(new RenderPointsSystem({
      application: this._pixi
    }));

    this._engine.addSystem(new RenderWinnerSystem({
      application: this._pixi
    }));

    this._engine.addSystem(new ElasticSystem());

    this._engine.addSystem(new PhysicsToAttributesSystem());

    this._engine.addSystem(new AttributesToRenderableSystem());

    this._engine.addSystem(new CreateCouplingLineSystem());

    this._engine.addSystem(new DrawDashSystem());

    this._engine.addSystem(new DrawGrabSystem());

    this._engine.addSystem(new RenderSystem({
      application: this._pixi
    }));
  }

  // Initializes the serpentity entities

  _initializeEntities() {

    SumoFactory.createArena(this._engine, {
      position: {
        x: this.horizontalResolution / 2,
        y: this.verticalResolution / 2
      }
    });

    const sumoA = SumoFactory.createPlayer1Sumo(null, {
      position: {
        x: this.horizontalResolution / 2 - 100,
        y: this.verticalResolution / 2
      }
    });

    const sumoB = SumoFactory.createPlayer2Sumo(null, {
      position: {
        x: this.horizontalResolution / 2 + 100,
        y: this.verticalResolution / 2
      }
    });

    const harness = SumoFactory.createHarness(null, {
      position: {
        x: this.horizontalResolution / 2,
        y: this.verticalResolution / 2
      }
    });

    SumoFactory.createRubberBand(this._engine, {
      entityA: sumoA,
      entityB: harness
    });

    SumoFactory.createRubberBand(this._engine, {
      entityA: sumoB,
      entityB: harness
    });

    // Walls

    SumoFactory.createInvisibleBlock(this._engine, {
      width: this.horizontalResolution * 2,
      height: this.verticalResolution * 0.1,
      position: {
        x: this.horizontalResolution / 2,
        y: -this.verticalResolution * 0.1
      }
    });

    SumoFactory.createInvisibleBlock(this._engine, {
      width: this.horizontalResolution * 2,
      height: this.verticalResolution * 0.1,
      position: {
        x: this.horizontalResolution / 2,
        y: this.verticalResolution + this.verticalResolution * 0.1
      }
    });

    // Points Detector

    SumoFactory.createPointsCollider(this._engine, {
      collisionTarget: sumoA,
      pointsTarget: 'red',
      height: this.verticalResolution,
      width: this.horizontalResolution,
      position: {
        x: this.horizontalResolution + this.horizontalResolution / 2,
        y: this.verticalResolution / 2
      }
    });

    SumoFactory.createPointsCollider(this._engine, {
      collisionTarget: sumoB,
      pointsTarget: 'blue',
      height: this.verticalResolution,
      width: this.horizontalResolution,
      position: {
        x: -this.horizontalResolution / 2,
        y: this.verticalResolution / 2
      }
    });

    // The game state
    SumoFactory.createGameState(this._engine);

    // To keep the coupling behind, we'll manually add the sumos later

    this._engine.addEntity(sumoA);
    this._engine.addEntity(sumoB);
    this._engine.addEntity(harness);
  }
};

export default internals.exports = {};

// autorun.bat
window.addEventListener('load', internals.onLoad);