mirror of
https://github.com/danbee/invaders
synced 2025-03-04 08:39:08 +00:00
117 lines
3.0 KiB
JavaScript
117 lines
3.0 KiB
JavaScript
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');
|
|
}
|
|
|
|
var bulletTime = 0;
|
|
|
|
function create () {
|
|
game.physics.startSystem(Phaser.Physics.ARCADE);
|
|
|
|
// Initialize player
|
|
player = game.add.sprite(512, 540, 'ship');
|
|
player.anchor.setTo(0.5, 0.5);
|
|
game.physics.enable(player, Phaser.Physics.ARCADE);
|
|
|
|
player.body.bounce.x = 0.5;
|
|
player.body.collideWorldBounds = true;
|
|
|
|
// Initialize bullets
|
|
bullets = game.add.group();
|
|
bullets.enableBody = true;
|
|
bullets.physicsBodyType = Phaser.Physics.ARCADE;
|
|
bullets.createMultiple(3, 'bullet');
|
|
bullets.setAll('anchor.x', 0.5);
|
|
bullets.setAll('anchor.y', 1);
|
|
bullets.setAll('checkWorldBounds', true);
|
|
bullets.setAll('outOfBoundsKill', true);
|
|
|
|
// Initialize aliens
|
|
aliens = game.add.group();
|
|
aliens.enableBody = true;
|
|
aliens.physicsBodyType = Phaser.Physics.ARCADE;
|
|
|
|
createAliens();
|
|
|
|
// Setup controls
|
|
cursors = game.input.keyboard.createCursorKeys();
|
|
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
|
}
|
|
|
|
function update () {
|
|
playerMovement();
|
|
|
|
// Firing?
|
|
if (fireButton.isDown) {
|
|
fireBullet();
|
|
}
|
|
|
|
game.physics.arcade.overlap(bullets, aliens, bulletHitsAlien, null, this);
|
|
}
|
|
|
|
function playerMovement () {
|
|
var maxVelocity = 500;
|
|
|
|
if (cursors.left.isDown && player.body.velocity.x > -maxVelocity) {
|
|
// Move to the left
|
|
player.body.velocity.x -= 20;
|
|
}
|
|
else if (cursors.right.isDown && player.body.velocity.x < maxVelocity) {
|
|
// Move to the right
|
|
player.body.velocity.x += 20;
|
|
}
|
|
else {
|
|
// Slow down
|
|
if (player.body.velocity.x > 0) {
|
|
player.body.velocity.x -= 4;
|
|
}
|
|
else if (player.body.velocity.x < 0) {
|
|
player.body.velocity.x += 4;
|
|
}
|
|
}
|
|
}
|
|
|
|
function fireBullet () {
|
|
if (game.time.now > bulletTime) {
|
|
bullet = bullets.getFirstExists(false);
|
|
|
|
if (bullet) {
|
|
// And fire it
|
|
bullet.reset(player.x, player.y - 16);
|
|
bullet.body.velocity.y = -400;
|
|
bullet.body.velocity.x = player.body.velocity.x / 4
|
|
bulletTime = game.time.now + 500;
|
|
}
|
|
}
|
|
}
|
|
|
|
function bulletHitsAlien (bullet, alien) {
|
|
bullet.kill();
|
|
alien.kill();
|
|
}
|
|
|
|
function createAliens () {
|
|
for (var y = 0; y < 4; y++) {
|
|
for (var x = 0; x < 10; x++) {
|
|
var alien = aliens.create(x * 75, y * 50, 'alien');
|
|
alien.anchor.setTo(0.5, 0.5);
|
|
alien.body.moves = false;
|
|
}
|
|
}
|
|
|
|
aliens.x = 64;
|
|
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.
|
|
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
|
|
tween.onLoop.add(descend, this);
|
|
}
|
|
|
|
function descend () { aliens.y += 10; }
|