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

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