You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. function randint(min, max) {
  2. return Math.floor(Math.random() * (max - min + 1)) + min;
  3. }
  4. function background(ctx, camera) {
  5. if (!background.cache) {
  6. let cache = [];
  7. for (let i = 0; i < window.innerWidth; ++i) {
  8. cache.push({
  9. x: randint(0, window.innerWidth * 5),
  10. y: randint(0, window.innerHeight * 5)
  11. });
  12. }
  13. background.cache = cache;
  14. }
  15. let cx = camera.x;
  16. let cy = camera.y;
  17. let cache = background.cache;
  18. let fs = ctx.fillStyle;
  19. ctx.fillStyle = "#FFFFFF";
  20. for (let i = 0, len = cache.length; i < len; ++i) {
  21. let p = cache[i];
  22. ctx.beginPath();
  23. let x = Math.floor(((p.x - cx) / 5) % window.innerWidth);
  24. let y = Math.floor(((p.y - cy) / 5) % window.innerHeight);
  25. ctx.arc(x, y, 1, 0, 2*Math.PI);
  26. ctx.closePath();
  27. ctx.fill();
  28. }
  29. ctx.fillStyle = fs;
  30. }
  31. class Entity {
  32. constructor(x, y, width, height, id) {
  33. this.pos = {x: x, y: y};
  34. this.vel = {x: 0, y: 0};
  35. this.width = width;
  36. this.height = height;
  37. this.id = id;
  38. }
  39. draw(ctx) {}
  40. set(obj) {
  41. this.pos = obj.pos;
  42. this.vel = obj.vel;
  43. }
  44. update(dt) {
  45. this.pos.x += this.vel.x * dt;
  46. this.pos.y += this.vel.y * dt;
  47. }
  48. }
  49. class Bullet extends Entity {
  50. constructor(x, y, vel, id, ownerId) {
  51. super(x, y, 5, 5, id);
  52. this.vel = vel;
  53. this.ownerId = ownerId;
  54. }
  55. draw(ctx, selfId) {
  56. if (selfId == this.ownerId) {
  57. ctx.fillStyle = "#FFFFFF";
  58. } else {
  59. ctx.fillStyle = "#FF0000";
  60. }
  61. ctx.beginPath();
  62. ctx.arc(-(this.width/2), -(this.height/2), this.width/2, 0, 2*Math.PI);
  63. ctx.closePath();
  64. ctx.fill();
  65. }
  66. set(obj) {
  67. super.set(obj);
  68. }
  69. }
  70. class Player extends Entity {
  71. constructor(x, y, id, rot) {
  72. super(x, y, 25, 60, id);
  73. this.rot = rot;
  74. this.rotVel = 0;
  75. }
  76. draw(ctx, selfId) {
  77. if (selfId == this.id) {
  78. ctx.fillStyle = "#FFFFFF";
  79. } else {
  80. ctx.fillStyle = "#FF0000";
  81. }
  82. ctx.rotate(this.rot);
  83. ctx.beginPath();
  84. ctx.moveTo(0, -(this.height/2));
  85. ctx.lineTo(-this.width, this.height/2);
  86. ctx.lineTo(this.width, this.height/2);
  87. ctx.closePath();
  88. ctx.fill();
  89. }
  90. set(obj) {
  91. super.set(obj);
  92. this.rot = obj.rot;
  93. this.rotVel = obj.rotVel;
  94. }
  95. update(dt) {
  96. super.update(dt);
  97. this.rot += this.rotVel * dt;
  98. }
  99. }
  100. function createEntity(obj) {
  101. if (obj.type == "player") {
  102. return new Player(obj.pos.x, obj.pos.y, obj.id, obj.rot);
  103. } else if (obj.type == "bullet") {
  104. return new Bullet(obj.pos.x, obj.pos.y, obj.vel, obj.id, obj.ownerId);
  105. } else {
  106. throw new Error("Unknown entity type: "+obj.type);
  107. }
  108. }
  109. export default class Game {
  110. constructor(sock, canvas) {
  111. this.sock = sock;
  112. this.canvas = canvas;
  113. this.ctx = canvas.getContext("2d");
  114. this.id = null;
  115. this.camera = {x: 0, y: 0};
  116. this.raf = null;
  117. this.prevTime = new Date().getTime();
  118. this.player = null;
  119. this.keymap = [];
  120. this.keymap[40] = "down";
  121. this.keymap[38] = "up";
  122. this.keymap[37] = "left";
  123. this.keymap[39] = "right";
  124. this.keymap[32] = "shoot";
  125. this.entities = [];
  126. sock.on("ready", () => {
  127. sock.send("get_id", {}, (err, res) => {
  128. this.id = res.id;
  129. });
  130. });
  131. sock.on("set", (msg) => {
  132. console.log(msg);
  133. if (!this.entities[msg.id])
  134. this.entities[msg.id] = createEntity(msg);
  135. else
  136. this.entities[msg.id].set(msg);
  137. });
  138. sock.on("despawn", (msg) => {
  139. delete this.entities[msg.id];
  140. });
  141. window.addEventListener("keydown", (evt) => {
  142. if (this.keymap[evt.keyCode]) {
  143. evt.preventDefault();
  144. this.sock.send("keydown", {
  145. key: this.keymap[evt.keyCode]
  146. });
  147. }
  148. });
  149. window.addEventListener("keyup", (evt) => {
  150. if (this.keymap[evt.keyCode]) {
  151. this.sock.send("keyup", {
  152. key: this.keymap[evt.keyCode]
  153. });
  154. }
  155. });
  156. this.update();
  157. }
  158. update() {
  159. let dt = new Date().getTime() - this.prevTime;
  160. this.prevTime = new Date().getTime();
  161. this.canvas.width = window.innerWidth;
  162. this.canvas.height = window.innerHeight;
  163. background(this.ctx, this.camera);
  164. let player = this.entities[this.id];
  165. if (player) {
  166. this.camera = {
  167. x: player.pos.x - (window.innerWidth / 2),
  168. y: player.pos.y - (window.innerHeight / 2)
  169. };
  170. }
  171. this.ctx.translate(-this.camera.x, -this.camera.y);
  172. this.entities.forEach((ent) => {
  173. this.ctx.save();
  174. this.ctx.translate(ent.pos.x, ent.pos.y);
  175. ent.draw(this.ctx, this.id);
  176. this.ctx.restore();
  177. ent.update(dt);
  178. });
  179. this.ctx.translate(this.camera.x, this.camera.y);
  180. this.raf = window.requestAnimationFrame(this.update.bind(this));
  181. }
  182. }