<commit_before>game.PlayerEntity = me.Entity.extend ({
        init: function(x, y, settings){
            this._super(me.Entity, "init", [x, y, {
            image: "player",
            width: 64,
            height: 64,
//            the actual images width and height
            spritewidth: "64",
            spriteheight: "64",
            getShape: function(){
                return(new me.Rect(0, 0, 64, 64)).toPolygon();
            }
             }]);
//            this sets the speed that the character is going
            this.body.setVelocity(10, 20);
            this.facing = "right";
//            this updates the time and date of the hits
            this.now = new Date().getTime();
            this.lastHit = this.now;
            this.lastAttack = new Date().getTime();
//            this follows the position of the character
            me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

            this.renderable.addAnimation("idle", [78]);
            this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 123, 124, 125], 80);
            this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);

            this.renderable.setCurrentAnimation("idle");
            },

            update: function(delta){
                this.now = new Date().getTime();
            if(me.input.isKeyPressed("right")){

//                velocity represents our current position, sets position of x by multiplying velocity by me.timer.tick
                    this.body.vel.x += this.body.accel.x * me.timer.tick;
//                    this flips the character around
                    this.flipX(true);
                    this.facing = "right";
            }
            else if(me.input.isKeyPressed("left")){
                this.facing = "left";
                this.body.vel.x -= this.body.accel.x * me.timer.tick;
//                this flips the character around
                this.flipX(false);
            }
            else{
                this.body.vel.x=0;
            }
//            this allows me to jump and not double jump or jump when falling
            if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
                this.body.jumping = true;
                this.body.vel.y -= this.body.accel.y * me.timer.tick;
            }

//                       if the a key is pressed then play attack animation
            if(me.input.isKeyPressed("attack")){
                if(!this.renderable.isCurrentAnimation("attack")){

//                    sets the character animation to an attack one if using the a key
                    this.renderable.setCurrentAnimation("attack", "idle");
//                    makes it so when we start the sequence we begin from the first animation
                        this.renderable.setAnimationFrame();
                }
            }
                //            this sets it so that if the character isnt moving then it shows the idle animation
                else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
                    if(!this.renderable.isCurrentAnimation("walk")){
                        this.renderable.setCurrentAnimation("walk");
                    }
        }
        else if(!this.renderable.isCurrentAnimation("attack")){
            this.renderable.setCurrentAnimation("idle");
        }
                me.collision.check(this, true, this.collideHandler.bind(this), true);

                this.body.update(delta);

            this._super(me.Entity, "update", [delta]);

            return true;
            },

            collideHandler: function(response){
                if(response.b.type==="EnemyBaseEntity"){
                    var ydif = this.pos.y - response.b.pos.y;
                    var xdif = this.pos.x - response.b.pos.x;

                     if(ydif<-40 && xdif< 70 && xdif>-35){
                        this.body.falling = false;
                        this.body.vel.y = -1;
                    }
//                    we can no longer jump onto towers
                    else if(xdif>-35 && this.facing === 'right' && (xdif<0) && ydif>-50){
                        this.body.vel.x = 0;
                        this.pos.x = this.pos.x -1;
                    }else if(xdif<70 && this.facing ==='left' && (xdif>0)){
                        this.body.vel.x = 0;
                        this.pos.x = this.pos.x +1;
                    }else if(ydif<-40){
                        this.body.falling = false;
                        this.body.vel.y = -1;
                    }
//                    if its been more then 400 miliseconds then update the last hit timer
                    if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000){

                        this.lastHit = this.now;
                        response.b.loseHealth();
                    }
                }
            }
});
//this is for the player base in tiled
game.PlayerBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity,'init', [x,y, {
                image: "tower",
                width: 100,
                height: 100,
                spritewidth: "100",
                spriteheight: "100",
                getShape: function(){
                    return (new me.Rect(0, 0, 70, 70)).toPolygon();
                }
        }]);
//    these are variables that are used later
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);

        this.type = "PlayerBase";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
//    if the players health is lower then 0 then he dies
    update:function(delta){
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    loseHealth: function(damage) {
        this.health = this.health - damage;
    },

    onCollision: function(){

    }
});
//all the same as above except is enemies
game.EnemyBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity,'init', [x,y, {
                image: "tower",
                width: 100,
                height: 100,
                spritewidth: "100",
                spriteheight: "100",
                getShape: function(){
                    return (new me.Rect(0, 0, 70, 70)).toPolygon();
                }
        }]);
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);

        this.type = "EnemyBaseEntity";
//        this sets the tower to not be burning
        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
    update:function(delta){
//        if the tower is deaded then burn it
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;

    },

    onCollision: function(){

    },
//    this sets it so the base loeses health
    loseHealth: function(){
        this.health--;
    }
});

game.EnemyCreep = me.Entity.extend({
    init: function(x,y,settings){
        this._super(me.Entity, 'init', [x, y,{
                image: "creep1",
                width: 32,
                height: 64,
                spritewidth: "32",
                spriteheight: "64",
                getShape: function(){
                    return (new me.Rect(0, 0, 32, 64)).toPolygon();
                }
        }]);
//    this sets the health
    this.health =10;
    this.alwaysUpdate = true;
//    this lets us know if the enemy is attacking or not
    this.attacking = false;
//    this is gonna shsow when the last time the creep attacked anything
    this.lastAttacking = new Date().getTime();
//    keeps track of when the crepe hit anything
    this.lastHit = new Date().getTime();
    this.now = new Date().getTime();
    this.body.setVelocity(3,20);

    this.type = "EnemyCreep";
//    sets walk animation
    this.renderable.addAnimation("walk", [3, 4, 5], 80);
    this.renderable.setCurrentAnimation("walk");
    },
    update: function(delta){
        this.now = new Date().getTime();
//        sets this so the creep moves
        this.body.vel.x -= this.body.accel.x * me.timer.tick;

        me.collision.check(this, true, this.collideHandler.bind(this), true);
        //        this updates the body
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);

        return true;

    },
    collideHandler: function(response) {
        if(response.b.type==='PlayerBase'){
            this.attacking=true;
            this.lastAttacking = this.now;
            this.body.vel.x = 0;
//            keeps moving the creep to the right to maintain position
            this.pos.x = this.pos.x + 1;
//            checks that it has been 1 second since the creep hit the base
            if((this.now-this.lastHit >= 1000)){
//                updates the last hit timer
                this.lastHit = this.now;
//                makes the playerbase call its losehealth function and have its health -1
                response.b.loseHealth(1);
            }
        }
    }

});
//made entties gamemanager
game.GameManager = Object.extend({
   init: function(x,y,settings){
     this.now = new Date().getTime();
     this.lastCreep = new Date().getTime();

     this.alwaysUpdate = true;
   },
    update: function(){
        this.now = new Date().getTime();
        if(Math.round(this.now/1000)%10 ===0 && (this.now - this.lastCreep >= 1000)){
            this.lastCreep = this.now;
            var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
            me.game.world.addChild(creepe,5);
        }
        return true;
    }

});<commit_msg>Code for video 22<commit_after>game.PlayerEntity = me.Entity.extend ({
        init: function(x, y, settings){
            this._super(me.Entity, "init", [x, y, {
            image: "player",
            width: 64,
            height: 64,
//            the actual images width and height
            spritewidth: "64",
            spriteheight: "64",
            getShape: function(){
                return(new me.Rect(0, 0, 64, 64)).toPolygon();
            }
             }]);
         this.type = "PlayerEntity";
         this.health = 20;
//            this sets the speed that the character is going
            this.body.setVelocity(10, 20);
            this.facing = "right";
//            this updates the time and date of the hits
            this.now = new Date().getTime();
            this.lastHit = this.now;
            this.lastAttack = new Date().getTime();
//            this follows the position of the character
            me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

            this.renderable.addAnimation("idle", [78]);
            this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 123, 124, 125], 80);
            this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);

            this.renderable.setCurrentAnimation("idle");
            },

            update: function(delta){
                this.now = new Date().getTime();
            if(me.input.isKeyPressed("right")){

//                velocity represents our current position, sets position of x by multiplying velocity by me.timer.tick
                    this.body.vel.x += this.body.accel.x * me.timer.tick;
//                    this flips the character around
                    this.flipX(true);
                    this.facing = "right";
            }
            else if(me.input.isKeyPressed("left")){
                this.facing = "left";
                this.body.vel.x -= this.body.accel.x * me.timer.tick;
//                this flips the character around
                this.flipX(false);
            }
            else{
                this.body.vel.x=0;
            }
//            this allows me to jump and not double jump or jump when falling
            if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
                this.body.jumping = true;
                this.body.vel.y -= this.body.accel.y * me.timer.tick;
            }

