Skip to content
Browse files

Added the ability to create plants by dragging and dropping them from…

… a menu.
  • Loading branch information...
1 parent bd291ec commit 9d9ac50bf2e8e0d11ba439b99f1a43f7f4aa4a50 @Septimus Septimus committed
View
BIN assets/textures/gui.back.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN assets/textures/gui.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
25 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}}
}
}
View
BIN assets/textures/plants.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN assets/textures/zombie.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN assets/textures/zombies_text.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
3 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);
}
View
56 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);
+
}
View
29 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;
+
}
}
View
34 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;
}
View
17 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 {

0 comments on commit 9d9ac50

Please sign in to comment.
Something went wrong with that request. Please try again.