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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. let Vec2 = require("./vec2");
  2. function randint(min, max) {
  3. return Math.floor(Math.random() * (max - min + 1)) + min;
  4. }
  5. function random(min, max) {
  6. return Math.random() * (max - min) + min;
  7. }
  8. function inRange(n, min, max) {
  9. return n >= min && n <= max;
  10. }
  11. class Rectangle {
  12. constructor(x, y, width, height) {
  13. this.x = x;
  14. this.y = y;
  15. this.width = width;
  16. this.height = height;
  17. }
  18. intersects(b) {
  19. let a = this;
  20. //console.log("if ("+a.x+", "+a.y+") intersects ("+b.x+", "+b.y+")");
  21. return (
  22. (inRange(a.x, b.x, b.x + b.width) || inRange(b.x, a.x, a.x + a.width)) &&
  23. (inRange(a.y, b.y, b.y + b.height) || inRange(b.y, a.y, a.y + a.height))
  24. );
  25. }
  26. clone() {
  27. return new Rectangle(this.x, this.y, this.width, this.height);
  28. }
  29. }
  30. class Entity {
  31. constructor(x, y, width, height, id, game) {
  32. this.width = width;
  33. this.height = height;
  34. this.mass = width * height;
  35. this.forceScalar = 1/this.mass;
  36. this.id = id;
  37. this.game = game;
  38. this.pos = new Vec2(x, y);
  39. this.vel = new Vec2(0, 0);
  40. this.vforce = new Vec2(0, 0);
  41. }
  42. get boundingRect() {
  43. if (this.oundingRectCache)
  44. return this.boundingRectCache;
  45. this.boundingRectCache = new Rectangle(this.pos.x, this.pos.y, this.width, this.height);
  46. return this.boundingRectCache;
  47. }
  48. intersectsPoint(e) {
  49. return (
  50. e.pos.x > this.pos.x && e.pos.x < this.pos.x + this.width &&
  51. e.pos.y > this.pos.y && e.pos.y < this.pos.y + this.height
  52. );
  53. }
  54. move(dt) {
  55. this.vforce.scale(this.forceScalar * dt);
  56. this.vel.add(this.vforce);
  57. this.pos.add(this.vel.clone().scale(dt));
  58. this.vforce.set(0, 0);
  59. }
  60. force(x, y) {
  61. this.vforce.x += x;
  62. this.vforce.y += y;
  63. }
  64. impulse(x, y) {
  65. this.vel.x += x;
  66. this.vel.y += y;
  67. }
  68. init() {}
  69. update() {}
  70. send() {}
  71. despawn() {
  72. this.game.players.forEach((p) => p.sock.send("despawn", {
  73. id: this.id
  74. }));
  75. delete this.game.entities[this.id];
  76. delete this.game.players[this.id];
  77. }
  78. }
  79. class Bullet extends Entity {
  80. constructor(pos, vel, ownerId, id, game) {
  81. super(pos.x, pos.y, 5, 5, id, game);
  82. this.ownerId = ownerId;
  83. this.vel = vel;
  84. setTimeout(() => this.despawn(), 1000);
  85. this.send(true);
  86. }
  87. send(first) {
  88. if (!first)
  89. return;
  90. this.game.players.forEach((p) => p.sock.send("set", {
  91. type: "bullet",
  92. id: this.id,
  93. ownerId: this.ownerId,
  94. pos: this.pos,
  95. vel: this.vel,
  96. }));
  97. }
  98. }
  99. class Player extends Entity {
  100. constructor(sock, id, game) {
  101. super(randint(-50, 50), randint(-50, 50), 25, 60, id, game);
  102. this.sock = sock;
  103. this.keys = {};
  104. this.dead = false;
  105. this.rot = 0;
  106. this.rotForce = 0;
  107. this.rotVel = 0;
  108. this.canShoot = true;
  109. this.health = 100;
  110. sock.on("request", (req) => {
  111. if (req.url == "get_id") {
  112. req.reply({
  113. id: this.id
  114. });
  115. } else if (req.url == "keydown") {
  116. this.keys[req.data.key] = true;
  117. } else if (req.url == "keyup") {
  118. delete this.keys[req.data.key];
  119. }
  120. game.entities.forEach((e) => e.send(true));
  121. });
  122. sock.on("close", () => this.despawn());
  123. }
  124. update(dt) {
  125. let f = new Vec2(0, 0);
  126. if (this.keys.up)
  127. f.set(0, -0.4);
  128. if (this.keys.down)
  129. f.set(0, 0.4);
  130. if (this.keys.left)
  131. this.rotForce -= 0.005;
  132. if (this.keys.right)
  133. this.rotForce += 0.005;
  134. if (this.keys.shoot && this.canShoot) {
  135. let vel = new Vec2(0, -1).rotate(this.rot).add(this.vel);
  136. let posmod = new Vec2(0, -this.height/2).rotate(this.rot);
  137. let pos = this.pos.clone().add(posmod);
  138. let b = new Bullet(pos, vel, this.id, this.game.id, this.game);
  139. this.game.spawn(b);
  140. this.canShoot = false;
  141. setTimeout(() => this.canShoot = true, 50);
  142. }
  143. f.rotate(this.rot);
  144. this.force(f.x, f.y);
  145. //Detect collissions
  146. this.game.entities.forEach((e) => {
  147. if (e instanceof Bullet) {
  148. if (e.ownerId !== this.id && this.intersectsPoint(e)) {
  149. this.health -= 3;
  150. e.despawn();
  151. if (this.health <= 0)
  152. this.despawn();
  153. }
  154. }
  155. });
  156. this.boundingRectCache = null;
  157. }
  158. get boundingRect() {
  159. if (this.boundingRectCache)
  160. return this.boundingRectCache;
  161. //0 1
  162. // *
  163. //3 2
  164. var rotated = [
  165. new Vec2(-this.width/2, -this.height/2),
  166. new Vec2(this.width/2, -this.height/2),
  167. new Vec2(this.width/2, this.height/2),
  168. new Vec2(-this.width/2, this.height/2)
  169. ].map((p) => p.rotate(this.rot));
  170. let tl = new Vec2(0, 0);
  171. let br = new Vec2(0, 0);
  172. rotated.forEach((p) => {
  173. if (p.x < tl.x)
  174. tl.x = p.x;
  175. if (p.y < tl.y)
  176. tl.y = p.y;
  177. if (p.x > br.x)
  178. br.x = p.x;
  179. if (p.y > br.y)
  180. br.y = p.y;
  181. });
  182. this.boundingRectCache = new Rectangle(
  183. this.pos.x + tl.x,
  184. this.pos.y + tl.y,
  185. br.x - tl.x,
  186. br.y - tl.y
  187. );
  188. return this.boundingRectCache;
  189. }
  190. move(dt) {
  191. super.move(dt);
  192. this.rotForce *= this.forceScalar * dt;
  193. this.rotVel += this.rotForce;
  194. this.rot = (this.rot + this.rotVel * dt) % (Math.PI * 2);
  195. this.rotForce = 0;
  196. }
  197. send(first) {
  198. let obj = {
  199. id: this.id,
  200. pos: this.pos,
  201. vel: this.vel,
  202. rot: this.rot,
  203. rotVel: this.rotVel,
  204. keys: this.keys,
  205. health: this.health
  206. }
  207. if (first)
  208. obj.type = "player";
  209. this.game.players.forEach((p) => p.sock.send("set", obj));
  210. }
  211. }
  212. export default class Game {
  213. constructor() {
  214. this.entities = [];
  215. this.players = [];
  216. this.updateTimeout = null;
  217. this.sendTimeout = null;
  218. this.prevTime = null;
  219. this.updateInterval = 1000/60;
  220. this.sendInterval = 1000/15;
  221. this.dt = 0;
  222. this.id = 1;
  223. }
  224. newPlayer(sock) {
  225. let p = new Player(sock, this.id, this);
  226. this.players[this.id] = p;
  227. this.spawn(p);
  228. }
  229. spawn(ent) {
  230. this.entities[this.id] = ent;
  231. this.id += 1;
  232. }
  233. start() {
  234. this.prevTime = new Date().getTime();
  235. this.update();
  236. this.send();
  237. }
  238. stop() {
  239. clearTimeout(this.updateTimeout);
  240. clearTimeout(this.sendTimeout);
  241. }
  242. update() {
  243. this.dt = new Date().getTime() - this.prevTime;
  244. this.prevTime = new Date().getTime();
  245. this.entities.forEach((e) => e.move(this.dt));
  246. this.entities.forEach((e) => e.update());
  247. this.updateTimeout = setTimeout(this.update.bind(this), this.updateInterval);
  248. }
  249. send() {
  250. this.entities.forEach((e) => e.send());
  251. this.sendTimeout = setTimeout(this.send.bind(this), this.sendInterval);
  252. }
  253. }