//                       if the a key is pressed then play attack animation
            if(me.input.isKeyPressed("attack")){
                if(!this.renderable.isCurrentAnimation("attack")){

//                    sets the character animation to an attack one if using the a key
                    this.renderable.setCurrentAnimation("attack", "idle");
//                    makes it so when we start the sequence we begin from the first animation
                        this.renderable.setAnimationFrame();
                }
            }
                //            this sets it so that if the character isnt moving then it shows the idle animation
                else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
                    if(!this.renderable.isCurrentAnimation("walk")){
                        this.renderable.setCurrentAnimation("walk");
                    }
        }
        else if(!this.renderable.isCurrentAnimation("attack")){
            this.renderable.setCurrentAnimation("idle");
        }
                me.collision.check(this, true, this.collideHandler.bind(this), true);

                this.body.update(delta);

            this._super(me.Entity, "update", [delta]);

            return true;
            },

            loseHealth: function(damage){
              this.health = this.health - damage;
            },

            collideHandler: function(response){
                if(response.b.type==="EnemyBaseEntity"){
                    var ydif = this.pos.y - response.b.pos.y;
                    var xdif = this.pos.x - response.b.pos.x;

                     if(ydif<-40 && xdif< 70 && xdif>-35){
                        this.body.falling = false;
                        this.body.vel.y = -1;
                    }
//                    we can no longer jump onto towers
                    else if(xdif>-35 && this.facing === 'right' && (xdif<0) && ydif>-50){
                        this.body.vel.x = 0;
                        this.pos.x = this.pos.x -1;
                    }else if(xdif<70 && this.facing ==='left' && (xdif>0)){
                        this.body.vel.x = 0;
                        this.pos.x = this.pos.x +1;
                    }else if(ydif<-40){
                        this.body.falling = false;
                        this.body.vel.y = -1;
                    }
//                    if its been more then 400 miliseconds then update the last hit timer
                    if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000){

                        this.lastHit = this.now;
                        response.b.loseHealth();
                    }
                }
            }
});
//this is for the player base in tiled
game.PlayerBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity,'init', [x,y, {
                image: "tower",
                width: 100,
                height: 100,
                spritewidth: "100",
                spriteheight: "100",
                getShape: function(){
                    return (new me.Rect(0, 0, 70, 70)).toPolygon();
                }
        }]);
//    these are variables that are used later
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);

        this.type = "PlayerBase";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
//    if the players health is lower then 0 then he dies
    update:function(delta){
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    loseHealth: function(damage) {
        this.health = this.health - damage;
    },

    onCollision: function(){

    }
});
//all the same as above except is enemies
game.EnemyBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity,'init', [x,y, {
                image: "tower",
                width: 100,
                height: 100,
                spritewidth: "100",
                spriteheight: "100",
                getShape: function(){
                    return (new me.Rect(0, 0, 70, 70)).toPolygon();
                }
        }]);
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);

        this.type = "EnemyBaseEntity";
//        this sets the tower to not be burning
        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
    update:function(delta){
//        if the tower is deaded then burn it
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;

    },

    onCollision: function(){

    },
//    this sets it so the base loeses health
    loseHealth: function(){
        this.health--;
    }
});

game.EnemyCreep = me.Entity.extend({
    init: function(x,y,settings){
        this._super(me.Entity, 'init', [x, y,{
                image: "creep1",
                width: 32,
                height: 64,
                spritewidth: "32",
                spriteheight: "64",
                getShape: function(){
                    return (new me.Rect(0, 0, 32, 64)).toPolygon();
                }
        }]);
//    this sets the health
    this.health =10;
    this.alwaysUpdate = true;
//    this lets us know if the enemy is attacking or not
    this.attacking = false;
//    this is gonna shsow when the last time the creep attacked anything
    this.lastAttacking = new Date().getTime();
//    keeps track of when the crepe hit anything
    this.lastHit = new Date().getTime();
    this.now = new Date().getTime();
    this.body.setVelocity(3,20);

    this.type = "EnemyCreep";
//    sets walk animation
    this.renderable.addAnimation("walk", [3, 4, 5], 80);
    this.renderable.setCurrentAnimation("walk");
    },
    update: function(delta){
        this.now = new Date().getTime();
//        sets this so the creep moves
        this.body.vel.x -= this.body.accel.x * me.timer.tick;

        me.collision.check(this, true, this.collideHandler.bind(this), true);
        //        this updates the body
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);

        return true;

    },
    collideHandler: function(response) {
        if(response.b.type==='PlayerBase'){
            this.attacking=true;
            this.lastAttacking = this.now;
            this.body.vel.x = 0;
//            keeps moving the creep to the right to maintain position
            this.pos.x = this.pos.x + 1;
//            checks that it has been 1 second since the creep hit the base
            if((this.now-this.lastHit >= 1000)){
//                updates the last hit timer
                this.lastHit = this.now;
//                makes the playerbase call its losehealth function and have its health -1
                response.b.loseHealth(1);
            }
        }else if (response.b.type==='PlayerEntity'){
            var xdif = this.pos.x - response.b.pos.x;

            this.attacking=true;
//            this.lastAttacking = this.now;

            if(xdif>0){
            this.pos.x = this.pos.x + 1;
            //            keeps moving the creep to the right to maintain position
            this.body.vel.x = 0;
        }
//            checks that it has been 1 second since the creep hit the base
            if((this.now-this.lastHit >= 1000) && xdif>0){
//                updates the last hit timer
                this.lastHit = this.now;
//                makes the playerbase call its losehealth function and have its health -1
                response.b.loseHealth(1);
            }
        }
    }

});
//made entties gamemanager
game.GameManager = Object.extend({
   init: function(x,y,settings){
     this.now = new Date().getTime();
     this.lastCreep = new Date().getTime();

     this.alwaysUpdate = true;
   },
    update: function(){
        this.now = new Date().getTime();
        if(Math.round(this.now/1000)%10 ===0 && (this.now - this.lastCreep >= 1000)){
            this.lastCreep = this.now;
            var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
            me.game.world.addChild(creepe,5);
        }
        return true;
    }

});<|endoftext|>


<commit_before>game.PlayerEntity = me.Entity.extend({
    init: function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "player",
            width: 64,
            height: 64,
            spritewidth: "64",
            spriteheight: "64",
            getShape: function(){
                return(new me.Rect(0, 0, 64, 64)).toPolygon();
            }
        }]);
       this.body.setVelocity(5, 20);
       //keeps track of which direction your character is going
       this.facing = "right";
       this.now = new Date().getTime();
       this.lastHit = this.now;
       this.lastAttack = new Date().getTime();  //Haven't used this
       me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

       this.renderable.addAnimation("idle", [78]);
       this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
       this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);

       this.renderable.setCurrentAnimation("idle");
    },

    update: function(delta){
        this.now = new Date().getTime();
        if(me.input.isKeyPressed("right")){
            //adds the position of my x by the velocity defined above in
            //setVelocity and multiplying it by me.timer.tick
            //me.timer.tick makes movement look smooth
            this.body.vel.x += this.body.accel.x * me.timer.tick;
            this.facing = "right";
            this.flipX(true);
        }else if(me.input.isKeyPressed("left")){
            this.facing = "left";
            this.body.vel.x -=this.body.accel.x * me.timer.tick;
            this.flipX(false);
        }else{
            this.body.vel.x = 0;
        }
        //i made the character go left when the left key is pressed
        if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
            this.jumping = true;
            this.body.vel.y -= this.body.accel.y * me.timer.tick;
        }
        //i am making the character jump when i press the space button
        if(me.input.isKeyPressed("attack")){
            if(!this.renderable.isCurrentAnimation("attack")){
                console.log(this.renderable.isCurrentAnimation("attack"));
                //sets the current animation to attack and once that is over
                //goes back to the idle animation
                this.renderable.setCurrentAnimation("attack", "idle");
                //makes it so the next time we start this sequence we begin
                //from the first animation, not from wherever we left off when we
                //switched to another animation
                this.renderable.setAnimationFrame();
            }
        }

        else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")) {
             if(!this.renderable.isCurrentAnimation("walk")){
                 this.renderable.setCurrentAnimation("walk");
        }
        }else if(!this.renderable.isCurrentAnimation("attack")){
           this.renderable.setCurrentAnimation("idle");
        }

        me.collision.check(this, true, this.collideHandler.bind(this), true);
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    collideHandler: function(response){
        if(response.b.type==='EnemyBaseEntity'){
            var ydif = this.pos.y - response.b.pos.y;
            var xdif = this.pos.x - response.b.pos.x;

            if(ydif<-40 && xdif< 70 && xdif>-35){
                this.body.falling = false;
                this.body.vel.y = -1;
            }
            else if(xdif>-35 && this.facing==='right' && (xdif<0)){
                this.body.vel.x = 0;
                this.pos.x = this.pos.x -1;
            }else if(xdif<70 && this.facing==='left' && (xdif>0)){
                this.body.vel.x = 0;
                this.pos.x = this.pos.x +1;
            }

            if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000){
                console.log("tower Hit");
                this.lastHit = this.now;
                response.b.loseHealth();
            }
        }
    }
});

game.PlayerBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "tower",
            width: 100,
            height: 100,
            spritewidth: "100",
            spriteheight: "100",
            getShape: function(){
                return (new me.Rect(0, 0, 100, 70)).toPolygon();
            }
        }]);
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);
        this.type = "PlayerBase";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },

    update:function(delta){
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    loseHealth: function(damage){
    	this.health = this.health - damage;
    },

    onCollision: function(){

    }

});

game.EnemyBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "tower",
            width: 100,
            height: 100,
            spritewidth: "100",
            spriteheight: "100",
            getShape: function(){
                return (new me.Rect(0, 0, 100, 70)).toPolygon();
            }
        }]);
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);
        this.type = "EnemyBaseEntity";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },

    update:function(delta){
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    onCollision: function(){

    },

    loseHealth: function(){
        this.health--;
    }

});

game.EnemyCreep = me.Entity.extend({
    init: function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "creep1",
            width: 32,
            height: 64,
            spritewidth: "32",
            spriteheight: "64",
            getShape: function(){
                return (new me.Rect(0, 0, 32, 64)).toPolygon();
            }
        }]);
        this.health = 10;
        this.alwaysUpdate = true;
        //this.attacking is letting us know if the creep is currently attacking
        this.attacking = false;
        //keeps track of when our creep last attacked anything
        this.lastAttacking = new Date().getTime();
        //keep track of the lasttime or creep hit anything
        this.lastHit = new Date().getTime();
        this.now = new Date().getTime();
        this.body.setVelocity(3, 20);

        this.type = "EnemyCreep";

        this.renderable.addAnimation("walk", [3, 4, 5], 80);
        this.renderable.setCurrentAnimation("walk");

    },

    update: function(delta){
    	this.now = new Date().getTime();

    	this.body.vel.x -= this.body.accel.x * me.timer.tick;

    	me.collision.check(this, true, this.collideHandler.bind(this), true);

    	this.body.update(delta);

        this._super(me.Entity, "update", [delta]);

        return true;
    },

    collideHandler: function(response){
    	if(response.b.type==='PlayerBase'){
    		this.attacking=true;
    		this.lastAttacking=this.now;
    		this.body.vel.x = 0;
    		//keeps moving the creep to the right to maintain its position
    		this.pos.x = this.pos.x + 1;
    		//checks that is has been at least 1 second since this creep hit a base
    		if((this.now-this.lastHit >= 1000)){
    			//updates the lastHit timer
    			this.lastHit = this.now;
    			//makes the player base call its longHealth function and passes it a
    			//damage of 1
    			repsonse.b.loseHealth(1);
    		}
    	}
    }
});

game.GameManager = Object.extend({
    init: function(x, y, settings){
        this.now = new Date().getTime();
        this.lastCreep = new Date().getTime();

        this.alwaysUpdate = true;
    },

    update: function(){
        this.now = new Date().getTime();

        if(Math.round(this.now/1000)%10 ===0 && (this.now - this.lastCreep >= 1000)){
            this.lastCreep = this.now;
            var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
            me.game.world.addChild(creepe, 5);
        }

        return true;
    }
});

<commit_msg>finished video 22 coded and commented<commit_after>game.PlayerEntity = me.Entity.extend({
    init: function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "player",
            width: 64,
            height: 64,
            spritewidth: "64",
            spriteheight: "64",
            getShape: function(){
                return(new me.Rect(0, 0, 64, 64)).toPolygon();
            }
        }]);
		this.type = "PlayerEntity";
		this.health = 20;
		this.body.setVelocity(5, 20);
		//keeps track of which direction your character is going
		this.facing = "right";
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.lastAttack = new Date().getTime();  //Haven't used this
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

		this.renderable.addAnimation("idle", [78]);
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
		this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);

		this.renderable.setCurrentAnimation("idle");
    },

    update: function(delta){
        this.now = new Date().getTime();
        if(me.input.isKeyPressed("right")){
            //adds the position of my x by the velocity defined above in
            //setVelocity and multiplying it by me.timer.tick
            //me.timer.tick makes movement look smooth
            this.body.vel.x += this.body.accel.x * me.timer.tick;
            this.facing = "right";
            this.flipX(true);
        }else if(me.input.isKeyPressed("left")){
            this.facing = "left";
            this.body.vel.x -=this.body.accel.x * me.timer.tick;
            this.flipX(false);
        }else{
            this.body.vel.x = 0;
        }
        //i made the character go left when the left key is pressed
        if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
            this.jumping = true;
            this.body.vel.y -= this.body.accel.y * me.timer.tick;
        }
        //i am making the character jump when i press the space button
        if(me.input.isKeyPressed("attack")){
            if(!this.renderable.isCurrentAnimation("attack")){
                console.log(this.renderable.isCurrentAnimation("attack"));
                //sets the current animation to attack and once that is over
                //goes back to the idle animation
                this.renderable.setCurrentAnimation("attack", "idle");
                //makes it so the next time we start this sequence we begin
                //from the first animation, not from wherever we left off when we
                //switched to another animation
                this.renderable.setAnimationFrame();
            }
        }

        else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")) {
             if(!this.renderable.isCurrentAnimation("walk")){
                 this.renderable.setCurrentAnimation("walk");
        }
        }else if(!this.renderable.isCurrentAnimation("attack")){
           this.renderable.setCurrentAnimation("idle");
        }

        me.collision.check(this, true, this.collideHandler.bind(this), true);
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    loseHealth: function(damage){
    	this.health = this.health - damage;
    	console.log(this.health);
    },

    collideHandler: function(response){
        if(response.b.type==='EnemyBaseEntity'){
            var ydif = this.pos.y - response.b.pos.y;
            var xdif = this.pos.x - response.b.pos.x;

            if(ydif<-40 && xdif< 70 && xdif>-35){
                this.body.falling = false;
                this.body.vel.y = -1;
            }
            else if(xdif>-35 && this.facing==='right' && (xdif<0)){
                this.body.vel.x = 0;
                this.pos.x = this.pos.x -1;
            }else if(xdif<70 && this.facing==='left' && (xdif>0)){
                this.body.vel.x = 0;
                this.pos.x = this.pos.x +1;
            }

            if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000){
                console.log("tower Hit");
                this.lastHit = this.now;
                response.b.loseHealth();
            }
        }
    }
});

game.PlayerBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "tower",
            width: 100,
            height: 100,
            spritewidth: "100",
            spriteheight: "100",
            getShape: function(){
                return (new me.Rect(0, 0, 100, 70)).toPolygon();
            }
        }]);
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);
        this.type = "PlayerBase";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },

    update:function(delta){
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    loseHealth: function(damage){
    	this.health = this.health - damage;
    },

    onCollision: function(){

    }

});

game.EnemyBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "tower",
            width: 100,
            height: 100,
            spritewidth: "100",
            spriteheight: "100",
            getShape: function(){
                return (new me.Rect(0, 0, 100, 70)).toPolygon();
            }
        }]);
        this.broken = false;
        this.health = 10;
        this.alwaysUpdate = true;
        this.body.onCollision = this.onCollision.bind(this);
        this.type = "EnemyBaseEntity";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },

    update:function(delta){
        if(this.health<=0){
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);

        this._super(me.Entity, "update", [delta]);
        return true;
    },

    onCollision: function(){

    },

    loseHealth: function(){
        this.health--;
    }

});

game.EnemyCreep = me.Entity.extend({
    init: function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
            image: "creep1",
            width: 32,
            height: 64,
            spritewidth: "32",
            spriteheight: "64",
            getShape: function(){
                return (new me.Rect(0, 0, 32, 64)).toPolygon();
            }
        }]);
        this.health = 10;
        this.alwaysUpdate = true;
        //this.attacking is letting us know if the creep is currently attacking
        this.attacking = false;
        //keeps track of when our creep last attacked anything
        this.lastAttacking = new Date().getTime();
        //keep track of the lasttime or creep hit anything
        this.lastHit = new Date().getTime();
        this.now = new Date().getTime();
        this.body.setVelocity(3, 20);

        this.type = "EnemyCreep";

        this.renderable.addAnimation("walk", [3, 4, 5], 80);
        this.renderable.setCurrentAnimation("walk");

    },

    update: function(delta){
    	this.now = new Date().getTime();

    	this.body.vel.x -= this.body.accel.x * me.timer.tick;

    	me.collision.check(this, true, this.collideHandler.bind(this), true);

    	this.body.update(delta);

        this._super(me.Entity, "update", [delta]);

        return true;
    },

    collideHandler: function(response){
    	if(response.b.type==='PlayerBase'){
    		this.attacking=true;
    		//this.lastAttacking=this.now;
    		this.body.vel.x = 0;
    		//keeps moving the creep to the right to maintain its position
    		this.pos.x = this.pos.x + 1;
    		//checks that is has been at least 1 second since this creep hit a base
    		if((this.now-this.lastHit >= 1000)){
    			//updates the lastHit timer
    			this.lastHit = this.now;
    			//makes the player base call its longHealth function and passes it a
    			//damage of 1
    			repsonse.b.loseHealth(1);
    		}
    	}else if (response.b.type==='PlayerEntity'){
    		var xdif = this.pos.x = response.b.pos.x;

    		this.attacking=true;
    		//this.lastAttacking=this.now;
    		if(xdif>0){
    			this.pos.x = this.pos.x + 1;
    			this.body.vel.x = 0;
    			//keeps moving the creep to the right to maintain its position
    		}
    		//checks that is has been at least 1 second since this creep hit something
    		if((this.now-this.lastHit >= 1000) && xdif>0){
    			//updates the lastHit timer
    			this.lastHit = this.now;
    			//makes the player call its longHealth function and passes it a
    			//damage of 1
    			repsonse.b.loseHealth(1);
    	}
    }

   }
});

game.GameManager = Object.extend({
    init: function(x, y, settings){
        this.now = new Date().getTime();
        this.lastCreep = new Date().getTime();

        this.alwaysUpdate = true;
    },

    update: function(){
        this.now = new Date().getTime();

        if(Math.round(this.now/1000)%10 ===0 && (this.now - this.lastCreep >= 1000)){
            this.lastCreep = this.now;
            var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
            me.game.world.addChild(creepe, 5);
        }

        return true;
    }
});

<|endoftext|>


<commit_before>angular.module('daos', ['firebase.utils'])
    .service('swapDao', ['fbutil', '$firebaseObject', function (fbutil, $firebaseObject) {

        var globalSwapRequestRef = fbutil.ref('/swapRequests');
        var globalOffersRef = fbutil.ref('/offers');
        var  bartersGlobalref = fbutil.ref('/barters');

        /**
         * @typedef {String} OfferId
         *
         * @typedef {Offer} SwapOffer
         * @property {number} amount
         * @property {string} unit
         * @property {OfferId} $id
         *
         *
         * @typedef {Object} SwapRequest
         * @property {String} $id
         * @property {number} created
         * @property {string} offeredBy
         * @property {SwapOffer} payFor
         * @property {SwapOffer} payWith

         * @typedef {Object} Offer
         * @property {String} title
         * @property {String} description
         * @property {String[]} swapRequests

         */
        /**
         *
         * @param {FirebaseObject} slave
         * @param {FirebaseObject} index
         * @param {function} childCallback
         */

        /**
         *
         * @param {string} swapRequestId
         *
         */
        this.findSwapRequestById = function (swapRequestId) {
            /*SwapRequest*/
            var swapRequest =$firebaseObject(globalSwapRequestRef.child(swapRequestId));

            swapRequest.$loaded().then(function () {
                var offerId = swapRequest.payFor.offerId;
                swapRequest.payFor.offer = $firebaseObject(globalOffersRef.child(offerId));
                offerId = swapRequest.payWith.offerId;
                swapRequest.payWith.offer = $firebaseObject(globalOffersRef.child(offerId));
            });
            return swapRequest;

        };//findSwapRequestById
        var findSwapRequestById = this.findSwapRequestById;

        /**
         * @param {Offer} offer
         */
        this.findSwapRequestsByOffer = function (offer) {
            var result = {};
            angular.forEach(offer.swapRequests, function (swapRequestProperties, swapRequestId) {
                var swapRequest = findSwapRequestById(swapRequestId);
                result[swapRequest.$id] = swapRequest;

            });
            return result;
        };//findSwapRequestsByOffer
        //..

        function dropReference(ref, key) {
            var firebaseRef = ref.child(key);
            firebaseRef.set(null, function (error) {
                if (error) {
                    alert(error);
                }
                else {
                    console.log("droped reference to " + key + " firebaseRef, " + firebaseRef.toString());
                }
            });
        }

        /**
         *
         * @param {SwapRequest} swapRequest
         */
        this.dropSwapRequest = function (swapRequest) {
            dropReference(globalSwapRequestRef, swapRequest.$id);
            dropReference(globalOffersRef.child(swapRequest.payFor.offerId).child("swapRequests"), swapRequest.$id);
            dropReference(globalOffersRef.child(swapRequest.payWith.offerId).child("swapRequests"), swapRequest.$id);
        };

        /**
         *
         * @param {OfferId} offerId
         */
        this.dropOffer = function(offerId,userName){
            globalOffersRef.child(offerId).once("value", function (offerSnap) {
                var offer = offerSnap.val();
                if (offer != null) {

                    var offeredBy = offer.offeredBy;
                    angular.forEach(offer.swapRequests, function (swapRequestProperties, swapRequestId) {
                        dropReference(globalSwapRequestRef, swapRequestId);

                    })
                }

            });

            var ref = bartersGlobalref.child(userName).child("offers");
            dropReference(ref,offerId);
            dropReference(globalOffersRef, offerId);

        }

    }])
;

<commit_msg>changed version and app name<commit_after>angular.module('daos', ['firebase.utils'])
    .service('swapDao', ['fbutil', '$firebaseObject', function (fbutil, $firebaseObject) {

        var globalSwapRequestRef = fbutil.ref('/swapRequests');
        var globalOffersRef = fbutil.ref('/offers');
        var  bartersGlobalref = fbutil.ref('/barters');

        /**
         * @typedef {String} OfferId
         *
         * @typedef {Offer} SwapOffer
         * @property {number} amount
         * @property {string} unit
         * @property {OfferId} $id
         *
         *
         * @typedef {Object} SwapRequest
         * @property {String} $id
         * @property {number} created
         * @property {string} offeredBy
         * @property {SwapOffer} payFor
         * @property {SwapOffer} payWith

         * @typedef {Object} Offer
         * @property {String} title
         * @property {String} description
         * @property {String[]} swapRequests

         */
        /**
         *
         * @param {FirebaseObject} slave
         * @param {FirebaseObject} index
         * @param {function} childCallback
         */

        /**
         * gets the (first) key in a structure:
         * {
         *   9387219734321431924 : {}
         * }
         * @param object
         */
        function getKey(object){
            var keys = Object.keys(object);
            if (keys && keys[0]){
                return keys[0];
            }
            else {
                return null;
            }

        }

        /**
         *
         * @param {string} swapRequestId
         *
         */
        this.findSwapRequestById = function (swapRequestId) {
            /*SwapRequest*/
            var swapRequest =$firebaseObject(globalSwapRequestRef.child(swapRequestId));

            swapRequest.$loaded().then(function () {
                var offerId = getKey(swapRequest.payFor);
                swapRequest.payFor.offer = $firebaseObject(globalOffersRef.child(offerId));
                swapRequest.payFor.swap = swapRequest.payFor[offerId];

                offerId = getKey(swapRequest.payWith);
                swapRequest.payWith.offer = $firebaseObject(globalOffersRef.child(offerId));
            });
            return swapRequest;

        };//findSwapRequestById
        var findSwapRequestById = this.findSwapRequestById;

        /**
         * @param {Offer} offer
         */
        this.findSwapRequestsByOffer = function (offer) {
            var result = {};
            angular.forEach(offer.swapRequests, function (swapRequestProperties, swapRequestId) {
                var swapRequest = findSwapRequestById(swapRequestId);
                result[swapRequest.$id] = swapRequest;

            });
            return result;
        };//findSwapRequestsByOffer
        //..

        function dropReference(ref, key) {
            var firebaseRef = ref.child(key);
            firebaseRef.set(null, function (error) {
                if (error) {
                    alert(error);
                }
                else {
                    console.log("droped reference to " + key + " firebaseRef, " + firebaseRef.toString());
                }
            });
        }

        /**
         *
         * @param {SwapRequest} swapRequest
         */
        this.dropSwapRequest = function (swapRequest) {
            dropReference(globalSwapRequestRef, swapRequest.$id);
            dropReference(globalOffersRef.child(swapRequest.payFor.$id).child("swapRequests"), swapRequest.$id);
            dropReference(globalOffersRef.child(swapRequest.payWith.$id).child("swapRequests"), swapRequest.$id);
        };

        /**
         *
         * @param {OfferId} offerId
         */
        this.dropOffer = function(offerId,userName){
            globalOffersRef.child(offerId).once("value", function (offerSnap) {
                var offer = offerSnap.val();
                if (offer != null) {

                    var offeredBy = offer.offeredBy;
                    angular.forEach(offer.swapRequests, function (swapRequestProperties, swapRequestId) {
                        dropReference(globalSwapRequestRef, swapRequestId);

                    })
                }

            });

            var ref = bartersGlobalref.child(userName).child("offers");
            dropReference(ref,offerId);
            dropReference(globalOffersRef, offerId);

        }

    }])
;

<|endoftext|>


<commit_before>// this class creates player on the playscreen; spritewidth and spriteheight sets size of player.
game.PlayerEntity = me.Entity.extend({
	init: function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			image: "player",
			width: 64,
			height: 64,
			spritewidth: "64",
			spriteheight: "64",
			getShape: function(){
				// height and width size of rectangle where player is located
				return(new me.Rect(0,0,64,64)).toPolygon();
			}
		}]);

		this.type = "PlayerEntity";

		// sets health of player
		this.health = game.data.playerHealth;

		// sets were player is at
		this.body.setVelocity(game.data.playerMoveSpeed, 20);

		//Keeps track of which direction your character is going
		this.facing = "right";

		// variables that help us tell when the last attack on base happend
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.dead = false;
		this.attack = game.data.playerAttack;
		this.lastAttack = new Date().getTime();

		// screen follows player everywhere(x and y axis)
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

		// animation when standing
		// renderable is class melonJS has made for animation
		this.renderable.addAnimation("idle", [78]);

		// animation when walking
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);

		//animation for when player is attacking
		this.renderable.addAnimation("attack", [65, 66, 67,68, 69, 70, 71, 72], 80);

		// animation that player starts with
		this.renderable.setCurrentAnimation("idle");
	},

	update: function(delta){
		// keeps our timer up to date
		this.now = new Date().getTime();
		// kills the player
		if (this.health <= 0) {
			this.dead = true;
		}

		if (me.input.isKeyPressed("right")) {
			this.facing = "left";
			// adds to the position of my x by velocity define above in setVelocity() and multiplying it by me.timertick: makes the movemnt look smooth
			this.body.vel.x += this.body.accel.x * me.timer.tick;

			this.flipX(true);
		}
		else if (me.input.isKeyPressed("left")){
			this.facing = "left";
			// allows player to move left
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.flipX(false);
		}
		else{
			this.body.vel.x = 0;
		}

		// if/else statment allows player to jump by checking if the space key is pressed and if player isnt already jumping or falling
		if (me.input.isKeyPressed("jump") && !this.jumping && !this.falling) {
			this.jumping = true;
			this.body.vel.y -= this.body.accel.y * me.timer.tick;
			me.audio.play("stomp");
		};

		// if/else stament adds animation for attack when attack key is pressed
		if (me.input.isKeyPressed("attack")) {
			//checks if animation has happen so it wont repeat again
			if (!this.renderable.isCurrentAnimation("attack")) {
				//Sets the current animation to attack and once that is over and goes back to idle animation
				this.renderable.setCurrentAnimation("attack", "idle");
				// Makes it so that the next time we start this sequence we begin from the first animation, not wherever we lfet off when we switched to another animation
				//this.renderable.setCurrentAnimationFrame();
				me.audio.play("jump");

			}
		}
		else if (this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")) {
		// makes animation happend
			if (!this.renderable.isCurrentAnimation("walk")) {

				this.renderable.setCurrentAnimation("walk");
			}
		}
		else if (!this.renderable.isCurrentAnimation("attack")){
			this.renderable.setCurrentAnimation("idle");
		}

		me.collision.check(this, true, this.collideHandler.bind(this), true);

		// if dont update the player wont know what to do with code above
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		return true;
	},

	// function makes player lose health
	loseHealth: function(damage){
		this.health = this.health - damage;
	},

	collideHandler: function(response){
		if (response.b.type === 'EnemyBaseEntity') {
			var ydif = this.pos.y - response.b.pos.y;
			var xdif = this.pos.x - response.b.pos.x;

			//helps us not collided with the top, left, and right of the tower
			if (ydif<-40 && xdif<70 && xdif>-35) {
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			else if (xdif>-35 && this.facing==='right' && (xdif<0)) {
				this.body.vel.x = 0;
				// this.pos.x = this.pos.x -1;
			}
			else if (xdif<70 && this.facing==='left' && xdif>0) {
				this.body.vel.x = 0;
				// this.pos.x = this.pos.x +1;
			}

			// after 1000 miliseconds sets last hit to now and tower loses health
			if (this.renderable.isCurrentAnimation("attack")
				&& this.now-this.lastHit >= game.data.playerAttackTimer) {
				this.lastHit = this.now;
				response.b.loseHealth(game.data.playerAttack);
			}
		}
		else if (response.b.type === 'EnemyCreep') {
			var xdif = this.pos.x - response.b.pos.x;
			var ydif = this.pos.y - response.b.pos.y;
			if (xdif>0) {
				// this.pos.x = this.pos.x + 1;
				if (this.facing === "left") {
					this.body.vel.x = 0;
				}
			}
			else{
				// this.pos.x = this.pos.x - 1;
				if (this.facing === "right") {
					this.vel.x = 0;
				}
			}
			if (this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= game.data.playerAttackTimer && (Math.abs(ydif) <= 40) && ((xdif>0) && this.facing === "left") || ((xdif<0) && this.facing === "right")) {
				this.lastHit = this.now;

				// if the creeps health is less than our attack code in if statement
				if (response.b.loseHealth  <= game.data.playerAttack) {
					// adds one gold per creep kill
					game.data.gold += 1;
				}

				response.b.loseHealth(game.data.playerAttack);
			}
		}
	}

});

game.Player2 = me.Entity.extend({
// sets properties of enemy player
	init: function(x, y, settings){
		this._super(me.Entity, "init", [x, y, {
			image: "Player2",
			width: 100,
			height: 85,
			spritewidth: "100",
			spriteheight: "85",
			getShape: function(){
				return(new me.Rect(0, 0 ,100, 85)).toPolygon();
			}
		}]);

		// sets health of player starting at 10
		this.health = 10;
		// always keeps the enemy player updated
		this.alwaysUpdate = true;

		// this.attacking lets us know if thge enemy is currently attacking
		this.attacking = false;

		// keeps track of when our creep last attacked anything
		this.lastAttacking = new Date().getTime();

		// keeps track of the ;ast time our creep hit anything
		this.lastHit = new Date().getTime();

		// timer
		this.new = new Date().getTime();

		// sets were player is located
		this.body.setVelocity(3, 20);

		// sets type of player
		this.type = "Player2";

		// sets the animation of enemy player
		this.renderable.addAnimation("walk", [0, 1, 2, 3, 4], 80);
		this.renderable.setCurrentAnimation("walk");
	},

	update: function(delta){
		// Timer set to check collisons refreshes every single time
		this.now = new Date().getTime();

		// makes creeps move
		this.body.vel.x += this.body.accel.x * me.timer.tick;

		// checks of creep is colliding with player
		//me.collison.check(this, true, this.collideHandler.bind(this), true);

		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);

		return true;

	},

	collideHandler: function(response){
		if (response.b.type === 'EnemyBase') {
			this.attacking = true;
			// this.lastAttacking = this.now;
			this.body.vel.x = 0;
			// keeps creep moving to the right to maintain its position
			this.pos.x = this.pos.x + 1;
			// checks that it has been at least 1 second since this creep hit a base
			if ((this.now-this.lastHit >= 1000)) {
				// updates the lasthit timer
				this.lastHit = this.now;
				// makes the player base call its loseHealth function and passes at a damge of 1
				response.b.loseHealth(1);
			}
		}
		else if (response.b.type === 'EnemyCreep') {
			var xdif = this.pos.x - response.b.pos.x;

			this.attacking = true;

			// this.lastAttacking = this.now;

			if (xdif>0) {
			// Kepps moving the creep to the right to maintain its position
				this.pos.x = this.pos.x + 1;
				this.body.vel.x = 0;
			}
			// checks that it has been less then 1 second since it hit somehting
			if ((this.now-this.lastHit >= 1000) && xdif>0) {
				// updates the last hit time
				this.lastHit = this.now;
				// makes the player call its lose health function and passes it as a damage of one
				response.b.loseHealth(1);
			}
		}
	}

});

