<commit_before>game.PlayerEntity = me.Entity.extend({
    init: function(x, y, settings) {
        // initializes shape and hit boxes of player.
        this.setSuper(x, y);
        // sets update and functions.
        this.setPlayerTimers();
        //sets atributes give nto player(movement, health, attack).
        this.setAttributes();
        // sets type to player entity.
        this.type = "PlayerEntity";
        // sets faces and attacks.
        this.setFlags();
        // allows screen to follow player.
        me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
        //  sets animations to player.
        this.addAnimation();
        // sets current animations to idle.
        this.renderable.setCurrentAnimation("idle");
    },

    setSuper: function(x, y) {
        this._super(me.Entity, 'init', [x, y, {
                image: "player",
                width: 25,
                height: 24,
                spritewidth: "25",
                spriteheight: "24",
                getShape: function() {
                    return(new me.Rect(0, 0, 25, 24)).toPolygon();
                }
            }]);
    },

    setPlayerTimers: function() {
        this.now = new Date().getTime();
        this.lastHit = this.now;
        this.lastSpear = this.now;
        this.lastAttack = new Date().getTime();
    },

    setAttributes: function() {
        this.health = game.data.playerHealth;
        this.body.setVelocity(game.data.playerMoveSpeed,  2);
        this.attack = game.data.playerAttack;
        this.body.gravity = 0;
    },

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

    addAnimation: function() {
        this.renderable.addAnimation("idle", [16]);
        this.renderable.addAnimation("walk", [38, 39], 80);
        this.renderable.addAnimation("walkup", [53, 54, 55], 80);
        this.renderable.addAnimation("walkdown", [17, 18], 80);
        this.renderable.addAnimation("attack", [23, 24, 25, 26, 27, 28], 80);
        this.renderable.addAnimation("attackdown", [1, 2, 3, 4, 5, 6, 7, 8], 80);
        this.renderable.addAnimation("attackup", [46, 47, 48, 49, 50, 51, 52], 80);
        this.renderable.addAnimation("death", [144, 146, 147, 148], 80);

    },

    update: function(delta) {
        // resets update.
        this.now = new Date().getTime();
        // check if player's health is below zero.
        this.dead = this.checkIfDead();
        // check for key pressings and movement.
        this.checkKeyPressesAndMoves();
        // check ability and keys.
        this.checkAbilityKeys();
        // sets animations and modes for player.
        this.setAnimation();
        // check for any collisions with other entities.
        me.collision.check(this, true, this.collideHandler.bind(this), true);
        this.body.update(delta);
        // update entity. return true.
        this._super(me.Entity, "update", [delta]);
        return true;
    },

    checkIfDead: function() {
        if (this.health <= 0) {
        this.renderable.setCurrentAnimation("death");
            return true;
        }
        return false;
    },

    checkKeyPressesAndMoves: function() {
        if (me.input.isKeyPressed("right")) {
            // check for right movement.
            this.moveRight();

        } else if (me.input.isKeyPressed("left")) {
            // check for left movement.
            this.moveLeft();
        } else if (me.input.isKeyPressed("up")) {
            // check for up movement.
            this.moveUp();

        } else if (me.input.isKeyPressed("down")) {
            // check for down movement.
            this.moveDown();
        } else {
            // if no movement, no velocity.
            this.body.vel.x = 0;
            this.body.vel.y = 0;
        }
        // check if attacking.
        this.attacking = me.input.isKeyPressed("attack");
        this.attacking = me.input.isKeyPressed("attackup");
        this.attacking = me.input.isKeyPressed("attackdown");

    },

    moveRight: function() {
        //Sets 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";
        this.flipX(false);
    },

    moveLeft: function() {
        //Sets 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 = "left";
        this.flipX(true);
    },

    moveUp: function() {
        //Sets 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.y -= this.body.accel.y * me.timer.tick;
        this.facing = "up";
        this.flipY(false);
    },

    moveDown: function() {
        //Sets 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.y += this.body.accel.y * me.timer.tick;
        this.facing = "down";
        this.flipY(false);
    },

    checkAbilityKeys: function() {
        if (me.input.isKeyPressed("skill1")) {
            //this.speedBurst();
        } else if (me.input.isKeyPressed("skill2")) {
            //this.eatCreep();
        } else if (me.input.isKeyPressed("skill3")) {
            this.throwSpear();
        }
    },

    throwSpear: function() {
        console.log(this.now + "this.now" + this.lastSpear + "lastSpear" + game.data.spearTimer + "SpearTimer" + game.data.ability3 + "skill3");
        if((this.now - this.lastSpear) >= game.data.spearTimer*1000 && game.data.skill3 > 0) {
            // creates Spear entity and updates spear.
            this.lastSpear = this.now;
            var spear = me.pool.pull("spear", this.pos.x, this.pos.y, {}, this.facing);
            me.game.world.addChild(spear, 10);
        }
    },

    loseHealth: function(damage) {
        // updates health to any damage.
        this.health = this.health - damage;

    },

    setAnimation: function() {
        if (this.attacking) {
            // Sets the current animation to attack and ocne done,
            // sets it back to idle.
            if (!this.renderable.isCurrentAnimation("attack")) {
                this.renderable.setCurrentAnimation("attack", "idle");
                // Makes sure that the nexxt time that we use this animation,
                // that we save the animation even if we swit
                this.renderable.setAnimationFrame();
            }
             else if (!this.renderable.isCurrentAnimation("attackup")) {
                this.renderable.setCurrentAnimation("attackup", "idle");
                // Makes sure that the nexxt time that we use this animation,
                // that we save the animation even if we swit
                this.renderable.setAnimationFrame();
            }
             else if (!this.renderable.isCurrentAnimation("attackdown")) {
                this.renderable.setCurrentAnimation("attackdown", "idle");
                // Makes sure that the nexxt time that we use this animation,
                // that we save the animation even if we swit
                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.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attackup")) {
            if (!this.renderable.isCurrentAnimation("walkup")) {
                this.renderable.setCurrentAnimation("walkup");

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

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

    collideHandler: function(response) {
        if (response.b.type === 'EnemyBaseEntity') {
            // checks for collision with enemy bases.
            this.collideWithEnemyBase(response);
        } else if (response.b.type === "EnemyCreep") {
            // checks for collision with enemy creeps.
            this.collideWithEnemyCreep(response);
        }

    },

    collideWithEnemyBase: function(response) {
        // checks x + y positions.
        var ydif = this.pos.y - response.b.pos.y;
        var xdif = this.pos.x - response.b.pos.x;
        // creates smaller hitbox and roof for enemy base.
        if (ydif < -40 && xdif < 70 && xdif > -35) {
            this.body.falling = false;
        } else if (xdif > -35 && this.facing === 'right' && (xdif < 0)) {
            this.body.vel.x = 0;
        } else if (xdif < 70 && this.facing === 'left' && (xdif > 0)) {
            this.body.vel.x = 0;
        }

        if (this.renderable.isCurrentAnimation("attack") && this.now - this.lastHit >= game.data.playerAttackTimer) {
            console.log("PlayerAttack: ", game.data.playerAttack);
            // If player is attacking, then damage to enemy base.
            this.lastHit = this.now;
            response.b.loseHealth(game.data.playerAttack);

        }
    },

    collideWithEnemyCreep: function(response) {
        // checks x + y positions.
        var xdif = this.pos.x - response.b.pos.x;
        var ydif = this.pos.y - response.b.pos.y;
        // stops movement of player.
        this.stopMovement(xdif);
        this.stopMovement2(ydif);
        // if attacking, attack enemy creep.
        if (this.checkAttack(xdif, ydif)) {
            this.hitCreep(response);
        };
         if (this.checkAttack2(xdif, ydif)) {
            this.hitCreep(response);
        };

    },

    stopMovement: function(xdif) {
        if (xdif > 0) {
            if (this.facing === "left") {
                // if attacking and facing left, stop player.
                this.body.vel.x = 0;
            }
        } else {
            if (this.facing === "right") {
                // if attacking and facing right, stop player.
                this.body.vel.x = 0;
            }
        }
    },

    stopMovement2: function(ydif) {
        if (ydif > 0) {
            if (this.facing === "up") {
                // if attacking and facing up, stop player.
                this.body.vel.y = 0;
            }
        } else {
            if (this.facing === "down") {
                // if attacking and facing down, stop player.
                this.body.vel.y = 0;
            }
        }
    },

    checkAttack: function(xdif, ydif) {
        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"))) {
            // if attacking, and attack timer true, and facing creep, then update hits.
            this.lastHit = this.now;
            // return true.
            return true;
        }
        // If not, return false.
        return false;
    },

    checkAttack2: function(xdif, ydif) {
        if (this.renderable.isCurrentAnimation("attack2") && this.now - this.lastHit >= game.data.playerAttackTimer
                && (Math.abs(ydif) <= 40) &&
                (((ydif > 0) && this.facing === "up") || ((ydif < 0) && this.facing === "down"))) {
            // if attacking, and attack timer true, and facing creep, then update hits.
            this.lastHit = this.now;
            // return true.
            return true;
        }
        // If not, return false.
        return false;
    },

    hitCreep: function(response) {
        if (response.b.health <= this.attack) {
            // if kill creep, earn +2 gold.
            game.data.gold += 2;
        }
        // creep lose health.
        response.b.loseHealth(game.data.playerAttack);
    }

});

<commit_msg>player<commit_after>game.PlayerEntity = me.Entity.extend({
    init: function(x, y, settings) {
        // initializes shape and hit boxes of player.
        this.setSuper(x, y);
        // sets update and functions.
        this.setPlayerTimers();
        //sets atributes give nto player(movement, health, attack).
        this.setAttributes();
        // sets type to player entity.
        this.type = "PlayerEntity";
        // sets faces and attacks.
        this.setFlags();
        // allows screen to follow player.
        me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
        //  sets animations to player.
        this.addAnimation();
        // sets current animations to idle.
        this.renderable.setCurrentAnimation("idle");
    },

    setSuper: function(x, y) {
        this._super(me.Entity, 'init', [x, y, {
                image: "player",
                width: 25,
                height: 24,
                spritewidth: "25",
                spriteheight: "24",
                getShape: function() {
                    return(new me.Rect(0, 0, 25, 24)).toPolygon();
                }
            }]);
    },

    setPlayerTimers: function() {
        this.now = new Date().getTime();
        this.lastHit = this.now;
        this.lastSpear = this.now;
        this.lastAttack = new Date().getTime();
    },

    setAttributes: function() {
        this.health = game.data.playerHealth;
        this.body.setVelocity(game.data.playerMoveSpeed,  2);
        this.attack = game.data.playerAttack;
        this.body.gravity = 0;
    },

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

    addAnimation: function() {
        this.renderable.addAnimation("idle", [16]);
        this.renderable.addAnimation("walk", [38, 39], 80);
        this.renderable.addAnimation("walkup", [53, 54, 55], 80);
        this.renderable.addAnimation("walkdown", [17, 18], 80);
        this.renderable.addAnimation("attack", [23, 24, 25, 26, 27, 28], 80);
        this.renderable.addAnimation("attackdown", [1, 2, 3, 4, 5, 6, 7, 8], 80);
        this.renderable.addAnimation("attackup", [46, 47, 48, 49, 50, 51, 52], 80);
        this.renderable.addAnimation("death", [144, 146, 147, 148], 80);

    },

    update: function(delta) {
        // resets update.
        this.now = new Date().getTime();
        // check if player's health is below zero.
        this.dead = this.checkIfDead();
        // check for key pressings and movement.
        this.checkKeyPressesAndMoves();
        // check ability and keys.
        this.checkAbilityKeys();
        // sets animations and modes for player.
        this.setAnimation();
        // check for any collisions with other entities.
        me.collision.check(this, true, this.collideHandler.bind(this), true);
        this.body.update(delta);
        // update entity. return true.
        this._super(me.Entity, "update", [delta]);
        return true;
    },

    checkIfDead: function() {
        if (this.health <= 0) {
        this.renderable.setCurrentAnimation("death");
            return true;
        }
        return false;
    },

    checkKeyPressesAndMoves: function() {
        if (me.input.isKeyPressed("right")) {
            // check for right movement.
            this.moveRight();

        } else if (me.input.isKeyPressed("left")) {
            // check for left movement.
            this.moveLeft();
        } else if (me.input.isKeyPressed("up")) {
            // check for up movement.
            this.moveUp();

        } else if (me.input.isKeyPressed("down")) {
            // check for down movement.
            this.moveDown();
        } else {
            // if no movement, no velocity.
            this.body.vel.x = 0;
            this.body.vel.y = 0;
        }
        // check if attacking.
        this.attacking = me.input.isKeyPressed("attack");
        this.attacking = me.input.isKeyPressed("attackup");
        this.attacking = me.input.isKeyPressed("attackdown");

    },

    moveRight: function() {
        //Sets 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";
        this.flipX(false);
    },

    moveLeft: function() {
        //Sets 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 = "left";
        this.flipX(true);
    },

    moveUp: function() {
        //Sets 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.y -= this.body.accel.y * me.timer.tick;
        this.facing = "up";
        this.flipY(false);
    },

    moveDown: function() {
        //Sets 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.y += this.body.accel.y * me.timer.tick;
        this.facing = "down";
        this.flipY(false);
    },

    checkAbilityKeys: function() {
        if (me.input.isKeyPressed("skill1")) {
            //this.speedBurst();
        } else if (me.input.isKeyPressed("skill2")) {
            //this.eatCreep();
        } else if (me.input.isKeyPressed("skill3")) {
            this.throwSpear();
        }
    },

    throwSpear: function() {
        console.log(this.now + "this.now" + this.lastSpear + "lastSpear" + game.data.spearTimer + "SpearTimer" + game.data.ability3 + "skill3");
        if((this.now - this.lastSpear) >= game.data.spearTimer*1000 && game.data.skill3 > 0) {
            // creates Spear entity and updates spear.
            this.lastSpear = this.now;
            var spear = me.pool.pull("spear", this.pos.x, this.pos.y, {}, this.facing);
            me.game.world.addChild(spear, 10);
        }
    },

    loseHealth: function(damage) {
        // updates health to any damage.
        this.health = this.health - damage;

    },

    setAnimation: function() {
        if (this.attacking) {
            // Sets the current animation to attack and ocne done,
            // sets it back to idle.
            if (!this.renderable.isCurrentAnimation("attack")) {
                this.renderable.setCurrentAnimation("attack", "idle");
                // Makes sure that the nexxt time that we use this animation,
                // that we save the animation even if we swit
                this.renderable.setAnimationFrame();
            }
             else if (!this.renderable.isCurrentAnimation("attackup")) {
                this.renderable.setCurrentAnimation("attackup", "idle");
                // Makes sure that the nexxt time that we use this animation,
                // that we save the animation even if we swit
                this.renderable.setAnimationFrame();
            }
             else if (!this.renderable.isCurrentAnimation("attackdown")) {
                this.renderable.setCurrentAnimation("attackdown", "idle");
                // Makes sure that the nexxt time that we use this animation,
                // that we save the animation even if we swit
                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.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attackup")) {
            if (!this.renderable.isCurrentAnimation("walkup")) {
                this.renderable.setCurrentAnimation("walkup");

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

            }
        }else if (!this.renderable.isCurrentAnimation("attack")) {
            this.renderable.setCurrentAnimation("idle");
        }
//        else if (!this.renderable.isCurrentAnimation("attackup")) {
//            this.renderable.setCurrentAnimation("idle");
//        }else if (!this.renderable.isCurrentAnimation("attackdown")) {
//            this.renderable.setCurrentAnimation("idle");
//        }
    },

    collideHandler: function(response) {
        if (response.b.type === 'EnemyBaseEntity') {
            // checks for collision with enemy bases.
            this.collideWithEnemyBase(response);
        } else if (response.b.type === "EnemyCreep") {
            // checks for collision with enemy creeps.
            this.collideWithEnemyCreep(response);
        }

    },

    collideWithEnemyBase: function(response) {
        // checks x + y positions.
        var ydif = this.pos.y - response.b.pos.y;
        var xdif = this.pos.x - response.b.pos.x;
        // creates smaller hitbox and roof for enemy base.
        if (ydif < -40 && xdif < 70 && xdif > -35) {
            this.body.falling = false;
        } else if (xdif > -35 && this.facing === 'right' && (xdif < 0)) {
            this.body.vel.x = 0;
        } else if (xdif < 70 && this.facing === 'left' && (xdif > 0)) {
            this.body.vel.x = 0;
        }

        if (this.renderable.isCurrentAnimation("attack") && this.now - this.lastHit >= game.data.playerAttackTimer) {
            console.log("PlayerAttack: ", game.data.playerAttack);
            // If player is attacking, then damage to enemy base.
            this.lastHit = this.now;
            response.b.loseHealth(game.data.playerAttack);

        }
    },

    collideWithEnemyCreep: function(response) {
        // checks x + y positions.
        var xdif = this.pos.x - response.b.pos.x;
        var ydif = this.pos.y - response.b.pos.y;
        // stops movement of player.
        this.stopMovement(xdif);
        this.stopMovement2(ydif);
        // if attacking, attack enemy creep.
        if (this.checkAttack(xdif, ydif)) {
            this.hitCreep(response);
        };
         if (this.checkAttack2(xdif, ydif)) {
            this.hitCreep(response);
        };

    },

    stopMovement: function(xdif) {
        if (xdif > 0) {
            if (this.facing === "left") {
                // if attacking and facing left, stop player.
                this.body.vel.x = 0;
            }
        } else {
            if (this.facing === "right") {
                // if attacking and facing right, stop player.
                this.body.vel.x = 0;
            }
        }
    },

    stopMovement2: function(ydif) {
        if (ydif > 0) {
            if (this.facing === "up") {
                // if attacking and facing up, stop player.
                this.body.vel.y = 0;
            }
        } else {
            if (this.facing === "down") {
                // if attacking and facing down, stop player.
                this.body.vel.y = 0;
            }
        }
    },

    checkAttack: function(xdif, ydif) {
        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"))) {
            // if attacking, and attack timer true, and facing creep, then update hits.
            this.lastHit = this.now;
            // return true.
            return true;
        }
        // If not, return false.
        return false;
    },

    checkAttack2: function(xdif, ydif) {
        if (this.renderable.isCurrentAnimation("attack2") && this.now - this.lastHit >= game.data.playerAttackTimer
                && (Math.abs(ydif) <= 40) &&
                (((ydif > 0) && this.facing === "up") || ((ydif < 0) && this.facing === "down"))) {
            // if attacking, and attack timer true, and facing creep, then update hits.
            this.lastHit = this.now;
            // return true.
            return true;
        }
        // If not, return false.
        return false;
    },

    hitCreep: function(response) {
        if (response.b.health <= this.attack) {
            // if kill creep, earn +2 gold.
            game.data.gold += 2;
        }
        // creep lose health.
        response.b.loseHealth(game.data.playerAttack);
    }

});

<|endoftext|>


<commit_before>
angular.module('mwFormBuilder').directive('mwFormBuilder', function () {

    return {
        replace: true,
        restrict: 'AE',
        scope: {
            formData: '=',
            readOnly: '=?',
            options: '=?',
            formStatus: '=?',
            onImageSelection: '&',
            api: '=?'
        },
        templateUrl: 'mw-form-builder.html',
        controllerAs: 'ctrl',
        bindToController: true,
        controller: function(mwFormUuid, MW_QUESTION_TYPES, mwFormBuilderOptions){
            var ctrl = this;
            if(!ctrl.formData.pages || !ctrl.formData.pages.length){
                ctrl.formData.pages = [];
                ctrl.formData.pages.push(createEmptyPage(1));
            }
            mwFormBuilderOptions.questionTypes = MW_QUESTION_TYPES;
            mwFormBuilderOptions.elementButtons = [];
            mwFormBuilderOptions.additionalSelects = [];

            if(ctrl.options){
                if(ctrl.options.questionTypes){
                    mwFormBuilderOptions.questionTypes = ctrl.options.questionTypes;
                }
                if(ctrl.options.additionalSelects){
                    mwFormBuilderOptions.additionalSelects = ctrl.options.additionalSelects;
                }
            }

            ctrl.addPage = function(){
                ctrl.formData.pages.push(createEmptyPage(ctrl.formData.pages.length+1));
            };

            function createEmptyPage(number){
                var defaultPageFlow = null;
                if(ctrl.possiblePageFlow){
                    defaultPageFlow = ctrl.possiblePageFlow[0];
                }

                return {
                    id: mwFormUuid.get(),
                    number: number,
                    name: null,
                    description: null,
                    pageFlow: defaultPageFlow,
                    elements: []
                };
            }

            function updatePageNumbers() {
                for(var i=0; i<ctrl.formData.pages.length; i++){
                    ctrl.formData.pages[i].number = i+1;
                }
                ctrl.updatePageFlow();
            }

            ctrl.addPageAfter=function(page){
                var index = ctrl.formData.pages.indexOf(page);
                var newIndex = index+1;
                var newPage = createEmptyPage(page.number+1);
                if(newIndex<ctrl.formData.pages.length){
                    ctrl.formData.pages.splice(newIndex,0, newPage);
                }else{
                    ctrl.formData.pages.push(newPage);
                }
                updatePageNumbers();
            };

            ctrl.moveDownPage= function(page){
                var fromIndex = ctrl.formData.pages.indexOf(page);
                var toIndex=fromIndex+1;
                if(toIndex<ctrl.formData.pages.length){
                    arrayMove(ctrl.formData.pages, fromIndex, toIndex);
                }
                updatePageNumbers();
            };

            ctrl.moveUpPage= function(page){
                var fromIndex = ctrl.formData.pages.indexOf(page);
                var toIndex=fromIndex-1;
                if(toIndex>=0){
                    arrayMove(ctrl.formData.pages, fromIndex, toIndex);
                }
                updatePageNumbers();
            };

            ctrl.removePage=function(page){
                var index = ctrl.formData.pages.indexOf(page);
                ctrl.formData.pages.splice(index,1);
                updatePageNumbers();
            };

            function arrayMove(arr, fromIndex, toIndex) {
                var element = arr[fromIndex];
                arr.splice(fromIndex, 1);
                arr.splice(toIndex, 0, element);
            }

            if(ctrl.api){
                ctrl.api.reset = function(){
                    for (var prop in ctrl.formData) {
                        if (ctrl.formData.hasOwnProperty(prop) && prop != 'pages') {
                            delete ctrl.formData[prop];
                        }
                    }

                    ctrl.formData.pages.length=0;
                    ctrl.formData.pages.push(createEmptyPage(1));

                }
            }

        },
        link: function (scope, ele, attrs){
            var ctrl = scope.ctrl;
            if(ctrl.formStatus){
                ctrl.formStatus.form = ctrl.form;
            }

            ctrl.possiblePageFlow = [];
            var defaultPageFlow = {
                nextPage: true,
                label: 'mwForm.pageFlow.goToNextPage'
            };
            ctrl.possiblePageFlow.push(defaultPageFlow);
            ctrl.isSamePageFlow = function (p1, p2){
                return (p1.page && p2.page &&  p1.page.id==p2.page.id) || p1.formSubmit && p2.formSubmit || p1.nextPage && p2.nextPage;
            };

            ctrl.updatePageFlow = function(){
                ctrl.possiblePageFlow.length=1;

                ctrl.formData.pages.forEach(function(page){

                    ctrl.possiblePageFlow.push({
                        page:{
                            id: page.id,
                            number: page.number
                        },
                        label: 'mwForm.pageFlow.goToPage'
                    });
                });

                ctrl.possiblePageFlow.push({
                    formSubmit:true,
                    label: 'mwForm.pageFlow.submitForm'
                });
                ctrl.formData.pages.forEach(function(page){
                    ctrl.possiblePageFlow.forEach(function(pageFlow){
                        if(page.pageFlow) {
                            if(ctrl.isSamePageFlow(pageFlow, page.pageFlow)){
                                page.pageFlow = pageFlow;
                            }
                        }else{
                            page.pageFlow = defaultPageFlow;
                        }

                        page.elements.forEach(function(element){
                            var question = element.question;
                            if(question && question.pageFlowModifier){
                                question.offeredAnswers.forEach(function(answer){
                                    if(answer.pageFlow){
                                        if(ctrl.isSamePageFlow(pageFlow, answer.pageFlow)){
                                            answer.pageFlow = pageFlow;
                                        }
                                    }
                                });
                            }

                        });
                    });
                });
            };

            scope.$watch('ctrl.formData.pages.length', function(newVal, oldVal){
                ctrl.updatePageFlow();
            });
        }
    };
});
<commit_msg>fixed up elementButtons.<commit_after>
angular.module('mwFormBuilder').directive('mwFormBuilder', function () {

    return {
        replace: true,
        restrict: 'AE',
        scope: {
            formData: '=',
            readOnly: '=?',
            options: '=?',
            formStatus: '=?',
            onImageSelection: '&',
            api: '=?'
        },
        templateUrl: 'mw-form-builder.html',
        controllerAs: 'ctrl',
        bindToController: true,
        controller: function(mwFormUuid, MW_QUESTION_TYPES, mwFormBuilderOptions){
            var ctrl = this;
            if(!ctrl.formData.pages || !ctrl.formData.pages.length){
                ctrl.formData.pages = [];
                ctrl.formData.pages.push(createEmptyPage(1));
            }
            mwFormBuilderOptions.questionTypes = MW_QUESTION_TYPES;
            mwFormBuilderOptions.elementButtons = [];
            mwFormBuilderOptions.additionalSelects = [];

            if(ctrl.options){
                if(ctrl.options.questionTypes){
                    mwFormBuilderOptions.questionTypes = ctrl.options.questionTypes;
                }
                if(ctrl.options.additionalSelects){
                    mwFormBuilderOptions.additionalSelects = ctrl.options.additionalSelects;
                }
                if(ctrl.options.elementButtons){
                    mwFormBuilderOptions.elementButtons = ctrl.options.elementButtons;
                }
            }

            ctrl.addPage = function(){
                ctrl.formData.pages.push(createEmptyPage(ctrl.formData.pages.length+1));
            };

            function createEmptyPage(number){
                var defaultPageFlow = null;
                if(ctrl.possiblePageFlow){
                    defaultPageFlow = ctrl.possiblePageFlow[0];
                }

                return {
                    id: mwFormUuid.get(),
                    number: number,
                    name: null,
                    description: null,
                    pageFlow: defaultPageFlow,
                    elements: []
                };
            }

            function updatePageNumbers() {
                for(var i=0; i<ctrl.formData.pages.length; i++){
                    ctrl.formData.pages[i].number = i+1;
                }
                ctrl.updatePageFlow();
            }

            ctrl.addPageAfter=function(page){
                var index = ctrl.formData.pages.indexOf(page);
                var newIndex = index+1;
                var newPage = createEmptyPage(page.number+1);
                if(newIndex<ctrl.formData.pages.length){
                    ctrl.formData.pages.splice(newIndex,0, newPage);
                }else{
                    ctrl.formData.pages.push(newPage);
                }
                updatePageNumbers();
            };

            ctrl.moveDownPage= function(page){
                var fromIndex = ctrl.formData.pages.indexOf(page);
                var toIndex=fromIndex+1;
                if(toIndex<ctrl.formData.pages.length){
                    arrayMove(ctrl.formData.pages, fromIndex, toIndex);
                }
                updatePageNumbers();
            };

            ctrl.moveUpPage= function(page){
                var fromIndex = ctrl.formData.pages.indexOf(page);
                var toIndex=fromIndex-1;
                if(toIndex>=0){
                    arrayMove(ctrl.formData.pages, fromIndex, toIndex);
                }
                updatePageNumbers();
            };

            ctrl.removePage=function(page){
                var index = ctrl.formData.pages.indexOf(page);
                ctrl.formData.pages.splice(index,1);
                updatePageNumbers();
            };

            function arrayMove(arr, fromIndex, toIndex) {
                var element = arr[fromIndex];
                arr.splice(fromIndex, 1);
                arr.splice(toIndex, 0, element);
            }

            if(ctrl.api){
                ctrl.api.reset = function(){
                    for (var prop in ctrl.formData) {
                        if (ctrl.formData.hasOwnProperty(prop) && prop != 'pages') {
                            delete ctrl.formData[prop];
                        }
                    }

                    ctrl.formData.pages.length=0;
                    ctrl.formData.pages.push(createEmptyPage(1));

                }
            }

        },
        link: function (scope, ele, attrs){
            var ctrl = scope.ctrl;
            if(ctrl.formStatus){
                ctrl.formStatus.form = ctrl.form;
            }

            ctrl.possiblePageFlow = [];
            var defaultPageFlow = {
                nextPage: true,
                label: 'mwForm.pageFlow.goToNextPage'
            };
            ctrl.possiblePageFlow.push(defaultPageFlow);
            ctrl.isSamePageFlow = function (p1, p2){
                return (p1.page && p2.page &&  p1.page.id==p2.page.id) || p1.formSubmit && p2.formSubmit || p1.nextPage && p2.nextPage;
            };

            ctrl.updatePageFlow = function(){
                ctrl.possiblePageFlow.length=1;

                ctrl.formData.pages.forEach(function(page){

                    ctrl.possiblePageFlow.push({
                        page:{
                            id: page.id,
                            number: page.number
                        },
                        label: 'mwForm.pageFlow.goToPage'
                    });
                });

                ctrl.possiblePageFlow.push({
                    formSubmit:true,
                    label: 'mwForm.pageFlow.submitForm'
                });
                ctrl.formData.pages.forEach(function(page){
                    ctrl.possiblePageFlow.forEach(function(pageFlow){
                        if(page.pageFlow) {
                            if(ctrl.isSamePageFlow(pageFlow, page.pageFlow)){
                                page.pageFlow = pageFlow;
                            }
                        }else{
                            page.pageFlow = defaultPageFlow;
                        }

                        page.elements.forEach(function(element){
                            var question = element.question;
                            if(question && question.pageFlowModifier){
                                question.offeredAnswers.forEach(function(answer){
                                    if(answer.pageFlow){
                                        if(ctrl.isSamePageFlow(pageFlow, answer.pageFlow)){
                                            answer.pageFlow = pageFlow;
                                        }
                                    }
                                });
                            }

                        });
                    });
                });
            };

            scope.$watch('ctrl.formData.pages.length', function(newVal, oldVal){
                ctrl.updatePageFlow();
            });
        }
    };
});
<|endoftext|>


<commit_before>// This is the code for the Orc 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() {
        		return(new me.Rect(0, 0, 64, 64)).toPolygon();
        	}
        }]);
    // Used for movement, this line sets the velocity in which the palyer moves across the map.
    this.body.setVelocity(5, 20);
    // Keeps track of which direction your player goes
    this.facing = "right";
    // Used to make the camera follow the player as he moves
    me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
    // These two lines add aniamtions for the Orc player from the spritesheet he is given in the data file. The numbers represent the different sprites used to imitate a walking animation.
    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 action sets the current animation to "idle" when the player is NOT moving.
    this.renderable.setCurrentAnimation("idle");

    },
   // These lines update the player if he ever moves across the map.
    update: function(delta) {
       if (me.input.isKeyPressed("right")) {
        //adds to the position of the x by the velocity defined above in
        //setVelocity() and multiplying it by me.timer.tick.
        //me.timer.tick makes the movement smooth
       	this.body.vel.x += this.body.accel.x * me.timer.tick;
       	this.facing = "right";
       	this.flipX(true);
     // This else function is used if the key is NOT being pressed, in that case, the velocity is returned to zero, and no movement is involved.
     // The else if states whether to move the character left or not
       } else if (me.input.isKeyPressed("left")) {
        this.body.vel.x -= this.body.accel.x * me.timer.tick;
        this.facing = "left";
        this.flipX(false);
       } else {
       	this.body.vel.x = 0;
       }
      // This if statement allows jumping and falling, albeit it being too floaty right now
        if(me.input.isKeyPressed("jump") && !this.jumping && !this.falling) {
        	this.jumping = true;
        	this.body.vel.y -= this.body.accel.y * me.timer.tick;
        }

      // These two if statements allow the attack action to go through with the animations
         if(me.input.isKeyPressed("attack")) {
       	  console.log("attack1");
       	  if(!this.renderable.isCurrentAnimation("attack")){
       	  	console.log("attack2");
       	  	this.renderable.setCurrentAnimation("attack");
       	  	this.renderable.setAnimationFrame();
       	  }
       }

       // These if and else statements declare when to use the "walk" and "idle" animations when the character is either moving or not.
       else if(this.body.vel.x !== 0) {
       if(!this.renderable.isCurrentAnimation("walk")) {
       	   this.renderable.setCurrentAnimation("walk");
        }
      } else{
      	this.renderable.setCurrentAnimation("idle");
      }
      if(me.input.isKeyPressed("attack")) {
       	  console.log("attack1");
       	  if(!this.renderable.isCurrentAnimation("attack")){
       	  	console.log("attack2");
       	  	this.renderable.setCurrentAnimation("attack");
       	  	this.renderable.setAnimationFrame();
       	  }
       }
     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(xdif>-35 && this.facing==='right') {
    			this.body.vel.x = 0;
    			this.pos.x = this.pos.x -1;
    		}
    	}
    }
});

