1
0
mirror of https://github.com/danbee/invaders synced 2025-03-04 08:39:08 +00:00

Continuous waves and high score.

This commit is contained in:
Dan Barber 2014-05-10 15:39:47 +01:00
parent 5fa22ff259
commit c3d22cf3f0

View File

@ -11,7 +11,8 @@ function preload () {
var bulletTime = 0, var bulletTime = 0,
initialPlayerPosition = 512; initialPlayerPosition = 512;
lives = 3, lives = 3,
score = 0; score = 0,
highScore = 0;
function create () { function create () {
game.physics.startSystem(Phaser.Physics.ARCADE); game.physics.startSystem(Phaser.Physics.ARCADE);
@ -28,7 +29,7 @@ function create () {
bullets = game.add.group(); bullets = game.add.group();
bullets.enableBody = true; bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(3, 'bullet'); bullets.createMultiple(5, 'bullet');
bullets.setAll('anchor.x', 0.5); bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 1); bullets.setAll('anchor.y', 1);
bullets.setAll('checkWorldBounds', true); bullets.setAll('checkWorldBounds', true);
@ -40,6 +41,7 @@ function create () {
aliens.physicsBodyType = Phaser.Physics.ARCADE; aliens.physicsBodyType = Phaser.Physics.ARCADE;
createAliens(); createAliens();
animateAliens();
// Initialize bombs // Initialize bombs
bombs = game.add.group(); bombs = game.add.group();
@ -63,8 +65,13 @@ function create () {
livesText = game.add.text(game.world.bounds.width - 16, 16, "LIVES: " + lives, style); livesText = game.add.text(game.world.bounds.width - 16, 16, "LIVES: " + lives, style);
livesText.anchor.set(1, 0); livesText.anchor.set(1, 0);
scoreText = game.add.text(16, 16, "SCORE: " + score, style); scoreText = game.add.text(game.world.centerX, 16, '', style);
scoreText.anchor.set(0, 0); scoreText.anchor.set(0.5, 0);
highScoreText = game.add.text(16, 16, '', style);
highScoreText.anchor.set(0, 0);
updateScore();
// Setup controls // Setup controls
cursors = game.input.keyboard.createCursorKeys(); cursors = game.input.keyboard.createCursorKeys();
@ -121,7 +128,7 @@ function fireBullet () {
bullet.reset(player.x, player.y - 16); bullet.reset(player.x, player.y - 16);
bullet.body.velocity.y = -400; bullet.body.velocity.y = -400;
bullet.body.velocity.x = player.body.velocity.x / 4 bullet.body.velocity.x = player.body.velocity.x / 4
bulletTime = game.time.now + 500; bulletTime = game.time.now + 400;
} }
} }
} }
@ -130,7 +137,11 @@ function bulletHitsAlien (bullet, alien) {
bullet.kill(); bullet.kill();
explode(alien); explode(alien);
score += 10; score += 10;
updateScoreText(); updateScore();
if (aliens.countLiving() == 0) {
newWave()
}
} }
function bombHitsPlayer (bomb, player) { function bombHitsPlayer (bomb, player) {
@ -159,8 +170,12 @@ function updateLivesText () {
livesText.text = "LIVES: " + lives; livesText.text = "LIVES: " + lives;
} }
function updateScoreText () { function updateScore () {
scoreText.text = "SCORE: " + score; if (score > highScore) {
highScore = score;
}
scoreText.text = pad(score, 6);
highScoreText.text = "HIGH: " + pad(highScore, 6);
} }
function respawnPlayer () { function respawnPlayer () {
@ -170,6 +185,13 @@ function respawnPlayer () {
}, 1000); }, 1000);
} }
function newWave () {
setTimeout(function () {
aliens.removeAll();
createAliens();
}, 1000);
}
function gameOver () { function gameOver () {
var style = { font: "bold 32px silkscreen", fill: "#ffffff", align: "center" }; var style = { font: "bold 32px silkscreen", fill: "#ffffff", align: "center" };
livesText = game.add.text(game.world.centerX, game.world.centerY, "GAME OVER", style); livesText = game.add.text(game.world.centerX, game.world.centerY, "GAME OVER", style);
@ -187,7 +209,9 @@ function createAliens () {
aliens.x = 64; aliens.x = 64;
aliens.y = 96; aliens.y = 96;
}
function animateAliens () {
// All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly. // All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly.
var tween = game.add.tween(aliens).to( { x: 300 }, 2500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true); var tween = game.add.tween(aliens).to( { x: 300 }, 2500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true);
@ -221,3 +245,11 @@ function descend () {
aliens.y += 8; aliens.y += 8;
} }
} }
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}