1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- class GameHandler
- {
- static rootObjects = []
- static nextId = 0;
- static renderMode = null;
- static setRenderMode(mode)
- {
- this.renderMode = mode;
- }
- static bDrawDebugFPS = false;
- static drawDebugFPS(val)
- {
- this.bDrawDebugFPS = val;
- }
- static gui;
- static init(fps=60)
- {
- if (!this.renderMode) this.renderMode = RENDER_MODES.P2D;
- switch (this.renderMode)
- {
- case RENDER_MODES.P2D:
- createCanvas(windowWidth, windowHeight);
- break;
- case RENDER_MODES.WEBGL:
- createCanvas(windowWidth, windowHeight, WEBGL);
- break;
- }
- frameRate(fps);
- smooth();
- this.gui = createGui();
- }
- static instanceGameObject(obj)
- {
- obj.id = this.nextId;
- this.nextId++;
- }
- static addRootObject(obj)
- {
- this.rootObjects.push(obj);
- }
- static prevMillis = 0;
- static delta = 0;
- static update()
- {
- this.delta = (millis() - this.prevMillis) / 1000;
- for (let i = 0; i < this.rootObjects.length; i++)
- this.rootObjects[i].update(this.delta);
- }
- static draw()
- {
- if (this.renderMode == RENDER_MODES.WEBGL) translate(-windowWidth / 2, -windowHeight / 2);
- if (this.bDrawDebugFPS)
- {
- textSize(12);
- noStroke();
- fill(0);
- text("FPS: " + frameRate(), 10, 20);
- }
- drawGui();
- for (let i = 0; i < this.rootObjects.length; i++)
- this.rootObjects[i].draw(this.delta);
- this.prevMillis = millis();
- }
- }
|