From b95d678c93cc3189734d9b8c12d48d997b1c2667 Mon Sep 17 00:00:00 2001
From: septimus <septimus98@gmail.com>
Date: Sun, 2 Aug 2015 03:24:46 +0000
Subject: [PATCH] Lot's of changes. Implemented awesomeness.

---
 assets/textures/background.ase    | Bin 2722 -> 0 bytes
 assets/textures/background.png    | Bin 2411 -> 12812 bytes
 assets/textures/cloud.png         | Bin 0 -> 1075 bytes
 assets/textures/mario.png         | Bin 5764 -> 0 bytes
 assets/textures/plants.json       |   2 +-
 assets/textures/spot.png          | Bin 1786 -> 1400 bytes
 assets/textures/zombie.json       |  22 ++++++++
 assets/textures/zombie.png        | Bin 0 -> 3254 bytes
 assets/textures/zombie_scaled.png | Bin 1860 -> 0 bytes
 index.html                        |   1 +
 js/Pea.js                         |  26 +++++----
 js/campers/Zomper.js              |  64 +++++++++++++++++++---
 js/main.js                        | 111 +++++++++++++++++++++++++++-----------
 js/plants/Peashooter.js           |  36 ++++++++-----
 js/vector.js                      |  85 +++++++++++++++++++++++++++++
 server.js                         |   9 ++++
 16 files changed, 296 insertions(+), 60 deletions(-)
 delete mode 100644 assets/textures/background.ase
 create mode 100644 assets/textures/cloud.png
 delete mode 100644 assets/textures/mario.png
 create mode 100644 assets/textures/zombie.json
 create mode 100644 assets/textures/zombie.png
 delete mode 100644 assets/textures/zombie_scaled.png
 create mode 100644 js/vector.js
 create mode 100644 server.js

diff --git a/assets/textures/background.ase b/assets/textures/background.ase
deleted file mode 100644
index 09a603451dd37b88061e792a13b911e0f33b8b0e..0000000000000000000000000000000000000000
GIT binary patch
diff --git a/assets/textures/background.png b/assets/textures/background.png
index db5516cac1dfb43809fed71631369348cb2d79b1..283cfcd39106e23d6dabe06fe6ba8937475e6b79 100644
GIT binary patch
diff --git a/assets/textures/cloud.png b/assets/textures/cloud.png
new file mode 100644
index 0000000000000000000000000000000000000000..366b56da2acecd2e729db532b54d2e96f1122658
GIT binary patch
diff --git a/assets/textures/mario.png b/assets/textures/mario.png
deleted file mode 100644
index d31af6d5a558b364b504caeb0f2d77e58220287d..0000000000000000000000000000000000000000
GIT binary patch
diff --git a/assets/textures/plants.json b/assets/textures/plants.json
index 369b5c4..f70b751 100644
--- a/assets/textures/plants.json
+++ b/assets/textures/plants.json
@@ -13,7 +13,7 @@
 			"frame": {"x": 48, "y": 16, "w": 16, "h": 16}
 		},
 		"pea":{
-			"frame": {"x": 0, "y": 36, "w": 16, "h": 16}
+			"frame": {"x": 0, "y": 32, "w": 16, "h": 16}
 		}
 	}
 }
\ No newline at end of file
diff --git a/assets/textures/spot.png b/assets/textures/spot.png
index 61d6f14157e6a3f8412e0f9c23aa34697c3fd6c1..2f4120c185164a3b46fa78c94a6d7b73c839b142 100644
GIT binary patch
diff --git a/assets/textures/zombie.json b/assets/textures/zombie.json
new file mode 100644
index 0000000..78799de
--- /dev/null
+++ b/assets/textures/zombie.json
@@ -0,0 +1,22 @@
+{
+	"frames": {
+		"zombie_0": {
+			"frame": {"x": 0, "y": 0, "w": 32, "h": 32}
+		},
+		"zombie_1":{
+			"frame": {"x": 32, "y": 0, "w": 32, "h": 32}
+		},
+		"zombie_2":{
+			"frame": {"x": 64, "y": 0, "w": 32, "h": 32}
+		},
+		"zombie_3":{
+			"frame": {"x": 96, "y": 0, "w": 32, "h": 32}
+		},
+		"zombie_hit_up":{
+			"frame": {"x": 0, "y": 0, "w": 32, "h": 32}
+		},
+		"zombie_hit_down":{
+			"frame": {"x": 32, "y": 0, "w": 32, "h": 32}
+		}
+	}
+}
\ No newline at end of file
diff --git a/assets/textures/zombie.png b/assets/textures/zombie.png
new file mode 100644
index 0000000000000000000000000000000000000000..baf0a4f0717c28f811041c44229d8781ffc373ab
GIT binary patch
diff --git a/assets/textures/zombie_scaled.png b/assets/textures/zombie_scaled.png
deleted file mode 100644
index 97f151442275abf7c84639a8b97ba973e01e3d87..0000000000000000000000000000000000000000
GIT binary patch
diff --git a/index.html b/index.html
index 9667028..dde31f9 100644
--- a/index.html
+++ b/index.html
@@ -1,6 +1,7 @@
 <html>
 	<head>
 		<script src="js/phaser.min.js"></script>
+		<script src="js/Pea.js"></script>
 		<script src="js/plants/Peashooter.js"></script>
 		<script src="js/plants/Sunflower.js"></script>
 		<script src="js/campers/Zomper.js"></script>
diff --git a/js/Pea.js b/js/Pea.js
index 84efd05..0c40ae2 100644
--- a/js/Pea.js
+++ b/js/Pea.js
@@ -1,11 +1,17 @@
-function Pea(manager, column, row) {
+function Pea(manager, sprite_x, sprite_y, damage, speed) {
 	this.manager = manager;
-	this.column = column;
-	this.row = row;
-}
-
-Pea.prototype.update = function() {
-
-	this.sprite.x += this.movement_speed * game.time.elapsed;
-
-};
\ No newline at end of file
+	this.damage = damage;
+	
+	this.sprite = game.add.sprite(sprite_x, sprite_y, 'plants');
+	this.sprite.width = 64;
+	this.sprite.height = 64;
+	
+	this.sprite.animations.add('idle', ['pea'], 2, true, false);
+	this.sprite.animations.play('idle', 2, true);
+	
+	game.physics.arcade.enable(this.sprite);
+	this.sprite.body.velocity.x = speed;
+	this.sprite.body.width = 16;
+	
+	this.sprite.anchor.setTo(0.5, 0);
+}
\ No newline at end of file
diff --git a/js/campers/Zomper.js b/js/campers/Zomper.js
index f8a0379..5f62a5d 100644
--- a/js/campers/Zomper.js
+++ b/js/campers/Zomper.js
@@ -1,18 +1,70 @@
 function Zomper(manager, row, sprite_y) {
 
+	// How many times we have to be hit before we go down
+	this.maxHealth = 2;
+	this.health = 2;
 	this.manager = manager;
-
 	this.row = row;
+	this.damage = 1;
+	this.speed = 100;
 
-	this.movement_speed = 0.05;
-	this.health = 100;
 	this.sprite = game.add.sprite(800, sprite_y, 'zombie');
-	this.sprite.scale.x = -1;
-
+	this.sprite.smoothed = false;
+	this.sprite.width = 64;
+	this.sprite.height = 64;
+	
+	this.sprite.animations.add('idle', ['zombie_1', 'zombie_2'], 2, true, false);
+	this.sprite.animations.add('hurt', ['zombie_3'], 2, true, false);
+	this.sprite.animations.add('hit', ['zombie_0'], 2, true, false);
+	this.sprite.animations.add('recoil', ['zombie_1'], 2, true, false);
+	this.sprite.animations.play('idle', 2, true);
+	
+	game.physics.arcade.enable(this.sprite);
+	this.sprite.body.velocity.x = -this.speed;
+	
+	this.sprite.anchor.setTo(0.5, 0);
 }
 
+Zomper.prototype.hit = function(damage) {
+	
+	this.sprite.animations.play('hurt', 2, false);
+	this.health -= damage;
+	
+	this.sprite.body.velocity.x += 175;
+	
+	if (this.health <= 0) {
+		this.die = true;
+	}
+};
+
 Zomper.prototype.update = function() {
+	
+	if(this.sprite.body.velocity.x < -this.speed) this.sprite.body.velocity.x = -this.speed;
+	
+	if(this.sprite.body.velocity.x > -this.speed) this.sprite.body.velocity.x -= 2;
+	
+	var time_since = game.time.now - this.time_since_cooldown;
+
+	var plant = this.manager.getClosestPlant(this.sprite.x);
+
+	// if theres a plant in this row and its 10 pixels away
+	if(plant != null && Phaser.Math.difference(plant.sprite.x, this.sprite.x) < 10) {
 
-	this.sprite.x -= this.movement_speed * game.time.elapsed;
+		var currentAnim = this.sprite.animations.currentAnim;
 
+		if(currentAnim.name == 'idle' && time_since >= this.cooldown) {
+			this.sprite.animations.play('hit', 4, false);
+		}
+		else if(currentAnim.isFinished && currentAnim.name == 'hit'){
+			plant.hit(1);
+			this.sprite.animations.play('recoil', 2, false);
+		}
+		else if(currentAnim.isFinished && currentAnim.name == 'recoil'){
+			this.time_since_cooldown = game.time.now;
+			this.sprite.animations.play('idle', 2, true);
+		}
+	}
+	else {
+		this.sprite.animations.play('idle', 2, true);
+	}
 };
