-
-
Save videlais/aeb954c23cc67c358ed2 to your computer and use it in GitHub Desktop.
Met subclass of Phaser.Sprite
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 Tool(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 13 bullets using the 'enemyBullet' cache key | |
this.bulletGroup.createMultiple(13, 'enemyBullet'); | |
// 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 = 1800; | |
// The time of the next shot | |
this.nextFire = 0; | |
// The velocity of each created or revived bullet | |
this.bulletSpeed = 150; | |
// The initial firing angle | |
this.firingAngle = 135; | |
// The 'triggerBox' | |
// If another sprite is within this rectangle, shoot at them | |
this.triggerBox = new Phaser.Rectangle( | |
x - (this.width * 3), | |
y - (this.height * 3), | |
this.width * 3.5, | |
this.height * 3.5); | |
// The 'explosionGroup' for spawning explosions | |
this.explosionGroup = new ExplosionGroup(this.game); | |
// Set that this sprite is 'alive' | |
this.alive = true; | |
} | |
Tool.prototype = Object.create(Phaser.Sprite.prototype); | |
Tool.prototype.constructor = Tool; | |
Tool.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 3 bullets 'dead' | |
if (this.game.time.now > this.nextFire && this.bulletGroup.countDead() > 3) | |
{ | |
// Update the timer | |
this.nextFire = this.game.time.now + this.fireRate; | |
// Run for three loops | |
for (var i = 0; i < 3; i += 1) { | |
// 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 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); | |
// Update the firing angle by 45 degrees | |
this.firingAngle += 45; | |
} | |
// Reset the firing angle to 135 degrees | |
this.firingAngle = 135; | |
} | |
} | |
}; | |
Tool.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