Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 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. class Segment{
  5. constructor(bx, by, ex, ey){
  6. this.bx = bx;
  7. this.by = by;
  8. this.ex = ex;
  9. this.ey = ey;
  10. this.dist = d(bx, by, ex, ey);
  11. }
  12. }
  13.  
  14. class Route{
  15. constructor(n, color, bx=0, by=0, ex=800, ey=600){
  16. this.s = [];
  17. this.color = color;
  18. for (let i=0; i<n; ++i){
  19. let bx_, by_, ex_, ey_;
  20. if (i==0){
  21. bx_ = bx;
  22. by_ = by;
  23. }
  24. else {
  25. bx_ = this.s[i-1].ex;
  26. by_ = this.s[i-1].ey;
  27. }
  28. ex_ = Math.random()*800;
  29. ey_ = Math.random()*600;
  30. if (i==n-1){
  31. ex_ = ex;
  32. ey_ = ey;
  33. }
  34. this.s[i] = new Segment(bx_, by_, ex_, ey_);
  35. }
  36. }
  37. draw(){
  38. context.strokeStyle = this.color;
  39. context.beginPath();
  40. context.moveTo(this.s[0].bx, this.s[0].by);
  41. for (let i=0; i<this.s.length; ++i){
  42. context.lineTo(this.s[i].ex, this.s[i].ey);
  43. }
  44. context.stroke();
  45. }
  46. }
  47.  
  48. class Enemy{
  49. constructor(){
  50. this.r_ind = Math.floor(Math.random()*r.length);
  51. this.s_ind = 0;
  52. this.d = 0;
  53. this.calcxy();
  54. this.image = duckTarget;
  55. this.sx = 50;
  56. this.sy = 50;
  57. this.speed = 5;
  58. this.finish = false;
  59. this.hp = 100;
  60. }
  61. calcxy(){
  62. if (this.finish || this.hp <= 0) return;
  63. this.x = r[this.r_ind].s[this.s_ind].bx;
  64. this.y = r[this.r_ind].s[this.s_ind].by;
  65. this.x += (r[this.r_ind].s[this.s_ind].ex - r[this.r_ind].s[this.s_ind].bx)*this.d/r[this.r_ind].s[this.s_ind].dist;
  66. this.y += (r[this.r_ind].s[this.s_ind].ey - r[this.r_ind].s[this.s_ind].by)*this.d/r[this.r_ind].s[this.s_ind].dist;
  67. }
  68. update(){
  69. if (this.finish || this.hp <= 0) return;
  70. this.d += this.speed;
  71. if (this.d > r[this.r_ind].s[this.s_ind].dist){
  72. this.d -= r[this.r_ind].s[this.s_ind].dist;
  73. this.s_ind++;
  74. if (this.s_ind >= r[this.r_ind].s.length){
  75. this.finish = true;
  76. }
  77. }
  78. this.calcxy();
  79. }
  80. draw(){
  81. if (this.finish || this.hp <= 0) return;
  82. drawImage(this.image, this.x-this.sx/2, this.y-this.sy/2, this.sx, this.sy);
  83. }
  84. }
  85.  
  86. class Tower{
  87. constructor(x, y){
  88. this.x = x;
  89. this.y = y;
  90. this.r = 200;
  91. this.target = [];
  92. this.dmg = 0.5;
  93. }
  94. shoot(){
  95. this.target = [];
  96. for (let i=0; i<e.length; ++i){
  97. if (d(this.x, this.y, e[i].x, e[i].y) < this.r && !e[i].finish && e[i].hp > 0){
  98. this.target.push(i);
  99. e[i].hp -= this.dmg;
  100. }
  101. }
  102. }
  103. update(){
  104. this.shoot();
  105. }
  106. draw(){
  107. drawImage(crystal, this.x-25, this.y-25, 50, 50);
  108. context.strokeStyle = "red";
  109. context.beginPath();
  110. for (let i=0; i<this.target.length; ++i){
  111. context.moveTo(this.x, this.y);
  112. context.lineTo(e[this.target[i]].x, e[this.target[i]].y);
  113. }
  114. context.stroke();
  115. }
  116. }
  117.  
  118. var r = [new Route(3, "blue"), new Route(5, "red"), new Route(7, "green")];
  119. var e = [];
  120. var tower = [];
  121. var t = 0;
  122. function update() {
  123. ++t;
  124. if (t%1==0){
  125. e.push(new Enemy())
  126. }
  127. for (let i=0; i<e.length; ++i){
  128. e[i].update();
  129. }
  130. for (let i=0; i<tower.length; ++i){
  131. tower[i].update();
  132. }
  133. }
  134. function draw() {
  135. for (let i=0; i<r.length; ++i){
  136. r[i].draw();
  137. }
  138. for (let i=0; i<e.length; ++i){
  139. e[i].draw();
  140. }
  141. for (let i=0; i<tower.length; ++i){
  142. tower[i].draw();
  143. }
  144. };
  145. function keyup(key) {
  146. // Show the pressed keycode in the console
  147. console.log("Pressed", key);
  148. };
  149. function mouseup() {
  150. tower.push(new Tower(mouseX, mouseY));
  151. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement