/
GameData.js
145 lines (118 loc) · 3.6 KB
/
GameData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* GameData
*
* Access global data in a convenient wrapper
*/
import DbAccess from '../Helpers/DbAccess';
export default class GameData {
constructor(game) {
this.game = game;
this._defineDefaultData();
}
_applySettings() {
this.game.sound.volume = Number(!this.game.data.settings.muted); //apply volume settings
}
_defineDefaultData() {
const defaults = this.game.cache.getJSON('preloadJSON').defaults;
this._resetPlayData();
this.stats = {
unlockedBirdSprites: [defaults.playerFrame],
kills: new Array(defaults.maxBirdFrame + 1).fill(0),
medals: new Array(defaults.medals.max + 1).fill(0),
bests: {
level: 0,
score: 0
}
};
this.settings = {
playerFrame: defaults.playerFrame,
vibration: defaults.settings.vibration,
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
saveStats() {
DbAccess.setKey('stats', this.stats);
}
saveSettings() {
DbAccess.setKey('settings', this.settings);
this._applySettings();
}
resetGame() {
this._resetPlayData();
DbAccess.setKey('savedGame', this.play);
}
saveGame() {
//auto-save in Db storage
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.play,
'settings': this.settings
});
//load long term info
let savedGame = DbAccess.getKey('savedGame');
let stats = DbAccess.getKey('stats');
let settings = DbAccess.getKey('settings');
//load long-term storage into cache
this.play = await savedGame;
this.stats = await stats;
this.settings = await settings;
this._applySettings();
}
/*
Data Helpers/Convenience methods that use the static getter/setter wrappers
*/
getLockedBirds(game) {
var allBirdIds = new Set();
for (let i = 0; i <= game.animationInfo.maxBirdFrame; i++) {
allBirdIds.add(i);
}
var unlockedBirds = new Set(this.stats.unlockedBirdSprites);
var lockedBirds = [...allBirdIds].filter(x => !unlockedBirds.has(x)); //find all bird ids that are not in the unlocked set but ARE in the allBird set
return lockedBirds;
}
/*
Math (and other) helpers
*/
// Returns a random number between min (inclusive) and max (exclusive)
static floatBetween(min, max) {
return Math.random() * (max - min) + min;
}
static scaleMultipler(multiplierData, level, randomize = true) {
const linearScaled = Phaser.Math.linear(multiplierData.min, multiplierData.max, level / multiplierData.maxLevel);
var delta = Math.min(linearScaled, multiplierData.max) - multiplierData.min;
if (randomize) {
delta *= Math.random();
}
var returnValue = delta + multiplierData.min;
if (typeof multiplierData.round !== 'undefined') {
returnValue = GameData.roundToNearest(returnValue, multiplierData.round.nearest);
}
return returnValue;
}
static roundToNearest(input, nearest) {
return Math.round(input / nearest) * nearest;
}
}