\ No newline at end of file
diff --git a/js/main.js b/js/main.js
index a36842f..d302804 100644
--- a/js/main.js
+++ b/js/main.js
@@ -1,4 +1,4 @@
-var game = new Phaser.Game(800, 600, Phaser.Auto, 'game');
+var game = new Phaser.Game(800, 576, Phaser.Auto, 'game');
 
 window.onload = function() {
 	game.state.add('gameState', new GameState());
@@ -7,7 +7,6 @@ window.onload = function() {
 
 function GameState() {
 	this.background = null;
-	this.mario = null;
 
 	this.manager = {
 		offsets: null,
@@ -16,17 +15,7 @@ function GameState() {
 		plants: [],
 		campers: [],
 		peas: [],
-		types: {
-			basic : {
-
-				animations: {
-					idle: [0]
-				},
-				update: function() {
-
-				}
-			}
-		},
+		last_spawn_time: 0,
 		setup: function(offsets) {
 			this.offsets = offsets;
 
@@ -40,6 +29,15 @@ function GameState() {
 				}
 			}
 		},
+		getClosestPlant: function(x) {
+			var closest = null;
+			for(var i = 0; i < this.plants.length; i++) {
+				if(closest == null || this.plants[i].sprite.x > closest.sprite.x) {
+					closest = this.plants[i];
+				}
+			}
+			return closest;
+		},
 		campersInRow: function(row) {
 			var count = 0;
 			for(var i = 0; i < this.campers.length; i++) {
@@ -77,10 +75,13 @@ function GameState() {
 				this, row, (64 + this.offsets.margin_y) * row + this.offsets.world_y - this.offsets.spot_y
 			));
 		},
-		addPea: function(column, row) {
+		shootPea: function(column, row, damage, speed) {
 			this.peas.push(new Pea(
+				this,
 				(64 + this.offsets.margin_x) * column + this.offsets.world_x - this.offsets.spot_x,
-				(64 + this.offsets.margin_y) * row + this.offsets.world_y - this.offsets.spot_y
+				(64 + this.offsets.margin_y) * row + this.offsets.world_y - this.offsets.spot_y,
+				damage,
+				speed
 			));
 		}
 	};
@@ -90,43 +91,93 @@ function GameState() {
 GameState.prototype = {
 	preload: function() {
 		game.load.image('background', 'assets/textures/background.png');
-		game.load.image('zombie', 'assets/textures/zombie_scaled.png');
 		game.load.image('spot', 'assets/textures/spot.png');
+		game.load.atlasJSONHash('zombie', 'assets/textures/zombie.png', 'assets/textures/zombie.json');
 		game.load.atlasJSONHash('plants', 'assets/textures/plants.png', 'assets/textures/plants.json');
 	},
 	create: function() {
+		
+		game.physics.startSystem(Phaser.Physics.ARCADE);
+		
 		this.background = game.add.sprite(0, 0, 'background');
 
 		game.stage.backgroundColor = '#FFFFFF';
-		this.mario = game.add.sprite(0, 0, 'zombie');
-		this.mario.width = 64;
-		this.mario.height = 64;
 
 		this.manager.setup({
 			world_x: 0,
-			world_y: 224,
+			world_y: 248,
 			margin_x: 16,
 			margin_y: 0,
 			spot_x: 0,
-			spot_y: 16
+			spot_y: 0
 		});
+		
 		this.manager.addPlant(0, 0, 'basic');
-		this.manager.addPlant(1, 1, 'basic');
-		this.manager.addCamper(1, 'basic');
-
+		this.manager.addPlant(0, 1, 'basic');
+		this.manager.addPlant(0, 2, 'basic');
+		this.manager.addPlant(0, 3, 'basic');
+		this.manager.addPlant(0, 4, 'basic');
 	},
 	update: function() {
+		
+		var last_spawn_time = game.time.now - this.manager.last_spawn_time;
+
+		// spawn a new camper every 2 seconds if theirs less than 10 in the game
+		if(this.manager.campers.length < 10 && last_spawn_time >= 2000) {
+			var row = Math.floor(Math.random() * (this.manager.height - 1));
+			this.manager.addCamper(row, 'basic');
+			this.manager.last_spawn_time = game.time.now;
+		}
 
-		//if(this.campers.length < 10) {
-		//}
-
+		// update all the plants
 		for(var i = 0; i < this.manager.plants.length; i++) {
 			this.manager.plants[i].update();
 		}
 
-		for(var i = 0; i < this.manager.campers.length; i++) {
-			this.manager.campers[i].update();
+		// clean up any campers who have died
+		var i = 0; while(i < this.manager.campers.length) {
+			var camper = this.manager.campers[i];
+			
+			camper.update();
+			
+			if(camper.die) {
+				camper.sprite.destroy();
+				this.manager.campers.splice(i, 1);
+			}
+			
+			i++;
 		}
-
+		
+		// clean up any peas that have died
+		var i = 0; while(i < this.manager.peas.length) {
+			var pea = this.manager.peas[i];
+			
+			if(pea.die) {
+				pea.sprite.destroy();
+				this.manager.peas.splice(i, 1);
+			}
+			
+			i++;
+		}
+		
+		// check collisions between peas and campers
+		var i = 0; while(i < this.manager.peas.length) {
+			var pea = this.manager.peas[i];
+			
+			for(var c = 0; c < this.manager.campers.length; c++) {
+				var camper = this.manager.campers[c];
+				
+				if(game.physics.arcade.overlap(pea.sprite, camper.sprite)) {
+					pea.die = true;
+					camper.hit(pea.damage);
+				}
+			}
+			
+			i++;
+		}
+	},
+	render: function() {
+		//for(var i = 0; i < this.manager.campers.length; i++) game.debug.body(this.manager.campers[i].sprite);
+		//for(var i = 0; i < this.manager.peas.length; i++) game.debug.body(this.manager.peas[i].sprite);
 	}
 };
\ No newline at end of file
diff --git a/js/plants/Peashooter.js b/js/plants/Peashooter.js
index afcef44..099cb95 100644
--- a/js/plants/Peashooter.js
+++ b/js/plants/Peashooter.js
@@ -1,34 +1,44 @@
 function Peashooter(manager, column, row, sprite_x, sprite_y) {
 
 	this.manager = manager;
-
 	this.column = column;
 	this.row = row;
-
-	this.last_action_time = 0;
+	this.time_since_cooldown = 0;
+	this.cooldown = 10;
 
 	this.sprite = game.add.sprite(sprite_x, sprite_y, 'plants');
 	this.sprite.smoothed = false;
 	this.sprite.width = 64;
 	this.sprite.height = 64;
-	this.sprite.animations.add('idle', [
-		'peashooter_3', 'peashooter_0'
-	], 2, true, false);
-	this.sprite.animations.add('shoot', [
-		'peashooter_0', 'peashooter_1', 'peashooter_2'
-	], 2, true, false);
+	
+	this.sprite.animations.add('idle', ['peashooter_3', 'peashooter_0'], 2, true, false);
+	this.sprite.animations.add('shoot', ['peashooter_0', 'peashooter_1', 'peashooter_2'], 2, true, false);
+	this.sprite.animations.add('recoil', ['peashooter_2', 'peashooter_1', 'peashooter_0'], 2, true, false);
 	this.sprite.animations.play('idle', 2, true);
 }
 
+Peashooter.prototype.hit = function(damage) {
+	
+};
+
 Peashooter.prototype.update = function() {
 
-	var time_since = game.time.now - this.last_action_time;
+	var time_since = game.time.now - this.time_since_cooldown;
 
 	if(this.manager.campersInRow(this.row) > 0) {
 
-		if(time_since >= 2000) {
-			this.last_action_time = game.time.now;
-			this.sprite.animations.play('shoot', 4, true);
+		var currentAnim = this.sprite.animations.currentAnim;
+
+		if(currentAnim.name == 'idle' && time_since >= this.cooldown) {
+			this.sprite.animations.play('shoot', 4, false);
+		}
+		else if(currentAnim.isFinished && currentAnim.name == 'shoot'){
+			this.manager.shootPea(this.column, this.row, 1, 200);
+			this.sprite.animations.play('recoil', 2, false);
+		}
+		else if(currentAnim.isFinished && currentAnim.name == 'recoil'){
+			this.time_since_cooldown = game.time.now;
+			this.sprite.animations.play('idle', 2, true);
 		}
 	}
 	else {
diff --git a/js/vector.js b/js/vector.js
new file mode 100644
index 0000000..b5cf319
--- /dev/null
+++ b/js/vector.js
@@ -0,0 +1,85 @@
+// Initializes the vector with x and y as its coordinates
+function Vector(x, y) {
+    'use strict';
+    this.x = x;
+    this.y = y;
+}
+
+// Returns the magnitude or length of the vector.
+Vector.prototype.magnitude = function () {
+    'use strict';
+    return Math.sqrt(this.x * this.x + this.y * this.y);
+}
+
+// Returns the square magnitude. Usefull when comparing lenghts of vectors,
+// as comparing magnitudes is slower, and comparing square magnitudes has the
+// same result.
+Vector.prototype.squareMagnitude = function () {
+    'use strict';
+    return Math.sqrt(this.x * this.x + this.y * this.y);
+}
+
+// Returns this vector with a magnitude of 1.
+Vector.prototype.normalized = function () {
+    'use strict';
+    return new Vector(this.x / this.magnitude(), this.y / this.magnitude());
+}
+
+// Gives this vector a magnitude of 1
+Vector.prototype.normalize = function () {
+    'use strict';
+    this.x /= this.magnitude();
+    this.y /= this.magnitude();
+}
+
+// Dot product of two vectors
+function dot (a, b) {
+    'use strict';
+    if (a instanceof Vector && b instanceof Vector) {
+        return a.x * b.x + a.y * b.y
+    }
+    
+    return undefined;
+}
+
+// Adds two vectors together, component wise.
+function add (a, b) {
+    'use strict';
+    if (a instanceof Vector && b instanceof Vector) {
+        return new Vector(a.x + b.x, a.y + b.y);
+    }
+    
+    return undefined
+}
+
+// Subtracts two vectors, component wise.
+function subtract (a, b) {
+    'use strict';
+    if (a instanceof Vector && b instanceof Vector) {
+        return new Vector(a.x - b.x, a.y - b.y);
+    }
+    
+    return undefined;
+}
+
+// Scalar multiplication of two vectors,
+function multiply (a, b) {
+    'use strict';
+    if (a instanceof Vector && b instanceof Vector) {
+        return new Vector(a.x * b.x, a.y * b.y);
+    }
+    
+    return undefined;
+}
+
+// Divides two vectors.
+function divide (a, b) {
+    'use strict';
+    if (a instanceof Vector && b instanceof Vector) {
+        return new Vector(a.x / b.x, a.y / b.y);
+    }
+    
+    return undefined;
+}
+
+// TODO: distance calculation function
\ No newline at end of file
diff --git a/server.js b/server.js
new file mode 100644
index 0000000..9cb5990
--- /dev/null
+++ b/server.js
@@ -0,0 +1,9 @@
+var express = require("express");
+var app = express();
+var fs = require('fs');
+
+// serve static files from the public directory
+app.use(express.static(__dirname + '/'));
+
+// bind the server to the port that c9 gives us
+app.listen(process.env.PORT);
\ No newline at end of file
