<commit_before>game.PlayerEntity = me.Entity.extend({
	init: function(x, y, settings){
		this.setSuper();
		this.setPlayerTimers();
		this.setAttributes();
		this.type = "PlayerEntity";
		this.setFlags();

		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

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

	setSuper: function(){
		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();
			}
		}]);
	},

	setPlayerTimers: function(){
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.lastAttack = new Date().getTime();
		//haven't used attack as of video 17
	},

	setAttributes: function(){
		this.health = game.data.playerHealth;
		this.body.setVelocity(game.data.playerMoveSpeed, 20);
		//sets movement speed
		//the "20" allows the y-location to change
		this.attack = game.data.playerAttack;
		//added for the gold feature, used when a creep is killed by the player
	},

	setFlags: function(){
		this.facing = "right";
		this.dead = false;
		//added to serve as an initial value for the player's ability to die
		this.attacking = false;
	},
	//flags are things that only have one or two values

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

	update: function(delta){
		this.now = new Date().getTime();
		this.dead = checkIfDead();
		this.checkKeyPressesAndMove();
		this.setAnimation();
		me.collision.check(this, true, this.collideHandler.bind(this), true);
		//passing on paramaters about character pertaining to collision
		this.body.update(delta);
		this._super(me.Entity, "update", [delta]);
		return true;
	},

	checkIfDead: function(){
		if(this.health <= 0){
			return true;
			//the lines of code that were previously here were eliminated because the melonJS update rendered them obsolete
		}
	},

	checkKeyPressesAndMove: function(){
		if(me.input.isKeyPressed("right")){
			this.moveRight();
		}
		else if(me.input.isKeyPressed("left")){
			this.moveLeft();
		}
		else{
			this.body.vel.x = 0;
		}

		if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
			this.jump();
		}
		//allows our player to jump, binds to key pressed in play.js
		this.attacking = me.input.isKeyPressed("attack");
	},

	moveRight: function(){
		//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 appear smooth
		this.body.vel.x += this.body.accel.x * me.timer.tick;
		this.facing = "right";
		//keeps track of what direction your character is going
		this.flipX(true);
	},

	moveLeft: function(){
		//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 appear smooth
		this.body.vel.x -= this.body.accel.x * me.timer.tick;
		this.facing = "left";
		//keeps track of what direction your character is going
		this.flipX(false);
	},

	jump: function(){
		this.body.jumping = true;
		this.body.vel.y -= this.body.accel.y * me.timer.tick;
	},

	setAnimation: function(){
		if(this.attacking){
			if(!this.renderable.isCurrentAnimation("attack")){
				//sets the current animation to attack and once that is over goes back to the idle animation
				this.renderable.setCurrentAnimation("attack", "idle");
				//makes it so that the next time we start this sequence we begin from the first animation, not wherever we left off when we switched to another animation
				this.renderable.setAnimationFrame();
			}
		}

		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
			if(!this.renderable.isCurrentAnimation("walk")){
				this.renderable.setCurrentAnimation("walk");
			}
		}
		//allows the character to walk
		else if(!this.renderable.isCurrentAnimation("attack")){
			this.renderable.setCurrentAnimation("idle");
		}
		//makes the character return to idle when not moving
	},

	loseHealth: function(damage){
		this.health = this.health - damage;
	},
	//causes depletion of health

	collideHandler: function(response){
		if(response.b.type==='EnemyBaseEntity'){
			this.collideWithEnemyBase(response);
			}
			//checking to see if it's been 1000 milliseconds (1 second) since the base was hit, so the player doesn't keep attacking over and over again
			else if(response.b.type==='EnemyCreep'){
				this.collideWithEnemyCreep(response);
			}

		}
	},

	collideWithEnemyBase: function(response){
		var ydif = this.pos.y - response.b.pos.y;
			var xdif = this.pos.x - response.b.pos.x;
			//response.b represents whatever we're colliding with

			if(ydif<-40 && xdif<70 && xdif>-35){
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			//the ydif was moved to before the xdif because the console reads from top to bottom, and should take collisions from above as a first priority

			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){
				this.lastHit = this.now;
				response.b.loseHealth(game.data.playerAttack);
	}

	collideWithEnemyCreep: function(response){
			var xdif = this.pos.x - response.b.pos.x;
			var ydif = this.pos.y - response.b.pos.y;

			this.stopMovement(xdif);
			if(this.checkAttack(xdif, ydif)){
				this.hitCreep(response);
			};
	},

	stopMovement: function(xdif){
		if (xdif>0){
					if(this.facing==="left"){
						this.body.vel.x = 0;
					}
				}
				else{
					if(this.facing==="right"){
						this.body.vel.x = 0;
					}
				}
				//these lines of code keep the player from walking through the enemy
	},

	checkAttack: function(xdif, ydif){
		if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= game.data.playerAttackTimer
				//had error on line 152 for a while, realized an additional parentheses was added by mistake
				&& (Math.abs(ydif) <=40) &&
				(((xdif>0) && this.facing==="left") || ((xdif<0) && this.facing==="right"))
				//the two parallel lines indicate "or"
				){
				this.lastHit = this.now;
				//if the creeps' health is less than our attack, execute code in an if statement
				return true;
				//this will execute all the code that is inside the above brackets
			}
			return false;
			//if the attack check is false then the function will not happen
	},

	hitCreep: function(response){
		if(response.b.health <= game.data.playerAttack){
					game.data.gold += 1;
					console.log("Current gold: " + game.data.gold);
				}
				//adds one gold for our creep kill
				response.b.loseHealth(game.data.playerAttack);
	}
});
//classes have both entities capitalized, while methods have only the second entity in caps<commit_msg>Video 34 (fixing refactor errors) completed<commit_after>game.PlayerEntity = me.Entity.extend({
	init: function(x, y, settings){
		this.setSuper(x, y);
		this.setPlayerTimers();
		this.setAttributes();
		this.type = "PlayerEntity";
		this.setFlags();

		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

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

	setSuper: function(x, y){
		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();
			}
		}]);
	},

	setPlayerTimers: function(){
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.lastAttack = new Date().getTime();
		//haven't used attack as of video 17
	},

	setAttributes: function(){
		this.health = game.data.playerHealth;
		this.body.setVelocity(game.data.playerMoveSpeed, 20);
		//sets movement speed
		//the "20" allows the y-location to change
		this.attack = game.data.playerAttack;
		//added for the gold feature, used when a creep is killed by the player
	},

	setFlags: function(){
		this.facing = "right";
		this.dead = false;
		//added to serve as an initial value for the player's ability to die
		this.attacking = false;
	},
	//flags are things that only have one or two values

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

	update: function(delta){
		this.now = new Date().getTime();
		this.dead = this.checkIfDead();
		this.checkKeyPressesAndMove();
		this.setAnimation();
		me.collision.check(this, true, this.collideHandler.bind(this), true);
		//passing on paramaters about character pertaining to collision
		this.body.update(delta);
		this._super(me.Entity, "update", [delta]);
		return true;
	},

	checkIfDead: function(){
		if(this.health <= 0){
			return true;
			//the lines of code that were previously here were eliminated because the melonJS update rendered them obsolete
		}
	},

	checkKeyPressesAndMove: function(){
		if(me.input.isKeyPressed("right")){
			this.moveRight();
		}
		else if(me.input.isKeyPressed("left")){
			this.moveLeft();
		}
		else{
			this.body.vel.x = 0;
		}

		if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
			this.jump();
		}
		//allows our player to jump, binds to key pressed in play.js
		this.attacking = me.input.isKeyPressed("attack");
	},

	moveRight: function(){
		//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 appear smooth
		this.body.vel.x += this.body.accel.x * me.timer.tick;
		this.facing = "right";
		//keeps track of what direction your character is going
		this.flipX(true);
	},

	moveLeft: function(){
		//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 appear smooth
		this.body.vel.x -= this.body.accel.x * me.timer.tick;
		this.facing = "left";
		//keeps track of what direction your character is going
		this.flipX(false);
	},

	jump: function(){
		this.body.jumping = true;
		this.body.vel.y -= this.body.accel.y * me.timer.tick;
	},

	setAnimation: function(){
		if(this.attacking){
			if(!this.renderable.isCurrentAnimation("attack")){
				//sets the current animation to attack and once that is over goes back to the idle animation
				this.renderable.setCurrentAnimation("attack", "idle");
				//makes it so that the next time we start this sequence we begin from the first animation, not wherever we left off when we switched to another animation
				this.renderable.setAnimationFrame();
			}
		}

		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
			if(!this.renderable.isCurrentAnimation("walk")){
				this.renderable.setCurrentAnimation("walk");
			}
		}
		//allows the character to walk
		else if(!this.renderable.isCurrentAnimation("attack")){
			this.renderable.setCurrentAnimation("idle");
		}
		//makes the character return to idle when not moving
	},

	loseHealth: function(damage){
		this.health = this.health - damage;
	},
	//causes depletion of health

	collideHandler: function(response){
		if(response.b.type==='EnemyBaseEntity'){
			this.collideWithEnemyBase(response);
			}
			//checking to see if it's been 1000 milliseconds (1 second) since the base was hit, so the player doesn't keep attacking over and over again
			else if(response.b.type==='EnemyCreep'){
				this.collideWithEnemyCreep(response);
			}

		}
	},

	collideWithEnemyBase: function(response){
		var ydif = this.pos.y - response.b.pos.y;
			var xdif = this.pos.x - response.b.pos.x;
			//response.b represents whatever we're colliding with

			if(ydif<-40 && xdif<70 && xdif>-35){
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			//the ydif was moved to before the xdif because the console reads from top to bottom, and should take collisions from above as a first priority

			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){
				this.lastHit = this.now;
				response.b.loseHealth(game.data.playerAttack);
	}

	collideWithEnemyCreep: function(response){
			var xdif = this.pos.x - response.b.pos.x;
			var ydif = this.pos.y - response.b.pos.y;

			this.stopMovement(xdif);
			if(this.checkAttack(xdif, ydif)){
				this.hitCreep(response);
			};
	},

	stopMovement: function(xdif){
		if (xdif>0){
					if(this.facing==="left"){
						this.body.vel.x = 0;
					}
				}
				else{
					if(this.facing==="right"){
						this.body.vel.x = 0;
					}
				}
				//these lines of code keep the player from walking through the enemy
	},

	checkAttack: function(xdif, ydif){
		if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= game.data.playerAttackTimer
				//had error on line 152 for a while, realized an additional parentheses was added by mistake
				&& (Math.abs(ydif) <=40) &&
				(((xdif>0) && this.facing==="left") || ((xdif<0) && this.facing==="right"))
				//the two parallel lines indicate "or"
				){
				this.lastHit = this.now;
				//if the creeps' health is less than our attack, execute code in an if statement
				return true;
				//this will execute all the code that is inside the above brackets
			}
			return false;
			//if the attack check is false then the function will not happen
	},

	hitCreep: function(response){
		if(response.b.health <= game.data.playerAttack){
					game.data.gold += 1;
					console.log("Current gold: " + game.data.gold);
				}
				//adds one gold for our creep kill
				response.b.loseHealth(game.data.playerAttack);
	}
});
//classes have both entities capitalized, while methods have only the second entity in caps<|endoftext|>


<commit_before>game.PlayerEntity = me.Entity.extend({
	init: function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			image: "player",
			width: 64,
			height: 64,
			spritewidth: "64",
			spriteheight: "64",
			getShape: function(){
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
			}
		}]);

		this.type = "PlayerEntity";
		this.health = game.data.playerHealth;
		this.body.setVelocity(game.data.playerMoveSpeed, 20);
		//sets movement speed
		//the "20" allows the y-location to change
		this.facing = "right";
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.dead = false;
		this.lastAttack = new Date().getTime();
		//havent used attack as of video 17
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

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

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

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

		if(this.health <= 0){
			this.dead = true;
			this.pos.x = 10;
			this.pos.y = 0;
			this.health = game.data.playerHealth;
		}

		if(me.input.isKeyPressed("right")){
			//adds to the position of my "x" by the velocity defined above in setVelocity() and multiplying it by me.timer.tick
			//me.timer.tick makes the movement appear smooth
			this.body.vel.x += this.body.accel.x * me.timer.tick;
			this.facing = "right";
			//keeps track of what direction your character is going
			this.flipX(true);
		}
		else if(me.input.isKeyPressed("left")){
			//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 appear smooth
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.facing = "left";
			//keeps track of what direction your character is going
			this.flipX(false);
		}
		else{
			this.body.vel.x = 0;
		}

		if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
			this.body.jumping = true;
			this.body.vel.y -= this.body.accel.y * me.timer.tick;
		}
		//allows our player to jump, binds to key pressed in play.js

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

		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
			if(!this.renderable.isCurrentAnimation("walk")){
				this.renderable.setCurrentAnimation("walk");
			}
		}
		//allows the character to walk
		else if(!this.renderable.isCurrentAnimation("attack")){
			this.renderable.setCurrentAnimation("idle");
		}
		//makes the character return to idle when not moving

		me.collision.check(this, true, this.collideHandler.bind(this), true);
		//passing on paramaters about character pertaining to collision

		this.body.update(delta);

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

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

	collideHandler: function(response){
		if(response.b.type==='EnemyBaseEntity'){
			var ydif = this.pos.y - response.b.pos.y;
			var xdif = this.pos.x - response.b.pos.x;
			//response.b represents whatever we're colliding with

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

			if(ydif<-40 && xdif<70 && xdif>-35){
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			//the ydif was moved to before the xdif because the console reads from top to bottom, and should take collisions from above as a first priority

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

			if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= game.data.playerAttackTimer){
				console.log("tower Hit");
				this.lastHit = this.now;
				response.b.loseHealth(game.data.playerAttack);
			}
			//checking to see if it's been 1000 milliseconds (1 second) since the base was hit, so the player doesn't keep attacking over and over again
			else if(response.b.type==='EnemyCreep'){
				var xdif = this.pos.x - response.b.pos.x;
				var ydif = this.pos.y - response.b.pos.y;

				if (xdif>0){
					this.pos.x = this.pos.x + 1;
					if(this.facing==="left"){
						this.body.vel.x = 0;
					}
				}
				else{
					this.pos.x = this.pos.x - 1;
					if(this.facing==="right"){
						this.body.vel.x = 0;
					}
				}
				//these lines of code keep the player from walking through the enemy

				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"))
					//the two parallel lines indicate "or"
					){
					this.lastHit = this.now;
					response.b.loseHealth(game.playerAttack);
				}
			}

		}
	}

});
//classes have both entities capitalized, while methods have only the second entity in caps
game.PlayerBaseEntity = me.Entity.extend({
	init : function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height: 100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function(){
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}

		}]);

		this.broken = false;
		this.health = game.data.playerBaseHealth;
		this.alwaysUpdate = true;
		this.body.onCollision = this.onCollision.bind(this);
		this.type = "PlayerBase";

		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		//adds the animation of the player base to the game screen
	},

	update:function(delta){
		if(this.health<=0){
			this.broken = true;
			this.renderable.setCurrentAnimation("broken");
		}
		//kills the game if the health reaches or goes below zero
		this.body.update(delta);

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

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

	onCollision: function(){

	}
});

game.EnemyBaseEntity = me.Entity.extend({
	init : function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height: 100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function(){
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}

		}]);

		this.broken = false;
		this.health = game.data.enemyBaseHealth;
		this.alwaysUpdate = true;
		this.body.onCollision = this.onCollision.bind(this);
		this.type = "EnemyBaseEntity";

		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		//adds the animation of the enemy base to the game screen
	},

	update:function(delta){
		if(this.health<=0){
			this.broken = true;
			this.renderable.setCurrentAnimation("broken");
		}
		//renders the base obsolite should it reach zero
		this.body.update(delta);

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

	onCollision: function(){

	},

	loseHealth: function(){
		this.health--;
	}
});
//this second "base" entity is for the enemy tower

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

		this.type = "EnemyCreep";

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

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

	update: function(delta){
		console.log(this.health);
		if(this.health <= 0){
			me.game.world.removeChild(this);
		}
		//removes the creep from the map if its health goes beneath zero

		this.now = new Date().getTime();

		this.body.vel.x -= this.body.accel.x * me.timer.tick;
		//makes the creep land on the ground floor and animate in a negative direction (left)

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

		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		//calls to the super

		return true;
	},

	collideHandler: function(response){
		if(response.b.type==='PlayerBase'){
			this.attacking=true;
			this.lastAttacking=this.now;
			this.body.vel.x = 0;
			//keeps moving the creep to the right to maintain its position
			this.pos.x = this.pos.x + 1;
			//checks that it has been at least one second since this creep hit a base

			if((this.now-this.lastHit >= 1000)){
				//updates the lastHit timer
				this.lastHit = this.now;
				response.b.loseHealth(game.data.enemyCreepAttack);
				//makes the player base call its loseHealth function and passes it a damage of one
			}
		}
		else if(response.b.type === 'PlayerEntity'){
			var xdif = this.pos.x - response.b.pos.x;

			this.attacking=true;
			//this.lastAttacking=this.now;
			if(xdif>0){
				this.pos.x = this.pos.x + 1;
				this.body.vel.x = 0;
			}
			//makes sure you have to be behind the creep

			if((this.now-this.lastHit >= 1000 && xdif>0)){
				this.lastHit = this.now;
				response.b.loseHealth(game.data.enemyCreepAttack);
			}
		}
	}
});
//more or less the same code as our previous entities - this time for the creeps

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)){
			//% (mod) checks to see if we have a multiple of 10
			//makes u wait at least a second for the next creep to spawn
			this.lastCreep = this.now;
			//this.now is the timer
			var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
			me.game.world.addChild(creepe, 5);
		}
		//keeps track of our timer and whether or not new creeps should be made

		return true;
	}
});<commit_msg>Comments for video 24 added<commit_after>game.PlayerEntity = me.Entity.extend({
	init: function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			image: "player",
			width: 64,
			height: 64,
			spritewidth: "64",
			spriteheight: "64",
			getShape: function(){
				return(new me.Rect(0, 0, 64, 64)).toPolygon();
			}
		}]);

		this.type = "PlayerEntity";
		this.health = game.data.playerHealth;
		this.body.setVelocity(game.data.playerMoveSpeed, 20);
		//sets movement speed
		//the "20" allows the y-location to change
		this.facing = "right";
		this.now = new Date().getTime();
		this.lastHit = this.now;
		this.dead = false;
		//added to serve as an initial value for the player's ability to die
		this.lastAttack = new Date().getTime();
		//havent used attack as of video 17
		me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

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

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

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

		if(this.health <= 0){
			this.dead = true;
			this.pos.x = 10;
			this.pos.y = 0;
			this.health = game.data.playerHealth;
		}

		if(me.input.isKeyPressed("right")){
			//adds to the position of my "x" by the velocity defined above in setVelocity() and multiplying it by me.timer.tick
			//me.timer.tick makes the movement appear smooth
			this.body.vel.x += this.body.accel.x * me.timer.tick;
			this.facing = "right";
			//keeps track of what direction your character is going
			this.flipX(true);
		}
		else if(me.input.isKeyPressed("left")){
			//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 appear smooth
			this.body.vel.x -= this.body.accel.x * me.timer.tick;
			this.facing = "left";
			//keeps track of what direction your character is going
			this.flipX(false);
		}
		else{
			this.body.vel.x = 0;
		}

		if(me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling){
			this.body.jumping = true;
			this.body.vel.y -= this.body.accel.y * me.timer.tick;
		}
		//allows our player to jump, binds to key pressed in play.js

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

		else if(this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")){
			if(!this.renderable.isCurrentAnimation("walk")){
				this.renderable.setCurrentAnimation("walk");
			}
		}
		//allows the character to walk
		else if(!this.renderable.isCurrentAnimation("attack")){
			this.renderable.setCurrentAnimation("idle");
		}
		//makes the character return to idle when not moving

		me.collision.check(this, true, this.collideHandler.bind(this), true);
		//passing on paramaters about character pertaining to collision

		this.body.update(delta);

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

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

	collideHandler: function(response){
		if(response.b.type==='EnemyBaseEntity'){
			var ydif = this.pos.y - response.b.pos.y;
			var xdif = this.pos.x - response.b.pos.x;
			//response.b represents whatever we're colliding with

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

			if(ydif<-40 && xdif<70 && xdif>-35){
				this.body.falling = false;
				this.body.vel.y = -1;
			}
			//the ydif was moved to before the xdif because the console reads from top to bottom, and should take collisions from above as a first priority

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

			if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= game.data.playerAttackTimer){
				console.log("tower Hit");
				this.lastHit = this.now;
				response.b.loseHealth(game.data.playerAttack);
			}
			//checking to see if it's been 1000 milliseconds (1 second) since the base was hit, so the player doesn't keep attacking over and over again
			else if(response.b.type==='EnemyCreep'){
				var xdif = this.pos.x - response.b.pos.x;
				var ydif = this.pos.y - response.b.pos.y;

				if (xdif>0){
					this.pos.x = this.pos.x + 1;
					if(this.facing==="left"){
						this.body.vel.x = 0;
					}
				}
				else{
					this.pos.x = this.pos.x - 1;
					if(this.facing==="right"){
						this.body.vel.x = 0;
					}
				}
				//these lines of code keep the player from walking through the enemy

				if(this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= game.data.playerAttackTimer
					//had error on line 152 for a while, realized an additional parentheses was added by mistake
					&& (Math.abs(ydif) <=40) &&
					(((xdif>0) && this.facing==="left") || ((xdif<0) && this.facing==="right"))
					//the two parallel lines indicate "or"
					){
					this.lastHit = this.now;
					response.b.loseHealth(game.playerAttack);
				}
			}

		}
	}

});
//classes have both entities capitalized, while methods have only the second entity in caps
game.PlayerBaseEntity = me.Entity.extend({
	init : function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height: 100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function(){
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}

		}]);

		this.broken = false;
		this.health = game.data.playerBaseHealth;
		this.alwaysUpdate = true;
		this.body.onCollision = this.onCollision.bind(this);
		this.type = "PlayerBase";

		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		//adds the animation of the player base to the game screen
	},

	update:function(delta){
		if(this.health<=0){
			this.broken = true;
			this.renderable.setCurrentAnimation("broken");
		}
		//kills the game if the health reaches or goes below zero
		this.body.update(delta);

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

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

	onCollision: function(){

	}
});

game.EnemyBaseEntity = me.Entity.extend({
	init : function(x, y, settings){
		this._super(me.Entity, 'init', [x, y, {
			image: "tower",
			width: 100,
			height: 100,
			spritewidth: "100",
			spriteheight: "100",
			getShape: function(){
				return (new me.Rect(0, 0, 100, 70)).toPolygon();
			}

		}]);

		this.broken = false;
		this.health = game.data.enemyBaseHealth;
		this.alwaysUpdate = true;
		this.body.onCollision = this.onCollision.bind(this);
		this.type = "EnemyBaseEntity";

		this.renderable.addAnimation("idle", [0]);
		this.renderable.addAnimation("broken", [1]);
		this.renderable.setCurrentAnimation("idle");
		//adds the animation of the enemy base to the game screen
	},

	update:function(delta){
		if(this.health<=0){
			this.broken = true;
			this.renderable.setCurrentAnimation("broken");
		}
		//renders the base obsolite should it reach zero
		this.body.update(delta);

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

	onCollision: function(){

	},

	loseHealth: function(){
		this.health--;
	}
});
//this second "base" entity is for the enemy tower

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

		this.type = "EnemyCreep";

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

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

	update: function(delta){
		console.log(this.health);
		if(this.health <= 0){
			me.game.world.removeChild(this);
		}
		//removes the creep from the map if its health goes beneath zero

		this.now = new Date().getTime();

		this.body.vel.x -= this.body.accel.x * me.timer.tick;
		//makes the creep land on the ground floor and animate in a negative direction (left)

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

		this.body.update(delta);

		this._super(me.Entity, "update", [delta]);
		//calls to the super

		return true;
	},

	collideHandler: function(response){
		if(response.b.type==='PlayerBase'){
			this.attacking=true;
			this.lastAttacking=this.now;
			this.body.vel.x = 0;
			//keeps moving the creep to the right to maintain its position
			this.pos.x = this.pos.x + 1;
			//checks that it has been at least one second since this creep hit a base

			if((this.now-this.lastHit >= 1000)){
				//updates the lastHit timer
				this.lastHit = this.now;
				response.b.loseHealth(game.data.enemyCreepAttack);
				//makes the player base call its loseHealth function and passes it a damage of one
			}
		}
		else if(response.b.type === 'PlayerEntity'){
			var xdif = this.pos.x - response.b.pos.x;

			this.attacking=true;
			//this.lastAttacking=this.now;
			if(xdif>0){
				this.pos.x = this.pos.x + 1;
				this.body.vel.x = 0;
			}
			//makes sure you have to be behind the creep

			if((this.now-this.lastHit >= 1000 && xdif>0)){
				this.lastHit = this.now;
				response.b.loseHealth(game.data.enemyCreepAttack);
			}
		}
	}
});
//more or less the same code as our previous entities - this time for the creeps

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)){
			//% (mod) checks to see if we have a multiple of 10
			//makes u wait at least a second for the next creep to spawn
			this.lastCreep = this.now;
			//this.now is the timer
			var creepe = me.pool.pull("EnemyCreep", 1000, 0, {});
			me.game.world.addChild(creepe, 5);
		}
		//keeps track of our timer and whether or not new creeps should be made

		return true;
	}
});<|endoftext|>


<commit_before>//this code is a class and has function in it so melon js will work.
game.PlayerEntity = me.Entity.extend({
   init: function(x, y, settings){
       //this is telling us the information about the character that we are trying to use
       this._super(me.Entity, 'init', [x, y, {
               image: "player",
               width: 64,
               height: 64,
               spritewidth: "64",
               spriteheight: "64",
               getShape: function(){
                   return(new me.Rect(0, 0, 64, 64)).toPolygon();

               }
       }]);
       //this line is the speed of my player
       this.body.setVelocity(4, 20);

       this.renderable.addAnimation("idle", [78]);
       //this will animate my character while he walks.
       this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 123, 124, 125] )

       this.renderable.setCurrentAnimation("idle");
   },
  //in update function it will check if I pressed the key so that it moves when I press it.
   update: function (delta){
          if(me.input.isKeyPressed("right")){
          //adds to the position of my x by the velocity defined above in
          //setVelocity() and multiplying it by me.timer.tick.
          //me.timer.tick makes the movement look smooth
           this.body.vel.x += this.body.accel.x * me.timer.tick;
           this.flipX(true);
          }else{
              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");
        }

            this.body.update(delta);

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

game.PlayerBaseEntitiy = 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, 100)).toPolygon();
           }
       }]);
       this.broken = false;
       this.health = 10;
       this.alwaysUpdate = true;
       this.body.onCollision = this.onCollision.bind(this);

       this.type = "PlayerBaseEntity";
   },

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

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

    onCollison: function (){

    }

});
game.EnemyBaseEntitiy = 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, 100)).toPolygon();
           }
       }]);
       this.broken = false;
       this.health = 10;
       this.alwaysUpdate = true;
       this.body.onCollision = this.onCollision.bind(this);

       this.type = "EnemyBaseEntity";
   },

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

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

    onCollison: function (){

    }

});<commit_msg>commented on video 11<commit_after>//this code is a class and has function in it so melon js will work.
game.PlayerEntity = me.Entity.extend({
   init: function(x, y, settings){
       //this is telling us the information about the character that we are trying to use
       this._super(me.Entity, 'init', [x, y, {
               image: "player",
               width: 64,
               height: 64,
               spritewidth: "64",
               spriteheight: "64",
               getShape: function(){
                   return(new me.Rect(0, 0, 64, 64)).toPolygon();

               }
       }]);
       //this line is the speed of my player
       this.body.setVelocity(4, 20);

       this.renderable.addAnimation("idle", [78]);
       //this will animate my character while he walks.
       this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 123, 124, 125] )

       this.renderable.setCurrentAnimation("idle");
   },
  //in update function it will check if I pressed the key so that it moves when I press it.
   update: function (delta){
          if(me.input.isKeyPressed("right")){
          //adds to the position of my x by the velocity defined above in
          //setVelocity() and multiplying it by me.timer.tick.
          //me.timer.tick makes the movement look smooth
           this.body.vel.x += this.body.accel.x * me.timer.tick;
           this.flipX(true);
          }else{
              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");
        }

            this.body.update(delta);

            this._super(me.Entity, "update", [delta])
            return true;
   }
});
//this is the information that we use for our player.
game.PlayerBaseEntitiy = me.Entity.extend({
   init : function(x, y, settings){
        this._super(me.Entity, 'init', [x, y, {
        //on the lines below is the description of the tower that we are going to use for our player.
           image: "tower",
           width: 100,
           height: 100,
           spritewidth: "100",
           spriteheight: "100",
           getShape: function(){
               return (new me.Rect(0, 0, 100, 100)).toPolygon();
           }
       }]);
   //this broken will say that the tower is not broken and is still up.
       this.broken = false;
       // this will be the health of the tower.
       this.health = 10;
       //if the screen move and we can't see the tower it is always goingn to check if has been destroyed or not.
       this.alwaysUpdate = true;
       //if the player runs into the tower he will coolide with it and be able to destroy it
       this.body.onCollision = this.onCollision.bind(this);
       //this is for other collisions and objects that will be for character.
       this.type = "PlayerBaseEntity";
   },

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

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

    onCollison: function (){

    }

});
//this is the enemy information
game.EnemyBaseEntitiy = 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, 100)).toPolygon();
           }
       }]);
       this.broken = false;
       this.health = 10;
       this.alwaysUpdate = true;
       this.body.onCollision = this.onCollision.bind(this);

       this.type = "EnemyBaseEntity";
   },

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

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

    onCollison: function (){

    }

});<|endoftext|>


<commit_before>//this is for the player. a basic setup
/**/game.PlayerEntity = me.Entity.extend ({
	init:function(x , y, settings){
       this._super(me.Entity, 'init', [x, y, {
       	image: "player",
       	width: 64,
       	height: 64,
       	spritewidth: "64",
       	spriteheight: "64",
       	getShape: function(){
       		return(new me.Rect(0, 0, 64, 64)).toPolygon();
       	}
       }]);

       this.body.setVelocity(4,20);
//keeps track of the direction your character is going
       this.facing = "right";
       this.now = new Date().getTime();
       this.lastHit = this.now;
       this.lastAttack = new Date().getTime(); //havent used it yet.
//screen will now follow the player
       me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

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

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

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

	update: function(delta){
		this.now = new Date().getTime();
	// to make sure if the sure is pressing on the key//adds to the position of my x by adding the velocity defined in setVelocity() and multiplying it by me.timer.tick makes the movement smooth
      if(me.input.isKeyPressed("right")) {
        this.body.vel.x += this.body.accel.x * me.timer.tick;
        this.facing = "right";
        this.flipX(true);
       }
// to help my player left and right
       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;
      }
// so my player will now jump.
      if (me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling) {
      	this.body.jumping = true;
      	this.body.vel.y -= this.body.accel.y * me.timer.tick;
      }
//to make my player attack so he can damage the towers
        if (me.input.isKeyPressed("attack")) {
       	if (!this.renderable.isCurrentAnimation("attack")) {
       		console.log(!this.renderable.isCurrentAnimation("attack"))
// sets animation to attack then once it  over it goes back to idle
       	this.renderable.setCurrentAnimation("attack", "idle");
//once the animation is over the sequence continues from the first animation idle no the one where we last left off from.
       	this.renderable.setAnimationFrame();
       	}
       }

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

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

       this.body.update(delta);

       this._super(me.Entity, "update", [delta]);
       return true;
	},
/**/
     collideHandler: function (response) {
     	if (response.b.type=== 'EnemyBaseEntity') {
        var ydif = this.pos.y - response.b.pos.y;
        var xdif = this.pos.x -response.b.pos.x;

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

        if(ydif<-40 && xdif< 70 && xdif>-35){
            this.body.falling = false;
            this.body.vel.y = -1;
          }

        else if (xdif >-35 && this.facing=== 'right' && (xdif<0)) {
            this.body.vel.x = 0;
            this.pos.x = this.x -1;
          }
        else if(xdif<70 && this.facing==='left' && (xdif>0)){
            this.body.vel.x = 0;
            this.pos.x = this.pos.x +1;
          }
        if (!this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000) {
        	console.log("tower Hit");
        	this.lastHit = this.now;
        	response.b.loseHealth();
          }
     	}
     }

});
/**/

game.PlayerBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
       this._super(me.Entity, 'init', [x, y, {
       image:"tower",
    	width:100,
    	height:100,
    	spritewidth:"100",
    	spriteheight:"100",
        getShape:function(){
        return (new me.Rect(0, 0, 100, 100)).toPolygon();

       }

    }]);

    this.broken = false;
    this.health = 10;
    this.alwaysUpdate = true;
    this.body.onCollision = this.onCollision.bind(this);

    this.type = "PlayerBase";

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

    },

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

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

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

    onCollision: function(){

    }

});
/**/
game.EnemyBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
       this._super(me.Entity, 'init', [x, y, {
       image:"tower",
    	width:100,
    	height:100,
    	spritewidth:"100",
    	spriteheight:"100",
        getShape:function(){
        return (new me.Rect(0, 0, 100, 70)).toPolygon();
       }

    }]);

    this.broken = false;
    this.health = 10;
    this.alwaysUpdate = true;
    this.body.onCollision = this.onCollision.bind(this);

    this.type = "EnemyBaseEntity";

     this.renderable.addAnimation("idle", [0]);
    //sets animation for enemybase
     this.renderable.addAnimation("broken",[1]);
    //burned base
     this.renderable.setCurrentAnimation("idle");
    // fixed base
    },

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

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

    onCollision: function(){

    },

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

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

    	this.type = "EnemyCreep";
    	//uses image
    	this.renderable.addAnimation("walk", [3, 4, 5], 80);
    	this.renderable.setCurrentAnimation("walk");
    },

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

    //makes the enemy move.
     this.body.vel.x -= this.body.accel.x * me.timer.tick;
//to collide with our player
    me.collision.check(this, true, this.collideHandler.bind(this), true);

    this.body.update(delta);

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

    return true;
    },

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

});

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

       this.alwaysUpdate = true;
    },

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

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

<commit_msg>commit 27<commit_after>//this is for the player. a basic setup
/**/game.PlayerEntity = me.Entity.extend ({
	init:function(x , y, settings){
       this._super(me.Entity, 'init', [x, y, {
       	image: "player",
       	width: 64,
       	height: 64,
       	spritewidth: "64",
       	spriteheight: "64",
       	getShape: function(){
       		return(new me.Rect(0, 0, 64, 64)).toPolygon();
       	}
       }]);
       this.type = "PlayerEntity";
       this.body.setVelocity(4,20);
//keeps track of the direction your character is going
       this.facing = "right";
       this.now = new Date().getTime();
       this.lastHit = this.now;
       this.lastAttack = new Date().getTime(); //havent used it yet.
//screen will now follow the player
       me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

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

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

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

	update: function(delta){
		this.now = new Date().getTime();
	// to make sure if the sure is pressing on the key//adds to the position of my x by adding the velocity defined in setVelocity() and multiplying it by me.timer.tick makes the movement smooth
      if(me.input.isKeyPressed("right")) {
        this.body.vel.x += this.body.accel.x * me.timer.tick;
        this.facing = "right";
        this.flipX(true);
       }
// to help my player left and right
       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;
      }
// so my player will now jump.
      if (me.input.isKeyPressed("jump") && !this.body.jumping && !this.body.falling) {
      	this.body.jumping = true;
      	this.body.vel.y -= this.body.accel.y * me.timer.tick;
      }
//to make my player attack so he can damage the towers
        if (me.input.isKeyPressed("attack")) {
       	if (!this.renderable.isCurrentAnimation("attack")) {
       		console.log(!this.renderable.isCurrentAnimation("attack"))
// sets animation to attack then once it  over it goes back to idle
       	this.renderable.setCurrentAnimation("attack", "idle");
//once the animation is over the sequence continues from the first animation idle no the one where we last left off from.
       	this.renderable.setAnimationFrame();
       	}
       }

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

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

       this.body.update(delta);

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

	},

	loseHealth: function(damage){
        this.health = this.health - damage;
	},
/**/
    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(ydif<-40 && xdif< 70 && xdif>-35){
            this.body.falling = false;
            this.body.vel.y = -1;
          }

        else if (xdif >-35 && this.facing=== 'right' && (xdif<0)) {
            this.body.vel.x = 0;
            this.pos.x = this.x -1;
          }
        else if(xdif<70 && this.facing==='left' && (xdif>0)){
            this.body.vel.x = 0;
            this.pos.x = this.pos.x +1;
          }
        if (!this.renderable.isCurrentAnimation("attack") && this.now-this.lastHit >= 1000) {
        	console.log("tower Hit");
        	this.lastHit = this.now;
        	response.b.loseHealth();
          }
     	}
     }

});
/**/

game.PlayerBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
       this._super(me.Entity, 'init', [x, y, {
       image:"tower",
    	width:100,
    	height:100,
    	spritewidth:"100",
    	spriteheight:"100",
        getShape:function(){
        return (new me.Rect(0, 0, 100, 100)).toPolygon();

       }

    }]);

    this.broken = false;
    this.health = 10;
    this.alwaysUpdate = true;
    this.body.onCollision = this.onCollision.bind(this);

    this.type = "PlayerBase";

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

    },

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

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

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

    onCollision: function(){

    }

});
/**/
game.EnemyBaseEntity = me.Entity.extend({
    init : function(x, y, settings){
       this._super(me.Entity, 'init', [x, y, {
       image:"tower",
    	width:100,
    	height:100,
    	spritewidth:"100",
    	spriteheight:"100",
        getShape:function(){
        return (new me.Rect(0, 0, 100, 70)).toPolygon();
       }

    }]);

    this.broken = false;
    this.health = 10;
    this.alwaysUpdate = true;
    this.body.onCollision = this.onCollision.bind(this);

    this.type = "EnemyBaseEntity";

     this.renderable.addAnimation("idle", [0]);
    //sets animation for enemybase
     this.renderable.addAnimation("broken",[1]);
    //burned base
     this.renderable.setCurrentAnimation("idle");
    // fixed base
    },

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

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

    onCollision: function(){

    },

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

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

    	this.type = "EnemyCreep";
    	//uses image
    	this.renderable.addAnimation("walk", [3, 4, 5], 80);
    	this.renderable.setCurrentAnimation("walk");
    },

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

    //makes the enemy move.
     this.body.vel.x -= this.body.accel.x * me.timer.tick;
//to collide with our player
    me.collision.check(this, true, this.collideHandler.bind(this), true);

    this.body.update(delta);

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

    return true;
    },

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

    		this.attacking=true;
    	//	this.lastAttacking=this.now;
    		this.body.vel.x = 0;
    		//keeps moving to the right to maintain its position.
    		this.pos.x = this.pos.x + 1;
    		//checks that it has been at least 1 seocnd since this creep hti the base
    		if ((this.now-this.lastHit >=1000)){
            //updates the lasthit timer
    			this.lastHit = this.now;
    		// makes the player base call its loseHealth function and passesa damage of 1.
    		response.b.loseHealth(1);
    	    }
    	}
    }

});

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

       this.alwaysUpdate = true;
    },

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

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

<|endoftext|>


