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.

index.js 6.5KB

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