Skip to content

Commit

Permalink
further simplification of the data definition and saving process
Browse files Browse the repository at this point in the history
  • Loading branch information
james committed Feb 23, 2017
1 parent cd03d46 commit 9219169
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
63 changes: 27 additions & 36 deletions src/objects/Helpers/GameData.js
Expand Up @@ -20,21 +20,7 @@ export default class GameData {
_defineDefaultData() {
const defaults = this.game.cache.getJSON('preloadJSON').defaults;

this.play = {
score: 0,
level: 0,
scoreBuffer: 0,
comboCount: 0,
player: null
};

this.savedGame = {
sprites: [],
player: null,
score: 0,
level: 0,
comboCount: 0
};
this._resetPlayData();

this.stats = {
unlockedBirdSprites: [defaults.playerFrame],
Expand All @@ -52,7 +38,20 @@ export default class GameData {
screenShake: defaults.settings.screenShake,
muted: defaults.settings.muted
};
}

_resetPlayData() {
this.play = {
score: 0,
level: 0,
comboCount: 0,
player: null,

serializedObjects: {
player: null,
sprites: []
}
};
}

//auto update long-term storage for certain properties
Expand All @@ -65,35 +64,27 @@ export default class GameData {
}

resetGame() {
this.savedGame.player = null;
this.savedGame.sprites = [];
this.savedGame.comboCount = 0;
this.savedGame.level = 0;
this.savedGame.score = 0;
DbAccess.setKey('savedGame', this.savedGame);

this.play.player = null;
this.play.score = 0;
this.play.level = 0;
this.play.comboCount = 0;
this.play.scoreBuffer = 0;
this._resetPlayData();
DbAccess.setKey('savedGame', this.play);
}

saveGame() {
//auto-save in Db storage
this.savedGame.player = this.play.player.serialize();
this.savedGame.sprites = this.game.spritePools.serialize();
this.savedGame.comboCount = this.play.comboCount;
this.savedGame.level = this.play.level;
this.savedGame.score = this.play.score + this.play.scoreBuffer;
this.play.scoreBuffer = 0;
DbAccess.setKey('savedGame', this.savedGame);
this.play.serializedObjects.player = this.play.player.serialize();
this.play.serializedObjects.sprites = this.game.spritePools.serialize();

//copy play data to a temp array and remove pointers to sprites (otherwise it crashes)
let save = {};
Object.assign(save, this.play);
save.player = null;

DbAccess.setKey('savedGame', save);
}

async load() {
await DbAccess.open(this.game, {
'stats': this.stats,
'savedGame': this.savedGame,
'savedGame': this.play,
'settings': this.settings
});

Expand All @@ -103,7 +94,7 @@ export default class GameData {
let settings = DbAccess.getKey('settings');

//load long-term storage into cache
this.savedGame = await savedGame;
this.play = await savedGame;
this.stats = await stats;
this.settings = await settings;

Expand Down
4 changes: 2 additions & 2 deletions src/objects/Pools.js
Expand Up @@ -61,8 +61,8 @@ export default class Pools {

this.game.world.bringToTop(this.getPool(PoopSplatter.className())); //ensure poop splatters stay in front of all other sprites

if (this.game.data.savedGame.sprites.length > 0) {
this.deserialize(this.game.data.savedGame.sprites);
if (this.game.data.play.serializedObjects.sprites.length > 0) {
this.deserialize(this.game.data.play.serializedObjects.sprites);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/objects/Sprites/Player.js
Expand Up @@ -55,8 +55,8 @@ export default class Player extends Bird {

this.invincibleJingle = this.game.add.audio('invincible');

if (this.game.data.savedGame.player) {
this.deserialize(this.game.data.savedGame.player);
if (this.game.data.play.serializedObjects.player) {
this.deserialize(this.game.data.play.serializedObjects.player);
}
}

Expand All @@ -66,6 +66,10 @@ export default class Player extends Bird {
if (info.posInvincibleJingle >= 0) {
this.makeInvincible(info.posInvincibleJingle);
}

this.body.velocity.x = 0;
this.body.velocity.y = 0;
this.goTowardsLastActivePointer = false;
}
serialize() {
const info = super.serialize();
Expand Down
9 changes: 6 additions & 3 deletions src/states/Game.js
Expand Up @@ -37,6 +37,7 @@ export default class Game extends Phaser.State {
this.add.existing(this.levelupCoin);

//Create the score label at top right of screen
this.scoreBuffer = 0;
this.scoreLabel = this.add.text(this.game.world.width - this.game.dimen.margin.sideOfScreen,
this.game.dimen.margin.sideOfScreen,
this.game.data.play.score,
Expand Down Expand Up @@ -182,6 +183,8 @@ export default class Game extends Phaser.State {
this.pauseText.visible = true;

if (this.game.data.play.player.alive) {
this.game.data.play.score += this.scoreBuffer;
this.scoreBuffer = 0;
this.game.data.saveGame();
}

Expand Down Expand Up @@ -247,10 +250,10 @@ export default class Game extends Phaser.State {

updateScoreFromBuffer(scoreInc) {
if (scoreInc > 0) {
this.game.data.play.scoreBuffer += scoreInc;
this.scoreBuffer += scoreInc;
}

let buffer = this.game.data.play.scoreBuffer;
let buffer = this.scoreBuffer;

if (buffer > 0) {
this.scoreLabelTween.start();
Expand All @@ -264,7 +267,7 @@ export default class Game extends Phaser.State {

this.game.data.play.score += change;
this.scoreLabel.setText(this.game.data.play.score.toLocaleString());
this.game.data.play.scoreBuffer = buffer - change;
this.scoreBuffer = buffer - change;

this.textUpdateTimer.add(this.game.durations.textUpdate, this.updateScoreFromBuffer, this);
}
Expand Down

0 comments on commit 9219169

Please sign in to comment.