-
-
Save videlais/bd90f1918ce8c7a32354 to your computer and use it in GitHub Desktop.
PicketMan subclass of Phaser.Sprite for #LOWREZJAM 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GameState.prototype.create = function() { | |
... | |
this.picketGroup = this.game.add.group(); | |
this.picketGroup.add(new PicketMan(this.game, 116, 9, 'picketMan')); | |
this.picketGroup.add(new PicketMan(this.game, 128, 7, 'picketMan')); | |
this.picketGroup.add(new PicketMan(this.game, 139, 7, 'picketMan')); | |
... | |
}; | |
GameState.prototype.update = function() { | |
... | |
this.game.physics.arcade.collide(this.player, this.picketGroup, | |
function(player) { | |
player.damage(1); | |
player.blink(); | |
}); | |
this.picketGroup.forEach(function(enemy) { | |
if (this.game.physics.arcade.collide(enemy.bulletGroup, this.player, | |
function(player, bullet) { | |
bullet.kill(); | |
player.damage(1); | |
player.blink(); | |
})) { | |
} | |
if (enemy.triggerBox.contains(this.player.x, this.player.y)) { | |
enemy.frame = 0; | |
enemy.shoot(); | |
} else { | |
enemy.frame = 1; | |
} | |
}, this); | |
... | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function PicketMan(game, x, y, cacheKey) { | |
// Call the Phaser.Sprite constructor | |
Phaser.Sprite.call(this, game, x, y, cacheKey); | |
// Enable physics | |
this.game.physics.enable(this); | |
// Set the (physics) body to be immovable | |
this.body.immovable = true; | |
// Center the anchor | |
this.anchor.setTo(0.5, 0.5); | |
// Create a group for the bullets | |
this.bulletGroup = this.game.add.group(); | |
// Enable all create items to have (physics) bodies | |
this.bulletGroup.enableBody = true; | |
// Set the type of physics bodies to be ARCADE | |
this.bulletGroup.physicsBodyType = Phaser.Physics.ARCADE; | |
// Create 5 bullets using the 'pickaxe' cache key | |
this.bulletGroup.createMultiple(5, 'pickaxe'); | |
// Check if bullets collide with world bounds | |
this.bulletGroup.setAll('checkWorldBounds', true); | |
// If they do collide with world bounds, they are killed | |
this.bulletGroup.setAll('outOfBoundsKill', true); | |
// Bullets only 'live' for 1600ms | |
this.bulletGroup.setAll('lifespan', 1600); | |
// Set the firing rate (how long between shooting) | |
this.fireRate = 4200; | |
// The time of the next shot | |
this.nextFire = 0; | |
// The velocity of each created or revived bullet | |
this.bulletSpeed = 550; | |
// The initial firing angle | |
this.firingAngle = 245; | |
// The 'triggerBox' | |
// If another sprite is within this rectangle, shoot at them | |
this.triggerBox = new Phaser.Rectangle( | |
x - (this.width * 2), | |
y - (this.height), | |
this.width * 2.5, | |
this.height * 1.5); | |
// The 'explosionGroup' for spawning explosions | |
this.explosionGroup = new ExplosionGroup(this.game); | |
// Set that this sprite is 'alive' | |
this.alive = true; | |
} | |
PicketMan.prototype = Object.create(Phaser.Sprite.prototype); | |
PicketMan.prototype.constructor = PicketMan; | |
PicketMan.prototype.shoot = function() { | |
// Is this sprite alive? | |
if (this.alive) { | |
// It is alive, so check if enough time has past since the last | |
// shot and that there are at least 1 bullets 'dead' | |
if (this.game.time.now > this.nextFire && this.bulletGroup.countDead() > 1) | |
{ | |
// Update the timer | |
this.nextFire = this.game.time.now + this.fireRate; | |
// Get the first 'dead' sprite | |
var bullet = this.bulletGroup.getFirstDead(); | |
// Reset its position to the same as this sprite's position | |
// making some adjustments for where the bullets should | |
// come from for this sprite. | |
bullet.reset(this.body.x + (this.width / 2), this.body.y + (this.height / 2) - 18); | |
// Set the 'anchor' to center (0.5 * width, 0.5 * height) | |
bullet.anchor.setTo(0.5, 0.5); | |
// Set the firing angle | |
bullet.angle = this.firingAngle; | |
// Set the firing trajectory as based on | |
// the angle and set velocity (linear diagonal motion) | |
this.game.physics.arcade.velocityFromAngle(bullet.angle, | |
this.bulletSpeed, | |
bullet.body.velocity); | |
//Unlike other projectiles, this trajectory | |
// needs to be curved. To do that, apply | |
// a high gravity to pull it down as it moves. | |
bullet.body.gravity.y = 895; | |
// Reset the lifespan so that it is kill()'ed after a | |
// certain amount of time | |
bullet.lifespan = 1600; | |
} | |
} | |
}; | |
PicketMan.prototype.update = function() { | |
// Since the hammers need to spin, | |
// iterate through the bulletGroup each update | |
// and add to each sprite's angle | |
this.bulletGroup.forEach(function(pickaxe) { | |
pickaxe.angle += 15; | |
}); | |
}; | |
PicketMan.prototype.kill = function() { | |
// Create an explosion at this position | |
this.explosionGroup.explode(this.x, this.y); | |
// kill() this sprite by calling Phaser.Sprite's | |
// own kill function and passing 'this' | |
Phaser.Sprite.prototype.kill.call(this); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment