Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

game.js 6.1KB

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