Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.55 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import Player from './player';
  4. import Ball from './ball';
  5. import PlayablePlayer from './playablePlayer';
  6.  
  7. class point {
  8. public x: number;
  9. public y: number;
  10. constructor(x: number, y: number) {
  11. this.x = x;
  12. this.y = y;
  13. }
  14.  
  15. }
  16.  
  17. class line {
  18. public beg: point;
  19. public end: point;
  20. constructor(beg: point, end: point) {
  21. this.beg = beg;
  22. this.end = end;
  23. }
  24. }
  25.  
  26. function vMult(ax: number, ay: number, bx: number, by: number): number {
  27. return ax * by - bx * ay;
  28. }
  29.  
  30. function intersection(l1: line, l2: line): [boolean, number, number] {
  31. let intersect = true;
  32. let x = 0;
  33. let y = 0;
  34. let v1 = vMult(l2.end.x - l2.beg.x, l2.end.y - l2.beg.y, l1.beg.x - l2.beg.x, l1.beg.y - l2.beg.y);
  35. let v2 = vMult(l2.end.x - l2.beg.x, l2.end.y - l2.beg.y, l1.end.x - l2.beg.x, l1.end.y - l2.beg.y);
  36. let v3 = vMult(l1.end.x - l1.beg.x, l1.end.y - l1.beg.y, l2.beg.x - l1.beg.x, l2.beg.y - l1.beg.y);
  37. let v4 = vMult(l1.end.x - l1.beg.x, l1.end.y - l1.beg.y, l2.end.x - l1.beg.x, l2.end.y - l1.beg.y);
  38.  
  39. intersect = ((v1 * v2) < 0 && (v3 * v4) < 0);
  40. console.log("INTERSECT: ", (v1 * v2) < 0 && (v3 * v4) < 0)
  41. console.log(v1, v2, v3, v4)
  42. console.log(l1)
  43. console.log(l2)
  44. if (intersect) {
  45. console.log("IT FUCKING INTERSECTS")
  46. let A1 = l1.end.y - l1.beg.y;
  47. let B1 = l1.beg.x - l1.end.x;
  48. let C1 = -l1.beg.x * (l1.end.y - l1.beg.y) + l1.beg.y * (l1.end.x - l1.beg.x);
  49.  
  50. let A2 = l2.end.y - l2.beg.y;
  51. let B2 = l2.beg.x - l2.end.x;
  52. let C2 = -l2.beg.x * (l2.end.y - l2.beg.y) + l2.beg.y * (l2.end.x - l2.beg.x);
  53.  
  54. let d = (A1 * B2 - B1 * A2);
  55. let dx = (-C1 * B2 + B1 * C2);
  56. let dy = (-A1 * C2 + C1 * A2);
  57. x = (dx / d);
  58. y = (dy / d);
  59. return [intersect, x, y];
  60. }
  61. return [intersect, x, y];
  62. }
  63.  
  64. const epsilonMove = 0.01;
  65.  
  66. enum side {
  67. none,
  68. up,
  69. down,
  70. right,
  71. left,
  72. }
  73.  
  74. function movePlayer(player: Player, up: number, down: number, left: number, right: number) {
  75. player.x += player.vX
  76. player.y += player.vY
  77.  
  78. // controls player not to cross bounds
  79. // on x && width
  80. if (player.x - player.width / 2 < left) {
  81. player.x = left + player.width / 2
  82. }
  83. if (player.x + player.width / 2 > right) {
  84. player.x = right - player.width / 2
  85. }
  86. // on y && height
  87. if (player.y - player.height / 2 < down) {
  88. player.y = down + player.height / 2
  89. }
  90. if (player.y + player.height / 2 > up) {
  91. player.y = up - player.height / 2
  92. }
  93. }
  94.  
  95. function movePlayerWithBall(player: Player, ball: Ball, up: number, down: number, left: number, right: number, collSide: side, collPX: number, collPY: number) {
  96. player.x += player.vX
  97. player.y += player.vY
  98. ball.x += player.vX
  99. ball.y += player.vY
  100.  
  101. // controls player not to cross bounds
  102. // on x && width
  103. if (player.x - player.width / 2 < left) {
  104. player.x = left + player.width / 2
  105. }
  106. if (player.x + player.width / 2 > right) {
  107. player.x = right - player.width / 2
  108. }
  109. // on y && height
  110. if (player.y - player.height / 2 < down) {
  111. player.y = down + player.height / 2
  112. }
  113. if (player.y + player.height / 2 > up) {
  114. player.y = up - player.height / 2
  115. }
  116.  
  117. if (up < ball.y + ball.diameter / 2 && collSide == side.up) {
  118. ball.y = up - ball.diameter / 2 - epsilonMove
  119. player.y -= ball.y - player.height / 2
  120.  
  121. let fullBallV = Math.sqrt(ball.x * ball.x + ball.y + ball.y)
  122. ball.vY = 0
  123. if (ball.vX < 0) {
  124. ball.vX = -fullBallV
  125. } else {
  126. ball.vX = fullBallV
  127. }
  128. }
  129. if (down > ball.y - ball.diameter / 2 && collSide == side.down) {
  130. ball.y = down + ball.diameter / 2 + epsilonMove
  131. player.y -= ball.y + player.height / 2
  132.  
  133. let fullBallV = Math.sqrt(ball.x * ball.x + ball.y + ball.y)
  134. ball.vY = 0
  135. if (ball.vX < 0) {
  136. ball.vX = -fullBallV
  137. } else {
  138. ball.vX = fullBallV
  139. }
  140. }
  141.  
  142. if (Math.abs(player.vX) < Math.abs(ball.vX)) {
  143. ball.x += ball.vX - player.vX
  144. }
  145. if (Math.abs(player.vY) < Math.abs(ball.vY)) {
  146. ball.y += ball.vY - player.vY
  147. }
  148. }
  149.  
  150. enum winner {
  151. noWinner,
  152. p1Win,
  153. p2Win,
  154. }
  155.  
  156. function moveBall(ball: Ball) {
  157. ball.x += ball.vX
  158. ball.y += ball.vY
  159. }
  160.  
  161. function fixBallPos(ball: Ball, height: number) {
  162. if (ball.y - ball.diameter < 0) {
  163. ball.y = ball.diameter
  164. ball.vY = -ball.vY
  165. }
  166. if (ball.y + ball.diameter > height) {
  167. ball.y = height - ball.diameter
  168. ball.vY = -ball.vY
  169. }
  170. }
  171.  
  172. function winnerCheck(ball: Ball, width: number): number {
  173. if (ball.x - ball.diameter < 0) {
  174. return winner.p2Win
  175. }
  176. if (ball.x + ball.diameter > width) {
  177. return winner.p1Win
  178. }
  179. return winner.noWinner
  180. }
  181.  
  182. class Game {
  183. public player1: Player;
  184. public player2: Player;
  185. public ball: Ball;
  186. public fieldHeight: number;
  187. public fieldWidth: number;
  188.  
  189. constructor(fieldHeight: number, fieldWidth: number) {
  190. this.fieldHeight = fieldHeight;
  191. this.fieldWidth = fieldWidth;
  192.  
  193. this.player1 = new Player(
  194. fieldWidth / 10,
  195. fieldHeight / 2,
  196. fieldHeight / 5,
  197. fieldWidth / 20,
  198. );
  199.  
  200. this.player2 = new Player(
  201. fieldWidth - fieldWidth / 10,
  202. fieldHeight / 2,
  203. fieldHeight / 5,
  204. fieldWidth / 20,
  205. );
  206. this.ball = new Ball(10, fieldWidth / 2, fieldHeight / 2, 2, 0);
  207. }
  208.  
  209. public getInfo(): any {
  210. return {
  211. ratio: this.fieldWidth / this.fieldHeight,
  212. racket: {
  213. w: this.player1.width / this.fieldWidth,
  214. h: this.player1.height / this.fieldHeight,
  215. },
  216. ball: {
  217. diameter: this.ball.diameter / this.fieldHeight,
  218. },
  219. };
  220. }
  221.  
  222. public collidePlayerBall(player: Player, ball: Ball): [boolean, side, number, number] {
  223. let isColliding = false;
  224. let collisionSide = side.none;
  225. let collisionPointX = 0;
  226. let collisionPointY = 0;
  227. // translate to player's fixed system
  228. ball.vX -= player.vX;
  229. ball.vY -= player.vY;
  230.  
  231.  
  232.  
  233. let pRight = player.x + player.width / 2;
  234. let pLeft = player.x - player.width / 2;
  235. let pUp = player.y + player.height / 2;
  236. let pDown = player.y - player.height / 2;
  237.  
  238. let bRight = ball.x + ball.diameter / 2;
  239. let bLeft = ball.x - ball.diameter / 2;
  240. let bUp = ball.y + ball.diameter / 2;
  241. let bDown = ball.y - ball.diameter / 2;
  242.  
  243. let pLeftLine = new line(new point(pLeft, pUp), new point(pLeft, pDown))
  244. let pRightLine = new line(new point(pRight, pUp), new point(pRight, pDown))
  245. let pUpLine = new line(new point(pLeft, pUp), new point(pRight, pUp))
  246. let pDownLine = new line(new point(pLeft, pDown), new point(pRight, pDown))
  247.  
  248. let bRightDownLine = new line(new point(bRight, bDown), new point(bRight + ball.vX, bDown + ball.vY))
  249. let bLeftDownLine = new line(new point(bLeft, bDown), new point(bLeft + ball.vX, bDown + ball.vY))
  250. let bRightUpLine = new line(new point(bRight, bUp), new point(bRight + ball.vX, bUp + ball.vY))
  251. let bLeftUpLine = new line(new point(bLeft, bUp), new point(bLeft + ball.vX, bUp + ball.vY))
  252.  
  253. // collision detection
  254. if (player == this.player2) {
  255. console.log("collision test", bRight, pLeft, bRight <= pLeft)
  256. }
  257. if (bRight <= pLeft) {
  258. console.log(bRight <= pLeft, "1");
  259. [isColliding, collisionPointX, collisionPointY] = intersection(pLeftLine, bRightDownLine);
  260. if (isColliding) {
  261. collisionPointX -= ball.diameter / 2
  262. collisionPointY += ball.diameter / 2
  263.  
  264. collisionSide = side.left
  265.  
  266. }
  267. [isColliding, collisionPointX, collisionPointY] = intersection(pLeftLine, bRightUpLine);
  268. if (isColliding) {
  269. collisionPointX -= ball.diameter / 2
  270. collisionPointY -= ball.diameter / 2
  271.  
  272. collisionSide = side.left
  273.  
  274. }
  275. }
  276. if (pRight <= bLeft) {
  277. if (player == this.player1)
  278. console.log(pRight <= bLeft, "2");
  279. [isColliding, collisionPointX, collisionPointY] = intersection(pRightLine, bLeftDownLine);
  280. if (isColliding) {
  281. collisionPointX += ball.diameter / 2
  282. collisionPointY += ball.diameter / 2
  283.  
  284. collisionSide = side.right
  285.  
  286. }
  287. [isColliding, collisionPointX, collisionPointY] = intersection(pRightLine, bLeftUpLine);
  288. if (isColliding) {
  289. collisionPointX += ball.diameter / 2
  290. collisionPointY -= ball.diameter / 2
  291.  
  292. collisionSide = side.right
  293.  
  294. }
  295. }
  296.  
  297. if (pUp <= bDown) {
  298. console.log(pUp <= bDown, "3");
  299. [isColliding, collisionPointX, collisionPointY] = intersection(pUpLine, bLeftDownLine);
  300. if (isColliding) {
  301. collisionPointX += ball.diameter / 2
  302. collisionPointY += ball.diameter / 2
  303.  
  304. collisionSide = side.up
  305.  
  306. }
  307. [isColliding, collisionPointX, collisionPointY] = intersection(pUpLine, bRightDownLine);
  308. if (isColliding) {
  309. collisionPointX -= ball.diameter / 2
  310. collisionPointY += ball.diameter / 2
  311.  
  312. collisionSide = side.up
  313.  
  314. }
  315. }
  316.  
  317. if (bUp <= pDown) {
  318. console.log(bUp <= pDown, "4");
  319. [isColliding, collisionPointX, collisionPointY] = intersection(pDownLine, bLeftUpLine);
  320. if (isColliding) {
  321. collisionPointX += ball.diameter / 2
  322. collisionPointY -= ball.diameter / 2
  323.  
  324. collisionSide = side.down
  325.  
  326. }
  327. [isColliding, collisionPointX, collisionPointY] = intersection(pDownLine, bRightUpLine);
  328. if (isColliding) {
  329. collisionPointX -= ball.diameter / 2
  330. collisionPointY -= ball.diameter / 2
  331.  
  332. collisionSide = side.down
  333.  
  334. }
  335. }
  336.  
  337. ball.vX += player.vX
  338. ball.vY += player.vY
  339. if (isColliding) {
  340. ball.x = collisionPointX
  341. ball.y = collisionPointY
  342. if (collisionSide == side.right && ball.vX < 0) {
  343. ball.vX = -ball.vX
  344. ball.x += epsilonMove
  345. return [isColliding, collisionSide, collisionPointX, collisionPointY]
  346. }
  347. if (collisionSide == side.left && ball.vX > 0) {
  348. ball.vX = -ball.vX
  349. ball.x -= epsilonMove
  350. return [isColliding, collisionSide, collisionPointX, collisionPointY]
  351. }
  352. if (collisionSide == side.up && ball.vY < 0) {
  353. ball.vY = -ball.vY
  354. ball.y += epsilonMove
  355. return [isColliding, collisionSide, collisionPointX, collisionPointY]
  356. }
  357. if (collisionSide == side.down && ball.vY > 0) {
  358. ball.vY = -ball.vY
  359. ball.y -= epsilonMove
  360. return [isColliding, collisionSide, collisionPointX, collisionPointY]
  361. }
  362. }
  363.  
  364. return [isColliding, collisionSide, collisionPointX, collisionPointY]
  365. }
  366.  
  367. public getState(): any {
  368. return {
  369. player_1: {
  370. x: this.player1.x / this.fieldWidth,
  371. y: this.player1.y / this.fieldHeight,
  372. },
  373. player_2: {
  374. x: this.player2.x / this.fieldWidth,
  375. y: this.player2.y / this.fieldHeight,
  376. },
  377. ball: {
  378. x: this.ball.x / this.fieldWidth,
  379. y: this.ball.y / this.fieldHeight,
  380. },
  381. };
  382. }
  383.  
  384. public isDone(): number {
  385. if (this.ball.x - this.ball.diameter / 2 <= 0)
  386. return 2;
  387. if (this.ball.x + this.ball.diameter / 2 >= this.fieldWidth)
  388. return 1;
  389. return 0;
  390. }
  391.  
  392. public getObjectsP1(): [PlayablePlayer, Player, Ball] {
  393. return [new PlayablePlayer(this.player1), Object.assign({}, this.player2), Object.assign({}, this.ball)];
  394. }
  395.  
  396. public getObjectsP2(): [PlayablePlayer, Player, Ball] {
  397. return [new PlayablePlayer(this.player2), Object.assign({}, this.player1), Object.assign({}, this.ball)];
  398. }
  399.  
  400. public saveObjects(st1: [PlayablePlayer, Player, Ball], st2: [PlayablePlayer, Player, Ball]) {
  401. const p1 = st1[0];
  402. const p2 = st2[0];
  403. this.checkSpeed(p1);
  404. this.checkSpeed(p2);
  405.  
  406. this.player1.vX = p1.vX;
  407. this.player1.vY = p1.vY;
  408. this.player2.vX = p2.vX;
  409. this.player2.vY = p2.vY;
  410.  
  411. let [collide1, collSide1, collPX1, collPY1] = this.collidePlayerBall(this.player1, this.ball)
  412. let [collide2, collSide2, collPX2, collPY2] = this.collidePlayerBall(this.player2, this.ball)
  413.  
  414. console.log(collide1, collSide1, collPX1, collPY1)
  415. console.log(collide2, collSide2, collPX2, collPY2)
  416.  
  417. if (collide1) {
  418. movePlayerWithBall(this.player1, this.ball, this.fieldHeight, 0, 0, this.fieldWidth / 3, collSide1, collPX1, collPY1)
  419. movePlayer(this.player2, this.fieldHeight, 0, this.fieldWidth * 2 / 3, this.fieldWidth)
  420. } else if (collide2) {
  421. movePlayer(this.player1, this.fieldHeight, 0, 0, this.fieldWidth / 3)
  422. movePlayerWithBall(this.player2, this.ball, this.fieldHeight, 0, this.fieldWidth * 2 / 3, this.fieldWidth, collSide2, collPX2, collPY2)
  423. } else {
  424. movePlayer(this.player1, this.fieldHeight, 0, 0, this.fieldWidth / 3)
  425. movePlayer(this.player2, this.fieldHeight, 0, this.fieldWidth * 2 / 3, this.fieldWidth)
  426. moveBall(this.ball)
  427. }
  428.  
  429. fixBallPos(this.ball, this.fieldHeight)
  430. }
  431.  
  432. // Validators
  433. public checkSpeed(p: Player) {
  434.  
  435. if (Math.sqrt(p.vX * p.vX + p.vY * p.vY) > 10) {
  436. throw new Error("speed can not be > 10");
  437. }
  438. }
  439. }
  440.  
  441. export default Game;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement