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 DrawDashSystem
from './systems/draw_dash';
12 import DrawGrabSystem
from './systems/draw_grab';
13 import ElasticSystem
from './systems/elastic';
14 import GrabSystem
from './systems/grab';
15 import PhysicsWorldControlSystem
from './systems/physics_world_control';
16 import PhysicsToAttributesSystem
from './systems/physics_to_attributes';
17 import RenderSystem
from './systems/render';
18 import AttributesToRenderableSystem
from './systems/attributes_to_renderable';
22 import SumoFactory
from './factories/sumo';
24 // External Dependencies
26 import Serpentity
from '@serpentity/serpentity';
27 import { Application
} from 'pixi.js';
28 import { Engine
} from 'matter-js';
30 /* global window document */
33 kBackgroundColor: 0xd8c590,
34 kNoElementError: 'No element found. Cannot render.',
36 // Handler for the window load event. Initializes and runs the app.
40 const sumo
= new internals
.Sumo(Object
.assign({
41 element: document
.getElementById('sumo-app-entry-point')
51 * Sumo - main entry point. Attached to window->load
55 * @param {object} config the configuration to extend the object
57 * @property {HTMLElement} [element=null] the element in which to render.
58 * Required, will throw if not provided
59 * @property {Number} [fps=60] the fps target to maintain
60 * @property {Number} [verticalResolution=224] how many pixels to render in the vertical
61 * axis (gets scaled if the canvas is larger)
62 * @property {Array<Number>} [aspectRatio=[2.76, 1]] the aspect ratio experssed as
63 * an array of two numbers, where aspect ratio x:y is [x, y] (eg. [16, 9])
66 internals
.Sumo
= class Sumo
{
70 // These defaults can get overridden by config
72 this.aspectRatio
= [2.76, 1];
73 this.verticalResolution
= 224;
75 Object
.assign(this, config
);
78 throw new Error(internals
.kNoElementError
);
81 this._engine
= new Serpentity();
83 this._previousTime
= 0;
84 this._looping
= false;
86 // Initialization functions
87 this._initializeCanvas();
88 this._initializeMatter();
89 this._initializePixi();
90 this._initializeSystems();
91 this._initializeEntities();
95 * Starts the main loop. Resets the FPS (if you change it it won't go
96 * live until after you stop and start the loop)
104 this._looping
= true;
105 this._frameDuration
= 1000 / this.fps
;
106 window
.requestAnimationFrame(this._loop
.bind(this));
112 * @function pauseLoop
118 this._looping
= false;
121 // The main loop used above. Runs the serpentity update process and
122 // attempts to maintain FPS. The rest is handled by the engine.
126 if (!this._looping
) {
130 window
.requestAnimationFrame(this._loop
.bind(this));
132 const currentFrameDuration
= currentTime
- this._previousTime
;
134 if (currentFrameDuration
> this._frameDuration
) {
136 // We're sending the currentTime since it gives better results for
137 // this type of renderer, though usually we expect the delta
138 this._engine
.update(currentFrameDuration
);
139 this._previousTime
= currentTime
;
143 // Creates a canvas for rendering
145 _initializeCanvas() {
147 this._canvas
= document
.createElement('canvas');
148 this.element
.appendChild(this._canvas
);
149 this._resizeCanvas();
150 window
.addEventListener('resize', this._resizeCanvas
.bind(this));
153 // Initialize MatterJs
155 _initializeMatter() {
157 this._matterJs
= Engine
.create();
159 this._matterJs
.world
.gravity
.y
= 0;
166 this._pixi
= new Application({
167 backgroundColor: internals
.kBackgroundColor
,
169 width: this._canvas
.width
,
170 height: this._canvas
.height
174 // Resizes the canvas to a square the size of the smallest magnitude
179 let width
= window
.innerWidth
;
180 let height
= Math
.round(width
* this.aspectRatio
[1] / this.aspectRatio
[0]);
182 if (window
.innerHeight
< height
) {
183 height
= window
.innerHeight
;
184 width
= Math
.round(height
* this.aspectRatio
[0] / this.aspectRatio
[1]);
187 this._canvas
.style
.width
= `${width}px`;
188 this._canvas
.style
.height
= `${height}px`;
190 this._canvas
.width
= Math
.round(this.verticalResolution
* this.aspectRatio
[0] / this.aspectRatio
[1]);
191 this._canvas
.height
= this.verticalResolution
;
194 // Initializes the serpentity systems
196 _initializeSystems() {
198 this._engine
.addSystem(new ControlMapperSystem());
200 this._engine
.addSystem(new DashSystem());
202 this._engine
.addSystem(new GrabSystem({
203 engine: this._matterJs
206 this._engine
.addSystem(new ApplyForceSystem());
208 this._engine
.addSystem(new PhysicsWorldControlSystem({
209 engine: this._matterJs
212 this._engine
.addSystem(new ElasticSystem());
214 this._engine
.addSystem(new PhysicsToAttributesSystem());
216 this._engine
.addSystem(new AttributesToRenderableSystem());
218 this._engine
.addSystem(new CreateCouplingLineSystem());
220 this._engine
.addSystem(new DrawDashSystem());
222 this._engine
.addSystem(new DrawGrabSystem());
224 this._engine
.addSystem(new RenderSystem({
225 application: this._pixi
229 // Initializes the serpentity entities
231 _initializeEntities() {
233 SumoFactory
.createArena(this._engine
, {
235 x: this.horizontalResolution
/ 2,
236 y: this.verticalResolution
/ 2
240 const sumoA
= SumoFactory
.createSumo(null, {
242 x: this.horizontalResolution
/ 2 - 100,
243 y: this.verticalResolution
/ 2
247 const sumoB
= SumoFactory
.createControllableSumo(null, {
249 x: this.horizontalResolution
/ 2 + 100,
250 y: this.verticalResolution
/ 2
254 const harness
= SumoFactory
.createHarness(null, {
256 x: this.horizontalResolution
/ 2,
257 y: this.verticalResolution
/ 2
261 SumoFactory
.createRubberBand(this._engine
, {
266 SumoFactory
.createRubberBand(this._engine
, {
271 SumoFactory
.createInvisibleBlock(this._engine
, {
272 width: this.horizontalResolution
* 2,
273 height: this.verticalResolution
* 0.1,
275 x: this.horizontalResolution
/ 2,
276 y: -this.verticalResolution
* 0.1
280 SumoFactory
.createInvisibleBlock(this._engine
, {
281 width: this.horizontalResolution
* 2,
282 height: this.verticalResolution
* 0.1,
284 x: this.horizontalResolution
/ 2,
285 y: this.verticalResolution
+ this.verticalResolution
* 0.1
289 // To keep the coupling behind, we'll manually add the sumos later
291 this._engine
.addEntity(sumoA
);
292 this._engine
.addEntity(sumoB
);
293 this._engine
.addEntity(harness
);
297 export default internals
.exports
= {};
300 window
.addEventListener('load', internals
.onLoad
);