-
Notifications
You must be signed in to change notification settings - Fork 21
/
lodeRunner.hiscore.js
352 lines (297 loc) · 8.54 KB
/
lodeRunner.hiscore.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
function showScoreTable(_dataId, _curScoreInfo, _callbackFun, _waitTime)
{
var hiScoreInfo;
var canvas2, scoreStage;
var titleText;
var recordId = -1;
var savedKeyDownHander;
var timeOutHandler = null;
init();
function init()
{
getHiScoreInfo();
if(_curScoreInfo) {
recordId = updateScoreInfo();
if(recordId < 0) {
if(_callbackFun) _callbackFun();
return; //don't need update score
}
}
createCanvas2();
createScoreStage();
setScoreBackground();
drawHiScoreList();
scoreStage.update();
if(recordId >= 0) {
inputHiScoreName();
} else {
anyKeyHandler();
if(typeof _waitTime == "undefined") _waitTime = 3500;
timeOutHandler = setTimeout( function() { closeScoreTable(); }, _waitTime);
}
}
function closeScoreTable()
{
removeCanvas2();
restoreKeyDownHandler();
if(_callbackFun) _callbackFun();
}
function getHiScoreInfo()
{
var infoJSON, levelMap;
if(_dataId == 1) {
infoJSON = getStorage(STORAGE_HISCORE_INFO2);
} else {
infoJSON = getStorage(STORAGE_HISCORE_INFO1);
}
if(infoJSON) {
var infoObj = JSON.parse(infoJSON);
hiScoreInfo = infoObj;
} else {
hiScoreInfo = [];
for(var i = 0; i < MAX_HISCORE_RECORD; i++) {
hiScoreInfo[i] = {s:0 , n:"" , l: 0};
}
if(_dataId == 1) {
hiScoreInfo[0].n = "STEVE JOBS";
hiScoreInfo[0].l = 1;
hiScoreInfo[0].s = 2500;
hiScoreInfo[1].n = "BILL GATES";
hiScoreInfo[1].l = 1;
hiScoreInfo[1].s = 1500;
} else {
hiScoreInfo[0].n = "SIMON";
hiScoreInfo[0].l = 1;
hiScoreInfo[0].s = 3500;
hiScoreInfo[1].n = "PETER PAN";
hiScoreInfo[1].l = 1;
hiScoreInfo[1].s = 2000;
hiScoreInfo[2].n = "DASH PARR"; // The Incredibles. He is the son of Bob Parr and Helen Parr (小飛)
hiScoreInfo[2].l = 1;
hiScoreInfo[2].s = 1000;
}
}
}
function setHiScoreInfo()
{
var infoJSON = JSON.stringify(hiScoreInfo);
if(_dataId == 1)
setStorage(STORAGE_HISCORE_INFO2, infoJSON);
else
setStorage(STORAGE_HISCORE_INFO1, infoJSON);
}
function updateScoreInfo()
{
var addId = -1;
if(_curScoreInfo.s <= 0) return addId; //don't save if scroe <= 0
for(var i = 0; i < MAX_HISCORE_RECORD; i++) {
_curScoreInfo.n = "";
if(_curScoreInfo.s >= hiScoreInfo[i].s) {
addId = i;
hiScoreInfo.splice(i, 0, _curScoreInfo);
hiScoreInfo.splice(MAX_HISCORE_RECORD, 1);
setHiScoreInfo();
break;
}
}
return addId;
}
function createCanvas2()
{
canvas2 = document.createElement('canvas');
canvas2.id = "canvas2";
canvas2.width = canvasX;
canvas2.height = canvasY;
//Set canvas top left position
var left = ((screenX1 - canvasX)/2|0),
top = ((screenY1 - canvasY)/2|0);
canvas2.style.left = (left>0?left:0) + "px";
canvas2.style.top = (top>0?top:0) + "px";
canvas2.style.position = "absolute";
document.body.appendChild(canvas2);
}
function createScoreStage()
{
scoreStage = new createjs.Stage(canvas);
}
function removeCanvas2()
{
scoreStage.removeAllChildren();
scoreStage.update();
document.body.removeChild(canvas2);
}
function setScoreBackground()
{
//set background color
var background = new createjs.Shape();
background.graphics.beginFill("black").drawRect(0, 0, canvas2.width, canvas2.height);
scoreStage.addChild(background);
}
function getNameStartPos(nameLength, itemId)
{
var x = (4+(MAX_HISCORE_NAME_LENGTH-nameLength)/2)*tileWScale;
var y = (itemId * 1.2 + 5) * tileHScale;
return { x: x, y: y };
}
function drawHiScoreList()
{
var title = "LODE RUNNER " + ((_dataId == 1)?"2":"1") + " HIGH SCORES";
var barTile;
drawText((NO_OF_TILES_X-title.length)/2*tileWScale, 0.5*tileHScale, title, scoreStage);
drawText(0.5*tileWScale, 3*tileHScale, "NO", scoreStage);
drawText(7*tileWScale, 3*tileHScale, "NAME", scoreStage);
drawText(14.5*tileWScale, 3*tileHScale, "LEVEL SCORE", scoreStage);
for(var x = 0; x < NO_OF_TILES_X; x++) {
barTile = new createjs.Bitmap(preload.getResult("ground"));
barTile.setTransform(x * tileWScale, 4.5*tileHScale , tileScale, tileScale);
scoreStage.addChild(barTile);
}
for(var i = 0; i < MAX_HISCORE_RECORD; i++) {
var pos = getNameStartPos(hiScoreInfo[i].n.length, i);
drawText(0.5*tileWScale, pos.y, ("0"+(i+1)).slice(-2) + ".", scoreStage); //no
if(hiScoreInfo[i].s > 0) {
if(hiScoreInfo[i].n.length > 0) {
drawText(pos.x, pos.y, hiScoreInfo[i].n, scoreStage, "D"); //name
}
drawText(15.5*tileWScale, pos.y, ("00"+hiScoreInfo[i].l).slice(-3), scoreStage); //level
drawText(20.5*tileWScale, pos.y, ("000000"+hiScoreInfo[i].s).slice(-7), scoreStage); //score
}
}
}
function anyKeyHandler()
{
savedKeyDownHander = document.onkeydown;
document.onkeydown = function(event) {
if(!event){ event = window.event; }
//if( event.keyCode == KEYCODE_ENTER) {
clearTimeout(timeOutHandler);
closeScoreTable();
//}
};
}
function restoreKeyDownHandler()
{
document.onkeydown = savedKeyDownHander;
}
function inputHiScoreName()
{
var name, nameText;
var curPos = 0;
var savedKeyDownHander, hiScoreTicker;
var cursor;
initInput();
function initInput()
{
name = []; //array of char
nameText = []; //text object
var pos = getNameStartPos(0, recordId);
cursor = new createjs.Sprite(textData, "FLASH");
cursor.setTransform(pos.x-tileWScale/2, pos.y , tileScale, tileScale);
scoreStage.addChild(cursor);
changeKeyDownHandler();
hiScoreTicker = createjs.Ticker.on("tick", scoreStage);
}
function changeKeyDownHandler()
{
savedKeyDownHander = document.onkeydown;
document.onkeydown = handleHiScoreName;
}
function cutTailSpace()
{
for(var i = name.length-1; i >= 0; i--) {
if(name[i] == " ") name.splice(i,1);
else break;
}
}
function inputFinish()
{
cutTailSpace();
redrawName();
//update name for score info
hiScoreInfo[recordId].n = array2String(name);
setHiScoreInfo();
//remove cursor
scoreStage.removeChild(cursor);
scoreStage.update();
createjs.Ticker.off("tick", hiScoreTicker);
setTimeout( function() { closeScoreTable(); }, 1500);
}
function removeNameText()
{
for(var i = 0; i < nameText.length; i++)
scoreStage.removeChild(nameText[i]);
}
function array2String(name)
{
var nameString = "";
for(var i = 0; i < name.length; i++) {
nameString += name[i];
}
return nameString;
}
function redrawName()
{
var pos = getNameStartPos(name.length, recordId);
removeNameText();
nameText = drawText(pos.x, pos.y, array2String(name), scoreStage, "D");
//change cursor position
if(name.length > 0) cursor.x = pos.x + curPos * tileWScale;
else cursor.x = pos.x - tileWScale/2;
//move cursor to top
moveChild2Top(scoreStage, cursor);
}
function handleHiScoreName(event)
{
if(!event){ event = window.event; } //cross browser issues exist
var code = event.keyCode;
if(curPos >= MAX_HISCORE_NAME_LENGTH &&
code != KEYCODE_BKSPACE && code != KEYCODE_LEFT && code != KEYCODE_ENTER)
{
soundPlay("beep"); //wrong key code
return false;
}
switch(true) {
case (code >= 48 && code <= 57): // 0 ~ 9
if(curPos == 0) { soundPlay("beep"); break; } //first char except numbers
name[curPos++] = String.fromCharCode(code);
break;
case (code >=65 && code <= 90): // A ~ Z
case (code >= 97 && code <= 122): //a ~ z
if( code > 90) code -= 32;
name[curPos++] = String.fromCharCode(code);
break;
case (code == KEYCODE_DOT): //'.'
if(curPos == 0) { soundPlay("beep"); break; } //first char except '.'
name[curPos++] = ".";
break;
case (code == KEYCODE_DASH || code == KEYCODE_HYPHEN || code == KEYCODE_SUBTRACT): //'-'
if(curPos == 0) { soundPlay("beep"); break; } //first char except '-'
name[curPos++] = "-";
break;
case (code == KEYCODE_SPACE): //space
if(curPos == 0) { soundPlay("beep"); break; } //first char except space
name[curPos++] = " ";
break;
case (code == KEYCODE_BKSPACE): //backspace
if(curPos == 0) break;
name.splice(--curPos, 1);
break;
case (code == KEYCODE_LEFT): //LEFT
if(curPos > 0) curPos--;
break;
case (code == KEYCODE_RIGHT): //RIGHT
if(curPos < name.length) curPos++;
break;
case (code == KEYCODE_ENTER): //ENTER
if(name.length > 0) inputFinish(true); //async
else soundPlay("beep");
default:
//debug(code);
if(code > 32) soundPlay("beep"); //wrong key code
break;
}
redrawName();
return false;
}
}
}