Advertisement
Guest User

Untitled

a guest
May 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. function d(x1, y1, x2, y2){
  2. return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
  3. }
  4.  
  5. function areCircleColliding(c1, c2){
  6. return d(c1.x, c1.y, c2.x, c2.y) < c1.r + c2.r;
  7. }
  8.  
  9. class Player{
  10. constructor(x, y, color){
  11. this.x = x;
  12. this.y = y;
  13. this.r = 20;
  14. this.speed = 5;
  15. this.color = color;
  16. this.reload_time = 20;
  17. this.reload = 0;
  18. this.hp = 20;
  19. }
  20. shoot(tX, tY){
  21. if (this.reload <= 0){
  22. b.push(new Bullet(this.x, this.y, tX, tY, this.color));
  23. this.reload = this.reload_time;
  24. }
  25. }
  26. update(){
  27. this.reload--;
  28. for (let i=0; i<b.length; ++i){
  29. if (b[i].color != this.color && areCircleColliding(b[i], this)){
  30. this.hp--;
  31. b[i] = b[b.length-1];
  32. b.pop();
  33. --i;
  34. }
  35. }
  36. if (this.x < 0) this.x = 0;
  37. if (this.x > 800) this.x = 800;
  38. if (this.y < 0) this.y = 0;
  39. if (this.y > 600) this.y = 600;
  40. }
  41. moveKeyboard(){
  42. if (isKeyPressed[87]) this.y -= this.speed;
  43. if (isKeyPressed[83]) this.y += this.speed;
  44. if (isKeyPressed[65]) this.x -= this.speed;
  45. if (isKeyPressed[68]) this.x += this.speed;
  46. }
  47. moveEnemy(){
  48. let xrun=0, yrun=0, mint=-1;
  49. for (let i=0; i<b.length; ++i){
  50. if (b[i].color != this.color){
  51. let alpha = Math.atan2(b[i].dy, b[i].dx) - Math.atan2(this.y-b[i].y, this.x - b[i].x);
  52. //console.log("alpha:", alpha);
  53. let dist = d(this.x, this.y, b[i].x, b[i].y) * Math.cos(alpha);
  54. //console.log("dist:", dist);
  55. let t = Math.max(dist/b[i].speed, 0);
  56. //console.log("t:", t);
  57. let bx = b[i].x + b[i].dx*t;
  58. let by = b[i].y + b[i].dy*t;
  59. if (d(bx, by, this.x, this.y) < this.r + b[i].r + this.r){
  60. if (mint==-1 || t < mint){
  61. mint = t;
  62. xrun = bx;
  63. yrun = by;
  64. }
  65. }
  66. }
  67. }
  68. if (mint != -1){
  69. let dist = d(xrun, yrun, this.x, this.y);
  70. let dx = (this.x-xrun)/dist*this.speed;
  71. let dy = (this.y-yrun)/dist*this.speed;
  72. if (dist == 0){
  73. dx = 0
  74. dy = this.speed;
  75. }
  76. this.x += dx;
  77. this.y += dy;
  78. }
  79. let ind = -1, mind = -1;
  80. for (let i=0; i<p.length; ++i){
  81. if (p[i].color != this.color && (mind == -1 || mind > d(this.x, this.y, p[i].x, p[i].y))){
  82. ind = i;
  83. mind = d(this.x, this.y, p[i].x, p[i].y);
  84. }
  85. }
  86. if (ind != -1){
  87. this.shoot(p[ind].x, p[ind].y);
  88. }
  89. }
  90. draw(){
  91. if (this.hp <= 0) return;
  92. context.fillStyle = this.color;
  93. context.beginPath();
  94. context.arc(this.x, this.y, this.r, 0, 2*Math.PI);
  95. context.fill();
  96. }
  97. };
  98.  
  99. class Bullet{
  100. constructor(x, y, tX, tY, color){
  101. this.x = x;
  102. this.y = y;
  103. this.r = 5;
  104. this.color = color;
  105. this.speed = 12;
  106. let dist = d(x, y, tX, tY);
  107. this.dx = (tX-x)/dist*this.speed;
  108. this.dy = (tY-y)/dist*this.speed;
  109. }
  110. update(){
  111. this.x += this.dx;
  112. this.y += this.dy;
  113. }
  114. draw(){
  115. context.fillStyle = this.color;
  116. context.beginPath();
  117. context.arc(this.x, this.y, this.r, 0, 2*Math.PI);
  118. context.fill();
  119. }
  120. };
  121.  
  122. var p = [];
  123. for (let i=0; i<13; ++i){
  124. if (i%2 == 0){
  125. p[i] = new Player(Math.random()*800, Math.random()*600, "blue");
  126. }else{
  127. p[i] = new Player(Math.random()*800, Math.random()*600, "red");
  128. }
  129. }
  130. var b = [];
  131.  
  132. function update() {
  133. for (let i=0; i<p.length; ++i){
  134. if (p[i].hp <= 0){
  135. p[i] = p[p.length-1];
  136. p.pop();
  137. i--;
  138. }else{
  139. p[i].update();
  140. p[i].moveEnemy();
  141. }
  142. }
  143. for (let i=0; i<b.length; ++i) b[i].update();
  144. }
  145. function draw() {
  146. for (let i=0; i<p.length; ++i){
  147. p[i].draw();
  148. }
  149. for (let i=0; i<b.length; ++i) b[i].draw();
  150. };
  151. function keyup(key) {
  152. };
  153. function mouseup() {
  154. p[0].shoot(mouseX, mouseY);
  155. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement