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

Add bombs!

This commit is contained in:
Dan Barber 2014-05-10 12:13:28 +01:00
parent c763d1427b
commit 70c415857c

View File

@ -1,10 +1,10 @@
var game = new Phaser.Game(1024, 576, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });
function preload () {
game.load.image('ship', '/images/ship.png');
game.load.image('bullet', '/images/bullet.png');
game.load.image('alien', '/images/alien.png');
game.load.image('bomb', '/images/bomb.png');
game.load.image('ship', 'images/ship.png');
game.load.image('bullet', 'images/bullet.png');
game.load.image('alien', 'images/alien.png');
game.load.image('bomb', 'images/bomb.png');
}
var bulletTime = 0;
@ -37,6 +37,16 @@ function create () {
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
cursors = game.input.keyboard.createCursorKeys();
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
@ -46,11 +56,15 @@ function update () {
playerMovement();
// Firing?
if (fireButton.isDown) {
if (fireButton.isDown && player.alive) {
fireBullet();
}
// Handle aliens dropping bombs
handleBombs();
game.physics.arcade.overlap(bullets, aliens, bulletHitsAlien, null, this);
game.physics.arcade.overlap(bombs, player, bombHitsPlayer, null, this);
}
function playerMovement () {
@ -94,6 +108,11 @@ function bulletHitsAlien (bullet, alien) {
alien.kill();
}
function bombHitsPlayer (bomb, player) {
bomb.kill();
player.kill();
}
function createAliens () {
for (var y = 0; y < 4; y++) {
for (var x = 0; x < 10; x++) {
@ -113,4 +132,25 @@ function createAliens () {
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; }