Skip to content

Instantly share code, notes, and snippets.

@videlais

videlais/Mega.js Secret

Created May 7, 2014
Embed
What would you like to do?
Source Code for Mega (#LOWREZJAM -- 2014)
function Mega(game, x, y, cacheKey) {
// Call the Phaser.Sprite constructor
Phaser.Sprite.call(this, game, x, y, cacheKey);
// Enable the default (set to ARCADE) physics system
this.game.physics.enable(this);
// Set the anchor (body.x and body.y position)
// to the center (0.5 * width, 0.5 * height)
// of the sprite
this.anchor.setTo(0.5, 0.5);
// Check to see if this sprite is colliding
// with the world bounds
this.body.collideWorldBounds = true;
// Set the gravity
this.body.gravity.y = 190;
// Set the health
this.health = 7;
// The default 'facing' is left
this.facing = "left";
// The horizontal (body.velocity.x) is 190
this.hozMove = 190;
// The vertical (body.velocity.y) is -230
this.vertMove = -230;
// The time between when jumping is allowed
this.jumpTimer = 0;
// Create an internal 'bulletGroup'
this.bulletGroup = this.game.add.group();
// Enable 'body' on all entries
this.bulletGroup.enableBody = true;
// Set the physics bodies to be ARCADE
this.bulletGroup.physicsBodyType = Phaser.Physics.ARCADE;
// Create 130 entries using the 'bullet' cache key
this.bulletGroup.createMultiple(130, 'bullet');
// Set that the world bounds are checked
this.bulletGroup.setAll('checkWorldBounds', true);
// kill() any entries that go out of (world) bounds
this.bulletGroup.setAll('outOfBoundsKill', true);
// The firing rate (in milliseconds)
this.fireRate = 410;
// The time between bullets
this.nextFire = 0;
// The body.velocity.x of the bullets
this.bulletSpeed = 280;
}
Mega.prototype = Object.create(Phaser.Sprite.prototype);
Mega.prototype.constructor = Mega;
Mega.prototype.moveLeft = function() {
// Check if the sprite is on the ground or touching
// another sprite
if ((this.body.onFloor() || this.body.touching.down)) {
if (this.facing === 'left') {
this.frame = 5;
} else {
this.frame = 0;
}
}
// Since we aren't touching another sprite
// or on the "floor" (colliding with a tilemap)
// use the jumping frames
else {
if (this.facing === 'left') {
this.frame = 9;
} else {
this.frame = 8;
}
}
// Set the negative velocity
this.body.velocity.x = -this.hozMove;
// Check and set the 'facing'
if (this.facing !== 'left') {
this.facing = 'left';
}
};
Mega.prototype.moveRight = function() {
// Check if the sprite is on the ground or touching
// another sprite
if ((this.body.onFloor() || this.body.touching.down)) {
if (this.facing === 'left') {
this.frame = 5;
} else {
this.frame = 0;
}
}
// Since we aren't touching another sprite
// or on the "floor" (colliding with a tilemap)
// use the jumping frames
else {
if (this.facing === 'left') {
this.frame = 9;
} else {
this.frame = 8;
}
}
// Set the positive velocity
this.body.velocity.x = this.hozMove;
// Check and set the 'facing'
if (this.facing !== 'right') {
this.facing = 'right';
}
};
Mega.prototype.shoot = function() {
// Check if the sprite is on the ground or touching
// another sprite
if ((this.body.onFloor() || this.body.touching.down)) {
if (this.facing === 'left') {
this.frame = 7;
} else {
this.frame = 6;
}
}
// Since we aren't touching another sprite
// or on the "floor" (colliding with a tilemap)
// use the jumping frames
else {
if (this.facing === 'left') {
this.frame = 11;
} else {
this.frame = 10;
}
}
// Check that enough time has past since the last shot
// AND make sure there are at least three 'dead' bullets to use
if (this.game.time.now > this.nextFire && this.bulletGroup.countDead() > 3)
{
// Update the timer
this.nextFire = this.game.time.now + this.fireRate;
// Get the first 'dead' bullet for recycling
var bullet = this.bulletGroup.getFirstDead();
// Check and adjust bullet starting position
// from which way Mega is 'facing'
if (this.facing === 'left') {
bullet.reset(this.body.x - (this.width / 4), this.body.y + (this.width / 2) - 8);
bullet.body.velocity.x = -this.bulletSpeed;
} else {
bullet.reset(this.body.x + this.width, this.body.y + (this.width / 2) - 8);
bullet.body.velocity.x = this.bulletSpeed;
}
}
};
Mega.prototype.jump = function() {
// Check that enough time has past since the last jump
// AND make sure Mega is on the floor or another sprite
if (this.game.time.now > this.jumpTimer &&
(this.body.onFloor() || this.body.touching.down)) {
// Set the (negative) velocity.y
this.body.velocity.y = this.vertMove;
// Update the jump timer
this.jumpTimer = this.game.time.now + 650;
// Adjust Mega to
// use the jumping frames
if (this.facing === 'left') {
this.frame = 9;
} else {
this.frame = 8;
}
}
};
Mega.prototype.idle = function() {
// Set the 'idle' frames
if (this.facing === 'left') {
this.frame = 5;
} else {
this.frame = 0;
}
};
Mega.prototype.moveDown = function() {
// If we are in the air, apply the inverse jumping force
if (!(this.body.onFloor() || this.body.touching.down)) {
this.body.velocity.y = -this.vertMove;
}
};
Mega.prototype.update = function() {
this.body.velocity.x = 0;
// Moving left
if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT) ||
this.game.input.keyboard.justPressed(Phaser.Keyboard.A)) {
this.moveLeft();
}
// Moving right
else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) ||
this.game.input.keyboard.justPressed(Phaser.Keyboard.D)) {
this.moveRight();
}
// Moving down
else if (this.game.input.keyboard.justPressed(Phaser.Keyboard.DOWN) ||
this.game.input.keyboard.justPressed(Phaser.Keyboard.S)) {
this.moveDown();
}
// Idling
else {
this.idle();
}
// Shooting needs to be seperate so that a directional
// button can be pressed AND shooting can occur while
// that happens. It's so Mega can run-and-gun at the
// same time.
if (this.game.input.keyboard.justPressed(Phaser.Keyboard.SPACEBAR)) {
this.shoot();
}
// Moving up (jumping) needs to be seperate for the same
// reason shooting does: Mega should be able to
// to run and jump without letting up on the
// directional input.
if (this.game.input.keyboard.justPressed(Phaser.Keyboard.UP) ||
this.game.input.keyboard.justPressed(Phaser.Keyboard.W)) {
this.jump();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment