Skip to content
Browse files

Added even awesomeness!

  • Loading branch information...
1 parent eabe38e commit bd291ec1a743c256d70635fe0b8232539492e6cb @Septimus Septimus committed
View
0 README.md
No changes.
View
3 assets/textures/plants.json
@@ -16,6 +16,7 @@
"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": 48, "w": 16, "h": 16}}
+ "sun": {"frame": {"x": 16, "y": 32, "w": 16, "h": 16}},
+ "heart": {"frame": {"x": 32, "y": 32, "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
7 css/fonts.css
@@ -0,0 +1,7 @@
+@font-face {
+ font-family: 'VT323';
+ font-style: normal;
+ font-weight: 400;
+ src: local('../assets/fonts/alterebro-pixel-font.ttf');
+ unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000;
+}
View
9 index.html
@@ -4,11 +4,16 @@
<title>Plants vs Campers</title>
<link href="css/styles.css" type="text/css" rel="stylesheet"/>
+ <!-- Fonts -->
+ <link href='css/fonts.css' rel='stylesheet' type='text/css'>
+
<!-- Load any external libraries -->
- <script src="js/phaser.min.js"></script>
+ <script src="js/phaser/phaser.min.js"></script>
<!-- Various misc bits of code -->
<script src="js/Props.js"></script>
+ <script src="js/cbd/GameObject.js"></script>
+ <script src="js/cbd/Component.js"></script>
<script src="js/Gui.js"></script>
<script src="js/Cursor.js"></script>
@@ -17,7 +22,7 @@
<script src="js/Cloud.js"></script>
<!-- Load in all the characters -->
- <script src="js/plants/Plant.js"></script>
+ <script src="js/plants/Character.js"></script>
<script src="js/plants/Peashooter.js"></script>
<script src="js/plants/Sunflower.js"></script>
<script src="js/plants/Nut.js"></script>
View
47 js/Gui.js
@@ -1,6 +1,43 @@
-function Gui() {
-
-
-
-
+function Gui(state) {
+
+ this.heart = game.add.sprite(16, 16, 'plants');
+ this.heart.width = 64;
+ this.heart.height = 64;
+ 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.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');
+
+ sprite.width = 64;
+ sprite.height = 64;
+ sprite.inputEnabled = true;
+
+ 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.events.onInputDown.add(function(){
+ sprite.animations.play('selected', 2, true);
+ }, state);
+
+ self[plant] = sprite;
+ i++;
+ });
+
+ this.sunText = game.add.text(96, 128, '#FontFixBecauseExternalFontsDontWorkWellWithPhaser', {
+ 'font': '40px Arial',
+ 'fillStyle': 'black'
+ });
+
}
View
31 js/Sun.js
@@ -6,31 +6,29 @@ function Sun(manager, sprite_x, sprite_y) {
this.sprite.height = 64;
this.props = new Props({
- speed: 50,
+ speed: 80,
cooldown: 1000
});
this.collecting = false;
- this.speed = 50;
- this.spawnCountdown = 1;
+
+ 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;
this.sprite.body.velocity.x = Math.random() * 30 - 10;
- //this.sprite.body.velocity.x = this.props.speed;
this.sprite.animations.add('idle', ['sun'], 2, true, false);
this.sprite.animations.play('idle', 2, true);
}
Sun.prototype.update = function() {
- //If the sun is getting collected we don't need to check if
- //the player is clicking on it
var time_since = game.time.now - this.props.cooldown_timer;
+ //If the sun is getting collected we don't need to check if
+ //the player is clicking on it
if(!this.collecting) {
-
//Stop moving the sun after about a second
if(time_since >= this.props.cooldown) {
this.sprite.body.velocity.x=0;
@@ -39,25 +37,28 @@ Sun.prototype.update = function() {
}
var mouseButton = game.input.activePointer.leftButton;
- var mouse_x = game.input.mousePointer.x - this.manager.offsets.world_x;
- var mouse_y = game.input.mousePointer.y - this.manager.offsets.world_y;
+ 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) {
+ 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.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.speed*(-this.sprite.x) / len;
- this.sprite.body.velocity.y = this.speed*(-this.sprite.y) / len;
- if(len<150)
+ 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)
this.sprite.alpha -= 0.05;
- if(len<50 && this.sprite.alpha<=0)
+ if(this.sprite.alpha<=0) {
this.props.dead = true;
+ }
}
}
View
4 js/cbd/Component.js
@@ -1,3 +1,7 @@
function Component (type) {
this.type = type;
+}
+
+Component.prototype.update = function () {
+
}
View
42 js/cbd/GameObject.js
@@ -1,12 +1,21 @@
function GameObject (name, tag, layer, row, column, components) {
+
this.name = name;
this.tag = tag;
+
+ this.props = new Props({
+ column: column,
+ row: row,
+ cooldown: 1000,
+ damage: 0,
+ speed: 100,
+ health: 2
+ });
this.layer = layer;
- this.row = row;
- this.column = column;
var attachedComponents = components;
}
+
GameObject.prototype.getColumnAndRow = function () {
return [this.column, this.row];
}
@@ -48,6 +57,33 @@ GameObject.prototype.compareLayer = function (layer) {
GameObject.prototype.update = function () {
for (var i = 0; i < this.attachedComponents[i]; i++) {
- attachedComponents[i].update();
+ if (this.attachedComponents.update) {
+ this.attachedComponents[i].update();
+ }
}
+}
+
+GameObject.prototype.render = function () {
+ for (var i = 0; i < this.attachedComponents[i]; i++) {
+ if (this.attachedComponents.render) {
+ this.attachedComponents[i].render();
+ }
+ }
+}
+
+function loadScript(url, callback)
+{
+ // Adding the script tag to the head as suggested before
+ var head = document.getElementsByTagName('head')[0];
+ var script = document.createElement('script');
+ script.type = 'text/javascript';
+ script.src = url;
+
+ // Then bind the event to the callback function.
+ // There are several events for cross browser compatibility.
+ script.onreadystatechange = callback;
+ script.onload = callback;
+
+ // Fire the loading
+ head.appendChild(script);
}
View
134 js/main.js
@@ -18,7 +18,10 @@ function GameState() {
clouds: [],
suns: [],
spawn_timer: 0,
+ money: 100,
+ gui: null,
cursor: null,
+ gui: null,
setup: function(offsets) {
this.offsets = offsets;
@@ -56,6 +59,7 @@ function GameState() {
if(this.plants[i].props.row == row) {
if(closest == null || this.plants[i].sprite.x > closest.sprite.x) {
closest = this.plants[i];
+ //console.log(closest);
}
}
}
@@ -125,6 +129,23 @@ function GameState() {
(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
));
+ },
+ cleanup: function() {
+
+ var objectArrays = ['plants', 'campers', 'peas', 'suns'];
+
+ for(var i = 0; i < objectArrays.length; i++) {
+
+ var objectArray = this[objectArrays[i]];
+
+ var ii = 0; while(ii < objectArray.length) {
+ if(objectArray[ii].props.die) {
+ objectArray[ii].sprite.destroy();
+ objectArray.splice(ii, 1);
+ } ii++;
+ }
+ }
+
}
};
@@ -156,100 +177,57 @@ GameState.prototype = {
spot_y: 0
});
- this.manager.addPlant(0, 0, 'basic');
- this.manager.addPlant(0, 1, 'basic');
- this.manager.addPlant(0, 2, 'basic');
- this.manager.addPlant(0, 3, 'sunflower');
- this.manager.addPlant(1, 3, 'nut');
- this.manager.addPlant(0, 4, 'sunflower');
- this.manager.addPlant(1, 4, 'nut');
- this.manager.addSun(150,150);
+ 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');
+ }
- game.add.text(0, 0, 'Controls - Arrow keys to move cursor', {
- 'font': '24px Arial',
- 'fillStyle': 'black'
- });
+ this.manager.gui = new Gui(this);
+
+ var manager = this.manager;
+ window.setTimeout(function(){
+ manager.gui.sunText.text = manager.money.toString();
+ }, 100);
},
update: function() {
+ var manager = this.manager;
// spawn a new camper every 2 seconds if theirs less than 10 in the game
- var spawn_timer_difference = game.time.now - this.manager.spawn_timer;
-
- if(this.manager.campers.length < 1 && spawn_timer_difference >= 2000) {
- var row = Math.floor(Math.random() * (this.manager.height - 1));
- this.manager.addCamper(row, 'basic');
- this.manager.spawn_timer = game.time.now;
+ 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));
+ manager.addCamper(row, 'basic');
+ manager.spawn_timer = game.time.now;
}
-
- // we use while loops below due to the fact that we're calling splice
- // inside of them, using a while loop forces the length to be re-indexed
- // update the plants
- var i = 0; while(i < this.manager.plants.length) {
-
- this.manager.plants[i].update();
-
- i++;
- }
+ manager.plants.map(function(plant){ plant.update(); });
+ manager.clouds.map(function(cloud){ cloud.update(); });
+ manager.suns.map(function(sun){ sun.update(); });
- // update the campers
- var i = 0; while(i < this.manager.campers.length) {
- var camper = this.manager.campers[i];
-
- camper.update();
-
- if(camper.props.die) {
- camper.sprite.destroy();
- this.manager.campers.splice(i, 1);
- }
-
- // this makes it so campers don't overlap
- for(var c = 0; c < this.manager.campers.length; c++) {
- game.physics.arcade.collide(camper.sprite, this.manager.campers[c].sprite);
- }
-
- 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.props.die) {
- pea.sprite.destroy();
- this.manager.peas.splice(i, 1);
+ manager.campers.map(function(camper_a){
+ camper_a.update();
+ if(!camper_a.dying) {
+ manager.campers.map(function(camper_b){
+ if(!camper_b.dying) {
+ game.physics.arcade.collide(camper_a.sprite, camper_b.sprite);
+ }
+ });
}
-
- 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];
-
+ manager.peas.map(function(pea){
+ manager.campers.map(function(camper){
if(game.physics.arcade.overlap(pea.sprite, camper.sprite)) {
pea.props.die = true;
camper.hit(pea.props.damage);
}
- }
-
- i++;
- }
-
- // Update the clouds
- for (var i = 0; i < this.manager.clouds.length; i++) {
- this.manager.clouds[i].update();
- }
+ });
+ });
- for( var i=0; i<this.manager.suns.length; i++) {
- this.manager.suns[i].update();
- }
+ manager.cursor.update();
- this.manager.cursor.update();
+ manager.cleanup();
},
render: function() {
//for(var i = 0; i < this.manager.campers.length; i++) game.debug.body(this.manager.campers[i].sprite);
View
95,198 js/phaser/phaser.js
95,198 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
0 js/phaser.min.js → js/phaser/phaser.min.js
File renamed without changes.
View
34 js/plants/Character.js
@@ -0,0 +1,34 @@
+function Character(manager, column, row, sprite_x, sprite_y) {
+ this.type = 'character';
+ this.manager = manager;
+
+ this.props = new Props({
+ column: column,
+ row: row,
+ cooldown: 1000,
+ damage: 0,
+ speed: 100,
+ health: 2
+ });
+
+ this.sprite = game.add.sprite(sprite_x, sprite_y, 'plants');
+ this.sprite.smoothed = false;
+ this.sprite.width = 64;
+ this.sprite.height = 64;
+
+ //Common functions
+
+ this.hit = function(damage){
+ // Decrease health by the amount of damage, but never allow health to drop below zero
+ this.props.health = Math.max(this.props.health - damage, 0);
+
+ // If health is zero, mark the plant as dead
+ if (this.props.health <= 0) {
+ this.props.die = true;
+ }
+ };
+
+ this.init();
+}
+
+Character.__proto__ = Component('character');
View
17 js/plants/Nut.js
@@ -1,12 +1,23 @@
+function Nut() {
+ Character.apply(this, arguments);
+}
+
Nut.prototype.init = function (){
this.last_action_time = 0;
this.props.health = 14;
+
+ this.sprite.animations.add('idle_1', ['nut_0'], 2, true, false);
+ this.sprite.animations.add('idle_2', ['nut_1'], 2, true, false);
+ this.sprite.animations.add('idle_3', ['nut_2'], 2, true, false);
+ this.sprite.animations.add('idle_4', ['nut_3'], 2, true, false);
+ this.sprite.animations.add('idle_5', ['nut_4'], 2, true, false);
+ this.sprite.animations.add('idle_6', ['nut_5'], 2, true, false);
+ this.sprite.animations.add('idle_7', ['nut_6'], 2, true, false);
+ this.sprite.animations.play('idle_0', 2, true);
};
Nut.prototype.update = function() {
-
// Update sprite based on health remaining
var idleAnimationIndex = (8 - (this.props.health / 2)); // maps health in range of 1-7 (1 means full health, 7 means almost no health)
- this.sprite.animations.play('idle' + idleAnimationIndex.toString(), 1, false);
-
+ this.sprite.animations.play('idle_' + idleAnimationIndex.toString(), 1, false);
};
View
6 js/plants/Peashooter.js
@@ -1,3 +1,7 @@
+function Peashooter() {
+ Character.apply(this, arguments);
+}
+
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);
@@ -9,7 +13,7 @@ Peashooter.prototype.init = function (){
Peashooter.prototype.update = function() {
var time_since = game.time.now - this.props.cooldown_timer;
-
+
if(this.manager.campersInRow(this.props.row) > 0) {
var currentAnim = this.sprite.animations.currentAnim;
View
36 js/plants/Plant.js
@@ -1,36 +0,0 @@
-function Character(manager, column, row, sprite, sprite_x, sprite_y) {
-
- this.manager = manager;
-
- this.props = new Props({
- column: column,
- row: row,
- cooldown: 1000,
- damage: 0,
- speed: 100,
- health: 2
- });
-
- this.sprite = game.add.sprite(sprite_x, sprite_y, 'plants');
- this.sprite.smoothed = false;
- this.sprite.width = 64;
- this.sprite.height = 64;
-}
-
-//Common functions
-
-Character.prototype.hit = function (damage) {
- // Decrease health by the amount of damage, but never allow health to drop below zero
- this.props.health = Math.max(this.props.health - damage, 0);
-
- // If health is zero, mark the plant as dead
- if (this.props.health === 0) {
- this.props.die = true;
- }
-}
-
-//PlantTypes
-
-Sunflower = Character;
-Peashooter = Character;
-Nut = Character;
View
12 js/plants/Sunflower.js
@@ -1,8 +1,10 @@
-Sunflower.prototype.init = function (){
+function Sunflower() {
+ Character.apply(this, arguments);
+}
+
+Sunflower.prototype.init = function () {
- this.props = new Props({
- cooldown: (Math.random() * 5000) + 5000
- });
+ this.props.cooldown = (Math.random() * 5000) + 5000;
this.sprite.animations.add('idle', ['sunflower_3', 'sunflower_0'], 2, true, false);
this.sprite.animations.play('idle', 2, true);
@@ -13,7 +15,7 @@ Sunflower.prototype.update = function() {
var time_since = game.time.now - this.props.cooldown_timer;
if (time_since >= this.props.cooldown) {
- this.next_sun_spawn_ms = (Math.random() * 5000) + 5000;
+ this.props.cooldown = (Math.random() * 5000) + 5000;
this.props.cooldown_timer = game.time.now;
this.manager.spawnSun(this.props.column, this.props.row);
}
View
79 js/utils/Class.js
@@ -0,0 +1,79 @@
+var PvZ = PvZ || {};
+
+
+// *** Example of creating a simple class within the PvZ namespace ! (PS: we should have namespaces Plants Vs Zombies === PvZ)
+
+// var Game = PvZ.Class.create('PvZ.Game', function Game(options, dependencies) {
+
+// this.dependencies = dependencies;
+// this.options = PvZ.Utils.extend({}, this.options, options);
+// this.config = PvZ.Config;
+
+// this.init();
+// });
+
+// *** this is how you extend a random class lets say you want to make a new player class
+
+// var Player = PvZ.Class.extend(PvZ.Game.Entity, 'PvZ.Game.Entity.Player', PvZ.Game.Entity); <--- assuming you have entities i think you guys call them characters
+
+// Player.prototype.init = function() {
+// this.controller = new O.Game.Controller;
+// PvZ.Logger.log("Player Created");
+
+// this.x = this.canvas.width / 2;
+// this.y = this.canvas.height / 2;
+
+// this.radius = 70;
+// this.speed = 10;
+// }
+
+PvZ.Class = (function() {
+ function makeNameSpace(namespace, constructor){
+ var parts = namespace.split('.');
+ var parent = window;
+
+ //loop thorugh namespace parts
+ var part;
+ for (var i = 0; i < parts.length; i++) {
+ part = parts[i];
+ if (typeof parent[part] === 'undefined') {
+ if(i === parts.length - 1 && typeof constructor === 'function' ){
+ parent[part] = constructor;
+ }else{
+ parent[part] = {};
+ }
+ }
+ parent = parent[part];
+ };
+
+ return parent;
+ }
+
+ return {
+ create: function(namespace, constructor) {
+ var newClass = makeNameSpace(namespace, constructor);
+ newClass = constructor;
+ newClass.prototype.fullClassName = namespace;
+ newClass.prototype.toString = function() {
+ return namespace;
+ }
+
+ return newClass;
+ },
+ extend: function(parent, namespace, constructor) {
+
+ var F = function() {};
+ try{
+ F.prototype = parent.prototype;
+ } catch (e){
+ throw new Error('Parent class does not exist..');
+ }
+
+ constructor.prototype = new F();
+ constructor.prototype.parent = parent;
+ constructor.prototype.constructor = constructor;
+ return PvZ.Class.create(namespace, constructor);
+ }
+ }
+
+})();
View
7 js/utils/LoadPhaser.js
@@ -0,0 +1,7 @@
+var isPhaserMinified = false;
+
+if (isPhaserMinified) {
+ loadScript("../phaser/phaser.min.js");
+} else {
+ loadScript("../phaser/phaser.js");
+}
View
0 js/utils/Resource.js
No changes.
View
49 js/utils/Util.js
@@ -0,0 +1,49 @@
+function Util(){}
+
+Util.prototype.deepExtend = function(out) {
+ out = out || {};
+ for (var i = 1; i < arguments.length; i++) {
+ var obj = arguments[i];
+
+ if (!obj)
+ continue;
+
+ for (var key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ if (typeof obj[key] === 'object')
+ this.extend(out[key], obj[key]);
+ else
+ out[key] = obj[key];
+ }
+ }
+ }
+
+ return out;
+};
+
+Util.prototype.extend = function(out) {
+ out = out || {};
+
+ for (var i = 1; i < arguments.length; i++) {
+ if (!arguments[i])
+ continue;
+
+ for (var key in arguments[i]) {
+ if (arguments[i].hasOwnProperty(key))
+ out[key] = arguments[i][key];
+ }
+ }
+
+ return out;
+};
+
+Util.prototype.generateUUID = function() {
+ var d = new Date().getTime();
+ var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
+ var r = (d + Math.random() * 16) % 16 | 0;
+ d = Math.floor(d / 16);
+ return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
+ });
+ return uuid;
+};
+

0 comments on commit bd291ec

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