<commit_before>game.PlayerEntity = me.Entity.extend({
    init: function (x, y, settings) {
        this._super(me.Entity, 'init', [x, y, {
                image: "player",
                width: 64,
                height: 64,
                spritewidth: "64",
                spriteheight: "64",
                getShape: function () {
                    return(new me.Rect(0, 0, 64, 64)).toPolygon();
                }
            }]);
        //sets velocity
        this.body.setVelocity(5, 20);

        this.facing = "right";
        this.now = new Date().getTime();
        this.lastHit = this.now;
        this.lastAttack = new Date().getTime();
        //makes screen follow player
        me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

        //adds animations for name, frames and speed
        this.renderable.addAnimation("idle", [78]);
        this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
        this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);
        //sets current animation for game start
        this.renderable.setCurrentAnimation("idle");
    },
    update: function (delta) {
        this.now = new Date().getTime();
        //control for moving right
        if (me.input.isKeyPressed("right")) {
            this.body.vel.x += this.body.accel.x * me.timer.tick;
            this.facing = "right";
            this.flipX(true);
        }
        //control for moving left
        else if (me.input.isKeyPressed("left")) {
            this.body.vel.x -= this.body.accel.x * me.timer.tick;
            this.facing = "left";
            this.flipX(false);
        }
        //for when mario stands still
        else {
            this.body.vel.x = 0;
        }
        //control for jumping
        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
                this.body.vel.y = -this.body.maxVel.y * me.timer.tick;
                this.body.jumping = true;
            }

        }

        if (me.input.isKeyPressed('attack')) {
            if (!this.renderable.isCurrentAnimation("attack")) {
                //sets animation to attack than to idle
                this.renderable.setCurrentAnimation("attack", "idle");
                //begin from first animation
                this.renderable.setAnimationFrame();
            }

        } else if (this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")) {
            if (!this.renderable.isCurrentAnimation("walk")) {
                this.renderable.setCurrentAnimation("walk");
                this.renderable.setAnimationFrame();
            }
        } else if (!this.renderable.isCurrentAnimation("attack")) {
            this.renderable.setCurrentAnimation("idle");
        }
        me.collision.check(this, true, this.collideHandler.bind(this), true);
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);
        return true;
    },
    collideHandler: function (response) {
        //collisions with the enemy base
        if (response.b.type === 'EnemyBaseEntity') {
            var ydif = this.pos.y - response.b.pos.y;
            var xdif = this.pos.x - response.b.pos.x;

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

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

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

        this.type = "PlayerBaseEntity";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
    update: function (delta) {
        if (this.health <= 0) {
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);
        return true;
    },
    loseHealth: function () {
        this.health = this.health - damage;
    },
    onCollision: function (damage) {

    }

});

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

        this.type = "EnemyBaseEntity";
        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
    update: function (delta) {
        if (this.health <= 0) {
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);
        return true;
    },
    onCollision: function () {

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

});

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

        this.type = "EnemyCreep";

        this.renderable.addAnimation("walk", [3, 4, 5], 80);
        this.renderable.setCurrentAnimation("walk");
    },
    update: function (delta) {

        this.now = new Date().getTime();

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

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

        this.body.update(delta);

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

        return true;
    },
    onCollision: function (response) {
        if (response.b.type === 'PlayerBase') {
            this.attacking = true;
            // this.lastAtacking = this.now;
            this.body.vel.x = 0;
            this.pos.x = this.pos.x + 1;
            if (this.now - this.lastHit >= 1000) {
                this.lastHit = this.now;
                response.b.loseHealth(1);
            }
        }
    }
});

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", 1400, 0, {});
            me.game.world.addChild(creepe, 5);
        }

        return true;
    }
});<commit_msg>creeps now attack bases<commit_after>game.PlayerEntity = me.Entity.extend({
    init: function (x, y, settings) {
        this._super(me.Entity, 'init', [x, y, {
                image: "player",
                width: 64,
                height: 64,
                spritewidth: "64",
                spriteheight: "64",
                getShape: function () {
                    return(new me.Rect(0, 0, 64, 64)).toPolygon();
                }
            }]);
        //sets velocity
        this.body.setVelocity(5, 20);

        this.facing = "right";
        this.now = new Date().getTime();
        this.lastHit = this.now;
        this.lastAttack = new Date().getTime();
        //makes screen follow player
        me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

        //adds animations for name, frames and speed
        this.renderable.addAnimation("idle", [78]);
        this.renderable.addAnimation("walk", [117, 118, 119, 120, 121, 122, 123, 124, 125], 80);
        this.renderable.addAnimation("attack", [65, 66, 67, 68, 69, 70, 71, 72], 80);
        //sets current animation for game start
        this.renderable.setCurrentAnimation("idle");
    },
    update: function (delta) {
        this.now = new Date().getTime();
        //control for moving right
        if (me.input.isKeyPressed("right")) {
            this.body.vel.x += this.body.accel.x * me.timer.tick;
            this.facing = "right";
            this.flipX(true);
        }
        //control for moving left
        else if (me.input.isKeyPressed("left")) {
            this.body.vel.x -= this.body.accel.x * me.timer.tick;
            this.facing = "left";
            this.flipX(false);
        }
        //for when mario stands still
        else {
            this.body.vel.x = 0;
        }
        //control for jumping
        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
                this.body.vel.y = -this.body.maxVel.y * me.timer.tick;
                this.body.jumping = true;
            }

        }

        if (me.input.isKeyPressed('attack')) {
            if (!this.renderable.isCurrentAnimation("attack")) {
                //sets animation to attack than to idle
                this.renderable.setCurrentAnimation("attack", "idle");
                //begin from first animation
                this.renderable.setAnimationFrame();
            }

        } else if (this.body.vel.x !== 0 && !this.renderable.isCurrentAnimation("attack")) {
            if (!this.renderable.isCurrentAnimation("walk")) {
                this.renderable.setCurrentAnimation("walk");
                this.renderable.setAnimationFrame();
            }
        } else if (!this.renderable.isCurrentAnimation("attack")) {
            this.renderable.setCurrentAnimation("idle");
        }
        me.collision.check(this, true, this.collideHandler.bind(this), true);
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);
        return true;
    },
    collideHandler: function (response) {
        //collisions with the enemy base
        if (response.b.type === 'EnemyBaseEntity') {
            var ydif = this.pos.y - response.b.pos.y;
            var xdif = this.pos.x - response.b.pos.x;

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

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

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

        this.type = "PlayerBase";

        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
    update: function (delta) {
        if (this.health <= 0) {
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);
        return true;
    },
    loseHealth: function (damage) {
        this.health = this.health - damage;
    },
    onCollision: function () {

    }

});

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

        this.type = "EnemyBaseEntity";
        this.renderable.addAnimation("idle", [0]);
        this.renderable.addAnimation("broken", [1]);
        this.renderable.setCurrentAnimation("idle");
    },
    update: function (delta) {
        if (this.health <= 0) {
            this.broken = true;
            this.renderable.setCurrentAnimation("broken");
        }
        this.body.update(delta);
        this._super(me.Entity, "update", [delta]);
        return true;
    },
    onCollision: function () {

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

});

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

        this.type = "EnemyCreep";

        this.renderable.addAnimation("walk", [3, 4, 5], 80);
        this.renderable.setCurrentAnimation("walk");
    },
    update: function (delta) {

        this.now = new Date().getTime();

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

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

        this.body.update(delta);

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

        return true;
    },
    collideHandler: function (response) {
        if (response.b.type === "PlayerBase") {
            console.log("attack");
            this.attacking = true;
            this.lastAtacking = this.now;
            this.body.vel.x = 0;
            this.pos.x = this.pos.x + 1;
            if (this.now - this.lastHit >= 1000) {
                this.lastHit = this.now;
                response.b.loseHealth(1);
            }
        }
    }
});

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", 1400, 0, {});
            me.game.world.addChild(creepe, 5);
        }

        return true;
    }
});<|endoftext|>


<commit_before>import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@material-ui/core/styles'
import InviteFriend from 'js/components/Settings/Profile/InviteFriendContainer'
import Stat from 'js/components/Settings/Profile/StatComponent'
import HappyIcon from '@material-ui/icons/Mood'
import Paper from '@material-ui/core/Paper'
import Typography from '@material-ui/core/Typography'

const spacingPx = 6

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap',
    margin: -spacingPx,
  },
  inviteFriendPaper: {
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    padding: 18,
    margin: spacingPx,
    flex: 2,
    flexBasis: '40%',
    minWidth: 200,
  },
  // stat: {}, // TODO: need to override inline styles in StatComponent
  thankYouPaper: {
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
    flexBasis: '50%',
    minWidth: 200,
    padding: 20,
    margin: spacingPx,
  },
  happyIcon: {
    minHeight: 24,
    minWidth: 24,
    marginRight: 8,
    color: 'black', // TODO: fix colors
  },
})

class ProfileInviteFriend extends React.Component {
  render() {
    const { app, classes, user } = this.props
    const friendWord = user.numUsersRecruited === 1 ? 'friend' : 'friends'
    const statStyle = {
      margin: spacingPx,
      flex: 1,
      paddingTop: 40,
      paddingBottom: 40,
    }
    return (

        <Paper elevation={1} className={classes.inviteFriendPaper}>
          <InviteFriend
            user={user}
            style={{
              width: '100%',
              maxWidth: 300,
            }}
          />
        </Paper>
        <Stat
          stat={user.numUsersRecruited}
          statText={`${friendWord} recruited`}
          style={statStyle}
        />
        <Stat
          stat={app.referralVcReward}
          statText={'extra Hearts when you recruit a new friend'}
          style={statStyle}
        />
        <Paper className={classes.thankYouPaper}>
          <HappyIcon className={classes.happyIcon} />
          <Typography variant={'body2'}>
            Thank you! Every new person raises more money for charity, and we
            depend on people like you to get the word out.
          </Typography>
        </Paper>

    )
  }
}

ProfileInviteFriend.propTypes = {
  app: PropTypes.shape({
    referralVcReward: PropTypes.number.isRequired,
  }),
  classes: PropTypes.object.isRequired,
  user: PropTypes.shape({
    numUsersRecruited: PropTypes.number.isRequired,
  }),
}

export default withStyles(styles)(ProfileInviteFriend)
<commit_msg>Use a functional component<commit_after>import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@material-ui/core/styles'
import InviteFriend from 'js/components/Settings/Profile/InviteFriendContainer'
import Stat from 'js/components/Settings/Profile/StatComponent'
import HappyIcon from '@material-ui/icons/Mood'
import Paper from '@material-ui/core/Paper'
import Typography from '@material-ui/core/Typography'

