Commit a6dd0b9c authored by Pablo R. Mier's avatar Pablo R. Mier

Invaders moved to js/Invader.js

parent 15faa5d6
...@@ -7,81 +7,6 @@ var MIN_INVADERS = 4; ...@@ -7,81 +7,6 @@ var MIN_INVADERS = 4;
var INITIAL_INVADERS = 6; var INITIAL_INVADERS = 6;
var MIN_GENERATION_TIME = Phaser.Time.SECOND * 2; var MIN_GENERATION_TIME = Phaser.Time.SECOND * 2;
// Extended sprite object for Invaders
Invader = function (ctx, genes, x, y) {
var game = ctx.game;
x = x || game.world.randomX;
y = y || game.world.randomY % (game.world.height - WALL_MARGIN * 1.5) ;
Phaser.Sprite.call(this, game, x, y, 'invader');
game.physics.enable(this, Phaser.Physics.ARCADE);
// Initialize genes by getting the default values from settings.json
this.genes = genes || function () {
var settings = ctx.settings;
var genes = {};
for(var gene in settings.genes){
genes[gene] = game.rnd.realInRange(settings.genes[gene].min, settings.genes[gene].max);
}
return genes;
}();
this.anchor.setTo(0.5, 0.5);
var alpha = Math.round(this.genes['alpha']);
this.tint = Phaser.Color.getColor(alpha, alpha, alpha);
this.body.velocity.x = this.genes['xvelocity'];
this.body.velocity.y = this.genes['yvelocity'];
this.scale.setTo(this.genes['scale'], this.genes['scale']);
this.body.collideWorldBounds = true;
this.body.bounce.set(1);
// Used to control the probability of x-y change in direction
this.lastTimeChanged = 0;
// Create a shield
var shield = this.game.make.graphics(0,0);
shield.lineStyle(1, 0x15AFF0, 1);
shield.drawCircle(-0.5, -0.5, 22);
//shield.anchor.setTo(0.5, 0.5);
this.addChild(shield);
shield.visible = false;
// Add the invader to the game (move this outside this class?)
game.add.existing(this);
};
Invader.prototype = Object.create(Phaser.Sprite.prototype);
Invader.prototype.constructor = Invader;
Invader.prototype.update = function() {
// Decide if it is time to change direction.
if (this.game.time.now > this.lastTimeChanged + DIR_CHANGE_MIN_TIME) {
this.lastTimeChanged = this.game.time.now;
if (this.game.rnd.frac() < this.genes['x_prob_change_dir']) {
this.body.velocity.x = -this.body.velocity.x;
}
if (this.game.rnd.frac() < this.genes['y_prob_change_dir']) {
this.body.velocity.y = -this.body.velocity.y;
}
}
};
Invader.prototype.showField = function (show, color) {
if (show == undefined) show = true;
if (color == undefined) color = 0x15AFF0;
this.getChildAt(0).tilt = color;
this.getChildAt(0).visible = show;
};
Invader.prototype.freeze = function (freeze) {
if (freeze == undefined) freeze = true;
if (freeze){
this.body.velocity.setTo(0, 0);
} else {
this.body.velocity.x = this.genes['xvelocity'];
this.body.velocity.y = this.genes['yvelocity'];
}
};
invadersApp.Game = function (game) { invadersApp.Game = function (game) {
...@@ -139,7 +64,7 @@ invadersApp.Game.prototype = { ...@@ -139,7 +64,7 @@ invadersApp.Game.prototype = {
// Initialize // Initialize
this.objects.invaders = []; this.objects.invaders = [];
for (var i = 0; i < INITIAL_INVADERS; i++) this.objects.invaders.push(new Invader(this)); for (var i = 0; i < INITIAL_INVADERS; i++) this.objects.invaders.push(new invadersApp.Invader(this));
this.player = new invadersApp.Player(this); this.player = new invadersApp.Player(this);
this.game.add.existing(this.player); this.game.add.existing(this.player);
...@@ -192,7 +117,6 @@ invadersApp.Game.prototype = { ...@@ -192,7 +117,6 @@ invadersApp.Game.prototype = {
if (alive > MIN_INVADERS){ if (alive > MIN_INVADERS){
invader.kill(); invader.kill();
} else { } else {
// Draw circle?
that.objects.invaders.forEach(function (invader) { that.objects.invaders.forEach(function (invader) {
invader.showField(true); invader.showField(true);
}); });
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<script src="Preloader.js"></script> <script src="Preloader.js"></script>
<script src="js/Utils.js"></script> <script src="js/Utils.js"></script>
<script src="js/Player.js"></script> <script src="js/Player.js"></script>
<script src="js/Invader.js"></script>
<script src="MainMenu.js"></script> <script src="MainMenu.js"></script>
<script src="Game.js"></script> <script src="Game.js"></script>
</head> </head>
......
var invadersApp = invadersApp || {};
// Extended sprite object for Invaders
invadersApp.Invader = function (ctx, genes, x, y) {
var game = ctx.game;
x = x || game.world.randomX;
y = y || game.world.randomY % (game.world.height - WALL_MARGIN * 1.5) ;
Phaser.Sprite.call(this, game, x, y, 'invader');
game.physics.enable(this, Phaser.Physics.ARCADE);
// Initialize genes by getting the default values from settings.json
this.genes = genes || function () {
var settings = ctx.settings;
var genes = {};
for(var gene in settings.genes){
genes[gene] = game.rnd.realInRange(settings.genes[gene].min, settings.genes[gene].max);
}
return genes;
}();
this.anchor.setTo(0.5, 0.5);
var alpha = Math.round(this.genes['alpha']);
this.tint = Phaser.Color.getColor(alpha, alpha, alpha);
this.body.velocity.x = this.genes['xvelocity'];
this.body.velocity.y = this.genes['yvelocity'];
var scale = this.genes['scale'];
this.scale.setTo(scale, scale);
this.body.collideWorldBounds = true;
this.body.bounce.set(1);
// Used to control the probability of x-y change in direction
this.lastTimeChanged = 0;
// Create a shield
var shield = this.game.make.graphics(0,0);
shield.lineStyle(1, 0x15AFF0, 1);
shield.drawCircle(-0.5, -0.5, 15*scale);
//shield.anchor.setTo(0.5, 0.5);
this.addChild(shield);
shield.visible = false;
shield.scale.setTo(1.5/scale, 1.5/scale);
// Add the invader to the game (move this outside this class?)
game.add.existing(this);
};
invadersApp.Invader.prototype = Object.create(Phaser.Sprite.prototype);
invadersApp.Invader.prototype.constructor = invadersApp.Invader;
invadersApp.Invader.prototype.update = function() {
// Decide if it is time to change direction.
if (this.game.time.now > this.lastTimeChanged + DIR_CHANGE_MIN_TIME) {
this.lastTimeChanged = this.game.time.now;
if (this.game.rnd.frac() < this.genes['x_prob_change_dir']) {
this.body.velocity.x = -this.body.velocity.x;
}
if (this.game.rnd.frac() < this.genes['y_prob_change_dir']) {
this.body.velocity.y = -this.body.velocity.y;
}
}
};
invadersApp.Invader.prototype.showField = function (show, color) {
if (show == undefined) show = true;
if (color == undefined) color = 0x15AFF0;
this.getChildAt(0).tilt = color;
this.getChildAt(0).visible = show;
};
invadersApp.Invader.prototype.freeze = function (freeze) {
if (freeze == undefined) freeze = true;
if (freeze){
this.body.velocity.setTo(0, 0);
} else {
this.body.velocity.x = this.genes['xvelocity'];
this.body.velocity.y = this.genes['yvelocity'];
}
};
invadersApp = invadersApp || {}; var invadersApp = invadersApp || {};
invadersApp.Player = function (ctx) { invadersApp.Player = function (ctx, shootDelay) {
this.shootDelay = shootDelay;
if (this.shootDelay === undefined) { this.shootDelay = 150; }
this.ctx = ctx; this.ctx = ctx;
this.game = ctx.game; this.game = ctx.game;
...@@ -32,6 +34,8 @@ invadersApp.Player.prototype.constructor = invadersApp.Player; ...@@ -32,6 +34,8 @@ invadersApp.Player.prototype.constructor = invadersApp.Player;
invadersApp.Player.prototype.update = function () { invadersApp.Player.prototype.update = function () {
if (this.game.physics.arcade.isPaused) return;
this.body.velocity.setTo(0, 0); this.body.velocity.setTo(0, 0);
if (this.ctx.cursors.left.isDown) { if (this.ctx.cursors.left.isDown) {
...@@ -52,7 +56,7 @@ invadersApp.Player.prototype.update = function () { ...@@ -52,7 +56,7 @@ invadersApp.Player.prototype.update = function () {
this.readyToFire = false; this.readyToFire = false;
// Grab the first bullet we can from the pool // Grab the first bullet we can from the pool
if (this.game.time.now > this.lastShootAt + 100) { if (this.game.time.now > this.lastShootAt + this.shootDelay) {
this.lastShootAt = this.game.time.now; this.lastShootAt = this.game.time.now;
var bullet = this.bullets.getFirstExists(false); var bullet = this.bullets.getFirstExists(false);
if (bullet) { if (bullet) {
......
...@@ -6,7 +6,7 @@ invadersApp.utils.TEXT_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!:.©_-/()"; ...@@ -6,7 +6,7 @@ invadersApp.utils.TEXT_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!:.©_-/()";
invadersApp.utils.addText = function (game, x, y, text, size) { invadersApp.utils.addText = function (game, x, y, text, size) {
var scale = size || 1; var scale = size || 1;
var font = game.add.retroFont('retroFont', 8, 8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!:.©_-/()", 0); var font = game.add.retroFont('retroFont', 8, 8, invadersApp.utils.TEXT_SET, 0);
font.text = text; font.text = text;
var img = game.add.image(x, y, font); var img = game.add.image(x, y, font);
img.scale.setTo(scale, scale); img.scale.setTo(scale, scale);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment