Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Enemy object
- *
- *
- * @param Int speed - Speed in pixels
- * @param Int hp - Hit points
- */
- function Enemy() {
- this.init = function (path, startPosition, startSpeed, startHp) {
- this.path = path;
- this.destination = 0;
- this.size = 5;
- this.startPosition = startPosition;
- this.position = startPosition;
- console.log(this.startPosition);
- this.direction = 0;
- this.speed = startSpeed;
- this.hp = startHp;
- this.maxHp = this.hp;
- this.findDirection();
- }
- this.findDirection = function () {
- if(this.position[1]>this.path[this.destination][1]) {
- this.direction = 0;
- } else if(this.position[0]<this.path[this.destination][0]) {
- this.direction = 1;
- } else if(this.position[1]<this.path[this.destination][1]) {
- this.direction = 2;
- } else if(this.position[0]>this.path[this.destination][0]) {
- this.direction = 3;
- }
- }
- this.update = function () {
- if(this.direction==0) {
- this.position[1]-=this.speed;
- } else if(this.direction==1) {
- this.position[0]+=this.speed;
- } else if(this.direction==2) {
- this.position[1]+=this.speed;
- } else if(this.direction==3) {
- this.position[0]-=this.speed;
- }
- //If we reach destination - find next one
- //... and we use pythagoras to find that out
- var a = b = c = 0;
- a = this.path[this.destination][0]-this.position[0];
- b = this.path[this.destination][1]-this.position[1];
- c = Math.sqrt((Math.pow(a, 2)+Math.pow(b, 2)));
- if(c<1) {
- this.destination++;
- //If enemy reached end position/castle
- if(this.destination >= this.path.length) {
- this.restart();
- } else {
- this.findDirection();
- }
- }
- }
- this.restart = function () {
- this.destination = 0;
- console.log(this.position);
- this.position = [18,432];
- console.log(this.position);
- this.findDirection();
- }
- this.draw = function () {
- canvas.fillStyle("blue");
- canvas.circle(
- this.position[0],
- this.position[1],
- this.size
- );
- }
- }
Add Comment
Please, Sign In to add comment