const spacingPx = 6

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap',
    margin: -spacingPx,
  },
  inviteFriendPaper: {
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    padding: 18,
    margin: spacingPx,
    flex: 2,
    flexBasis: '40%',
    minWidth: 200,
  },
  thankYouPaper: {
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
    flexBasis: '50%',
    minWidth: 200,
    padding: 20,
    margin: spacingPx,
  },
  happyIcon: {
    minHeight: 24,
    minWidth: 24,
    marginRight: 8,
    color: 'black', // TODO: fix colors
  },
})

const ProfileInviteFriend = props => {
  const { app, classes, user } = props
  const friendWord = user.numUsersRecruited === 1 ? 'friend' : 'friends'
  const statStyle = {
    margin: spacingPx,
    flex: 1,
    paddingTop: 40,
    paddingBottom: 40,
  }
  return (

      <Paper elevation={1} className={classes.inviteFriendPaper}>
        <InviteFriend
          user={user}
          style={{
            width: '100%',
            maxWidth: 300,
          }}
        />
      </Paper>
      <Stat
        stat={user.numUsersRecruited}
        statText={`${friendWord} recruited`}
        style={statStyle}
      />
      <Stat
        stat={app.referralVcReward}
        statText={'extra Hearts when you recruit a new friend'}
        style={statStyle}
      />
      <Paper className={classes.thankYouPaper}>
        <HappyIcon className={classes.happyIcon} />
        <Typography variant={'body2'}>
          Thank you! Every new person raises more money for charity, and we
          depend on people like you to get the word out.
        </Typography>
      </Paper>

  )
}

ProfileInviteFriend.propTypes = {
  app: PropTypes.shape({
    referralVcReward: PropTypes.number.isRequired,
  }),
  classes: PropTypes.object.isRequired,
  user: PropTypes.shape({
    numUsersRecruited: PropTypes.number.isRequired,
  }),
}

export default withStyles(styles)(ProfileInviteFriend)
<|endoftext|>


<commit_before>"use strict";

var util = require('util');
var stream = require('stream');
var _ = require('lodash');

var RiakFsWriteStream = function(riakfs, _path, options) {

    if (!(this instanceof RiakFsWriteStream)) {
        return new RiakFsWriteStream(riakfs, _path, options);
    }

    options = _.partialRight(_.merge, _.defaults)(options || {}, {
        flags: 'w',
        encoding: null,
        highWaterMark: 256 * 1024,
        decodeStrings: true
    });

    stream.Writable.call(this, options);

    this.flags = options.flags;
    this.riakfs = riakfs;
    this.path = _path;
    this.bytesWritten = 0;

    this.open();

    this.once('finish', this.close);
};

util.inherits(RiakFsWriteStream, stream.Writable);

module.exports = RiakFsWriteStream;

RiakFsWriteStream.prototype.destroySoon = RiakFsWriteStream.prototype.end;

RiakFsWriteStream.prototype.destroy = function() {
    if (this.destroyed) {
        return;
    }

    this.destroyed = true;

    if (this.fd) {
        this.close();
    }
};

RiakFsWriteStream.prototype.open = function() {
    var self = this;

    if (self._opening || self.fd) {
        return;
    }
    self._opening = true;

    self.riakfs.open(self.path, self.flags)
        .then(function(fd) {
            self.fd = fd;
            self.emit('open', self.fd);
        })
        .catch (function(err) {
            self.emit('error', err);
        });
};

RiakFsWriteStream.prototype.close = function() {
    var self = this;

    function _close() {
        self.riakfs.close(self.fd)
            .then(function() {
                self.fd = null;
                self.emit('close');
            })
            .catch (function(err) {
                self.emit('error', err);
            });
    }

    if (self._closed) {
        return process.nextTick(self.emit.bind(self, 'close'));
    }

    if (!self.fd) {
        return self.once('open', _close);
    }

    self._closed = true;

    _close();
};

RiakFsWriteStream.prototype._write = function(chunk, encoding, callback) {
    var self = this;

    if (!self.fd) {
        return self.once('open', function() {
            self._write(chunk, encoding, callback);
        });
    }

    self.riakfs.write(self.fd, chunk, 0, chunk.length, null)
        .then(function(written) {
            self.bytesWritten += written;
            return true;
        })
        .nodeify(callback);
};
<commit_msg>Catch finish event and close all resources before re-emitting it<commit_after>"use strict";

var util = require('util');
var stream = require('stream');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;

var RiakFsWriteStream = function(riakfs, _path, options) {

    if (!(this instanceof RiakFsWriteStream)) {
        return new RiakFsWriteStream(riakfs, _path, options);
    }

    options = _.partialRight(_.merge, _.defaults)(options || {}, {
        flags: 'w',
        encoding: null,
        highWaterMark: 256 * 1024,
        decodeStrings: true
    });

    stream.Writable.call(this, options);

    this.flags = options.flags;
    this.riakfs = riakfs;
    this.path = _path;
    this.bytesWritten = 0;

    this.open();
};

util.inherits(RiakFsWriteStream, stream.Writable);

module.exports = RiakFsWriteStream;

RiakFsWriteStream.prototype.destroySoon = RiakFsWriteStream.prototype.end;

// catch Writable `finish` event and close everything first
// this is a temporary fix for
// https://github.com/nodejs/node/issues/2994
// https://github.com/nodejs/node/issues/4672
// https://github.com/nodejs/node/pull/2314
RiakFsWriteStream.prototype.emit = function () {
    var self = this, args = arguments, event = args[0];
    if(event !== 'finish'){
        EventEmitter.prototype.emit.apply(self, args);
    } else {
        self.once('close', function () {
            EventEmitter.prototype.emit.apply(self, args);
        });
        self.close();
    }
};

RiakFsWriteStream.prototype.destroy = function() {
    if (this.destroyed) {
        return;
    }

    this.destroyed = true;

    if (this.fd) {
        this.close();
    }
};

RiakFsWriteStream.prototype.open = function() {
    var self = this;

    if (self._opening || self.fd) {
        return;
    }
    self._opening = true;

    self.riakfs.open(self.path, self.flags)
        .then(function(fd) {
            self.fd = fd;
            self.emit('open', self.fd);
        })
        .catch (function(err) {
            self.emit('error', err);
        });
};

RiakFsWriteStream.prototype.close = function() {
    var self = this;

    function _close() {
        self.riakfs.close(self.fd)
            .then(function() {
                self.fd = null;
                self.emit('close');
            })
            .catch (function(err) {
                self.emit('error', err);
            });
    }

    if (self._closed) {
        return process.nextTick(self.emit.bind(self, 'close'));
    }

    if (!self.fd) {
        return self.once('open', _close);
    }

    self._closed = true;

    _close();
};

RiakFsWriteStream.prototype._write = function(chunk, encoding, callback) {
    var self = this;

    if (!self.fd) {
        return self.once('open', function() {
            self._write(chunk, encoding, callback);
        });
    }

    self.riakfs.write(self.fd, chunk, 0, chunk.length, null)
        .then(function(written) {
            self.bytesWritten += written;
            return true;
        })
        .nodeify(callback);
};
<|endoftext|>