-
Notifications
You must be signed in to change notification settings - Fork 1
/
levelselect.js
194 lines (154 loc) · 5.46 KB
/
levelselect.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// -------------------------------------
// START THE GAME
// -------------------------------------
var CANVAS_WIDTH = 800;
var CANVAS_HEIGHT = 600;
var PLAYER_DATA = null; // just declare as global variable for now
LevelSelect = function(game){
// define needed variables for mygame.LevelSelect
this.game = game;
this.holdicons = [];
};
LevelSelect.prototype = {
preload: function() {
this.game.load.spritesheet('levelselecticons', 'levelselecticons.png', 96, 96);
this.game.load.bitmapFont('font72', 'font72.png', 'font72.xml'); // created with http://kvazars.com/littera/
this.initProgressData();
},
create: function() {
this.game.stage.backgroundColor = 0x80a0ff;
this.game.add.bitmapText(256, 24, 'font72', 'Select a level!', 48);
this.createLevelIcons();
this.animateLevelIcons();
},
update: function() {
// nothing to do but wait until player selects a level
},
render: function() {
// display some debug info..?
},
initProgressData: function() {
// array might be undefined at first time start up
if (!PLAYER_DATA) {
// retrieve from local storage (to view in Chrome, Ctrl+Shift+J -> Resources -> Local Storage)
var str = window.localStorage.getItem('mygame_progress');
// error checking, localstorage might not exist yet at first time start up
try {
PLAYER_DATA = JSON.parse(str);
} catch(e){
PLAYER_DATA = []; //error in the above string(in this case,yes)!
};
// error checking just to be sure, if localstorage contains something else then a JSON array (hackers?)
if (Object.prototype.toString.call( PLAYER_DATA ) !== '[object Array]' ) {
PLAYER_DATA = [];
};
};
},
createLevelIcons: function() {
var levelnr = 0;
for (var y=0; y < 3; y++) {
for (var x=0; x < 4; x++) {
// next level
levelnr = levelnr + 1;
// check if array not yet initialised
if (typeof PLAYER_DATA[levelnr-1] !== 'number') {
// value is null or undefined, i.e. array not defined or too short between app upgrades with more levels
if (levelnr == 1) {
PLAYER_DATA[levelnr-1] = 0; // level 1 should never be locked
} else {
PLAYER_DATA[levelnr-1] = -1;
};
};
// player progress info for this level
var playdata = PLAYER_DATA[levelnr-1];
// decide which icon
var isLocked = true; // locked
var stars = 0; // no stars
// check if level is unlocked
if (playdata > -1) {
isLocked = false; // unlocked
if (playdata < 4) {stars = playdata;}; // 0..3 stars
};
// calculate position on screen
var xpos = 160 + (x*128);
var ypos = 120 + (y*128);
// create icon
this.holdicons[levelnr-1] = this.createLevelIcon(xpos, ypos, levelnr, isLocked, stars);
var backicon = this.holdicons[levelnr-1].getAt(0);
// keep level nr, used in onclick method
backicon.health = levelnr;
// input handler
backicon.inputEnabled = true;
backicon.events.onInputDown.add(this.onSpriteDown, this);
};
};
},
// -------------------------------------
// Add level icon buttons
// -------------------------------------
createLevelIcon: function(xpos, ypos, levelnr, isLocked, stars) {
// create new group
var IconGroup = this.game.add.group();
IconGroup.x = xpos;
IconGroup.y = ypos;
// keep original position, for restoring after certain tweens
IconGroup.xOrg = xpos;
IconGroup.yOrg = ypos;
// determine background frame
var frame = 0;
if (isLocked == false) {frame = 1};
// add background
var icon1 = this.game.add.sprite(0, 0, 'levelselecticons', frame);
IconGroup.add(icon1);
// add stars, if needed
if (isLocked == false) {
var txt = this.game.add.bitmapText(24, 16, 'font72', ''+levelnr, 48);
var icon2 = this.game.add.sprite(0, 0, 'levelselecticons', (2+stars));
IconGroup.add(txt);
IconGroup.add(icon2);
};
return IconGroup;
},
onSpriteDown: function(sprite, pointer) {
// retrieve the iconlevel
var levelnr = sprite.health;
if (PLAYER_DATA[levelnr-1] < 0) {
// indicate it's locked by shaking left/right
var IconGroup = this.holdicons[levelnr-1];
var xpos = IconGroup.xOrg;
var tween = this.game.add.tween(IconGroup)
.to({ x: xpos+6 }, 20, Phaser.Easing.Linear.None)
.to({ x: xpos-5 }, 20, Phaser.Easing.Linear.None)
.to({ x: xpos+4 }, 20, Phaser.Easing.Linear.None)
.to({ x: xpos-3 }, 20, Phaser.Easing.Linear.None)
.to({ x: xpos+2 }, 20, Phaser.Easing.Linear.None)
.to({ x: xpos }, 20, Phaser.Easing.Linear.None)
.start();
} else {
// simulate button press animation to indicate selection
var IconGroup = this.holdicons[levelnr-1];
var tween = this.game.add.tween(IconGroup.scale)
.to({ x: 0.9, y: 0.9}, 100, Phaser.Easing.Linear.None)
.to({ x: 1.0, y: 1.0}, 100, Phaser.Easing.Linear.None)
.start();
// it's a little tricky to pass selected levelnr to callback function, but this works:
tween._lastChild.onComplete.add(function(){this.onLevelSelected(sprite.health);}, this);
};
},
animateLevelIcons: function() {
// slide all icons into screen
for (var i=0; i < this.holdicons.length; i++) {
// get variables
var IconGroup = this.holdicons[i];
IconGroup.y = IconGroup.y + 600;
var y = IconGroup.y;
// tween animation
this.game.add.tween(IconGroup).to( {y: y-600}, 500, Phaser.Easing.Back.Out, true, (i*40));
};
},
onLevelSelected: function(levelnr) {
// pass levelnr variable to 'Game' state
this.game.state.states['game']._levelNumber = levelnr;
this.state.start('game');
}
};