<commit_msg>video 30 Refactor init code and comments<commit_after>// this class creates player on the playscreen; spritewidth and spriteheight sets size of player.
game.PlayerEntity = me.Entity.extend({
	init: function(x, y, settings){
		this.setSuper();
		this.setPlayerTimers();
		this.setAttributes();

		this.type = "PlayerEntity";

		this.setFlags();

		// screen follows player everywhere(x and y axis)
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

		this.addAnimation();

		// animation that player starts with
		this.renderable.setCurrentAnimation("idle");
	},

	// has most of init function
	setSuper: function(){
		this._super(me.Entity, 'init', [x, y, {
			image: "player",
			width: 64,
			height: 64,
			spritewidth: "64",
			spriteheight: "64",
			getShape: function(){
				// height and width size of rectangle where player is located
				return(new me.Rect(0,0,64,64)).toPolygon();
			}
		}]);
	},

	// Fuction holds all of our timers
	setPlayerTimers: function(){
		// variables that help us tell when the last attack on base happend
		this.now = new Date().getTime();
		this.lastHit = this.now;

		// havent used this
		this.lastAttack = new Date().getTime();
	},

	// function holds things we editited from player
	setAttributes: function(){
		// sets health of player
		this.health = game.data.playerHealth;

		// sets were player is at
		this.body.setVelocity(game.data.playerMoveSpeed, 20)

		this.attack = game.data.playerAttack;
	},

	// function hold different animations
	addAnimation: function(){
		// animation when standing
		// renderable is class melonJS has made for animation
		this.renderable.addAnimation("idle", [78]);

		// animation when walking
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);

		//animation for when player is attacking
		this.renderable.addAnimation("attack", [65, 66, 67,68, 69, 70, 71, 72], 80);
	},

	setFlags: function(){
		//Keeps track of which direction your character is going
		this.facing = "right";

		this.dead = false;
	},

	update: function(delta){
		// keeps our timer up to date
		this.now = new Date().getTime();
		// kills the player
		if (this.health <= 0) {
			this.dead = true;
		}

		if (me.input.isKeyPressed("right")) {
			this.facing = "left";
			// adds to the position of my x by velocity define above in setVelocity() and multiplying it by me.timertick: makes the movemnt look smooth
			this.body.vel.x += this.body.accel.x * me.timer.tick;

			this.flipX(true);
		}
		else if (me.input.isKeyPressed("left")){
			this.facing = "left";
			// allows player to move left
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.flipX(false);
		}
		else{
			this.body.vel.x = 0;
		}

		// if/else statment allows player to jump by checking if the space key is pressed and if player isnt already jumping or falling
		if (me.input.isKeyPressed("jump") && !this.jumping && !this.falling) {
			this.jumping = true;
			this.body.vel.y -= this.body.accel.y * me.timer.tick;
			me.audio.play("stomp");
		};

		// if/else stament adds animation for attack when attack key is pressed
		if (me.input.isKeyPressed("attack")) {
			//checks if animation has happen so it wont repeat again
			if (!this.renderable.isCurrentAnimation("attack")) {
				//Sets the current animation to attack and once that is over and goes back to idle animation
				this.renderable.setCurrentAnimation("attack", "idle");
				// Makes it so that the next time we start this sequence we begin from the first animation, not wherever we lfet off when we switched to another animation
				//this.renderable.setCurrentAnimationFrame();
				me.audio.play("jump");

			}
		}
		else if (this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")) {
		// makes animation happend
			if (!this.renderable.isCurrentAnimation("walk")) {

				this.renderable.setCurrentAnimation("walk");
			}
		}
		else if (!this.renderable.isCurrentAnimation("attack")){
			this.renderable.setCurrentAnimation("idle");
		}

		me.collision.check(this, true, this.collideHandler.bind(this), true);

		// if dont update the player wont know what to do with code above
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		return true;
	},

	// function makes player lose health
	loseHealth: function(damage){
		this.health = this.health - damage;
	},

	collideHandler: function(response){
		if (response.b.type === 'EnemyBaseEntity') {
			var ydif = this.pos.y - response.b.pos.y;
			var xdif = this.pos.x - response.b.pos.x;

			//helps us not collided with the top, left, and right of the tower
			if (ydif<-40 && xdif<70 && xdif>-35) {
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			else if (xdif>-35 && this.facing==='right' && (xdif<0)) {
				this.body.vel.x = 0;
				// this.pos.x = this.pos.x -1;
			}
			else if (xdif<70 && this.facing==='left' && xdif>0) {
				this.body.vel.x = 0;
				// this.pos.x = this.pos.x +1;
			}

			// after 1000 miliseconds sets last hit to now and tower loses health
			if (this.renderable.isCurrentAnimation("attack")
				&& this.now-this.lastHit >= game.data.playerAttackTimer) {
				this.lastHit = this.now;
				response.b.loseHealth(game.data.playerAttack);
			}
		}
		else if (response.b.type === 'EnemyCreep') {
			var xdif = this.pos.x - response.b.pos.x;
			var ydif = this.pos.y - response.b.pos.y;
			if (xdif>0) {
				// this.pos.x = this.pos.x + 1;
				if (this.facing === "left") {
					this.body.vel.x = 0;
				}
			}
			else{
				// this.pos.x = this.pos.x - 1;
				if (this.facing === "right") {
					this.vel.x = 0;
				}
			}
			if (this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= game.data.playerAttackTimer && (Math.abs(ydif) <= 40) && ((xdif>0) && this.facing === "left") || ((xdif<0) && this.facing === "right")) {
				this.lastHit = this.now;

				// if the creeps health is less than our attack code in if statement
				if (response.b.loseHealth  <= game.data.playerAttack) {
					// adds one gold per creep kill
					game.data.gold += 1;
				}

				response.b.loseHealth(game.data.playerAttack);
			}
		}
	}

});

game.Player2 = me.Entity.extend({
// sets properties of enemy player
	init: function(x, y, settings){
		this._super(me.Entity, "init", [x, y, {
			image: "Player2",
			width: 100,
			height: 85,
			spritewidth: "100",
			spriteheight: "85",
			getShape: function(){
				return(new me.Rect(0, 0 ,100, 85)).toPolygon();
			}
		}]);

		// sets health of player starting at 10
		this.health = 10;
		// always keeps the enemy player updated
		this.alwaysUpdate = true;

		// this.attacking lets us know if thge enemy is currently attacking
		this.attacking = false;

		// keeps track of when our creep last attacked anything
		this.lastAttacking = new Date().getTime();

		// keeps track of the ;ast time our creep hit anything
		this.lastHit = new Date().getTime();

		// timer
		this.new = new Date().getTime();

		// sets were player is located
		this.body.setVelocity(3, 20);

		// sets type of player
		this.type = "Player2";

		// sets the animation of enemy player
		this.renderable.addAnimation("walk", [0, 1, 2, 3, 4], 80);
		this.renderable.setCurrentAnimation("walk");
	},

	update: function(delta){
		// Timer set to check collisons refreshes every single time
		this.now = new Date().getTime();

		// makes creeps move
		this.body.vel.x += this.body.accel.x * me.timer.tick;

		// checks of creep is colliding with player
		//me.collison.check(this, true, this.collideHandler.bind(this), true);

		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);

		return true;

	},

	collideHandler: function(response){
		if (response.b.type === 'EnemyBase') {
			this.attacking = true;
			// this.lastAttacking = this.now;
			this.body.vel.x = 0;
			// keeps creep moving to the right to maintain its position
			this.pos.x = this.pos.x + 1;
			// checks that it has been at least 1 second since this creep hit a base
			if ((this.now-this.lastHit >= 1000)) {
				// updates the lasthit timer
				this.lastHit = this.now;
				// makes the player base call its loseHealth function and passes at a damge of 1
				response.b.loseHealth(1);
			}
		}
		else if (response.b.type === 'EnemyCreep') {
			var xdif = this.pos.x - response.b.pos.x;

			this.attacking = true;

			// this.lastAttacking = this.now;

			if (xdif>0) {
			// Kepps moving the creep to the right to maintain its position
				this.pos.x = this.pos.x + 1;
				this.body.vel.x = 0;
			}
			// checks that it has been less then 1 second since it hit somehting
			if ((this.now-this.lastHit >= 1000) && xdif>0) {
				// updates the last hit time
				this.lastHit = this.now;
				// makes the player call its lose health function and passes it as a damage of one
				response.b.loseHealth(1);
			}
		}
	}

});

<|endoftext|>


<commit_before>game.PlayerEntity = me.Entity.extend({
	init:function(x, y, settings){  //our constructor function
		this._super(me.Entity, 'init', [x, y, { //reaching into the constructor of Entity
			image: "player",  // "player" in resources.js
			width: 64, //how much space should they reserve for the sprite
			height: 64,
			spritewidth: "64", //width and height of sprite is 64
			spriteheight: "64", //width, height, spritewidth, spriteheight almost always same number
			getShape: function() {
				return(new me.Rect(0, 0, 64, 64)).toPolygon(); //return a rectangle of what "player" can walk into
				// 0's are top corners and 64's are the numbers representing the width of the height of the box we're using
			}
		}]);
		this.type = "PlayerEntity";
		this.health = 20;
		this.body.setVelocity(5, 20); //our character moves 5 units to the right
		this.facing = "right"; //when our character spawns, our character will face right
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.lastAttack = new Date().getTime();
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH); //code makes it so that the camera position stays on our character
		this.renderable.addAnimation("idle", [78]); //sets idle animation to number 78
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80); //walking uses animations 117-125 and 80 milliseconds through each frame
		this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80); //attack animation uses animations 65-72 and 80 milliseconds through each frame

		this.renderable.setCurrentAnimation("idle"); //sets our starting animation as idle

	},

	update: function(delta) {
		this.now = new Date().getTime();
		if(me.input.isKeyPressed("right")) {
			//adds to the position of my x by the velocity defined above in
			//setVelocity() and multiplying it by me.timer.tick.
			//me.timer.tick makes the movement look smooth
			this.body.vel.x += this.body.accel.x * me.timer.tick;
			this.facing = "right"; //when the right key is inputed, our character will face the right side and then move to the right.
			this.flipX(true); //flips our walking animation this is facing the left to the right instead
		}
		else if(me.input.isKeyPressed("left")) {
			this.facing = "left"; //when the left key is inputed, our character will face left and then move left all in one go
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.flipX(false); //our animation already is facing left so it won't flip
		}
		else {
			this.body.vel.x = 0;
		}

		if(me.input.isKeyPressed("jump") && !this.jumping && !this.falling) { //if we click jump and if we're not jumping or falling
			this.body.jumping = true; //our character will jump
			this.body.vel.y -= this.body.accel.y * me.timer.tick;
		}

		if(me.input.isKeyPressed("attack")) { //attack inputted
			if(!this.renderable.isCurrentAnimation("attack")) { //current animation is not attack
				this.renderable.setCurrentAnimation("attack", "idle"); //sets our animation to run our attack animation then turns it back to idle
				this.renderable.setAnimationFrame(); //makes it so that the next time we start the animation, it starts from the first animation and not wherever we left off when we switched to another animation
			}
		}

		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack") ) { //only goes to walk animation if hes moving
			if(!this.renderable.isCurrentAnimation("walk")) { //don't start walking animation if you are already walking
			this.renderable.setCurrentAnimation("walk");
			}
		}
		else if (!this.renderable.isCurrentAnimation("attack")) {
			this.renderable.setCurrentAnimation("idle"); //if we're not walking, our animation would b idle
		}

		me.collision.check(this, true, this.collideHandler.bind(this), true);
		this.body.update(delta) ; //animation is updating on the fly

		this._super(me.Entity, "update", [delta]); //updates our animations gfor me.Entity
		return true;
	},

	loseHealth: function(damage) {
		this.health = this.health - damage;
		console.log(this.health);
	},

	collideHandler: function(response){
		if(response.b.type==='EnemyBaseEntity') { //checks if we're colliding with 'EnemyBaseEntity'
			var ydif = this.pos.y - response.b.pos.y; //represents difference between player y position and the enemy base y position
			var xdif = this.pos.x - response.b.pos.x; //represents difference between player x position and the enemy base x position

			if(ydif<-40 && xdif<70 && xdif>-35){
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			else if(xdif>-35 && this.facing==='right' && (xdif<0)) { //if xdif is greater than -35 and facing right (xdif is less than 0)
				this.body.vel.x = 0;
				this.pos.x = this.pos.x -1;
			}
			else if(xdif<70 && this.facing==='left' && xdif>0) { //if ydif is less than 70 and facing left (xdif greater than 0)
				this.body.vel.x = 0;
				this.pos.x = this.pos.x +1;
			}

			if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000) {
				this.lastHit = this.now;
				response.b.loseHealth();
			}
		}
		else if(response.b.type==='EnemyCreep') {
			var xdif = this.pos.x - response.b.pos.x;
			var ydif = this.pos.y - response.b.pos.y;

			if (xdif>0) {
				this.pos.x = this.pos.x +1;
				if(this.facing==="left") {
					this.body.vel.x = 0;
				}
			}
			else{
				this.pos.x = this.pos.x - 1;
				if(this.facing==="right") {
					this.body.vel.x = 0;
				}
			}
			if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000
			&& (Math.abs(ydif) <=40) &&
			((xdif>0) && this.facing==="left") || ((xdif<0) && this.facing==="right") ){
				this.lastHit = this.now;
				response.b.loseHealth(1);
			}
		}
	}
});

game.PlayerBaseEntity = me.Entity.extend({
	init : function(x, y, settings) { //same like PlayerEntity above for our constructor function
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height:100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function() {
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}
		}]);
		this.broken = false; //tower has not been destroyed
		this.health = 10; //sets its hp to 10
		this.alwaysUpdate = true; //always updates even if its not on screen
		this.body.onCollision = this.onCollision.bind(this); //if you can colide with turret/tower
		this.type = "PlayerBase"; //type for other collisions so we can check what we're running into when we're hitting otherstuff

		this.renderable.addAnimation("idle", [0]); //animation number 0 is our idle animation
		this.renderable.addAnimation("broken", [1]); //animation number 1 is our broken tower animation
		this.renderable.setCurrentAnimation("idle"); //sets the current/starting animation to our idle animation stated above

	},

	update:function(delta) { //delta represents time from last update
		if(this.health<=0) { //if health is less than or equal to 0, then our turret is dead
			this.broken = true;
			this.renderable.setCurrentAnimation("broken"); //if our tower is dead, then animation turns from "idle" to "broken"
		}
		this.body.update(delta); //updates(herpderp)

		this._super(me.Entity, "update", [delta]); //call super in any update function and pass it as a update function then re
		return true;
	},

	loseHealth: function(damage) {
		this.health = this.health - damage;
	},

	onCollision: function() {

	}

});

game.EnemyBaseEntity = me.Entity.extend({ //same freakin thing as the player base entity
	init : function(x, y, settings) {
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height:100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function() {
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}
		}]);
		this.broken = false;
		this.health = 10;
		this.alwaysUpdate = true;
		this.body.onCollision = this.onCollision.bind(this);

		this.type = "EnemyBaseEntity";

		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");

	},

	update:function(delta) {
		if(this.health<=0) {
			this.broken = true;
			this.renderable.setCurrentAnimation("broken");
		}
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		return true;
	},

	onCollision: function() {

	},

	loseHealth: function() {
		this.health--;
	}

});

game.EnemyCreep = me.Entity.extend({
	init : function(x, y, settings) {
		this._super(me.Entity, 'init', [x, y, {
			image: "creep1",
			width: 32,
			height: 64,
			spritewidth: "32",
			spriteheight: "64",
			getShape: function() {
				return (new me.Rect(0, 0, 32, 64)).toPolygon();
			}
		}]);
		this.health = 10;
		this.alwaysUpdate = true;
		this.attacking  = false;
		this.lastHit = new Date().getTime();
		this.now = new Date().getTime();
		this.body.setVelocity(3, 20);

		this.type = "EnemyCreep";

		this.renderable.addAnimation("walk", [3, 4, 5], 80);
		this.renderable.setCurrentAnimation("walk");
	},

	loseHealth: function(damage) {
		this.health = this.health - damage;
	},

	update: function(delta) {
		console.log(this.health);
		if(this.health <= 0) {
			me.game.world.removeChild(this);
		}
		this.now = new Date().getTime();
		this.body.vel.x -= this.body.accel.x * me.timer.tick;
		me.collision.check(this, true, this.collideHandler.bind(this), true);
		this.body.update(delta);
		this._super(me.Entity, "update", [delta]);
		return true;

	},

	collideHandler: function(response) {
		if(response.b.type==='PlayerBase') {
			this.attacking=true;
			this.body.vel.x = 0;
			this.pos.x = this.pos.x + 1;
			if((this.now-this.lastHit >= 1000)) {
				this.lastHit = this.now;
				response.b.loseHealth(1);
			}
		}
		else if (response.b.type==='PlayerEntity') {
			var xdif = this.pos.x - response.b.pos.x;
			this.attacking=true;
			if(xdif>0) {
				this.pos.x = this.pos.x + 1;
				this.body.vel.x = 0;
			}
			if((this.now-this.lastHit >= 1000) && xdif>0) {
				this.lastHit = this.now;
				response.b.loseHealth(1);
			}
		}
	}
});

game.GameManager = Object.extend({
	init: function(x, y, settings) {
		this.now = new Date().getTime();
		this.lastCreep = new Date().getTime();

		this.alwaysUpdate = true;
	},

	update: function() {
		this.now = new Date().getTime();

		if(Math.round(this.now/1000)%10 ===0 && (this.now - this.lastCreep >= 1000)) {
			this.lastCreep = this.now;
			var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
			me.game.world.addChild(creepe, 5);
		}

		return true;
	}
})
<commit_msg>Video 23 ERROR FIXED(THANKS DAN!!!)<commit_after>game.PlayerEntity = me.Entity.extend({
	init:function(x, y, settings){  //our constructor function
		this._super(me.Entity, 'init', [x, y, { //reaching into the constructor of Entity
			image: "player",  // "player" in resources.js
			width: 64, //how much space should they reserve for the sprite
			height: 64,
			spritewidth: "64", //width and height of sprite is 64
			spriteheight: "64", //width, height, spritewidth, spriteheight almost always same number
			getShape: function() {
				return(new me.Rect(0, 0, 64, 64)).toPolygon(); //return a rectangle of what "player" can walk into
				// 0's are top corners and 64's are the numbers representing the width of the height of the box we're using
			}
		}]);
		this.type = "PlayerEntity";
		this.health = 20;
		this.body.setVelocity(5, 20); //our character moves 5 units to the right
		this.facing = "right"; //when our character spawns, our character will face right
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.lastAttack = new Date().getTime();
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH); //code makes it so that the camera position stays on our character
		this.renderable.addAnimation("idle", [78]); //sets idle animation to number 78
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80); //walking uses animations 117-125 and 80 milliseconds through each frame
		this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80); //attack animation uses animations 65-72 and 80 milliseconds through each frame

		this.renderable.setCurrentAnimation("idle"); //sets our starting animation as idle

	},

	update: function(delta) {
		this.now = new Date().getTime();
		if(me.input.isKeyPressed("right")) {
			//adds to the position of my x by the velocity defined above in
			//setVelocity() and multiplying it by me.timer.tick.
			//me.timer.tick makes the movement look smooth
			this.body.vel.x += this.body.accel.x * me.timer.tick;
			this.facing = "right"; //when the right key is inputed, our character will face the right side and then move to the right.
			this.flipX(true); //flips our walking animation this is facing the left to the right instead
		}
		else if(me.input.isKeyPressed("left")) {
			this.facing = "left"; //when the left key is inputed, our character will face left and then move left all in one go
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.flipX(false); //our animation already is facing left so it won't flip
		}
		else {
			this.body.vel.x = 0;
		}

		if(me.input.isKeyPressed("jump") && !this.jumping && !this.falling) { //if we click jump and if we're not jumping or falling
			this.body.jumping = true; //our character will jump
			this.body.vel.y -= this.body.accel.y * me.timer.tick;
		}

		if(me.input.isKeyPressed("attack")) { //attack inputted
			if(!this.renderable.isCurrentAnimation("attack")) { //current animation is not attack
				this.renderable.setCurrentAnimation("attack", "idle"); //sets our animation to run our attack animation then turns it back to idle
				this.renderable.setAnimationFrame(); //makes it so that the next time we start the animation, it starts from the first animation and not wherever we left off when we switched to another animation
			}
		}

		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack") ) { //only goes to walk animation if hes moving
			if(!this.renderable.isCurrentAnimation("walk")) { //don't start walking animation if you are already walking
			this.renderable.setCurrentAnimation("walk");
			}
		}
		else if (!this.renderable.isCurrentAnimation("attack")) {
			this.renderable.setCurrentAnimation("idle"); //if we're not walking, our animation would b idle
		}

		me.collision.check(this, true, this.collideHandler.bind(this), true);
		this.body.update(delta) ; //animation is updating on the fly

		this._super(me.Entity, "update", [delta]); //updates our animations gfor me.Entity
		return true;
	},

	loseHealth: function(damage) {
		console.log(this.health);
		this.health = this.health - damage;
	},

	collideHandler: function(response){
		if(response.b.type==='EnemyBaseEntity') { //checks if we're colliding with 'EnemyBaseEntity'
			var ydif = this.pos.y - response.b.pos.y; //represents difference between player y position and the enemy base y position
			var xdif = this.pos.x - response.b.pos.x; //represents difference between player x position and the enemy base x position

			if(ydif<-40 && xdif<70 && xdif>-35){
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			else if(xdif>-35 && this.facing==='right' && (xdif<0)) { //if xdif is greater than -35 and facing right (xdif is less than 0)
				this.body.vel.x = 0;
				this.pos.x = this.pos.x -1;
			}
			else if(xdif<70 && this.facing==='left' && xdif>0) { //if ydif is less than 70 and facing left (xdif greater than 0)
				this.body.vel.x = 0;
				this.pos.x = this.pos.x +1;
			}

			if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000) {
				this.lastHit = this.now;
				response.b.loseHealth();
			}
		}
		else if(response.b.type === 'EnemyCreep'){
			var xdif = this.pos.x - response.b.pos.x;
			var ydif = this.pos.y - response.b.pos.y;
			if (xdif>0) {
				this.pos.x = this.pos.x + 1;
				if(this.facing === 'left'){
					this.body.vel.x = 0;
				}
			}
			else{
				this.pos.x = this.pos.x - 1;
				if (this.facing === 'right') {
					this.body.vel.x = 0;
				};
			};
			if (this.renderable.isCurrentAnimation('attack') && this.now-this.lastHit >= 1000 && (Math.abs(ydif<=40) &&
				((xdif>0) && this.facing === 'left') || ((xdif < 0 ) && this.facing === 'right')
				)){
				this.lastHit = this.now;
				response.b.loseHealth(1);
			};
		}
	}
});

