GameHandler.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. class GameHandler
  2. {
  3. static rootObjects = []
  4. static nextId = 0;
  5. static renderMode = null;
  6. static setRenderMode(mode)
  7. {
  8. this.renderMode = mode;
  9. }
  10. static bDrawDebugFPS = false;
  11. static drawDebugFPS(val)
  12. {
  13. this.bDrawDebugFPS = val;
  14. }
  15. static init(fps = 60)
  16. {
  17. if (!this.renderMode) this.renderMode = RENDER_MODES.P2D;
  18. switch (this.renderMode)
  19. {
  20. case RENDER_MODES.P2D:
  21. createCanvas(windowWidth, windowHeight);
  22. break;
  23. case RENDER_MODES.WEBGL:
  24. createCanvas(windowWidth, windowHeight, WEBGL);
  25. break;
  26. }
  27. frameRate(fps);
  28. smooth();
  29. }
  30. static instanceGameObject(obj)
  31. {
  32. obj.id = this.nextId;
  33. this.nextId++;
  34. }
  35. static addRootObject(obj)
  36. {
  37. this.rootObjects.push(obj);
  38. }
  39. static prevMillis = 0;
  40. static delta = 0;
  41. static update()
  42. {
  43. this.delta = (millis() - this.prevMillis) / 1000;
  44. for (let i = 0; i < this.rootObjects.length; i++)
  45. this.rootObjects[i].update(this.delta);
  46. }
  47. static draw()
  48. {
  49. if (this.renderMode == RENDER_MODES.WEBGL) translate(-windowWidth / 2, -windowHeight / 2);
  50. if (this.bDrawDebugFPS)
  51. {
  52. textSize(12);
  53. noStroke();
  54. fill(0);
  55. text("FPS: " + frameRate(), 10, 20);
  56. }
  57. for (let i = 0; i < this.rootObjects.length; i++)
  58. this.rootObjects[i].draw(this.delta);
  59. this.prevMillis = millis();
  60. }
  61. }