GameHandler.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 gui;
  16. static init(fps=60)
  17. {
  18. if (!this.renderMode) this.renderMode = RENDER_MODES.P2D;
  19. switch (this.renderMode)
  20. {
  21. case RENDER_MODES.P2D:
  22. createCanvas(windowWidth, windowHeight);
  23. break;
  24. case RENDER_MODES.WEBGL:
  25. createCanvas(windowWidth, windowHeight, WEBGL);
  26. break;
  27. }
  28. frameRate(fps);
  29. smooth();
  30. this.gui = createGui();
  31. }
  32. static instanceGameObject(obj)
  33. {
  34. obj.id = this.nextId;
  35. this.nextId++;
  36. }
  37. static addRootObject(obj)
  38. {
  39. this.rootObjects.push(obj);
  40. }
  41. static prevMillis = 0;
  42. static delta = 0;
  43. static update()
  44. {
  45. this.delta = (millis() - this.prevMillis) / 1000;
  46. for (let i = 0; i < this.rootObjects.length; i++)
  47. this.rootObjects[i].update(this.delta);
  48. }
  49. static draw()
  50. {
  51. if (this.renderMode == RENDER_MODES.WEBGL) translate(-windowWidth / 2, -windowHeight / 2);
  52. if (this.bDrawDebugFPS)
  53. {
  54. textSize(12);
  55. noStroke();
  56. fill(0);
  57. text("FPS: " + frameRate(), 10, 20);
  58. }
  59. drawGui();
  60. for (let i = 0; i < this.rootObjects.length; i++)
  61. this.rootObjects[i].draw(this.delta);
  62. this.prevMillis = millis();
  63. }
  64. }