Setup
// Firebase connection Stuff
var firebaseRef = new Firebase("https://mmoasteroids.firebaseio.com");
var firebaseRefGame = firebaseRef.child('game');
Slide 28
Slide 28 text
Start up the user’s ship
// Add player's ship to Firebase
var myship = firebaseRefGame.child('players').push();
// Schedule player removal on disconnect at the backend
myship.onDisconnect().remove();
Slide 29
Slide 29 text
// Write new ship location to Firebase on any update
myship.set({
ship: {
acc: this.acc, vel: this.vel,
x: this.x, y: this.y,
rot: this.rot,
accb: KEY_STATUS.up
},
user: currentUser
});
Write our ship to firebase on updates
Slide 30
Slide 30 text
// Sync new enemy ships from Firebase to local game state
firebaseRefGame.child('players').on('child_added', function (snapshot) {
if (snapshot.key() !== myship.key()) {
var enemy = new EnemyShip();
enemy.acc = snapshot.val().ship.acc;
enemy.vel = snapshot.val().ship.vel;
enemy.x = snapshot.val().ship.x;
enemy.y = snapshot.val().ship.y;
enemy.rot = snapshot.val().ship.rot;
enemy.accb = snapshot.val().ship.accb;
enemy.user = snapshot.val().user;
enemy.visible = true;
enemy.fref = firebaseRefGame.child('players').child(snapshot.key());
}});
We read enemy ships too...
Slide 31
Slide 31 text
firebaseRefGame.child('players').on('child_removed', function (snapshot) {
if (snapshot.key() !== myship.key()) {
var enemy = Game.sprites[snapshot.key()];
enemy.visible = false;
delete Game.sprites[snapshot.key()];
Game.explosionAt(snapshot.val().ship.x, snapshot.val().ship.y);
} else {
Game.ship.collision(null);
}
})
We handle enemy ship “removal”