From 9d9ac50bf2e8e0d11ba439b99f1a43f7f4aa4a50 Mon Sep 17 00:00:00 2001
From: septimus <septimus98@gmail.com>
Date: Mon, 3 Aug 2015 03:15:59 +0000
Subject: [PATCH] Added the ability to create plants by dragging and dropping
 them from a menu.

---
 assets/textures/gui.back.png     | Bin 0 -> 3548 bytes
 assets/textures/gui.png          | Bin 0 -> 1833 bytes
 assets/textures/plants.json      |  25 ++++++++++-------
 assets/textures/plants.png       | Bin 3161 -> 3195 bytes
 assets/textures/zombie.png       | Bin 3216 -> 3223 bytes
 assets/textures/zombies_text.png | Bin 0 -> 6220 bytes
 js/Cursor.js                     |   3 +++
 js/Gui.js                        |  56 +++++++++++++++++++++++++++++----------
 js/Sun.js                        |  29 +++++++++++---------
 js/main.js                       |  34 +++++++++++++++++-------
 js/plants/Peashooter.js          |  17 +++++++-----
 11 files changed, 112 insertions(+), 52 deletions(-)
 create mode 100644 assets/textures/gui.back.png
 create mode 100644 assets/textures/gui.png
 create mode 100644 assets/textures/zombies_text.png

diff --git a/assets/textures/gui.back.png b/assets/textures/gui.back.png
new file mode 100644
index 0000000000000000000000000000000000000000..42c9e4aa196605b2d2c477e324c62522da08be74
GIT binary patch
diff --git a/assets/textures/gui.png b/assets/textures/gui.png
new file mode 100644
index 0000000000000000000000000000000000000000..901e4f655a8d8187130ef1921412e0987ffc6399
GIT binary patch
diff --git a/assets/textures/plants.json b/assets/textures/plants.json
index 8f8afc7..f27d46c 100644
--- a/assets/textures/plants.json
+++ b/assets/textures/plants.json
@@ -4,19 +4,24 @@
 		"peashooter_1": {"frame": {"x": 16, "y": 16, "w": 16, "h": 16}},
 		"peashooter_2": {"frame": {"x": 32, "y": 16, "w": 16, "h": 16}},
 		"peashooter_3": {"frame": {"x": 48, "y": 16, "w": 16, "h": 16}},
-		"nut_0": 		{"frame": {"x": 0, 	"y": 48, "w": 16, "h": 16}},
-        "nut_1": 		{"frame": {"x": 16, "y": 48, "w": 16, "h": 16}},
-        "nut_2": 		{"frame": {"x": 32, "y": 48, "w": 16, "h": 16}},
-        "nut_3": 		{"frame": {"x": 48, "y": 48, "w": 16, "h": 16}},
-        "nut_4": 		{"frame": {"x": 0, 	"y": 64, "w": 16, "h": 16}},
-        "nut_5": 		{"frame": {"x": 16, "y": 64, "w": 16, "h": 16}},
-        "nut_6": 		{"frame": {"x": 32, "y": 64, "w": 16, "h": 16}},
-		"pea":			{"frame": {"x": 0, 	"y": 32, "w": 16, "h": 16}},
+		
+		"nut_0": 		{"frame": {"x": 0, 	"y": 32, "w": 16, "h": 16}},
+        "nut_1": 		{"frame": {"x": 16, "y": 32, "w": 16, "h": 16}},
+        "nut_2": 		{"frame": {"x": 32, "y": 32, "w": 16, "h": 16}},
+        "nut_3": 		{"frame": {"x": 48, "y": 32, "w": 16, "h": 16}},
+        "nut_4": 		{"frame": {"x": 64, "y": 32, "w": 16, "h": 16}},
+        "nut_5": 		{"frame": {"x": 0,  "y": 48, "w": 16, "h": 16}},
+        "nut_6": 		{"frame": {"x": 16, "y": 48, "w": 16, "h": 16}},
+        "nut_7":        {"frame": {"x": 32, "y": 48, "w": 16, "h": 16}},
+        "nut_8":        {"frame": {"x": 48, "y": 48, "w": 16, "h": 16}},
+        
 		"sunflower_0":  {"frame": {"x": 0, 	"y": 0,  "w": 16, "h": 16}},
 		"sunflower_1":  {"frame": {"x": 16, "y": 0,  "w": 16, "h": 16}},
 		"sunflower_2":  {"frame": {"x": 32, "y": 0,  "w": 16, "h": 16}},
 		"sunflower_3":  {"frame": {"x": 48, "y": 0,  "w": 16, "h": 16}},
-		"sun":          {"frame": {"x": 16, "y": 32, "w": 16, "h": 16}},
-		"heart":        {"frame": {"x": 32, "y": 32, "w": 16, "h": 16}}
+		
+		"pea":			{"frame": {"x": 64, "y": 16, "w": 16, "h": 16}},
+		"sun":          {"frame": {"x": 64, "y": 0,  "w": 16, "h": 16}},
+		"heart":        {"frame": {"x": 64, "y": 48, "w": 16, "h": 16}}
 	}
 }
\ No newline at end of file
diff --git a/assets/textures/plants.png b/assets/textures/plants.png
index c8f6f86b28f847187240668c78627ee21943a26b..e2138d53c329dae81183b2ac9378a22b90b37ed1 100644
GIT binary patch
diff --git a/assets/textures/zombie.png b/assets/textures/zombie.png
index 95f3f3c87d24ed50496fff44f23ff20dc8325a56..e0b05b26f7939d6cdfd9e3056b5a5411feaaed06 100644
GIT binary patch
diff --git a/assets/textures/zombies_text.png b/assets/textures/zombies_text.png
new file mode 100644
index 0000000000000000000000000000000000000000..68dfeb2714c35121cd85a4aa17cf6b39e7ff9991
GIT binary patch
diff --git a/js/Cursor.js b/js/Cursor.js
index 59a9fe1..3b62b0b 100644
--- a/js/Cursor.js
+++ b/js/Cursor.js
@@ -34,6 +34,9 @@ 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;
+
     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 920727f..a7192a6 100644
--- a/js/Gui.js
+++ b/js/Gui.js
@@ -1,43 +1,71 @@
 function Gui(state) {
 	
-	this.heart = game.add.sprite(16, 16, 'plants');
-	this.heart.width = 64;
-	this.heart.height = 64;
+	this.manager = state.manager;
+	
+	this.plant_selected = false;
+	
+	//14
+	
+	this.heart = game.add.sprite(48, 48, 'plants');
+	this.heart.width = 32;
+	this.heart.height = 32;
 	this.heart.animations.add('full', ['heart'], 2, true, false);
 	this.heart.animations.add('med', ['heart'], 2, true, false);
 	this.heart.animations.add('low', ['heart'], 2, true, false);
 	this.heart.animations.play('full', 2, true);
 	
-	this.sun = game.add.sprite(80, 16, 'plants');
-	this.sun.width = 64;
-	this.sun.height = 64;
+	this.guiSprite = game.add.sprite(32, 32, 'gui');
+	this.guiSprite.smoothed = false;
+	this.guiSprite.width = 296;
+	this.guiSprite.height = 128;
+	
+	this.sun = game.add.sprite(this.guiSprite.x + 22, this.guiSprite.y + 22, 'plants');
+	this.sun.width = 32;
+	this.sun.height = 32;
 	this.sun.animations.add('idle', ['sun'], 2, true, false);
 	this.sun.animations.play('idle', 2, true);
 	
 	var self = this; var i = 0;
 	['peashooter', 'sunflower', 'nut'].map(function(plant){
 		
-		var sprite = game.add.sprite(64 * i + 128, 16, 'plants');
+		// this doesn't do anything, it just sits there
+		var placeholder_sprite = game.add.sprite(64 * i + 115, 60, 'plants');
+		placeholder_sprite.width = 64;
+		placeholder_sprite.height = 64;
+		placeholder_sprite.animations.add('idle', [plant + '_0'], 2, true, false);
+		placeholder_sprite.animations.play('idle', 2, true);
+		
+		var sprite = game.add.sprite(placeholder_sprite.x, placeholder_sprite.y, 'plants');
 		
 		sprite.width = 64;
 		sprite.height = 64;
 		sprite.inputEnabled = true;
+		sprite.input.enableDrag();
 		
-		sprite.animations.add('unselected', [plant + '_0'], 2, true, false);
-		sprite.animations.add('selected', [plant + '_1'], 2, true, false);
-		sprite.animations.play('unselected', 2, true);
+		sprite.animations.add('idle', [plant + '_0'], 2, true, false);
+		sprite.animations.play('idle', 2, true);
 		
 		sprite.events.onInputDown.add(function(){
-			sprite.animations.play('selected', 2, true);
+			self.plant_selected = true;
+		}, state);
+		
+		sprite.events.onInputUp.add(function(){
+			self.plant_selected = false;
+			sprite.x = placeholder_sprite.x;
+			sprite.y = placeholder_sprite.y;
+			self.manager.addPlantAtMouse(plant);
 		}, state);
 		
 		self[plant] = sprite;
 		i++;
 	});
 	
-	this.sunText = game.add.text(96, 128, '#FontFixBecauseExternalFontsDontWorkWellWithPhaser', {
-		'font': '40px Arial',
-		'fillStyle': 'black'
+	this.sunText = game.add.text(this.sun.x - 2 + this.sun.width / 2, this.sun.y + 32 + this.sun.height / 2, '#FontFixBecauseExternalFontsDontWorkWellWithPhaser', {
+		font: '20px Arial',
+		fillStyle: 'black',
+		align: 'center'
 	});
 	
+	this.sunText.anchor.set(0.5);
+	
 }
\ No newline at end of file
diff --git a/js/Sun.js b/js/Sun.js
index 2a555d1..599741e 100644
--- a/js/Sun.js
+++ b/js/Sun.js
@@ -6,14 +6,12 @@ function Sun(manager, sprite_x, sprite_y) {
 	this.sprite.height = 64;
 	
 	this.props = new Props({
-	    speed: 80,
+	    speed: 500,
 	    cooldown: 1000
 	});
 	
 	this.collecting = false;
 	
-	this.props.cooldown_timer = game.time.now;
-	
 	game.physics.arcade.enable(this.sprite);
 	this.sprite.body.acceleration = new Phaser.Point(0, 150);
 	this.sprite.body.velocity.y = -Math.random() * 50 - 50;
@@ -45,20 +43,27 @@ Sun.prototype.update = function() {
         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 += 10;
+                this.manager.money++;
                 this.manager.gui.sunText.text = this.manager.money.toString();
             }
         }
     }
     else {
-        this.props.speed += 5;
-        var len = Math.sqrt(this.sprite.x*this.sprite.x+this.sprite.y*this.sprite.y);
-        this.sprite.body.velocity.x = this.props.speed*(-this.sprite.x) / len + 100;
-        this.sprite.body.velocity.y = this.props.speed*(-this.sprite.y) / len + 100;
-        if(len<100)
+        
+        var len = this.sprite.position.distance(this.manager.gui.sun.position);
+        
+        game.physics.arcade.accelerateToXY(
+            this.sprite, 
+            this.manager.gui.sun.x, 
+            this.manager.gui.sun.y, 
+            this.props.speed, 999, 999
+        );
+        
+        if(len < 100)
             this.sprite.alpha -= 0.05;
-        if(this.sprite.alpha<=0) {
-            this.props.dead = true;
-        }
+        
+        if(len < 10)
+            this.props.die = true;
+        
     }
 }
\ No newline at end of file
diff --git a/js/main.js b/js/main.js
index 9e319e3..eb10c55 100644
--- a/js/main.js
+++ b/js/main.js
@@ -18,7 +18,7 @@ function GameState() {
 		clouds: [],
 		suns: [],
 		spawn_timer: 0,
-		money: 100,
+		money: 0,
 		gui: null,
 		cursor: null,
 		gui: null,
@@ -88,7 +88,7 @@ function GameState() {
 				var plantClass = Peashooter; // default plant
 				
 				switch (type) {
-					case 'basic': plantClass = Peashooter; break;
+					case 'peashooter': plantClass = Peashooter; break;
 					case 'sunflower': plantClass = Sunflower; break;
 					case 'nut': plantClass = Nut; break;
 				}
@@ -104,6 +104,19 @@ function GameState() {
 				return false;
 			}
 		},
+		addPlantAtMouse: function(type) {
+			var mouse_x = game.input.mousePointer.x - this.offsets.world_x, 
+        		mouse_y = game.input.mousePointer.y - this.offsets.world_y;
+    
+		    var column = Math.floor(mouse_x / (64 + this.offsets.margin_x));
+		    var row    = Math.floor(mouse_y / (64 + this.offsets.margin_y));
+		    
+		    if(column > -1 && column < this.width && row > -1 && row < this.height) {
+		    	return this.addPlant(column, row, type);
+		    }
+		    
+		    return false;
+		},
 		addCamper: function(row, type) {
 			this.campers.push(new Zomper(
 				this, row, (64 + this.offsets.margin_y) * row + this.offsets.world_y - this.offsets.spot_y
@@ -156,6 +169,7 @@ GameState.prototype = {
 		game.load.image('background', 'assets/textures/background.png');
 		game.load.image('spot', 'assets/textures/spot.png');
 		game.load.image('cloud', 'assets/textures/cloud.png');
+		game.load.image('gui', 'assets/textures/gui.png');
 		game.load.atlasJSONHash('cursors', 'assets/textures/cursor.png', 'assets/textures/cursor.json');
 		game.load.atlasJSONHash('zombie', 'assets/textures/zombie.png', 'assets/textures/zombie.json');
 		game.load.atlasJSONHash('plants', 'assets/textures/plants.png', 'assets/textures/plants.json');
@@ -177,11 +191,13 @@ GameState.prototype = {
 			spot_y: 0
 		});
 		
-		for(var i = 0; i < this.manager.height; i++) {
-			this.manager.addPlant(0, i, 'basic');
-			this.manager.addPlant(1, i, 'sunflower');
-			this.manager.addPlant(2, i, 'nut');
-		}
+		/*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');
+		}*/
 		
 		this.manager.gui = new Gui(this);
 		
@@ -195,8 +211,8 @@ 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 >= 2000) {
-			var row = Math.floor(Math.random() * (manager.height - 1));
+		if(manager.campers.length < 10 && spawn_timer_difference >= 100) {
+			var row = Math.floor(Math.random() * manager.height);
 			manager.addCamper(row, 'basic');
 			manager.spawn_timer = game.time.now;
 		}
diff --git a/js/plants/Peashooter.js b/js/plants/Peashooter.js
index 39b806e..c5c12b6 100644
--- a/js/plants/Peashooter.js
+++ b/js/plants/Peashooter.js
@@ -3,11 +3,14 @@ function Peashooter() {
 }
 
 Peashooter.prototype.init = function (){
-	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);
+	this.props.cooldown = 100;
 	this.props.damage = 1;
+	this.props.speed = 2;
+	
+	this.sprite.animations.add('idle', ['peashooter_3', 'peashooter_0'], this.props.speed, false, false);
+	this.sprite.animations.add('shoot', ['peashooter_0', 'peashooter_1', 'peashooter_2'], this.props.speed, false, false);
+	this.sprite.animations.add('recoil', ['peashooter_2', 'peashooter_1', 'peashooter_0'], this.props.speed, false, false);
+	this.sprite.animations.play('idle', this.props.speed, true);
 }
 
 Peashooter.prototype.update = function() {
@@ -19,15 +22,15 @@ Peashooter.prototype.update = function() {
 		var currentAnim = this.sprite.animations.currentAnim;
 
 		if(currentAnim.name == 'idle' && time_since >= this.props.cooldown) {
-			this.sprite.animations.play('shoot', 4, false);
+			this.sprite.animations.play('shoot', this.props.speed, false);
 		}
 		else if(currentAnim.isFinished && currentAnim.name == 'shoot'){
 			this.manager.shootPea(this.props.column, this.props.row, 1, 200);
-			this.sprite.animations.play('recoil', 2, false);
+			this.sprite.animations.play('recoil', this.props.speed, false);
 		}
 		else if(currentAnim.isFinished && currentAnim.name == 'recoil'){
 			this.props.cooldown_timer = game.time.now;
-			this.sprite.animations.play('idle', 2, true);
+			this.sprite.animations.play('idle', this.props.speed, true);
 		}
 	}
 	else {
