GameHandler.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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()
  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. smooth();
  28. }
  29. static instanceGameObject(obj)
  30. {
  31. obj.id = this.nextId;
  32. this.nextId++;
  33. }
  34. static addRootObject(obj)
  35. {
  36. this.rootObjects.push(obj);
  37. }
  38. static update()
  39. {
  40. for (let i = 0; i < this.rootObjects.length; i++)
  41. this.rootObjects[i].update(1 / frameRate());
  42. }
  43. static draw()
  44. {
  45. if (this.renderMode == RENDER_MODES.WEBGL) translate(-windowWidth / 2, -windowHeight / 2);
  46. if (this.bDrawDebugFPS)
  47. {
  48. textSize(12);
  49. noStroke();
  50. fill(0);
  51. text("FPS: " + frameRate(), 10, 20);
  52. }
  53. for (let i = 0; i < this.rootObjects.length; i++)
  54. this.rootObjects[i].draw(1 / frameRate());
  55. }
  56. }