1 import PhysicsWorldControlSystem
from './systems/physics_world_control';
2 import PhysicsToAttributesSystem
from './systems/physics_to_attributes';
3 import CreateCouplingLineSystem
from './systems/create_coupling_line';
4 import RenderSystem
from './systems/render';
5 import AttributesToRenderableSystem
from './systems/attributes_to_renderable';
6 import SumoFactory
from './factories/sumo';
7 import Serpentity
from '@serpentity/serpentity';
8 import { Application
} from 'pixi.js';
9 import { Engine
} from 'matter-js';
11 /* global window document */
14 kBackgroundColor: 0xd8c590,
15 kNoElementError: 'No element found. Cannot render.',
17 // Handler for the window load event. Initializes and runs the app.
21 const sumo
= new internals
.Sumo({
22 element: document
.getElementById('sumo-app-entry-point')
27 internals
.exports
.sumo
= sumo
;
32 * Sumo - main entry point. Attached to window->load
36 * @param {object} config the configuration to extend the object
38 * @property {HTMLElement} [element=null] the element in which to render.
39 * Required, will throw if not provided
40 * @property {Number} [fps=60] the fps target to maintain
41 * @property {Number} [verticalResolution=224] how many pixels to render in the vertical
42 * axis (gets scaled if the canvas is larger)
43 * @property {Array<Number>} [aspectRatio=[2.76, 1]] the aspect ratio experssed as
44 * an array of two numbers, where aspect ratio x:y is [x, y] (eg. [16, 9])
47 internals
.Sumo
= class Sumo
{
52 this.aspectRatio
= [2.76, 1];
53 this.verticalResolution
= 224;
55 Object
.assign(this, config
);
58 throw new Error(internals
.kNoElementError
);
61 this._engine
= new Serpentity();
63 this._previousTime
= 0;
64 this._looping
= false;
66 // Initialization functions
67 this._initializeCanvas();
68 this._initializeMatter();
69 this._initializePixi();
70 this._initializeSystems();
71 this._initializeEntities();
75 * Starts the main loop. Resets the FPS (if you change it it won't go
76 * live until after you stop and start the loop)
85 this._frameDuration
= 1000 / this.fps
;
86 window
.requestAnimationFrame(this._loop
.bind(this));
98 this._looping
= false;
101 // The main loop used above. Runs the serpentity update process and
102 // attempts to maintain FPS. The rest is handled by the engine.
106 if (!this._looping
) {
110 window
.requestAnimationFrame(this._loop
.bind(this));
112 const currentFrameDuration
= currentTime
- this._previousTime
;
114 if (currentFrameDuration
> this._frameDuration
) {
116 // We're sending the currentTime since it gives better results for
117 // this type of renderer, though usually we expect the delta
118 this._engine
.update(currentTime
);
119 this._previousTime
= currentTime
;
123 // Creates a canvas for rendering
125 _initializeCanvas() {
127 this._canvas
= document
.createElement('canvas');
128 this.element
.appendChild(this._canvas
);
129 this._resizeCanvas();
130 window
.addEventListener('resize', this._resizeCanvas
.bind(this));
133 // Initialize MatterJs
135 _initializeMatter() {
137 this._matterJs
= Engine
.create();
139 this._matterJs
.world
.gravity
.y
= 0;
146 this._pixi
= new Application({
147 backgroundColor: internals
.kBackgroundColor
,
149 width: this._canvas
.width
,
150 height: this._canvas
.height
154 // Resizes the canvas to a square the size of the smallest magnitude
159 let width
= window
.innerWidth
;
160 let height
= Math
.round(width
* this.aspectRatio
[1] / this.aspectRatio
[0]);
162 if (window
.innerHeight
< height
) {
163 height
= window
.innerHeight
;
164 width
= Math
.round(height
* this.aspectRatio
[0] / this.aspectRatio
[1]);
167 this._canvas
.style
.width
= `${width}px`;
168 this._canvas
.style
.height
= `${height}px`;
170 this._canvas
.width
= Math
.round(this.verticalResolution
* this.aspectRatio
[0] / this.aspectRatio
[1]);
171 this._canvas
.height
= this.verticalResolution
;
174 // Initializes the serpentity systems
176 _initializeSystems() {
178 this._engine
.addSystem(new PhysicsWorldControlSystem({
179 engine: this._matterJs
182 this._engine
.addSystem(new PhysicsToAttributesSystem());
184 this._engine
.addSystem(new AttributesToRenderableSystem());
186 this._engine
.addSystem(new CreateCouplingLineSystem());
188 this._engine
.addSystem(new RenderSystem({
189 application: this._pixi
193 // Initializes the serpentity entities
195 _initializeEntities() {
197 const entityA
= SumoFactory
.createSumo(null, {
204 const entityB
= SumoFactory
.createSumo(null, {
211 const entityC
= SumoFactory
.createSumo(null, {
218 SumoFactory
.createRubberBand(this._engine
, {
223 SumoFactory
.createRubberBand(this._engine
, {
228 // To keep the coupling behind, we'll manually add the sumos later
230 this._engine
.addEntity(entityA
);
231 this._engine
.addEntity(entityB
);
232 this._engine
.addEntity(entityC
);
236 export default internals
.exports
= {};
239 window
.addEventListener('load', internals
.onLoad
);