Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *  Enemy object
  3.  *
  4.  *
  5.  *  @param Int speed - Speed in pixels
  6.  *  @param Int hp - Hit points
  7.  */
  8. function Enemy() {
  9.  
  10.   this.init = function (path, startPosition, startSpeed, startHp) {
  11.     this.path = path;
  12.     this.destination = 0;
  13.    
  14.     this.size = 5;
  15.    
  16.     this.startPosition = startPosition;
  17.     this.position = startPosition;
  18.     console.log(this.startPosition);
  19.    
  20.     this.direction = 0;
  21.     this.speed = startSpeed;
  22.    
  23.     this.hp = startHp;
  24.     this.maxHp = this.hp;
  25.    
  26.     this.findDirection();
  27.   }
  28.  
  29.   this.findDirection = function () {
  30.     if(this.position[1]>this.path[this.destination][1]) {
  31.       this.direction = 0;
  32.     } else if(this.position[0]<this.path[this.destination][0]) {
  33.       this.direction = 1;
  34.     } else if(this.position[1]<this.path[this.destination][1]) {
  35.       this.direction = 2;
  36.     } else if(this.position[0]>this.path[this.destination][0]) {
  37.       this.direction = 3;
  38.     }
  39.   }
  40.  
  41.   this.update = function () {
  42.     if(this.direction==0) {
  43.       this.position[1]-=this.speed;
  44.     } else if(this.direction==1) {
  45.       this.position[0]+=this.speed;
  46.     } else if(this.direction==2) {
  47.       this.position[1]+=this.speed;
  48.     } else if(this.direction==3) {
  49.       this.position[0]-=this.speed;
  50.     }
  51.    
  52.     //If we reach destination - find next one
  53.     //... and we use pythagoras to find that out
  54.     var a = b = c = 0;
  55.     a = this.path[this.destination][0]-this.position[0];
  56.     b = this.path[this.destination][1]-this.position[1];
  57.     c = Math.sqrt((Math.pow(a, 2)+Math.pow(b, 2)));
  58.     if(c<1) {
  59.       this.destination++;
  60.      
  61.       //If enemy reached end position/castle
  62.       if(this.destination >= this.path.length) {
  63.         this.restart();
  64.       } else {
  65.         this.findDirection();
  66.       }
  67.     }
  68.   }
  69.  
  70.   this.restart = function () {
  71.     this.destination = 0;
  72.     console.log(this.position);
  73.     this.position = [18,432];
  74.     console.log(this.position);
  75.     this.findDirection();
  76.   }
  77.  
  78.   this.draw = function () {
  79.     canvas.fillStyle("blue");
  80.     canvas.circle(
  81.       this.position[0],
  82.       this.position[1],
  83.       this.size
  84.     );
  85.   }
  86. }
Add Comment
Please, Sign In to add comment