Skip to content
Browse files

Lot's of changes. Implemented awesomeness.

  • Loading branch information...
1 parent 0c574ee commit b95d678c93cc3189734d9b8c12d48d997b1c2667 @Septimus Septimus committed
View
BIN assets/textures/background.ase
Binary file not shown.
View
BIN assets/textures/background.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/cloud.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/mario.png
Deleted file not rendered
View
2 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}
}
}
}
View
BIN assets/textures/spot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
22 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}
+ }
+ }
+}
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/zombie_scaled.png
Deleted file not rendered
View
1 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>
View
26 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;
-
-};
+ 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);
+}
View
64 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);
+ }
};
View
111 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);
}
};
View
36 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 {
View
85 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
View
9 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);

0 comments on commit b95d678

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