mirror of
https://github.com/danbee/invaders
synced 2025-03-04 08:39:08 +00:00
Add bombs!
This commit is contained in:
parent
c763d1427b
commit
70c415857c
@ -1,10 +1,10 @@
|
|||||||
var game = new Phaser.Game(1024, 576, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });
|
var game = new Phaser.Game(1024, 576, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });
|
||||||
|
|
||||||
function preload () {
|
function preload () {
|
||||||
game.load.image('ship', '/images/ship.png');
|
game.load.image('ship', 'images/ship.png');
|
||||||
game.load.image('bullet', '/images/bullet.png');
|
game.load.image('bullet', 'images/bullet.png');
|
||||||
game.load.image('alien', '/images/alien.png');
|
game.load.image('alien', 'images/alien.png');
|
||||||
game.load.image('bomb', '/images/bomb.png');
|
game.load.image('bomb', 'images/bomb.png');
|
||||||
}
|
}
|
||||||
|
|
||||||
var bulletTime = 0;
|
var bulletTime = 0;
|
||||||
@ -37,6 +37,16 @@ function create () {
|
|||||||
|
|
||||||
createAliens();
|
createAliens();
|
||||||
|
|
||||||
|
// Initialize bombs
|
||||||
|
bombs = game.add.group();
|
||||||
|
bombs.enableBody = true;
|
||||||
|
bombs.physicsBodyType = Phaser.Physics.ARCADE;
|
||||||
|
bombs.createMultiple(10, 'bomb');
|
||||||
|
bombs.setAll('anchor.x', 0.5);
|
||||||
|
bombs.setAll('anchor.y', 0.5);
|
||||||
|
bombs.setAll('checkWorldBounds', true);
|
||||||
|
bombs.setAll('outOfBoundsKill', true);
|
||||||
|
|
||||||
// Setup controls
|
// Setup controls
|
||||||
cursors = game.input.keyboard.createCursorKeys();
|
cursors = game.input.keyboard.createCursorKeys();
|
||||||
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
||||||
@ -45,12 +55,16 @@ function create () {
|
|||||||
function update () {
|
function update () {
|
||||||
playerMovement();
|
playerMovement();
|
||||||
|
|
||||||
// Firing?
|
// Firing?
|
||||||
if (fireButton.isDown) {
|
if (fireButton.isDown && player.alive) {
|
||||||
fireBullet();
|
fireBullet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle aliens dropping bombs
|
||||||
|
handleBombs();
|
||||||
|
|
||||||
game.physics.arcade.overlap(bullets, aliens, bulletHitsAlien, null, this);
|
game.physics.arcade.overlap(bullets, aliens, bulletHitsAlien, null, this);
|
||||||
|
game.physics.arcade.overlap(bombs, player, bombHitsPlayer, null, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
function playerMovement () {
|
function playerMovement () {
|
||||||
@ -80,7 +94,7 @@ function fireBullet () {
|
|||||||
bullet = bullets.getFirstExists(false);
|
bullet = bullets.getFirstExists(false);
|
||||||
|
|
||||||
if (bullet) {
|
if (bullet) {
|
||||||
// And fire it
|
// And fire it
|
||||||
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
|
||||||
@ -94,6 +108,11 @@ function bulletHitsAlien (bullet, alien) {
|
|||||||
alien.kill();
|
alien.kill();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bombHitsPlayer (bomb, player) {
|
||||||
|
bomb.kill();
|
||||||
|
player.kill();
|
||||||
|
}
|
||||||
|
|
||||||
function createAliens () {
|
function createAliens () {
|
||||||
for (var y = 0; y < 4; y++) {
|
for (var y = 0; y < 4; y++) {
|
||||||
for (var x = 0; x < 10; x++) {
|
for (var x = 0; x < 10; x++) {
|
||||||
@ -106,11 +125,32 @@ function createAliens () {
|
|||||||
aliens.x = 64;
|
aliens.x = 64;
|
||||||
aliens.y = 50;
|
aliens.y = 50;
|
||||||
|
|
||||||
// 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: 284 }, 2500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true);
|
var tween = game.add.tween(aliens).to( { x: 284 }, 2500, Phaser.Easing.Sinusoidal.InOut, true, 0, 1000, true);
|
||||||
|
|
||||||
// When the tween loops it calls descend
|
// When the tween loops it calls descend
|
||||||
tween.onLoop.add(descend, this);
|
tween.onLoop.add(descend, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
function descend () { aliens.y += 10; }
|
function handleBombs () {
|
||||||
|
aliens.forEachAlive(function (alien) {
|
||||||
|
chanceOfDroppingBomb = game.rnd.integerInRange(0, 1000);
|
||||||
|
if (chanceOfDroppingBomb == 0) {
|
||||||
|
dropBomb(alien);
|
||||||
|
}
|
||||||
|
}, this)
|
||||||
|
}
|
||||||
|
|
||||||
|
function dropBomb (alien) {
|
||||||
|
bomb = bombs.getFirstExists(false);
|
||||||
|
|
||||||
|
if (bomb) {
|
||||||
|
|
||||||
|
// And drop it
|
||||||
|
bomb.reset(alien.x + aliens.x, alien.y + aliens.y + 16);
|
||||||
|
bomb.body.velocity.y = +100;
|
||||||
|
bomb.body.gravity.y = 250
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function descend () { aliens.y += 8; }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user