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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. return (
  21. (inRange(a.x, b.x, b.x + b.width) || inRange(b.x, a.x, a.x + a.width)) &&
  22. (inRange(a.y, b.y, b.y + b.height) || inRange(b.y, a.y, a.y + a.height))
  23. );
  24. }
  25. clone() {
  26. return new Rectangle(this.x, this.y, this.width, this.height);
  27. }
  28. }
  29. class Entity {
  30. constructor(x, y, width, height, id, game) {
  31. this.width = width;
  32. this.height = height;
  33. this.mass = width * height;
  34. this.forceScalar = 1/this.mass;
  35. this.id = id;
  36. this.game = game;
  37. this.pos = new Vec2(x, y);
  38. this.vel = new Vec2(0, 0);
  39. this.vforce = new Vec2(0, 0);
  40. }
  41. get boundingRect() {
  42. if (this.oundingRectCache)
  43. return this.boundingRectCache;
  44. this.boundingRectCache = new Rectangle(this.pos.x, this.pos.y, this.width, this.height);
  45. return this.boundingRectCache;
  46. }
  47. intersectsPoint(e) {
  48. let rect = this.boundingRect;
  49. return (
  50. e.pos.x > rect.x && e.pos.x < rect.x + rect.width &&
  51. e.pos.y > rect.y && e.pos.y < rect.y + rect.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(), 2000);
  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(-300, 300), randint(-300, 300), 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. if (this.keys.sprint)
  128. f.set(0, -5);
  129. else
  130. f.set(0, -2);
  131. }
  132. if (this.keys.down)
  133. f.set(0, 2);
  134. if (this.keys.left)
  135. this.rotForce -= 0.03;
  136. if (this.keys.right)
  137. this.rotForce += 0.03;
  138. if (this.keys.shoot && this.canShoot) {
  139. let vel = new Vec2(0, -1).rotate(this.rot).add(this.vel);
  140. let posmod = new Vec2(0, -this.height/2).rotate(this.rot);
  141. let pos = this.pos.clone().add(posmod);
  142. let b = new Bullet(pos, vel, this.id, this.game.id, this.game);
  143. this.game.spawn(b);
  144. this.canShoot = false;
  145. setTimeout(() => this.canShoot = true, 100);
  146. }
  147. f.rotate(this.rot);
  148. this.force(f.x, f.y);
  149. this.vel.scale(0.9);
  150. this.rotVel *= 0.86;
  151. //Detect collissions
  152. this.game.entities.forEach((e) => {
  153. if (e instanceof Bullet) {
  154. if (e.ownerId !== this.id && this.intersectsPoint(e)) {
  155. this.health -= 10;
  156. e.despawn();
  157. if (this.health <= 0)
  158. this.despawn();
  159. }
  160. }
  161. });
  162. this.boundingRectCache = null;
  163. }
  164. get boundingRect() {
  165. if (this.boundingRectCache)
  166. return this.boundingRectCache;
  167. //0 1
  168. // *
  169. //3 2
  170. var rotated = [
  171. new Vec2(-this.width/2, -this.height/2),
  172. new Vec2(this.width/2, -this.height/2),
  173. new Vec2(this.width/2, this.height/2),
  174. new Vec2(-this.width/2, this.height/2)
  175. ].map((p) => p.rotate(this.rot));
  176. let tl = new Vec2(0, 0);
  177. let br = new Vec2(0, 0);
  178. rotated.forEach((p) => {
  179. if (p.x < tl.x)
  180. tl.x = p.x;
  181. if (p.y < tl.y)
  182. tl.y = p.y;
  183. if (p.x > br.x)
  184. br.x = p.x;
  185. if (p.y > br.y)
  186. br.y = p.y;
  187. });
  188. this.boundingRectCache = new Rectangle(
  189. this.pos.x + tl.x,
  190. this.pos.y + tl.y,
  191. br.x - tl.x,
  192. br.y - tl.y
  193. );
  194. return this.boundingRectCache;
  195. }
  196. move(dt) {
  197. super.move(dt);
  198. this.rotForce *= this.forceScalar * dt;
  199. this.rotVel += this.rotForce;
  200. this.rot = (this.rot + this.rotVel * dt) % (Math.PI * 2);
  201. this.rotForce = 0;
  202. }
  203. send(first) {
  204. let obj = {
  205. id: this.id,
  206. pos: this.pos,
  207. vel: this.vel,
  208. rot: this.rot,
  209. rotVel: this.rotVel,
  210. keys: this.keys,
  211. health: this.health
  212. }
  213. if (first)
  214. obj.type = "player";
  215. this.game.players.forEach((p) => p.sock.send("set", obj));
  216. }
  217. }
  218. export default class Game {
  219. constructor() {
  220. this.entities = [];
  221. this.players = [];
  222. this.updateTimeout = null;
  223. this.sendTimeout = null;
  224. this.prevTime = null;
  225. this.updateInterval = 1000/30;
  226. this.sendInterval = 1000/15;
  227. this.dt = 0;
  228. this.id = 1;
  229. }
  230. newPlayer(sock) {
  231. let p = new Player(sock, this.id, this);
  232. this.players[this.id] = p;
  233. this.spawn(p);
  234. }
  235. spawn(ent) {
  236. this.entities[this.id] = ent;
  237. this.id += 1;
  238. }
  239. start() {
  240. this.prevTime = new Date().getTime();
  241. this.update();
  242. this.send();
  243. }
  244. stop() {
  245. clearTimeout(this.updateTimeout);
  246. clearTimeout(this.sendTimeout);
  247. }
  248. update() {
  249. this.dt = new Date().getTime() - this.prevTime;
  250. this.prevTime = new Date().getTime();
  251. let dimx = 40000;
  252. let dimy = 40000;
  253. this.entities.forEach((e) => {
  254. e.move(this.dt);
  255. if (e.pos.x > dimx)
  256. e.pos.x = -dimx;
  257. else if (e.pos.x < -dimx)
  258. e.pos.x = dimx;
  259. if (e.pos.y > dimy)
  260. e.pos.y = -dimy;
  261. else if (e.pos.y < -dimy)
  262. e.pos.y = dimy;
  263. });
  264. this.entities.forEach((e) => e.update());
  265. this.updateTimeout = setTimeout(this.update.bind(this), this.updateInterval);
  266. }
  267. send() {
  268. this.entities.forEach((e) => e.send());
  269. this.sendTimeout = setTimeout(this.send.bind(this), this.sendInterval);
  270. }
  271. }