game.PlayerBaseEntity = me.Entity.extend({
	init : function(x, y, settings) { //same like PlayerEntity above for our constructor function
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height:100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function() {
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}
		}]);
		this.broken = false; //tower has not been destroyed
		this.health = 10; //sets its hp to 10
		this.alwaysUpdate = true; //always updates even if its not on screen
		this.body.onCollision = this.onCollision.bind(this); //if you can colide with turret/tower
		this.type = "PlayerBase"; //type for other collisions so we can check what we're running into when we're hitting otherstuff

		this.renderable.addAnimation("idle", [0]); //animation number 0 is our idle animation
		this.renderable.addAnimation("broken", [1]); //animation number 1 is our broken tower animation
		this.renderable.setCurrentAnimation("idle"); //sets the current/starting animation to our idle animation stated above

	},

	update:function(delta) { //delta represents time from last update
		if(this.health<=0) { //if health is less than or equal to 0, then our turret is dead
			this.broken = true;
			this.renderable.setCurrentAnimation("broken"); //if our tower is dead, then animation turns from "idle" to "broken"
		}
		this.body.update(delta); //updates(herpderp)

		this._super(me.Entity, "update", [delta]); //call super in any update function and pass it as a update function then re
		return true;
	},

	loseHealth: function(damage) {
		this.health = this.health - damage;
	},

	onCollision: function() {

	}

});

game.EnemyBaseEntity = me.Entity.extend({ //same freakin thing as the player base entity
	init : function(x, y, settings) {
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height:100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function() {
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}
		}]);
		this.broken = false;
		this.health = 10;
		this.alwaysUpdate = true;
		this.body.onCollision = this.onCollision.bind(this);

		this.type = "EnemyBaseEntity";

		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");

	},

	update:function(delta) {
		if(this.health<=0) {
			this.broken = true;
			this.renderable.setCurrentAnimation("broken");
		}
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		return true;
	},

	onCollision: function() {

	},

	loseHealth: function() {
		this.health--;
	}

});

game.EnemyCreep = me.Entity.extend({
	init : function(x, y, settings) {
		this._super(me.Entity, 'init', [x, y, {
			image: "creep1",
			width: 32,
			height: 64,
			spritewidth: "32",
			spriteheight: "64",
			getShape: function() {
				return (new me.Rect(0, 0, 32, 64)).toPolygon();
			}
		}]);
		this.health = 10;
		this.alwaysUpdate = true;
		this.attacking  = false;
		this.lastHit = new Date().getTime();
		this.now = new Date().getTime();
		this.body.setVelocity(3, 20);

		this.type = "EnemyCreep";

		this.renderable.addAnimation("walk", [3, 4, 5], 80);
		this.renderable.setCurrentAnimation("walk");
	},

	loseHealth: function(damage) {
		this.health = this.health - damage;
	},

	update: function(delta) {
		console.log(this.health);
		if(this.health <= 0) {
			me.game.world.removeChild(this);
		}
		this.now = new Date().getTime();
		this.body.vel.x -= this.body.accel.x * me.timer.tick;
		me.collision.check(this, true, this.collideHandler.bind(this), true);
		this.body.update(delta);
		this._super(me.Entity, "update", [delta]);
		return true;

	},

	collideHandler: function(response) {
		if(response.b.type==='PlayerBase') {
			this.attacking=true;
			this.body.vel.x = 0;
			this.pos.x = this.pos.x + 1;
			if((this.now-this.lastHit >= 1000)) {
				this.lastHit = this.now;
				response.b.loseHealth(1);
			}
		}
		else if (response.b.type==='PlayerEntity') {
			var xdif = this.pos.x - response.b.pos.x;
			this.attacking=true;
			if(xdif>0) {
				this.pos.x = this.pos.x + 1;
				this.body.vel.x = 0;
			}
			if((this.now-this.lastHit >= 1000) && xdif>0) {
				this.lastHit = this.now;
				response.b.loseHealth(1);
			}
		}
	}
});

game.GameManager = Object.extend({
	init: function(x, y, settings) {
		this.now = new Date().getTime();
		this.lastCreep = new Date().getTime();

		this.alwaysUpdate = true;
	},

	update: function() {
		this.now = new Date().getTime();

		if(Math.round(this.now/1000)%10 ===0 && (this.now - this.lastCreep >= 1000)) {
			this.lastCreep = this.now;
			var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
			me.game.world.addChild(creepe, 5);
		}

		return true;
	}
})
<|endoftext|>


<commit_before>'use strict';

var _ = require('lodash');
var async = require('async');

var mongoose = require('mongoose');
var Location = mongoose.model('Location');
var Visit = mongoose.model('Visit');

exports.location = function(req, res, next) {
    var location = new Location(_.assign(_.pick(req.body, 'name', 'type'), {
        coordinates: [req.body.lat, req.body.lng]
    }));

    // Create first visit at this location with the addLocation mission completed
    // TODO: this should probably go in the Location Model pre save or something, then one can also change FixtureCreator.location
    var visit = new Visit({
        person: req.user.id,
        location: location.id,
        completed: new Date(), // TODO: it should set this as default
        missions: [{
            type: 'addLocation',
            outcome: true
        }]
    });

    location.save(function(err) {
        if (err) {
            return next(err);
        }
        visit.save(function(err) {
            if (err) {
                return next(err);
            }
            return res.send(location);
        });
    });
};

exports.list = function(req, res, next) {
    Location.find(function(err, locations) {
        if (err) {
            return next(err);
        }

        async.each(locations,
            function(location, cb) {
                location.computeNextVisitBonusDate(req.user, cb);
            },
            function(err) {
                if (err) { return next(err); }
                locations = _.map(locations, function(l) {
                    return l.toApiObject(req.user);
                });
                return res.send(locations);
            });
    });
};
<commit_msg>Converting to API object before sending location after creation<commit_after>'use strict';

var _ = require('lodash');
var async = require('async');

var mongoose = require('mongoose');
var Location = mongoose.model('Location');
var Visit = mongoose.model('Visit');

exports.location = function(req, res, next) {
    var location = new Location(_.assign(_.pick(req.body, 'name', 'type'), {
        coordinates: [req.body.lat, req.body.lng]
    }));

    // Create first visit at this location with the addLocation mission completed
    // TODO: this should probably go in the Location Model pre save or something, then one can also change FixtureCreator.location
    var visit = new Visit({
        person: req.user.id,
        location: location.id,
        completed: new Date(), // TODO: it should set this as default
        missions: [{
            type: 'addLocation',
            outcome: true
        }]
    });

    location.save(function(err) {
        if (err) {
            return next(err);
        }
        visit.save(function(err) {
            if (err) {
                return next(err);
            }
            return res.send(location.toApiObject());
        });
    });
};

exports.list = function(req, res, next) {
    Location.find(function(err, locations) {
        if (err) {
            return next(err);
        }

        async.each(locations,
            function(location, cb) {
                location.computeNextVisitBonusDate(req.user, cb);
            },
            function(err) {
                if (err) { return next(err); }
                locations = _.map(locations, function(l) {
                    return l.toApiObject(req.user);
                });
                return res.send(locations);
            });
    });
};
<|endoftext|>