From 032ee09436910c710c8fea3ee10b5a99093fefb1 Mon Sep 17 00:00:00 2001
From: septimus <septimus98@gmail.com>
Date: Mon, 3 Aug 2015 06:20:07 +0000
Subject: [PATCH] Added touch support to the rest of the game. Made it so
 campers and peas that go off the screen are cleaned up.

---
 js/Cursor.js         |  7 +++----
 js/Gui.js            |  2 +-
 js/Pea.js            |  8 +++++++-
 js/Sun.js            | 37 +++++++++++++++++++------------------
 js/campers/Zomper.js | 10 ++++++++--
 js/main.js           | 14 +++++++-------
 6 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/js/Cursor.js b/js/Cursor.js
index 3b62b0b..55b9c53 100644
--- a/js/Cursor.js
+++ b/js/Cursor.js
@@ -21,8 +21,8 @@ Cursor.prototype.update = function() {
     this.sprite.bringToTop();
     
     var keyboard = game.input.keyboard;
-    var mouse_x = game.input.mousePointer.x - this.manager.offsets.world_x, 
-        mouse_y = game.input.mousePointer.y - this.manager.offsets.world_y;
+    var mouse_x = game.input.activePointer.x - this.manager.offsets.world_x, 
+        mouse_y = game.input.activePointer.y - this.manager.offsets.world_y;
     
     this.props.column = Math.floor(mouse_x / (64 + this.manager.offsets.margin_x));
     this.props.column = Math.max(Math.min(this.props.column, this.manager.width - 1), 0);
@@ -34,8 +34,7 @@ Cursor.prototype.update = function() {
     //https://www.youtube.com/watch?v=dQw4w9WgXcQ
     // ^ Best song ever ^
 
-    this.sprite.visible = 
-        game.input.mousePointer.leftButton.isDown && this.manager.gui.plant_selected;
+    this.sprite.visible = this.manager.gui.plant_selected;
 
     if(this.manager.getPlantAt(this.props.column, this.props.row) != null) {
         this.sprite.animations.play('red', 2, true);
diff --git a/js/Gui.js b/js/Gui.js
index 0d5a6d4..c67ac30 100644
--- a/js/Gui.js
+++ b/js/Gui.js
@@ -62,7 +62,7 @@ function Gui(state) {
 		i++;
 	});
 	
-	this.sunText = game.add.text(this.sun.x - 2 + this.sun.width / 2, this.sun.y + 32 + this.sun.height / 2, '#FontFixBecauseExternalFontsDontWorkWellWithPhaser', {
+	this.sunText = game.add.text(this.sun.x - 2 + this.sun.width / 2, this.sun.y + 48 + this.sun.height / 2, '#FontFixBecauseExternalFontsDontWorkWellWithPhaser', {
 		font: '20px Arial',
 		fillStyle: 'black',
 		align: 'center'
diff --git a/js/Pea.js b/js/Pea.js
index 1a1577f..a39589f 100644
--- a/js/Pea.js
+++ b/js/Pea.js
@@ -18,4 +18,10 @@ function Pea(manager, sprite_x, sprite_y, damage, speed) {
 	this.sprite.body.width = 16;
 	
 	this.sprite.anchor.setTo(0.5, 0);
-}
\ No newline at end of file
+}
+
+Pea.prototype.update = function() {
+	if(this.sprite.x > game.width) {
+		this.props.die = true;
+	}
+};
\ No newline at end of file
diff --git a/js/Sun.js b/js/Sun.js
index 599741e..1121c49 100644
--- a/js/Sun.js
+++ b/js/Sun.js
@@ -1,5 +1,5 @@
-function Sun(manager, sprite_x, sprite_y) {
-	this.manager = manager;
+function Sun(state, sprite_x, sprite_y) {
+	this.manager = state.manager;
 	
 	this.sprite = game.add.sprite(sprite_x, sprite_y, 'plants');
 	this.sprite.width = 64;
@@ -10,6 +10,8 @@ function Sun(manager, sprite_x, sprite_y) {
 	    cooldown: 1000
 	});
 	
+	this.props.cooldown_timer = game.time.now;
+	
 	this.collecting = false;
 	
 	game.physics.arcade.enable(this.sprite);
@@ -19,6 +21,19 @@ function Sun(manager, sprite_x, sprite_y) {
 	
 	this.sprite.animations.add('idle', ['sun'], 2, true, false);
 	this.sprite.animations.play('idle', 2, true);
+	
+	this.sprite.inputEnabled = true;
+		
+    //If the mouse button has been pressed in the last 50ms
+    //We're gonna check if this sun got clicked on
+    var self = this;
+	this.sprite.events.onInputDown.add(function(){
+	    if(game.input.activePointer.duration < 50) {
+	        self.collecting = true;
+            self.manager.money++;
+            self.manager.gui.sunText.text = self.manager.money.toString();
+	    }
+	}, state);
 }
 
 Sun.prototype.update = function() {
@@ -29,24 +44,10 @@ Sun.prototype.update = function() {
     if(!this.collecting) {
         //Stop moving the sun after about a second
         if(time_since >= this.props.cooldown) {
-            this.sprite.body.velocity.x=0;
-            this.sprite.body.velocity.y=0;
+            this.sprite.body.velocity.x = 0;
+            this.sprite.body.velocity.y = 0;
             this.sprite.body.acceleration = new Phaser.Point(0,0);
         }
-        
-        var mouseButton = game.input.activePointer.leftButton;
-        var mouse_x = game.input.mousePointer.x; 
-        var mouse_y = game.input.mousePointer.y;
-        
-        //If the mouse button has been pressed in the last 50ms
-        //We're gonna check if this sun got clicked on
-        if(mouseButton.isDown && mouseButton.duration < 50) {
-            if(this.sprite.x<mouse_x && this.sprite.x+this.sprite.width>mouse_x && this.sprite.y<mouse_y && this.sprite.y+this.sprite.height>mouse_y) {
-                this.collecting = true;
-                this.manager.money++;
-                this.manager.gui.sunText.text = this.manager.money.toString();
-            }
-        }
     }
     else {
         
diff --git a/js/campers/Zomper.js b/js/campers/Zomper.js
index 2eecee7..5b7bbe4 100644
--- a/js/campers/Zomper.js
+++ b/js/campers/Zomper.js
@@ -6,11 +6,11 @@ function Zomper(manager, row, sprite_y) {
 		row: row,
 		cooldown: 1000,
 		damage: 1,
-		speed: 100,
+		speed: 75,
 		health: 2
 	});
 
-	this.sprite = game.add.sprite(800, sprite_y, 'zombie');
+	this.sprite = game.add.sprite(game.width + 32, sprite_y, 'zombie');
 	this.sprite.smoothed = false;
 	this.sprite.width = 64;
 	this.sprite.height = 64;
@@ -47,6 +47,12 @@ Zomper.prototype.hit = function(damage) {
 
 Zomper.prototype.update = function() {
 
+	// this means that they are completely off to the left of the screen
+	if(this.sprite.x < -this.sprite.width) {
+		this.manager.health -= 1;
+		this.props.die = true;
+	}
+
 	var plant = this.manager.getClosestPlant(this.props.row, this.sprite.x);
 
 	//If the zomper is dead, but hasn't fuly faded out yet.
diff --git a/js/main.js b/js/main.js
index bd20061..3454c01 100644
--- a/js/main.js
+++ b/js/main.js
@@ -8,6 +8,7 @@ window.onload = function() {
 function GameState() {
 	this.background = null;
 
+	var state = this;
 	this.manager = {
 		offsets: null,
 		width: 10,
@@ -18,6 +19,7 @@ function GameState() {
 		clouds: [],
 		suns: [],
 		spawn_timer: 0,
+		health: 3,
 		money: 0,
 		gui: null,
 		cursor: null,
@@ -138,7 +140,7 @@ function GameState() {
 		},
 		spawnSun: function (column, row) {
 			this.suns.push(new Sun(
-				this,
+				state,
 				(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
 			));
@@ -192,11 +194,8 @@ GameState.prototype = {
 		});
 		
 		/*for(var i = 0; i < this.manager.height; i++) {
-			this.manager.addPlant(0, i, 'peashooter');
-			this.manager.addPlant(1, i, 'peashooter');
-			this.manager.addPlant(2, i, 'peashooter');
-			this.manager.addPlant(3, i, 'nut');
-			this.manager.addPlant(4, i, 'nut');
+			for(var ii = 0; ii < 8; ii++)
+				this.manager.addPlant(ii, i, 'sunflower');
 		}*/
 		
 		this.manager.gui = new Gui(this);
@@ -211,7 +210,7 @@ GameState.prototype = {
 		
 		// spawn a new camper every 2 seconds if theirs less than 10 in the game
 		var spawn_timer_difference = game.time.now - manager.spawn_timer;
-		if(manager.campers.length < 10 && spawn_timer_difference >= 100) {
+		if(manager.campers.length < 10 && spawn_timer_difference >= 2000) {
 			var row = Math.floor(Math.random() * manager.height);
 			manager.addCamper(row, 'basic');
 			manager.spawn_timer = game.time.now;
@@ -233,6 +232,7 @@ GameState.prototype = {
 		});
 		
 		manager.peas.map(function(pea){
+			pea.update();
 			manager.campers.map(function(camper){
 				if(game.physics.arcade.overlap(pea.sprite, camper.sprite)) {
 					pea.props.die = true;
