phaser/examples/games/invaders.js

297 lines
7.2 KiB
JavaScript
Raw Normal View History

2013-10-22 02:58:20 +00:00
2013-11-01 18:04:54 +00:00
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
2013-10-22 02:58:20 +00:00
function preload() {
2013-10-31 05:31:54 +00:00
game.load.image('bullet', 'assets/games/invaders/bullet.png');
game.load.image('enemyBullet', 'assets/games/invaders/enemy-bullet.png');
game.load.spritesheet('invader', 'assets/games/invaders/invader32x32x4.png', 32, 32);
game.load.image('ship', 'assets/games/invaders/player.png');
game.load.spritesheet('kaboom', 'assets/games/invaders/explode.png', 128, 128);
game.load.image('starfield', 'assets/games/invaders/starfield.png');
game.load.image('background', 'assets/games/starstruck/background2.png');
2013-10-22 02:58:20 +00:00
}
var player;
var aliens;
var bullets;
var bulletTime = 0;
var cursors;
var fireButton;
var explosions;
2013-10-31 05:31:54 +00:00
var starfield;
2013-11-01 18:04:54 +00:00
var score = 0;
var scoreString = '';
2013-11-01 17:02:06 +00:00
var scoreText;
2013-11-01 18:04:54 +00:00
var lives;
2013-11-01 17:02:06 +00:00
var enemyBullet;
2013-11-01 18:04:54 +00:00
var firingTimer = 0;
2013-11-01 17:02:06 +00:00
var stateText;
2013-12-07 02:05:07 +00:00
var livingEnemies=[];
2013-10-22 02:58:20 +00:00
function create() {
2013-10-31 05:31:54 +00:00
// The scrolling starfield background
starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');
2013-11-01 17:02:06 +00:00
2013-10-31 05:31:54 +00:00
// Our bullet group
bullets = game.add.group();
bullets.createMultiple(30, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 1);
bullets.setAll('outOfBoundsKill', true);
2013-11-01 17:02:06 +00:00
// The enemy's bullets
enemyBullets = game.add.group();
enemyBullets.createMultiple(30, 'enemyBullet');
enemyBullets.setAll('anchor.x', 0.5);
enemyBullets.setAll('anchor.y', 1);
enemyBullets.setAll('outOfBoundsKill', true);
2013-10-31 05:31:54 +00:00
// The hero!
2013-10-22 02:58:20 +00:00
player = game.add.sprite(400, 500, 'ship');
player.anchor.setTo(0.5, 0.5);
2013-10-31 05:31:54 +00:00
// The baddies!
aliens = game.add.group();
2013-10-22 02:58:20 +00:00
2013-11-01 17:02:06 +00:00
createAliens();
2013-11-19 05:34:08 +00:00
// The score
2013-11-01 18:04:54 +00:00
scoreString = 'Score : ';
2013-11-19 05:34:08 +00:00
scoreText = game.add.text(10, 10, scoreString + score, { fontSize: '34px', fill: '#fff' });
2013-11-01 17:02:06 +00:00
2013-11-19 05:34:08 +00:00
// Lives
2013-11-01 18:04:54 +00:00
lives = game.add.group();
2013-11-19 05:34:08 +00:00
game.add.text(game.world.width - 100, 10, 'Lives : ', { fontSize: '34px', fill: '#fff' });
2013-11-01 17:02:06 +00:00
2013-11-19 05:34:08 +00:00
// Text
stateText = game.add.text(game.world.centerX,game.world.centerY,'', { fontSize: '84px', fill: '#fff' });
stateText.anchor.setTo(0.5, 0.5);
stateText.visible = false;
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
for (var i = 0; i < 3; i++)
{
2013-11-19 05:34:08 +00:00
var ship = lives.create(game.world.width - 100 + (30 * i), 60, 'ship');
ship.anchor.setTo(0.5, 0.5);
2013-11-01 18:04:54 +00:00
ship.angle = 90;
ship.alpha = 0.4;
}
2013-11-01 17:02:06 +00:00
// An explosion pool
explosions = game.add.group();
explosions.createMultiple(30, 'kaboom');
explosions.forEach(setupInvader, this);
// And some controls to play the game with
cursors = game.input.keyboard.createCursorKeys();
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}
function createAliens () {
2013-11-01 18:04:54 +00:00
for (var y = 0; y < 4; y++)
2013-10-22 02:58:20 +00:00
{
for (var x = 0; x < 10; x++)
{
2013-10-31 05:31:54 +00:00
var alien = aliens.create(x * 48, y * 50, 'invader');
2013-11-19 05:34:08 +00:00
alien.anchor.setTo(0.5, 0.5);
alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true);
2013-10-31 05:31:54 +00:00
alien.play('fly');
2013-10-22 02:58:20 +00:00
}
}
aliens.x = 100;
aliens.y = 50;
2013-11-01 17:02:06 +00:00
// All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly.
2013-11-19 05:34:08 +00:00
var tween = game.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
2013-10-22 02:58:20 +00:00
2013-10-31 05:31:54 +00:00
// When the tween completes it calls descend, before looping again
2013-10-22 02:58:20 +00:00
tween.onComplete.add(descend, this);
}
2013-10-31 05:31:54 +00:00
function setupInvader (invader) {
invader.anchor.x = 0.5;
invader.anchor.y = 0.5;
invader.animations.add('kaboom');
}
2013-10-22 02:58:20 +00:00
function descend() {
2013-10-31 05:31:54 +00:00
2013-10-22 02:58:20 +00:00
aliens.y += 10;
2013-10-31 05:31:54 +00:00
2013-10-22 02:58:20 +00:00
}
function update() {
2013-10-31 05:31:54 +00:00
// Scroll the background
starfield.tilePosition.y += 2;
// Reset the player, then check for movement keys
player.body.velocity.setTo(0, 0);
2013-10-22 02:58:20 +00:00
if (cursors.left.isDown)
2013-10-22 02:58:20 +00:00
{
player.body.velocity.x = -200;
}
else if (cursors.right.isDown)
2013-10-22 02:58:20 +00:00
{
player.body.velocity.x = 200;
}
2013-10-31 05:31:54 +00:00
// Firing?
if (fireButton.isDown)
2013-10-22 02:58:20 +00:00
{
fireBullet();
}
2013-11-19 05:34:08 +00:00
if (game.time.now > firingTimer)
2013-11-01 18:04:54 +00:00
{
2013-11-01 17:02:06 +00:00
enemyFires();
}
2013-10-31 05:31:54 +00:00
// Run collision
2013-10-22 02:58:20 +00:00
game.physics.collide(bullets, aliens, collisionHandler, null, this);
2013-11-01 17:02:06 +00:00
game.physics.collide(enemyBullets, player, enemyHitsPlayer, null, this);
}
2013-11-01 18:04:54 +00:00
function collisionHandler (bullet, alien) {
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
// When a bullet hits an alien we kill them both
bullet.kill();
alien.kill();
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
// Increase the score
score+=20;
scoreText.content=scoreString+score;
2013-11-01 17:02:06 +00:00
// And create an explosion :)
var explosion = explosions.getFirstDead();
2013-11-01 18:04:54 +00:00
explosion.reset(alien.body.x, alien.body.y);
2013-11-01 17:02:06 +00:00
explosion.play('kaboom', 30, false, true);
2013-11-01 18:04:54 +00:00
if (aliens.countLiving() == 0)
{
score += 1000;
scoreText.content = scoreString + score;
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
enemyBullets.callAll('kill',this);
stateText.content = " You Won, \n Click to restart";
stateText.visible = true;
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
//the "click to restart" handler
game.input.onTap.addOnce(restart,this);
2013-11-01 17:02:06 +00:00
}
2013-10-22 02:58:20 +00:00
}
2013-11-01 18:04:54 +00:00
function enemyHitsPlayer (player,bullet) {
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
bullet.kill();
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
live = lives.getFirstAlive();
2013-11-01 17:02:06 +00:00
2013-11-19 05:34:08 +00:00
if (live)
2013-11-01 18:04:54 +00:00
{
live.kill();
}
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
// And create an explosion :)
var explosion = explosions.getFirstDead();
explosion.reset(player.body.x, player.body.y);
explosion.play('kaboom', 30, false, true);
// When the player dies
2013-11-19 05:34:08 +00:00
if (lives.countLiving() < 1)
2013-11-01 18:04:54 +00:00
{
player.kill();
enemyBullets.callAll('kill');
stateText.content=" GAME OVER \n Click to restart";
stateText.visible = true;
//the "click to restart" handler
game.input.onTap.addOnce(restart,this);
}
2013-11-01 17:02:06 +00:00
}
function enemyFires () {
2013-11-01 18:04:54 +00:00
// Grab the first bullet we can from the pool
enemyBullet = enemyBullets.getFirstExists(false);
2013-11-01 17:02:06 +00:00
2013-12-07 02:05:07 +00:00
livingEnemies.length=0;
aliens.forEachAlive(function(alien){
// put every living enemy in an array
livingEnemies.push(alien);
});
if (enemyBullet && livingEnemies.length > 0)
2013-11-01 18:04:54 +00:00
{
var random=game.rnd.integerInRange(0,livingEnemies.length);
// randomly select one of them
var shooter=livingEnemies[random];
// And fire the bullet from this enemy
2013-11-01 18:04:54 +00:00
enemyBullet.reset(shooter.body.x, shooter.body.y);
2013-11-01 18:04:54 +00:00
game.physics.moveToObject(enemyBullet,player,120);
firingTimer = game.time.now + 2000;
}
2013-11-19 05:34:08 +00:00
2013-11-01 17:02:06 +00:00
}
2013-10-22 02:58:20 +00:00
function fireBullet () {
2013-10-31 05:31:54 +00:00
// To avoid them being allowed to fire too fast we set a time limit
2013-10-22 02:58:20 +00:00
if (game.time.now > bulletTime)
{
2013-10-31 05:31:54 +00:00
// Grab the first bullet we can from the pool
2013-10-22 02:58:20 +00:00
bullet = bullets.getFirstExists(false);
if (bullet)
{
2013-10-31 05:31:54 +00:00
// And fire it
bullet.reset(player.x, player.y + 8);
bullet.body.velocity.y = -400;
bulletTime = game.time.now + 200;
2013-10-22 02:58:20 +00:00
}
}
}
function resetBullet (bullet) {
2013-10-31 05:31:54 +00:00
// Called if the bullet goes out of the screen
2013-10-22 02:58:20 +00:00
bullet.kill();
2013-10-31 05:31:54 +00:00
2013-10-22 02:58:20 +00:00
}
2013-11-01 18:04:54 +00:00
function restart () {
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
// A new level starts
//resets the life count
lives.callAll('revive');
// And brings the aliens back from the dead :)
aliens.removeAll();
createAliens();
2013-11-01 17:02:06 +00:00
2013-11-01 18:04:54 +00:00
//revives the player
player.revive();
//hides the text
stateText.visible = false;
2013-11-01 17:02:06 +00:00
2013-10-22 02:58:20 +00:00
}