]>
git.r.bdr.sh - rbdr/sumo/blob - lib/sumo.js
81f948f74848695144ca298d546e6a6b66714fa5
1 import RenderSystem
from './systems/render';
2 import SumoFactory
from './factories/sumo';
3 import Serpentity
from '@serpentity/serpentity';
4 import { Application
} from 'pixi.js';
6 /* global window document */
9 kBackgroundColor: 0xd8c590,
10 kNoElementError: 'No element found. Cannot render.',
12 // Handler for the window load event. Initializes and runs the app.
16 const sumo
= new internals
.Sumo({
17 element: document
.getElementById('sumo-app-entry-point')
22 internals
.exports
.sumo
= sumo
;
27 * Sumo - main entry point. Attached to window->load
31 * @param {object} config the configuration to extend the object
33 * @property {HTMLElement} [element=null] the element in which to render.
34 * Required, will throw if not provided
35 * @property {Number} [fps=60] the fps target to maintain
36 * @property {Number} [verticalResolution=224] how many pixels to render in the vertical
37 * axis (gets scaled if the canvas is larger)
38 * @property {Array<Number>} [aspectRatio=[2.76, 1]] the aspect ratio experssed as
39 * an array of two numbers, where aspect ratio x:y is [x, y] (eg. [16, 9])
42 internals
.Sumo
= class Sumo
{
47 this.aspectRatio
= [2.76, 1];
48 this.verticalResolution
= 224;
50 Object
.assign(this, config
);
53 throw new Error(internals
.kNoElementError
);
56 this._engine
= new Serpentity();
58 this._previousTime
= 0;
59 this._looping
= false;
61 // Initialization functions
62 this._initializeCanvas();
63 this._initializePixi();
64 this._initializeSystems();
65 this._initializeEntities();
69 * Starts the main loop. Resets the FPS (if you change it it won't go
70 * live until after you stop and start the loop)
79 this._frameDuration
= 1000 / this.fps
;
80 window
.requestAnimationFrame(this._loop
.bind(this));
92 this._looping
= false;
95 // The main loop used above. Runs the serpentity update process and
96 // attempts to maintain FPS. The rest is handled by the engine.
100 if (!this._looping
) {
104 window
.requestAnimationFrame(this._loop
.bind(this));
106 const currentFrameDuration
= currentTime
- this._previousTime
;
108 if (currentFrameDuration
> this._frameDuration
) {
110 // We're sending the currentTime since it gives better results for
111 // this type of renderer, though usually we expect the delta
112 this._engine
.update(currentTime
);
113 this._previousTime
= currentTime
;
117 // Creates a canvas for rendering
119 _initializeCanvas() {
121 this._canvas
= document
.createElement('canvas');
122 this.element
.appendChild(this._canvas
);
123 this._resizeCanvas();
124 window
.addEventListener('resize', this._resizeCanvas
.bind(this));
131 this._pixi
= new Application({
132 backgroundColor: internals
.kBackgroundColor
,
134 width: this._canvas
.width
,
135 height: this._canvas
.height
139 // Resizes the canvas to a square the size of the smallest magnitude
144 let width
= window
.innerWidth
;
145 let height
= Math
.round(width
* this.aspectRatio
[1] / this.aspectRatio
[0]);
147 if (window
.innerHeight
< height
) {
148 height
= window
.innerHeight
;
149 width
= Math
.round(height
* this.aspectRatio
[0] / this.aspectRatio
[1]);
152 this._canvas
.style
.width
= `${width}px`;
153 this._canvas
.style
.height
= `${height}px`;
155 this._canvas
.width
= Math
.round(this.verticalResolution
* this.aspectRatio
[0] / this.aspectRatio
[1]);
156 this._canvas
.height
= this.verticalResolution
;
159 // Initializes the serpentity systems
161 _initializeSystems() {
163 this._engine
.addSystem(new RenderSystem({
164 application: this._pixi
168 // Initializes the serpentity entities
170 _initializeEntities() {
172 SumoFactory
.createSumo(this._engine
, {
179 SumoFactory
.createSumo(this._engine
, {
188 export default internals
.exports
= {};
191 window
.addEventListener('load', internals
.onLoad
);