1 import 'babel-polyfill';
3 import Config
from './config';
7 import ApplyForceSystem
from './systems/apply_force';
8 import CreateCouplingLineSystem
from './systems/create_coupling_line';
9 import ControlMapperSystem
from './systems/control_mapper';
10 import DashSystem
from './systems/dash';
11 import DetectPointsCollisionSystem
from './systems/detect_points_collision';
12 import DetectWinnerSystem
from './systems/detect_winner';
13 import DrawDashSystem
from './systems/draw_dash';
14 import DrawGrabSystem
from './systems/draw_grab';
15 import ElasticSystem
from './systems/elastic';
16 import GrabSystem
from './systems/grab';
17 import PhysicsWorldControlSystem
from './systems/physics_world_control';
18 import PhysicsToAttributesSystem
from './systems/physics_to_attributes';
19 import RenderPointsSystem
from './systems/render_points';
20 import RenderSystem
from './systems/render';
21 import RenderWinnerSystem
from './systems/render_winner';
22 import AttributesToRenderableSystem
from './systems/attributes_to_renderable';
26 import SumoFactory
from './factories/sumo';
28 // External Dependencies
30 import Serpentity
from '@serpentity/serpentity';
31 import { Application
} from 'pixi.js';
32 import { Engine
} from 'matter-js';
35 kBackgroundColor: 0xd8c590,
36 kNoElementError: 'No element found. Cannot render.',
38 // Handler for the window load event. Initializes and runs the app.
42 const sumo
= new internals
.Sumo(Object
.assign({
43 element: document
.getElementById('sumo-app-entry-point')
53 * Sumo - main entry point. Attached to window->load
57 * @param {object} config the configuration to extend the object
59 * @property {HTMLElement} [element=null] the element in which to render.
60 * Required, will throw if not provided
61 * @property {Number} [fps=60] the fps target to maintain
62 * @property {Number} [verticalResolution=224] how many pixels to render in the vertical
63 * axis (gets scaled if the canvas is larger)
64 * @property {Array<Number>} [aspectRatio=[2.76, 1]] the aspect ratio experssed as
65 * an array of two numbers, where aspect ratio x:y is [x, y] (eg. [16, 9])
68 internals
.Sumo
= class Sumo
{
72 // These defaults can get overridden by config
74 this.aspectRatio
= [2.76, 1];
75 this.verticalResolution
= 224;
77 Object
.assign(this, config
);
80 throw new Error(internals
.kNoElementError
);
83 this._engine
= new Serpentity();
85 this._previousTime
= 0;
86 this._looping
= false;
88 // Initialization functions
89 this._initializeCanvas();
90 this._initializeMatter();
91 this._initializePixi();
92 this._initializeSystems();
93 this._initializeEntities();
97 * Starts the main loop. Resets the FPS (if you change it it won't go
98 * live until after you stop and start the loop)
100 * @function startLoop
106 this._looping
= true;
107 this._frameDuration
= 1000 / this.fps
;
108 window
.requestAnimationFrame(this._loop
.bind(this));
114 * @function pauseLoop
120 this._looping
= false;
123 // The main loop used above. Runs the serpentity update process and
124 // attempts to maintain FPS. The rest is handled by the engine.
128 if (!this._looping
) {
132 window
.requestAnimationFrame(this._loop
.bind(this));
134 const currentFrameDuration
= currentTime
- this._previousTime
;
136 if (currentFrameDuration
> this._frameDuration
) {
138 // We're sending the currentTime since it gives better results for
139 // this type of renderer, though usually we expect the delta
140 this._engine
.update(currentFrameDuration
);
141 this._previousTime
= currentTime
;
145 // Creates a canvas for rendering
147 _initializeCanvas() {
149 this._canvas
= document
.createElement('canvas');
150 this.element
.appendChild(this._canvas
);
151 this._resizeCanvas();
152 window
.addEventListener('resize', this._resizeCanvas
.bind(this));
155 // Initialize MatterJs
157 _initializeMatter() {
159 this._matterJs
= Engine
.create();
161 this._matterJs
.world
.gravity
.y
= 0;
168 this._pixi
= new Application({
169 backgroundColor: internals
.kBackgroundColor
,
171 width: this._canvas
.width
,
172 height: this._canvas
.height
176 // Resizes the canvas to a square the size of the smallest magnitude
181 let width
= window
.innerWidth
;
182 let height
= Math
.round(width
* this.aspectRatio
[1] / this.aspectRatio
[0]);
184 if (window
.innerHeight
< height
) {
185 height
= window
.innerHeight
;
186 width
= Math
.round(height
* this.aspectRatio
[0] / this.aspectRatio
[1]);
189 this._canvas
.style
.width
= `${width}px`;
190 this._canvas
.style
.height
= `${height}px`;
192 this._canvas
.width
= Math
.round(this.verticalResolution
* this.aspectRatio
[0] / this.aspectRatio
[1]);
193 this._canvas
.height
= this.verticalResolution
;
196 // Initializes the serpentity systems
198 _initializeSystems() {
200 this._engine
.addSystem(new ControlMapperSystem());
202 this._engine
.addSystem(new DashSystem());
204 this._engine
.addSystem(new GrabSystem({
205 engine: this._matterJs
208 this._engine
.addSystem(new ApplyForceSystem());
210 this._engine
.addSystem(new PhysicsWorldControlSystem({
211 engine: this._matterJs
214 this._engine
.addSystem(new DetectPointsCollisionSystem());
216 this._engine
.addSystem(new DetectWinnerSystem());
218 this._engine
.addSystem(new RenderPointsSystem({
219 application: this._pixi
222 this._engine
.addSystem(new RenderWinnerSystem({
223 application: this._pixi
226 this._engine
.addSystem(new ElasticSystem());
228 this._engine
.addSystem(new PhysicsToAttributesSystem());
230 this._engine
.addSystem(new AttributesToRenderableSystem());
232 this._engine
.addSystem(new CreateCouplingLineSystem());
234 this._engine
.addSystem(new DrawDashSystem());
236 this._engine
.addSystem(new DrawGrabSystem());
238 this._engine
.addSystem(new RenderSystem({
239 application: this._pixi
243 // Initializes the serpentity entities
245 _initializeEntities() {
247 SumoFactory
.createArena(this._engine
, {
249 x: this.horizontalResolution
/ 2,
250 y: this.verticalResolution
/ 2
254 const sumoA
= SumoFactory
.createPlayer1Sumo(null, {
256 x: this.horizontalResolution
/ 2 - 100,
257 y: this.verticalResolution
/ 2
261 const sumoB
= SumoFactory
.createPlayer2Sumo(null, {
263 x: this.horizontalResolution
/ 2 + 100,
264 y: this.verticalResolution
/ 2
268 const harness
= SumoFactory
.createHarness(null, {
270 x: this.horizontalResolution
/ 2,
271 y: this.verticalResolution
/ 2
275 SumoFactory
.createRubberBand(this._engine
, {
280 SumoFactory
.createRubberBand(this._engine
, {
287 SumoFactory
.createInvisibleBlock(this._engine
, {
288 width: this.horizontalResolution
* 2,
289 height: this.verticalResolution
* 0.1,
291 x: this.horizontalResolution
/ 2,
292 y: -this.verticalResolution
* 0.1
296 SumoFactory
.createInvisibleBlock(this._engine
, {
297 width: this.horizontalResolution
* 2,
298 height: this.verticalResolution
* 0.1,
300 x: this.horizontalResolution
/ 2,
301 y: this.verticalResolution
+ this.verticalResolution
* 0.1
307 SumoFactory
.createPointsCollider(this._engine
, {
308 collisionTarget: sumoA
,
310 height: this.verticalResolution
,
311 width: this.horizontalResolution
,
313 x: this.horizontalResolution
+ this.horizontalResolution
/ 2,
314 y: this.verticalResolution
/ 2
318 SumoFactory
.createPointsCollider(this._engine
, {
319 collisionTarget: sumoB
,
320 pointsTarget: 'blue',
321 height: this.verticalResolution
,
322 width: this.horizontalResolution
,
324 x: -this.horizontalResolution
/ 2,
325 y: this.verticalResolution
/ 2
330 SumoFactory
.createGameState(this._engine
);
332 // To keep the coupling behind, we'll manually add the sumos later
334 this._engine
.addEntity(sumoA
);
335 this._engine
.addEntity(sumoB
);
336 this._engine
.addEntity(harness
);
340 export default internals
.exports
= {};
343 window
.addEventListener('load', internals
.onLoad
);