// These two globs of text contain the data of the two towers in the game, for player and the enemy.
game.PlayerBaseEntity = me.Entity.extend({
     init: function(x, y, settings) {
     	// These variables state the height and width of the sprite.
       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();
              }
       }]);
       // Functions for when if and when the tower is broken, when the health is equal to zero, and the type states it as the player base.
       this.broken = false;
       this.health = 10;
       this.alwaysUpdate = true;
       this.body.onCollision = this.onCollision.bind(this);
       console.log("init");
       // Animations for the towers, same as the Enemy Base. Each one ("broken" and "idle") is used to state whether the tower is almost destroyed or fine.
       this.type = "PlayerBaseEntity";
       this.renderable.addAnimation("idle", [0]);
       this.renderable.addAnimation("broken", [1]);
       this.renderable.setCurrentAnimation("idle");

     },
     // Updating functions for when the health of the tower is zero
     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() {

     }
});
// Same thing as the Player Base entity
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);
       console.log("init");
       this.type = "EnemyBaseEntity";

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

     },

     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() {

     }
});<commit_msg>2/5/15 Changes 2:<commit_after>// This is the code for the Orc 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() {
        		return(new me.Rect(0, 0, 64, 64)).toPolygon();
        	}
        }]);
    // Used for movement, this line sets the velocity in which the palyer moves across the map.
    this.body.setVelocity(5, 20);
    // Keeps track of which direction your player goes
    this.facing = "right";
    // Used to make the camera follow the player as he moves
    me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
    // These two lines add aniamtions for the Orc player from the spritesheet he is given in the data file. The numbers represent the different sprites used to imitate a walking animation.
    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 action sets the current animation to "idle" when the player is NOT moving.
    this.renderable.setCurrentAnimation("idle");

    },
   // These lines update the player if he ever moves across the map.
    update: function(delta) {
       if (me.input.isKeyPressed("right")) {
        //adds to the position of the x by the velocity defined above in
        //setVelocity() and multiplying it by me.timer.tick.
        //me.timer.tick makes the movement smooth
       	this.body.vel.x += this.body.accel.x * me.timer.tick;
       	this.facing = "right";
       	this.flipX(true);
     // This else function is used if the key is NOT being pressed, in that case, the velocity is returned to zero, and no movement is involved.
     // The else if states whether to move the character left or not
       } else if (me.input.isKeyPressed("left")) {
        this.body.vel.x -= this.body.accel.x * me.timer.tick;
        this.facing = "left";
        this.flipX(false);
       } else {
       	this.body.vel.x = 0;
       }
      // This if statement allows jumping and falling, albeit it being too floaty right now
        if(me.input.isKeyPressed("jump") && !this.jumping && !this.falling) {
        	this.jumping = true;
        	this.body.vel.y -= this.body.accel.y * me.timer.tick;
        }

      // These two if statements allow the attack action to go through with the animations
         if(me.input.isKeyPressed("attack")) {
       	  console.log("attack1");
       	  if(!this.renderable.isCurrentAnimation("attack")){
       	  	console.log("attack2");
       	  	this.renderable.setCurrentAnimation("attack");
       	  	this.renderable.setAnimationFrame();
       	  }
       }

       // These if and else statements declare when to use the "walk" and "idle" animations when the character is either moving or not.
       else if(this.body.vel.x !== 0) {
       if(!this.renderable.isCurrentAnimation("walk")) {
       	   this.renderable.setCurrentAnimation("walk");
        }
      } else{
      	this.renderable.setCurrentAnimation("idle");
      }
      if(me.input.isKeyPressed("attack")) {
       	  console.log("attack1");
       	  if(!this.renderable.isCurrentAnimation("attack")){
       	  	console.log("attack2");
       	  	this.renderable.setCurrentAnimation("attack");
       	  	this.renderable.setAnimationFrame();
       	  }
       }
     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(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;
          }
    		}
    	}
    }
});

// These two globs of text contain the data of the two towers in the game, for player and the enemy.
game.PlayerBaseEntity = me.Entity.extend({
     init: function(x, y, settings) {
     	// These variables state the height and width of the sprite.
       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();
              }
       }]);
       // Functions for when if and when the tower is broken, when the health is equal to zero, and the type states it as the player base.
       this.broken = false;
       this.health = 10;
       this.alwaysUpdate = true;
       this.body.onCollision = this.onCollision.bind(this);
       console.log("init");
       // Animations for the towers, same as the Enemy Base. Each one ("broken" and "idle") is used to state whether the tower is almost destroyed or fine.
       this.type = "PlayerBaseEntity";
       this.renderable.addAnimation("idle", [0]);
       this.renderable.addAnimation("broken", [1]);
       this.renderable.setCurrentAnimation("idle");

     },
     // Updating functions for when the health of the tower is zero
     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() {

     }
});
// Same thing as the Player Base entity
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);
       console.log("init");
       this.type = "EnemyBaseEntity";

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

     },

     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() {

     }
});<|endoftext|>


<commit_before>game.PlayerEntity = me.Entity.extend({
	//initializes the function
	init: function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			// makes the image of the player appear.
			image: "player",
			//width of the space created
			width: 64,
			//height of the space created
			height: 64,
			//width for the player
			spritewidth: "64",
			//height for the player
			spriteheight: "64",
			//function of the shape of the screen
			getShape: function(){
				//creates a rectangle by 64 by 64
				// 0, 0 is the origin
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
			}
		}]);
		//sets the speen of the player when it moves to the right
		this.body.setVelocity(5, 20);

		this.renderable.addAnimation("idle", [78]);
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);

		this.renderable.setCurrentAnimation("idle");

	},
	// updates the fucntion
update: function(delta){
	if(me.input.isKeyPressed("right")){
		//adds ro the position of my x by the velocity defined in
		//set velocity() 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.flipX(true);
	}else{
		//for when the rigth arrow isnt clicked
		this.body.vel.x = 0;
	}
	if(this.body.vel.x !==0 ){
	if(!this.renderable.isCurrentAnimation("walk")){
		this.renderable.setCurrentAnimation("walk");

	}else{
		this.renderable.setCurrentAnimation("idle");
	}
}

	//updates the function to true
	this.body.update(delta);

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

});<commit_msg>Commented for video 10<commit_after>game.PlayerEntity = me.Entity.extend({
	//initializes the function
	init: function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			// makes the image of the player appear.
			image: "player",
			//width of the space created
			width: 64,
			//height of the space created
			height: 64,
			//width for the player
			spritewidth: "64",
			//height for the player
			spriteheight: "64",
			//function of the shape of the screen
			getShape: function(){
				//creates a rectangle by 64 by 64
				// 0, 0 is the origin
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
			}
		}]);
		//sets the speen of the player when it moves to the right
		this.body.setVelocity( 5, 20);
		//adds animation to standing starting position
		this.renderable.addAnimation("idle", [78]);
		//adds animation to walking
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
		//adds animation to non-moving position.
		this.renderable.setCurrentAnimation("idle");

	},
	// updates the fucntion
update: function(delta){
	if(me.input.isKeyPressed("right")){
		//adds ro the position of my x by the velocity defined in
		//set velocity() 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;
		//makes the character face the right
		this.flipX(true);
	}else{
		//for when the rigth arrow isnt clicked
		this.body.vel.x = 0;
	}

	//if the button is pushed then the character will walk but if not it will execute the else code.
	if(this.body.vel.x !==0 ){
	if(!this.renderable.isCurrentAnimation("walk")){
		this.renderable.setCurrentAnimation("walk");

	}else{
		//if not, make it stand still.
		this.renderable.setCurrentAnimation("idle");
	}
}

	//updates the function to true
	this.body.update(delta);
	//this line of code is to update the animation
	this._super(me.Entity, "update", [delta]);
	return true;
}

});<|endoftext|>


<commit_before>game.PlayerEntity = me.Entity.extend ({
	// this is a class
	init: function(x, y, settings) {
		// melon js uses this constructor on most things to help us set up
		this._super(me.Entity, "init", [x, y, {
			// this means reaching to the constructor of entites
			image: "player",
			width: 64,
			// tells the screen what amount of space to reserve
			height: 64,
			spritewidth: "64",
			// spritewidth/height are passing the main information tells what to do with the image
			spriteheight: "64",
			// these are setting the properties of the picture o the right demensions so the picture appears properly
			getShape: function() {
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
				// rect is what the guy can walk in to
			}
		}]);

		this.body.setVelocity(5, 20);
		// this a lso changes the y velocity of the character
		// this is the movement speed of the character
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
		// this says wherever the player goes the screen will follow him

		this.renderable.addAnimation("idle", [78]);
		// when the character is still this is what he will look like
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
		// this is going to be what the cahracter is going to change into

		this.renderable.setCurrentAnimation("idle");
		this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72] , 80);
	},

	update: function(delta) {
		// this function is what happens on the fly
		if(me.input.isKeyPressed("right")) {
			// set the position of my x by adding the velocity to find above in set veloctiy
			// setVeloctiy() and multiplying it by timer.tick
			// me.timer.tick makes the movement look smooth
			this.body.vel.x += this.body.accel.x * me.timer.tick;
			this.flipX(true);
			// this is flipping the animation around

		}
		else if(me.input.isKeyPressed("left")) {
			// set the position of my x by adding the velocity to find above in set veloctiy
			// setVeloctiy() and multiplying it by timer.tick
			// me.timer.tick makes the movement look smooth
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.flipX(false);
			// this is flipping the animation around

		}
		else {
			this.body.vel.x = 0;
		}
		if (me.input.isKeyPressed('jump')) {
     	 // make sure we are not already jumping or falling
      		if (!this.body.jumping && !this.body.falling) {
        // set current vel to the maximum defined value
        // gravity will then do the rest
        	this.body.vel.y = -this.body.maxVel.y * me.timer.tick;
        // set the jumping flag
        	this.body.jumping = true;
        }
    }
		if(me.input.isKeyPressed("attack")) {
			if (!this.renderable.isCurrentAnimation("attack")) {
				// set current animation to attack and once that is over
				// goes back to the idle animations
				this.renderable.setCurrentAnimation("attack", "idle");
				// makes it so that the next time we start this sequence we begin from the first animation not where we left off when we switched to another animation
				this.renderable.setAnimationFrame();
			}
		}
		if(this.body.vel.x !== 0)
		//allowing the guy to not immediately walk
		{
			if(!this.renderable.isCurrentAnimation("walk")) {
				this.renderable.setCurrentAnimation("walk");
				// this says it doesnt want to start the walk animation if it is already walking
			}
		}
		else if(!this.renderable.isCurrentAnimation("attack")) {
			this.renderable.setCurrentAnimation("idle");
		}
		// delta is the change in time that has happens
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		// this is updating the animations on the fly
		return true;
	}
});

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 shows the height of the bases
			}
}]);
		this.broken = false;
		// this is saying that the tower has not yet been touched/attacked
		this.health = 10;
		// starting energy for tower
		this.alwaysUpdate = true;
		// updates the screen whether or not we are not looking at it
		this.body.onCollision = this.onCollision.bind(this);
		// if somebody runs into the tower it will be able to collide with it

		this.type = "PlayerBase";
		// this is a type you can use to check to see what you are running into
		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		// renderable is a class built in melon js that we can play with the animation with

	},

	update: function(delta) {
		if( this.health <= 0) {
			this.broken =  true;
			// this means that the character is dead
			this.renderable.setCurrentAnimation("broken");
	}
		this.body.update(delta);
		// updates the code

		this._super(me.Entity, "update", [delta]);
		// telling the superclass to update
		return true;
	},
		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 shows the hight of the bases
			}
}]);
		this.broken = false;
		// this is saying that the tower has not yet been
		this.health = 10;
		// starting energy for tower
		this.alwaysUpdate = true;
		// updates the screen whether or not we are not looking at it
		this.body.onCollision = this.onCollision.bind(this);
		// if somebody runs into the tower it will be able to collide with it

		this.type = "EnemyBase";
		// this is a type you can use to check to see what you are running into
		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		// renderable is a class built in melon js that we can play with the animation with

	},

	update: function(delta) {
		if( this.health <= 0) {
			this.broken =  true;
			// this means that the character is dead
			this.renderable.setCurrentAnimation("broken");
	}
		this.body.update(delta);
		// updates the code

		this._super(me.Entity, "update", [delta]);
		// telling the super class to update
		return true;
	},
		onCollision: function() {

		}

});<commit_msg>Video 15<commit_after>game.PlayerEntity = me.Entity.extend ({
	// this is a class
	init: function(x, y, settings) {
		// melon js uses this constructor on most things to help us set up
		this._super(me.Entity, "init", [x, y, {
			// this means reaching to the constructor of entites
			image: "player",
			width: 64,
			// tells the screen what amount of space to reserve
			height: 64,
			spritewidth: "64",
			// spritewidth/height are passing the main information tells what to do with the image
			spriteheight: "64",
			// these are setting the properties of the picture o the right demensions so the picture appears properly
			getShape: function() {
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
				// rect is what the guy can walk in to
			}
		}]);

		this.body.setVelocity(5, 20);
		// this also changes the y velocity of the character
		// this is the movement speed of the character
		this.facing = "right";
		// keeps track of which direction the character is going
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
		// this says wherever the player goes the screen will follow him

		this.renderable.addAnimation("idle", [78]);
		// when the character is still this is what he will look like
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
		// this is going to be what the cahracter is going to change into

		this.renderable.setCurrentAnimation("idle");
		this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72] , 80);
	},

	update: function(delta) {
		// this function is what happens on the fly
		if(me.input.isKeyPressed("right")) {
			// set the position of my x by adding the velocity to find above in set veloctiy
			// setVeloctiy() and multiplying it by timer.tick
			// me.timer.tick makes the movement look smooth
			this.facing = "right";
			this.body.vel.x += this.body.accel.x * me.timer.tick;
			this.flipX(true);
			// this is flipping the animation around

		}
		else if(me.input.isKeyPressed("left")) {
			// set the position of my x by adding the velocity to find above in set veloctiy
			// setVeloctiy() and multiplying it by timer.tick
			// me.timer.tick makes the movement look smooth
			this.facing = "left";
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.flipX(false);
			// this is flipping the animation around

		}
		else {
			this.body.vel.x = 0;
		}
		if (me.input.isKeyPressed('jump')) {
     	 // make sure we are not already jumping or falling
      		if (!this.body.jumping && !this.body.falling) {
        // set current vel to the maximum defined value
        // gravity will then do the rest
        	this.body.vel.y = -this.body.maxVel.y * me.timer.tick;
        // set the jumping flag
        	this.body.jumping = true;
        }
    }
		if(me.input.isKeyPressed("attack")) {
			if (!this.renderable.isCurrentAnimation("attack")) {
				// set current animation to attack and once that is over
				// goes back to the idle animations
				this.renderable.setCurrentAnimation("attack", "idle");
				// makes it so that the next time we start this sequence we begin from the first animation not where we left off when we switched to another animation
				this.renderable.setAnimationFrame();
			}
		}
		if(this.body.vel.x !== 0)
		//allowing the guy to not immediately walk
		{
			if(!this.renderable.isCurrentAnimation("walk")) {
				this.renderable.setCurrentAnimation("walk");
				// this says it doesnt want to start the walk animation if it is already walking
			}
		}
		else if(!this.renderable.isCurrentAnimation("attack")) {
			this.renderable.setCurrentAnimation("idle");
		}
		me.collision.check(this, true, this.collideHandler.bind(this), true);
		// passing a parameter into collide function
		// delta is the change in time that has happens
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		// this is updating the animations on the fly
		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;

			console.log("xdif " + xdif + " ydif " + ydif);

			if (xdif>-20 && this.facing=== 'right' xdif<0) {
				this.body.vel.x = 0;
				// stops the player from moving
				this.pos.x = this.pos.x - 1;
				// slighty turns the character
			}
			else if (xdif<60 && this.facing=== 'left' && xdif>0) {
				this.body.vel.x = 0;
				// stops the player from moving
				this.pos.x = this.pos.x + 1;
				// cant walk into castle from left or right
			}

		}
		// this is going to determine what happens when we hit the enemy entity
	}
});

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 shows the height of the bases
			}
}]);
		this.broken = false;
		// this is saying that the tower has not yet been touched/attacked
		this.health = 10;
		// starting energy for tower
		this.alwaysUpdate = true;
		// updates the screen whether or not we are not looking at it
		this.body.onCollision = this.onCollision.bind(this);
		// if somebody runs into the tower it will be able to collide with it

		this.type = "PlayerBase";
		// this is a type you can use to check to see what you are running into
		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		// renderable is a class built in melon js that we can play with the animation with

	},

	update: function(delta) {
		if( this.health <= 0) {
			this.broken =  true;
			// this means that the character is dead
			this.renderable.setCurrentAnimation("broken");
	}
		this.body.update(delta);
		// updates the code

		this._super(me.Entity, "update", [delta]);
		// telling the superclass to update
		return true;
	},
		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 shows the hight of the bases
			}
}]);
		this.broken = false;
		// this is saying that the tower has not yet been
		this.health = 10;
		// starting energy for tower
		this.alwaysUpdate = true;
		// updates the screen whether or not we are not looking at it
		this.body.onCollision = this.onCollision.bind(this);
		// if somebody runs into the tower it will be able to collide with it

		this.type = "EnemyBase";
		// this is a type you can use to check to see what you are running into
		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		// renderable is a class built in melon js that we can play with the animation with

	},

	update: function(delta) {
		if( this.health <= 0) {
			this.broken =  true;
			// this means that the character is dead
			this.renderable.setCurrentAnimation("broken");
	}
		this.body.update(delta);
		// updates the code

		this._super(me.Entity, "update", [delta]);
		// telling the super class to update
		return true;
	},
		onCollision: function() {

		}

});<|endoftext|>


<commit_before>//both letters have to be capitual
//in the bracets you do the work
//melon js uses the init function
//function is x and y
//super reaching to to conductor of the entity
//image is 64 and width is 64
//it returns to polygon
//updates function
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();
                }
        }]);
        //sets the velocity to 5 and 0
        this.body.setVelocity(5, 20);

    },

    update: function(delta){
        if(me.input.isKeyPressed("right")){
            //adds to the postiton of my x by the velocity above in
            //setvelocity() and multiplying it by timer tick
            //me.timer.tick makes the movement look smooth
            this.body.vel.x += this.body.accel.x * me.timer.tick;
        }else{
            //sets the body to vel x 0
            this.body.vel.x = 0;
        }
        //the body is update to delta
        //the return is true
        this.body.update(delta);
        return true;
    }
});<commit_msg>video 10<commit_after>//both letters have to be capitual
//in the bracets you do the work
//melon js uses the init function
//function is x and y
//super reaching to to conductor of the entity
//image is 64 and width is 64
//it returns to polygon
//updates function
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();
                }
        }]);
        //sets the velocity to 5 and 0
        this.body.setVelocity(5, 20);
        //adding animation
        //idle is 78
        //walk is 117, 119, 120, 121, 122, 123, 124, 125], 80
        this.renderable.addAnimation("idle", [78]);
        this.renderable.addAnimation("walk", [117, 119, 120, 121, 122, 123, 124, 125], 80);
        //renderable is idle 78
        this.renderable.setCurrentAnimation("idle");
    },

    update: function(delta){
        if(me.input.isKeyPressed("right")){
            //adds to the postiton of my x by the velocity above in
            //setvelocity() and multiplying it by timer tick
            //me.timer.tick makes the movement look smooth
            this.body.vel.x += this.body.accel.x * me.timer.tick;
        }else{
            //sets the body to vel x 0
            this.body.vel.x = 0;
        }
        //the body is update to delta
        //the return is true
        this.body.update(delta);
        return true;
    }
});<|endoftext|>


<commit_before>// player's class
game.PlayerEntity = me.Entity.extend ({
	//constructor function
	init: function(x, y, settings){
		//reachers the constructor function for enitity
		this._super(me.Entity, 'init', [x, y, {
			//settings. shows the player
			image: "player",
			//sets aside a width of 64 pixels for the sprite
			width: 64,
			//sets aside a height of 64 pixels for the sprite
			height: 64,
			//gives the sprite a width of 64.
			spritewidth : "64",
			//gives the sprite a width of 64
			spriteheight: "64",
			getShape: function(){
				//returns a rectangle of what the player walks into
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
			}
		}]);
		//sets movemet speed. allows player to move horizantally and vertically
		this.body.setVelocity(5, 20);
		//keeps track of what direction the character is facing
		this.facing = "right";
		//checks what time it is in the game
		this.now = new Date().getTime();
		this.lastHit = this.now;
		//sets delay on when the player can hit
		this.lastAttack = new Date().getTime();
		//makesit so the player is always on the screen
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
		//gives player animation while standing
		this.renderable.addAnimation("idle", [78]);
		//gives player animation while walking
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
		//gives player animation while attacking
		this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);
		//the player's start animation
		this.renderable.setCurrentAnimation("idle");
	},

	//delata is the change in time that's happening
	update: function(delta){
		this.now = new Date().getTime();
		if(me.input.isKeyPressed("right")){
			//when right key is pressed, 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";
			this.flipX(true);
		}

		else if(me.input.isKeyPressed("left")){
			//when left key is pressed, 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.facing = "left";
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.flipX(false);
		}

		//if the right key isn't being pressed, the player doesn't move
		else{
			this.body.vel.x = 0;
		}

		//when up key is pressed, player jumps
		if(me.input.isKeyPressed("jump")){
			if(!this.body.jumping && !this.body.falling){
				this.body.jumping = true;
				this.body.vel.y -= this.body.accel.y * me.timer.tick;
			}
		}
		//runs if the attack key is pressed
		if(me.input.isKeyPressed("attack")){
			if(!this.renderable.isCurrentAnimation("attack")){
				//sets current animation to attack. goes back to idle oncethe attack is over it goes back to idle
				this.renderable.setCurrentAnimation("attack", "idle")
				//makes it so that next time the button is pressed the player starts from the first animation, not where it left off
				this.renderable.setAnimationFrame();
			}
		}
		//runs if the player is moving horizantally
		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
			//runs if the player isn't already running the walk animation
			if(!this.renderable.isCurrentAnimation("walk")){
				//gives the player the walking animation
				this.renderable.setCurrentAnimation("walk");
			}
		}
		//runs if player is standing still
		else if(!this.renderable.isCurrentAnimation("attack")){
			//gives the player the idle animation
			this.renderable.setCurrentAnimation("idle");
		}

		me.collision.check(this, true, this.collideHandler.bind(this), true);
		//tells above code to work
		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;
		}
		//player will collide witht the top of the base
		if(ydif<-40 && xdif< 70 && xdif>-35){
			this.body.falling = false;
			this.body.vel.y = -1;
		}
		//player will colide with the base while facing to the right
		else if(xdif>-35 && this.facing==='right' && (xdif<0)){
			this.body.vel.x = 0;
			this.pos.x = this.pos.x -1;
		}
		//player will colide with the base while facing to the left
		else if(xdif<70 && this.facing==='left' && (ydif>0)){
			this.body.vel.x = 0;
			this.pos.x = this.pos.x +1;
		}

		//sets how long the base has to be hit before it will break
		if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000){
			this.lastHit = this.now;
			response.b.loseHealth();
		}
	}

});

//tower class
game.PlayerBaseEntity = me.Entity.extend({
	init: function(x, y, settings){
		//reachers the constructor function for tower
		this._super(me.Entity, 'init', [x, y, {
			//settings. shoes the tower
			image: "tower",
			//sets aside a width of 100 pixels for the tower
			width: 100,
			//sets aside a height of 100 pixels for the tower
			height: 100,
			//gives the tower a width of 100.
			spritewidth : "100",
			//gives the tower a width of 100
			spriteheight: "100",
			getShape: function(){
				//returns a rectangle of what the tower walks into
				return(new me.Rect(0, 0, 100, 200)).toPolygon();
			}
		}]);
		//says that tower hasn't been destroyed
		this.broken = false;
		//gives tower a "health" of ten
		this.health = 10;
		//makes sure the tower's status is always updating, eben when it isn't on the map
		this.alwaysUpdate = true;
		//makes teh tower collidable
		this.body.onCollision = this.onCollision.bind(this);
		//checks what player is running into
		this.type = "PlayerBaseEntity";
		//adds the defualt animatin for the game
		this.renderable.addAnimation("idle", [0]);
		//adds the animation for when the tower is broken
		this.renderable.addAnimation("broken", [1]);
		//sets the desfault animation
		this.renderable.setCurrentAnimation("idle");

	},

	update:function(delta){
		//runs if health is less than or equal to 0
		if(this.health<=0){
			//makes the tower "broken"
			this.broken = true;
			//sets animation for "broken"
			this.renderable.setCurrentAnimation("broken");
		}
		//updates tower status
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		return true;
	},
	//function that runs when base is touched
	onCollision: function(){

	}
});

//tower class
game.EnemyBaseEntity = me.Entity.extend({
	init: function(x, y, settings){
		//reachers the constructor function for tower
		this._super(me.Entity, 'init', [x, y, {
			//settings. shoes the tower
			image: "tower",
			//sets aside a width of 100 pixels for the tower
			width: 100,
			//sets aside a height of 100 pixels for the tower
			height: 100,
			//gives the tower a width of 100.
			spritewidth : "100",
			//gives the tower a width of 100
			spriteheight: "100",
			getShape: function(){
				//returns a rectangle of what the tower walks into
				return(new me.Rect(0, 0, 100, 70)).toPolygon();
			}
		}]);
		//says that tower hasn't been destroyed
		this.broken = false;
		//gives tower a "health" of ten
		this.health = 10;
		//makes sure the tower's status is always updating, eben when it isn't on the map
		this.alwaysUpdate = true;
		//makes teh tower collidable
		this.body.onCollision = this.onCollision.bind(this);
		//checks what player is running into
		this.type = "EnemyBaseEntity";
		//adds the defualt animatin for the game
		this.renderable.addAnimation("idle", [0]);
		//adds the animation for when the tower is broken
		this.renderable.addAnimation("broken", [1]);
		//sets the default animation
		this.renderable.setCurrentAnimation("idle");
	},

	update:function(delta){
		//runs if health is less than or equal to 0
		if(this.health<=0){
			//makes the tower "broken"
			this.broken = true;
			//sets animation for "broken"
			this.renderable.setCurrentAnimation("broken");
		}
		//updates tower status
		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		return true;
	},
	//function that runs when base is touched
	onCollision: function(){

	},

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

game.EnemyCreep = me.Entity.extend({
	init: function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			//settings. shoes the player
			image: "player",
			//sets aside a width of 32 pixels for the sprite
			width: 32,
			//sets aside a height of 64 pixels for the sprite
			height: 64,
			//gives the sprite a width of 32
			spritewidth : "32",
			//gives the sprite a width of 64
			spriteheight: "64",
			getShape: function(){
				//returns a rectangle of what the player walks into
				return(new me.Rect(0, 0, 32, 64)).toPolygon();
			}
		}]);
		//sets health for enemy
		this.health = 10;
		this.alwaysUpdate = true;

		//sets speed for enemy
		this.body.setVelocity(3, 20);

		//sets enemy type
		this.type = "EnemyCreep";

		//creates walking animation for enemy
		this.renderable.addAnimation("walk", [3,4,5], 80);
		this.renderable.setCurrentAnimation("walk");
	},

	update: function(){

	}
});

//sets in game timers
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 20 with comments<commit_after>// player's class
game.PlayerEntity = me.Entity.extend ({
	//constructor function
	init: function(x, y, settings){
		//reachers the constructor function for enitity
		this._super(me.Entity, 'init', [x, y, {
			//settings. shoes the player
			image: "player",
			//sets aside a width of 64 pixels for the sprite
			width: 64,
			//sets aside a height of 64 pixels for the sprite
			height: 64,
			//gives the sprite a width of 64.
			spritewidth : "64",
			//gives the sprite a width of 64
			spriteheight: "64",
			getShape: function(){
				//returns a rectangle of what the player walks into
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
			}
		}]);
		//sets movemet speed. allows player to move horizantally and vertically
		this.body.setVelocity(5, 20);
		//keeps track of which way the character is going
		this.facing = "right";
		//variable for keeping track of time and date
		this.now = new Date().getTime();
		//same ^^
		this.lastHit = this.now;
		//keeps the player from attacking multiple times
		this.lastAttack = new Date().getTime();
		//makesit so the player is always on the screen
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
		//gives player animation while standing
		this.renderable.addAnimation("idle", [78]);
		//gives player animation while walking
		this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
		//gives player animation while attacking
		this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);
		//the player's start animation
		this.renderable.setCurrentAnimation("idle");
	},

	//delta is the change in time that's happening
	update: function(delta){
		//keeps timer updated
		this.now = new Date().getTime();
		//runs if the right key is pressed
		if(me.input.isKeyPressed("right")){
			//when right key is pressed, 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;
			//so the program knows the character is facing right
			this.facing = "right";
			//flips the animation
			this.flipX(true);
		}

		else if(me.input.isKeyPressed("left")){
			//when right key is pressed, 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;
			//so the program knows the character is facing left
			this.facing = "left";
			//doesn't flip the animation
			this.flipX(false);
		}

		//if the right key isn't being pressed, the player doesn't move
		else{
			this.body.vel.x = 0;
		}
		//runs only if the up key is pressed, the player isn't already jumping or falling
		if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
			//makes the player jump
			this.body.jumping = true;
			//sets velocity of the jump and the time
			this.body.vel.y -= this.body.accel.y * me.timer.tick;
		}

		//runs if the attack key is pressed
		if(me.input.isKeyPressed("attack")){
			if(!this.renderable.isCurrentAnimation("attack")){
				//sets current animation to attack. goes back to idle oncethe attack is over it goes back to idle
				this.renderable.setCurrentAnimation("attack", "idle")
				//makes it so that next time the button is pressed the player starts from the first animation, not where it left off
				this.renderable.setAnimationFrame();
			}
		}

		//runs if the player is moving horizantally and not attacking
		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
			//runs if the player isn't already running the walk animation
			if(!this.renderable.isCurrentAnimation("walk")){
				//gives the player the walking animation
				this.renderable.setCurrentAnimation("walk");
			}
		}
		//runs if player is standing still and not attacking
		else if(!this.renderable.isCurrentAnimation("attack")){
			//gives the player the idle animation
			this.renderable.setCurrentAnimation("idle");
		}
		//checks to see if player is colliding with base
		me.collision.check(this, true, this.collideHandler.bind(this), true);
		//tells above code to work
		this.body.update(delta);
		//updates the code
		this._super(me.Entity, "update", [delta]);
		return true;
	},
	//function for when player collides with tower
	collideHandler: function(response){
		//runs if the player collides with the enemy base
		if (response.b.type === 'EnemyBaseEntity') {
			//represents the difference between player's y distance and enemy's y distance
			var ydif = this.pos.y - response.b.pos.y;
			//represents the difference between player's and enemy base's x distance
			var xdif = this.pos.x - response.b.pos.x;
			//runs if the player is on top of the enemy base
			if (ydif < -40 && xdif < 60 && xdif > -35) {
				//stops the player from moving down
				this.body.falling = false;
				//keeps the player from falling through the tower
				this.body.vel.y = -1;
			}
			//runs if the player's x position is 37 units away from the tower while facing right
			else if (xdif > -36 && this.facing === "right" && xdif < 0) {
				//stops player from moving
				this.body.vel.x = 0;
				//moves player slightly away from tower
				this.pos.x = this.pos.x -1;
			}
			//runs if the player's x position is 74 units away from the tower while facing left
			else if (xdif < 75 && this.facing === "left" && xdif > 0) {
				//stops player from moving
				this.body.vel.x = 0;
				//moves player slightly away from tower
				this.pos.x = this.pos.x +1;
			}
			//runs if the player is attacking and its been 400 milliseconds since the last hit
			if (this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000) {
				//so the computer knows th eplayer just hit the tower
				this.lastHit = this.now;
				//calls the loseHealth function
				response.b.loseHealth();
			}
		}
	}
});

//tower class
game.PlayerBaseEntity = me.Entity.extend({
	init: function(x, y, settings){
		//reachers the constructor function for tower
		this._super(me.Entity, 'init', [x, y, {
			//settings. shoes the tower
			image: "tower",
			//sets aside a width of 100 pixels for the tower
			width: 100,
			//sets aside a height of 100 pixels for the tower
			height: 100,
			//gives the tower a width of 100.
			spritewidth : "100",
			//gives the tower a width of 100
			spriteheight: "100",
			getShape: function(){
				//returns a rectangle of what the tower walks into
				return(new me.Rect(0, 0, 100, 60)).toPolygon();
			}
		}]);
		//says that tower hasn't been destroyed
		this.broken = false;
		//gives tower a "health" of ten
		this.health = 10;
		//makes sure the tower's status is always updating, eben when it isn't on the map
		this.alwaysUpdate = true;
		//makes teh tower collidable
		this.body.onCollision = this.onCollision.bind(this);
		//checks what player is running into
		this.type = "PlayerBaseEntity";
		//adds the defualt animatin for the game
		this.renderable.addAnimation("idle", [0]);
		//adds the animation for when the tower is broken
		this.renderable.addAnimation("broken", [1]);
		//sets the desfault animation
		this.renderable.setCurrentAnimation("idle");

	},

	update:function(delta){
		//runs if health is less than or equal to 0
		if(this.health<=0){
			//makes the tower "broken"
			this.broken = true;
			//sets animation for "broken"
			this.renderable.setCurrentAnimation("broken");
		}
		//updates tower status
		this.body.update(delta);
		//updates
		this._super(me.Entity, "update", [delta]);
		return true;
	},
	//function that runs when base is touched
	onCollision: function(){

	}
});

//tower class
game.EnemyBaseEntity = me.Entity.extend({
	init: function(x, y, settings){
		//reachers the constructor function for tower
		this._super(me.Entity, 'init', [x, y, {
			//settings. shoes the tower
			image: "tower",
			//sets aside a width of 100 pixels for the tower
			width: 100,
			//sets aside a height of 100 pixels for the tower
			height: 100,
			//gives the tower a width of 100.
			spritewidth : "100",
			//gives the tower a width of 100
			spriteheight: "100",
			getShape: function(){
				//returns a rectangle of what the tower walks into
				return(new me.Rect(0, 0, 100, 60)).toPolygon();
			}
		}]);
		//says that tower hasn't been destroyed
		this.broken = false;
		//gives tower a "health" of ten
		this.health = 10;
		//makes sure the tower's status is always updating, eben when it isn't on the map
		this.alwaysUpdate = true;
		//makes the tower collidable
		this.body.onCollision = this.onCollision.bind(this);
		//checks what player is running into
		this.type = "EnemyBaseEntity";
		//adds the defualt animatin for the game
		this.renderable.addAnimation("idle", [0]);
		//adds the animation for when the tower is broken
		this.renderable.addAnimation("broken", [1]);
		//sets the default animation
		this.renderable.setCurrentAnimation("idle");
	},

	update:function(delta){
		//runs if health is less than or equal to 0
		if(this.health<=0){
			//makes the tower "broken"
			this.broken = true;
			//sets animation for "broken"
			this.renderable.setCurrentAnimation("broken");
		}
		//updates tower status
		this.body.update(delta);
		//updates
		this._super(me.Entity, "update", [delta]);
		return true;
	},
	//function that runs when base is touched
	onCollision: function(){

	},

	loseHealth: function(){
		//makes the tower loose 1 health on each hit
		this.health--;
	}
});

game.EnemyCreep = me.Entity.extend({
	init: function(x, y, settings){
			//reaches the constructor function for enitity
			this._super(me.Entity, 'init', [x, y, {
				//settings. shows the creep
				image: "creep1",
				//sets aside a width of 64 pixels for the sprite
				width: 32,
				//sets aside a height of 64 pixels for the sprite
				height: 64,
				//gives the sprite a width of 64.
				spritewidth : "32",
				//gives the sprite a width of 64
				spriteheight: "64",
			}]);
			//sets health to ten
			this.health = 10;
			//makes the creep's satus continuosly update
			this.alwaysUpdate = true;
			//sets the creep's horizantal and vertical speed
			this.body.setVelocity(3, 20);
			//sets the sprite's type
			this.type = "EnemyCreep";
			//creates the walking animation
			this.renderable.addAnimation("walk", [3, 4, 5], 80);
			//applies the walking animation
			this.renderable.setCurrentAnimation("walk");
		},

		//delta is the change in time that's happening
		update: function(delta){
			//makes the creep move
			this.body.vel.x -= this.body.accel.x *  me.timer.tick;
			//basic update functions
			this.body.update(delta);
			this._super(me.Entity, "update", [delta]);
			return true;
		}

});

//class that runs all the timers and occurences that aren't inside any of the other entities
game.GameManager = Object.extend({
	//constructor function
	init: function(x, y, settings){
		//sets timer
		this.now = new Date().getTime();
		//keeps track of last time creep was made
		this.lastCreep = new Date().getTime();
		//keeps the function updating
		this.alwaysUpdate = true;
	},

	update: function(){
		//keeps track of timer
		this.now = new Date().getTime();
		//checks to make sure there is a multiple of ten. makes sure its been at least a second since last creep has been made
		if(Math.round(this.now/1000)%10 === 0 && (this.now - this.lastCreep >= 1000)){
			//updates timer
			this.lastCreep = this.now;
			//creates and inserts creep into worls
			var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
			//adds the creeps to the worls
			me.game.world.addChild(creepe, 5);
		}
		//updates
		return true;
	}
});<|endoftext|>