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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. this.name = "";
  117. sock.on("request", (req) => {
  118. if (req.url == "get_id") {
  119. req.reply({
  120. id: this.id
  121. });
  122. this.name = req.data.name;
  123. setTimeout(() => {
  124. game.entities.forEach((e) => e.send(true));
  125. }, 100);
  126. } else if (req.url == "keydown") {
  127. this.keys[req.data.key] = true;
  128. } else if (req.url == "keyup") {
  129. delete this.keys[req.data.key];
  130. }
  131. });
  132. sock.on("close", () => this.despawn());
  133. this.healthInterval = setInterval(() => {
  134. if (this.health < 100)
  135. this.health += 1;
  136. }, 200);
  137. }
  138. despawn() {
  139. super.despawn();
  140. clearInterval(this.healthInterval);
  141. }
  142. update(dt) {
  143. let f = new Vec2(0, 0);
  144. if (this.keys.up) {
  145. if (this.keys.sprint)
  146. f.set(0, -5);
  147. else
  148. f.set(0, -2);
  149. }
  150. if (this.keys.down)
  151. f.set(0, 2);
  152. if (this.keys.left)
  153. this.rotForce -= 0.03;
  154. if (this.keys.right)
  155. this.rotForce += 0.03;
  156. //Shoot
  157. if (this.keys.shoot && this.canShoot) {
  158. let vel = new Vec2(0, -1).rotate(this.rot).add(this.vel);
  159. let posmod = new Vec2(0, -this.height/2).rotate(this.rot);
  160. let pos = this.pos.clone().add(posmod);
  161. let b = new Bullet(pos, vel, this.id, this.game.id, this.game);
  162. this.game.spawn(b);
  163. this.canShoot = false;
  164. setTimeout(() => this.canShoot = true, 100);
  165. }
  166. f.rotate(this.rot);
  167. this.force(f.x, f.y);
  168. this.vel.scale(0.9);
  169. this.rotVel *= 0.8;
  170. //Detect collissions
  171. this.game.entities.forEach((e) => {
  172. if (e instanceof Bullet) {
  173. if (e.ownerId !== this.id && this.intersectsPoint(e)) {
  174. this.health -= 20;
  175. e.despawn();
  176. if (this.health <= 0)
  177. this.despawn();
  178. }
  179. }
  180. });
  181. this.boundingRectCache = null;
  182. }
  183. get boundingRect() {
  184. if (this.boundingRectCache)
  185. return this.boundingRectCache;
  186. //0 1
  187. // *
  188. //3 2
  189. var rotated = [
  190. new Vec2(-this.width/2, -this.height/2),
  191. new Vec2(this.width/2, -this.height/2),
  192. new Vec2(this.width/2, this.height/2),
  193. new Vec2(-this.width/2, this.height/2)
  194. ].map((p) => p.rotate(this.rot));
  195. let tl = new Vec2(0, 0);
  196. let br = new Vec2(0, 0);
  197. rotated.forEach((p) => {
  198. if (p.x < tl.x)
  199. tl.x = p.x;
  200. if (p.y < tl.y)
  201. tl.y = p.y;
  202. if (p.x > br.x)
  203. br.x = p.x;
  204. if (p.y > br.y)
  205. br.y = p.y;
  206. });
  207. this.boundingRectCache = new Rectangle(
  208. this.pos.x + tl.x,
  209. this.pos.y + tl.y,
  210. br.x - tl.x,
  211. br.y - tl.y
  212. );
  213. return this.boundingRectCache;
  214. }
  215. move(dt) {
  216. super.move(dt);
  217. this.rotForce *= this.forceScalar * dt;
  218. this.rotVel += this.rotForce;
  219. this.rot = (this.rot + this.rotVel * dt) % (Math.PI * 2);
  220. this.rotForce = 0;
  221. }
  222. sendSet(obj) {
  223. this.sendSetQueue.push(obj);
  224. }
  225. send(first) {
  226. let obj = {
  227. id: this.id,
  228. pos: {x: round(this.pos.x), y: round(this.pos.y)},
  229. vel: {x: round(this.vel.x), y: round(this.vel.y)},
  230. rot: round(this.rot),
  231. rotVel: round(this.rotVel),
  232. keys: this.keys,
  233. health: this.health
  234. }
  235. if (first) {
  236. obj.type = "player";
  237. obj.name = this.name;
  238. }
  239. this.game.players.forEach((p) => p.sendSet(obj));
  240. }
  241. }
  242. export default class Game {
  243. constructor() {
  244. this.entities = [];
  245. this.players = [];
  246. this.updateTimeout = null;
  247. this.sendTimeout = null;
  248. this.prevTime = null;
  249. this.updateInterval = 1000/30;
  250. this.sendInterval = 1000/20;
  251. this.dt = 0;
  252. this.id = 1;
  253. }
  254. newPlayer(sock) {
  255. let p = new Player(sock, this.id, this);
  256. this.players[this.id] = p;
  257. this.spawn(p);
  258. }
  259. spawn(ent) {
  260. this.entities[this.id] = ent;
  261. this.id += 1;
  262. }
  263. start() {
  264. this.prevTime = new Date().getTime();
  265. this.update();
  266. this.send();
  267. }
  268. stop() {
  269. clearTimeout(this.updateTimeout);
  270. clearTimeout(this.sendTimeout);
  271. }
  272. update() {
  273. this.dt = new Date().getTime() - this.prevTime;
  274. this.prevTime = new Date().getTime();
  275. let dimx = 40000;
  276. let dimy = 40000;
  277. this.entities.forEach((e) => {
  278. e.move(this.dt);
  279. if (e.pos.x > dimx)
  280. e.pos.x = -dimx;
  281. else if (e.pos.x < -dimx)
  282. e.pos.x = dimx;
  283. if (e.pos.y > dimy)
  284. e.pos.y = -dimy;
  285. else if (e.pos.y < -dimy)
  286. e.pos.y = dimy;
  287. });
  288. this.entities.forEach((e) => e.update());
  289. this.updateTimeout = setTimeout(this.update.bind(this), this.updateInterval);
  290. }
  291. send() {
  292. this.entities.forEach((e) => e.send());
  293. this.players.forEach((p) => {
  294. p.sock.send("set", p.sendSetQueue);
  295. p.sendSetQueue = [];
  296. });
  297. this.sendTimeout = setTimeout(this.send.bind(this), this.sendInterval);
  298. }
  299. }