Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. var px=200, py=300, ex=600, ey=300, pr=0, er=0, php = 20, ehp = 20;
  2. var nb = 0;
  3. var bx = [], by = [], dx = [], dy = [], color = [];
  4. var pspeed = 5/10, bspeed = 15/10, r = 10*10;
  5.  
  6. function d(x1, y1, x2, y2){
  7. return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
  8. }
  9.  
  10. function shoot(x, y, tx, ty, c){
  11. bx[nb] = x;
  12. by[nb] = y;
  13. let dist = d(x, y, tx, ty);
  14. dx[nb] = (tx-x)*bspeed/dist;
  15. dy[nb] = (ty-y)*bspeed/dist;
  16. color[nb] = c;
  17. ++nb;
  18. }
  19.  
  20. var danger = Math.sqrt(2)*(30+5)/2 + 5;
  21.  
  22. function move_enemy(){
  23. let run_away_x, run_away_y, run_away_dx, run_away_dy, mint = -1;
  24. for (let i=0; i<nb; ++i){
  25. let t = d(bx[i], by[i], ex, ey)/bspeed;
  26. let nx = bx[i] + t*dx[i];
  27. let ny = by[i] + t*dy[i];
  28. if (d(nx, ny, ex, ey) <= danger){
  29. if (mint == -1 || mint > t) {
  30. mint = t;
  31. run_away_x = nx;
  32. run_away_y = ny;
  33. run_away_dx = dx[i];
  34. run_away_dy = dy[i];
  35. }
  36. }
  37. }
  38. if (mint > -1){
  39. let dist = d(run_away_x, run_away_y, ex, ey);
  40. if (dist > 0.1){
  41. ex += (ex-run_away_x)*pspeed/dist;
  42. ey += (ey-run_away_y)*pspeed/dist;
  43. }else{
  44. ex += -run_away_dy/bspeed*pspeed;
  45. ey += run_away_dx/bspeed*pspeed;
  46. }
  47. }
  48. if (er == 0){
  49. er = r;
  50. shoot(ex, ey, px, py, "red");
  51. }
  52. }
  53.  
  54. function move_player_bot(){
  55. let run_away_x, run_away_y, run_away_dx, run_away_dy, mint = -1;
  56. for (let i=0; i<nb; ++i){
  57. let t = d(bx[i], by[i], px, py)/bspeed;
  58. let nx = bx[i] + t*dx[i];
  59. let ny = by[i] + t*dy[i];
  60. if (d(nx, ny, px, py) <= danger){
  61. if (mint == -1 || mint > t) {
  62. mint = t;
  63. run_away_x = nx;
  64. run_away_y = ny;
  65. run_away_dx = dx[i];
  66. run_away_dy = dy[i];
  67. }
  68. }
  69. }
  70. if (mint > -1){
  71. let dist = d(run_away_x, run_away_y, px, py);
  72. if (dist > 0.1){
  73. px += (px-run_away_x)*pspeed/dist;
  74. py += (py-run_away_y)*pspeed/dist;
  75. }else{
  76. px += -run_away_dy/bspeed*pspeed;
  77. py += run_away_dx/bspeed*pspeed;
  78. }
  79. }
  80. if (pr == 0){
  81. pr = r;
  82. shoot(px, py, ex, ey, "blue");
  83. }
  84. }
  85.  
  86. function rem_bullet(i){
  87. nb--;
  88. bx[i] = bx[nb];
  89. by[i] = by[nb];
  90. dx[i] = dx[nb];
  91. dy[i] = dy[nb];
  92. color[i] = color[nb];
  93. }
  94.  
  95. function player_move(){
  96. if (isKeyPressed[65]) px -= pspeed;
  97. if (isKeyPressed[68]) px += pspeed;
  98. if (isKeyPressed[87]) py -= pspeed;
  99. if (isKeyPressed[83]) py += pspeed;
  100. }
  101.  
  102. function update() {
  103. if (php <= 0 || ehp <= 0) return;
  104. if (pr > 0) --pr;
  105. if (er > 0) --er;
  106. player_move();
  107. //move_player_bot();
  108. move_enemy();
  109. for (let i=0; i<nb; ++i){
  110. bx[i] += dx[i];
  111. by[i] += dy[i];
  112. if (color[i] == 'red' && areColliding(px-15, py-15, 30, 30, bx[i]-2.5, by[i]-2.5, 5, 5)){
  113. php--;
  114. rem_bullet(i);
  115. --i;
  116. continue;
  117. }
  118. if (color[i] == 'blue' && areColliding(ex-15, ey-15, 30, 30, bx[i]-2.5, by[i]-2.5, 5, 5)){
  119. ehp--;
  120. rem_bullet(i);
  121. --i;
  122. continue;
  123. }
  124. }
  125. }
  126. function draw() {
  127. context.fillStyle = "blue";
  128. context.fillRect(px-15, py-15, 30, 30);
  129. context.fillStyle = "red";
  130. context.fillRect(ex-15, ey-15, 30, 30);
  131. for (let i=0; i<nb; ++i){
  132. context.fillStyle = color[i];
  133. context.fillRect(bx[i]-2.5, by[i]-2.5, 5, 5);
  134. }
  135. };
  136. function keyup(key) {
  137. // Show the pressed keycode in the console
  138. console.log("Pressed", key);
  139. };
  140. function mouseup() {
  141. if (pr == 0){
  142. pr = r;
  143. shoot(px, py, mouseX, mouseY, "blue");
  144. }
  145. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement