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

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