Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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