GameHandler.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /************************************************************************
  2. * GameHandler.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. const GameHandler = {
  22. nextId: 0,
  23. rootObjects: [],
  24. renderMode: null,
  25. bDrawDebugFPS: false,
  26. debugFpsLabel: null,
  27. prevMillis: 0,
  28. delta: 0,
  29. db: null,
  30. dbWidth: 1920,
  31. dbHeight: 1080,
  32. setRenderMode: function(mode)
  33. {
  34. this.renderMode = mode;
  35. },
  36. setDoubleBufferSize: function(w, h)
  37. {
  38. this.dbWidth = w;
  39. this.dbHeight = h;
  40. },
  41. pixelDen: 1,
  42. setPixelDensity: function(val)
  43. {
  44. this.pixelDen = val;
  45. },
  46. pixelDenMobile: 2,
  47. setPixelDensityMobile: function(val)
  48. {
  49. this.pixelDenMobile = val;
  50. },
  51. drawDebugFPS(val)
  52. {
  53. this.bDrawDebugFPS = val;
  54. },
  55. isMobile: null,
  56. init: function(fps = 60)
  57. {
  58. this.isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  59. if (!this.renderMode) this.renderMode = RENDER_MODES.P2D;
  60. switch (this.renderMode)
  61. {
  62. case RENDER_MODES.P2D:
  63. createCanvas(windowWidth, windowHeight);
  64. this.db = createGraphics(this.dbWidth, this.dbHeight);
  65. break;
  66. case RENDER_MODES.WEBGL:
  67. createCanvas(windowWidth, windowHeight, WEBGL);
  68. this.db = createGraphics(this.dbWidth, this.dbHeight, WEBGL);
  69. this.db.smooth();
  70. break;
  71. }
  72. frameRate(fps);
  73. if (this.isMobile)
  74. pixelDensity(this.pixelDenMobile);
  75. else
  76. pixelDensity(this.pixelDen);
  77. smooth();
  78. if (this.renderMode == RENDER_MODES.WEBGL)
  79. {
  80. translate(-windowWidth / 2, -windowHeight / 2);
  81. db.translate(-this.dbWidth / 2, -this.dbHeight / 2);
  82. }
  83. if (this.bDrawDebugFPS)
  84. {
  85. this.debugFpsLabel = new Label("debugFps", `FPS: ${frameRate()}`);
  86. this.addRootObject(this.debugFpsLabel);
  87. }
  88. },
  89. instanceGameObject: function(obj)
  90. {
  91. obj.id = this.nextId;
  92. this.nextId++;
  93. },
  94. addRootObject: function(obj)
  95. {
  96. this.rootObjects.push(obj);
  97. obj.isRoot = true;
  98. obj.setup();
  99. },
  100. removeRootObjectById: function(id)
  101. {
  102. for (let i = 0; i < this.rootObjects.length; i++)
  103. {
  104. if (this.rootObjects[i].id == id)
  105. this.rootObjects.splice(i, 1);
  106. }
  107. },
  108. upframecount: 0,
  109. upframenum: 20,
  110. update: function()
  111. {
  112. if (this.bDrawDebugFPS)
  113. {
  114. if (frameCount % this.upframenum == 0)
  115. {
  116. this.debugFpsLabel.setText(`FPS: ${
  117. Math.round(this.upframecount * 1000) / 1000
  118. }`);
  119. this.upframecount = 0;
  120. }
  121. else
  122. this.upframecount = max(this.upframecount, frameRate());
  123. }
  124. this.delta = (millis() - this.prevMillis) / 1000;
  125. for (let i = 0; i < this.rootObjects.length; i++)
  126. this.rootObjects[i].update(this.delta);
  127. },
  128. draw: function()
  129. {
  130. this.db.clear();
  131. for (let i = 0; i < this.rootObjects.length; i++)
  132. this.rootObjects[i].draw(this.delta, this.db);
  133. this.db.push();
  134. this.db.strokeWeight(5);
  135. this.db.noFill();
  136. this.db.rect(0, 0, this.dbWidth, this.dbHeight);
  137. this.db.pop();
  138. imageMode(CENTER);
  139. if (windowWidth / windowHeight < this.dbWidth / this.dbHeight)
  140. {
  141. this.db.screenWidth = windowWidth;
  142. this.db.screenHeight = windowWidth * (this.dbHeight / this.dbWidth);
  143. }
  144. else
  145. {
  146. this.db.screenHeight = windowHeight;
  147. this.db.screenWidth = windowHeight * (this.dbWidth / this.dbHeight);
  148. }
  149. image(this.db, windowWidth / 2, windowHeight / 2, this.db.screenWidth, this.db.screenHeight);
  150. this.prevMillis = millis();
  151. }
  152. }
  153. function windowResized()
  154. {
  155. resizeCanvas(windowWidth, windowHeight);
  156. }