Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import structures from "./structures.js";
  2. export default class Level {
  3. constructor(canvas) {
  4. this.canvas = canvas;
  5. this.ctx = canvas.getContext("2d");
  6. this.lastTime = null;
  7. this.raf = null;
  8. this.minPhysFPS = 20;
  9. this.minFPS = 5;
  10. this.backgroundColor = "#87CEFA";
  11. this.layerOrder = {
  12. platform: 0,
  13. player: 1,
  14. };
  15. this.layers = [];
  16. this.entities = [];
  17. this.colliders = [];
  18. this.structures = [];
  19. this.evts = [];
  20. }
  21. spawnEntity(ent, x, y) {
  22. ent.pos.set(x, y);
  23. let layerIdx = this.layerOrder[ent.layerName];
  24. if (layerIdx == null)
  25. throw new Error("Unknown layer name: "+ent.layerName);
  26. if (this.layers[layerIdx] == null)
  27. this.layers[layerIdx] = [];
  28. this.entities.push(ent);
  29. this.layers[layerIdx].push(ent);
  30. if (ent.has("collider"))
  31. this.colliders.push(ent);
  32. ent._init();
  33. }
  34. spawnStructure(structure, x, y) {
  35. structure.pos.set(x, y);
  36. this.structures.push(structure);
  37. structure.init();
  38. }
  39. physicsLayer(dt, layer) {
  40. layer.forEach(ent =>
  41. ent._update(dt));
  42. layer.forEach(ent =>
  43. ent._postUpdate(dt));
  44. }
  45. physics(dt) {
  46. // Collide with structures
  47. this.structures.forEach(s => {
  48. if (!s.attrs.wall && !s.attrs.platform)
  49. return;
  50. this.colliders.forEach(ent => {
  51. if (!s.bounds.intersects(ent.bounds))
  52. return;
  53. ent.t.collider.collideStructure(s);
  54. });
  55. });
  56. // Collide with entities
  57. this.colliders.forEach(ent => {
  58. this.colliders.forEach(ent2 => {
  59. if (ent === ent2 || !ent.bounds.intersects(ent2.bounds))
  60. return;
  61. ent.t.collider.collideEntity(ent2);
  62. });
  63. });
  64. this.layers.forEach(l => this.physicsLayer(dt, l));
  65. }
  66. draw() {
  67. this.ctx.resetTransform();
  68. this.ctx.beginPath();
  69. this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  70. this.layers.forEach(l => l.forEach(ent => ent.draw(this.ctx)));
  71. this.structures.forEach(struct => struct.draw(this.ctx));
  72. }
  73. update(time) {
  74. if (this.lastTime != null) {
  75. let dt = (time - this.lastTime) / 1000;
  76. if (1 / dt < this.minFPS) {
  77. console.log(
  78. "Too long between updates ("+dt.toFixed(2)+"s, "+
  79. (1 / dt).toFixed(2)+"FPS). Skipping.");
  80. } else {
  81. // We accept really low FPSes,
  82. // but if the FPS gets too low, we want to run multiple
  83. // physics steps in the frame.
  84. let physdt = dt;
  85. let nSteps = 1;
  86. while (1 / physdt < this.minPhysFPS) {
  87. physdt /= 2;
  88. nSteps *= 2;
  89. }
  90. if (nSteps > 1) {
  91. console.log(
  92. "Too long between frames for physics ("+dt.toFixed(2)+"s). "+
  93. "Running "+nSteps+" simulations ("+physdt.toFixed(2)+"s) in one frame.");
  94. }
  95. for (let i = 0; i < nSteps; ++i)
  96. this.physics(physdt);
  97. }
  98. this.draw();
  99. }
  100. this.lastTime = time;
  101. this.raf = requestAnimationFrame(this.update.bind(this));
  102. }
  103. start() {
  104. this.canvas.style.background = this.backgroundColor;
  105. if (this.raf == null) {
  106. this.lastTime = null;
  107. this.update();
  108. console.log("Started.");
  109. }
  110. }
  111. stop() {
  112. if (this.raf != null) {
  113. cancelAnimationFrame(this.raf);
  114. this.raf = null;
  115. console.log("Stopped.");
  116. }
  117. }
  118. }