-
Notifications
You must be signed in to change notification settings - Fork 57
/
lodeRunner.misc.js
306 lines (266 loc) · 7.67 KB
/
lodeRunner.misc.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
//============
// MISC
//============
var DEBUG = 0;
var demoSoundOff = 1;
function debug(string)
{
if(DEBUG) console.log(string);
}
function assert(expression, msg)
{
console.assert(expression, msg);
}
function error(funName, string)
{
console.log("Error In " + funName + "( ): " + string);
}
function getScreenSize()
{
var x, y;
//----------------------------------------------------------------------
// Window size and scrolling:
// URL: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
//----------------------------------------------------------------------
if (typeof (window.innerWidth) == 'number') {
//Non-IE
x = window.innerWidth;
y = window.innerHeight;
} else if ((document.documentElement) &&
(document.documentElement.clientWidth || document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
x = document.documentElement.clientWidth;
y = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
//IE 4 compatible
x = document.body.clientWidth;
y = document.body.clientHeight;
}
return {x:x, y:y};
}
//================================
// show tips messgae
//===============================
function showTipsMsg(_tipsTxt, _stage, _scale, _tipsTxt1)
{
var TEXT_SIZE = 72* _scale;
var TEXT_COLOR = "#ff2020";
var tipsText = new createjs.Text(_tipsTxt, "bold " + TEXT_SIZE + "px Helvetica", TEXT_COLOR);
var screenX1 = _stage.canvas.width;
var screenY1 = _stage.canvas.height;
tipsText.x = (screenX1) / 2 | 0;
tipsText.y = screenY1/2 - tipsText.getBounds().height*5/4 | 0;
tipsText.shadow = new createjs.Shadow("white", 3, 3, 2);
tipsText.textAlign = "center";
_stage.addChild(tipsText);
createjs.Tween.get(tipsText).set({alpha:1}).wait(50).to({scaleX:1.2, scaleY:1.2, alpha:0}, 3500)
.call(function(){_stage.removeChild(tipsText);});
if(_tipsTxt1 != null) {
// two tips
var tipsText1 = new createjs.Text(_tipsTxt1, "bold " + TEXT_SIZE + "px Helvetica", TEXT_COLOR);
tipsText1.x = (screenX1) / 2 | 0;
tipsText1.y = screenY1/2 | 0;
tipsText1.shadow = new createjs.Shadow("white", 3, 3, 2);
tipsText1.textAlign = "center";
_stage.addChild(tipsText1);
createjs.Tween.get(tipsText1).set({alpha:1}).wait(50).to({scaleX:1.2, scaleY:1.2, alpha:0}, 3500)
.call(function(){_stage.removeChild(tipsText1);});
}
}
//==========================================
// move z-index to top for a child of stage
//==========================================
function moveChild2Top(stage, obj)
{
stage.setChildIndex(obj, stage.getNumChildren() - 1);
}
//==========================
// BEGIN for Sound function
//==========================
var soundOff = 0;
function themeSoundPlay(name)
{
soundPlay(name + curTheme);
}
function soundDisable()
{
if(playMode == PLAY_AUTO) return 1;
if(playMode == PLAY_DEMO || playMode == PLAY_DEMO_ONCE) return demoSoundOff;
else return soundOff;
}
function soundPlay(name)
{
if(soundDisable()) return;
if(typeof name == "string") {
return createjs.Sound.play(name);
} else {
name.stop(); //12/21/2014 , for support soundJS 0.6.0
name.play();
}
}
function soundStop(name)
{
if(typeof name == "string") {
return createjs.Sound.stop(name);
} else {
name.stop();
}
}
function soundPause(name)
{
if(soundDisable()) return;
if(typeof name == "string") {
return createjs.Sound.pause(name);
} else {
name.paused=true; //SoundJS 0.6.2 API Changed, 8/28/2016
}
}
function soundResume(name)
{
if(soundDisable()) return;
if(typeof name == "string") {
return createjs.Sound.resume(name);
} else {
name.paused=false; //SoundJS 0.6.2 API Changed, 8/28/2016
}
}
//==============================
// get time zone for php format
//==============================
function phpTimeZone()
{
var d = new Date()
var n = d.getTimezoneOffset();
var n1 = Math.abs(n);
//------------------------------------------------------------------------------------
// AJAX POST and Plus Sign ( + ) — How to Encode:
// Use encodeURIComponent() in JS and in PHP you should receive the correct values.
// http://stackoverflow.com/questions/1373414/ajax-post-and-plus-sign-how-to-encode
//------------------------------------------------------------------------------------
//+0:00, +1:00, +8:00, -8:00 ....
return ((n <=0)?encodeURIComponent("+"):"-")+ (n1/60|0) + ":" + ("0"+n1%60).slice(-2);
}
//========================================
// get local time (YYYY-MM-DD HH:MM:SS)
//========================================
function getLocalTime()
{
var d = new Date();
return (("000"+d.getFullYear()).slice(-4)+ "-" +("0"+(d.getMonth() + 1)).slice(-2)+ "-" +("0"+d.getDate()).slice(-2)+ " " +
("0"+d.getHours()).slice(-2) + ":" + ("0"+d.getMinutes()).slice(-2) + ":" + ("0"+d.getSeconds()).slice(-2));
}
//===============
// Random Object
//===============
function rangeRandom(minValue, maxValue, seedValue)
{
var rndList, idx, items;
var min, max;
var reset;
var seed = 0;
function rndStart()
{
var swapId, tmp;
rndList = [];
for(var i = 0; i < items; i++) rndList[i] = min + i;
for(var i = 0; i < items; i++) {
if(seedValue > 0) {
seed = seedValue;
swapId = (seedRandom() * items) | 0;
} else {
swapId = (Math.random() * items) | 0;
}
tmp = rndList[i];
rndList[i] = rndList[swapId];
rndList[swapId] = tmp;
}
idx = 0;
//debug(rndList);
}
function seedRandom()
{
var x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
this.get = function ()
{
if( idx >= items) {
rndStart();
reset = 1;
} else {
reset = 0;
}
return rndList[idx++];
}
this.rndReset = function ()
{
return reset;
}
//---------
// initial
//---------
reset = 0;
min = minValue;
max = maxValue;
if(min > max) { //swap
var tmp;
tmp = min;
min = max;
max = tmp;
}
items = max - min + 1;
rndStart();
}
//======================================
// get demo data by playData (wData.js)
//======================================
function getDemoData(playData)
{
wDemoData = [];
switch(playData) {
case 1:
if( typeof wfastDemoData1 !== "undefined" ) wDemoData = wfastDemoData1;
break;
case 2:
if( typeof wfastDemoData2 !== "undefined" ) wDemoData = wfastDemoData2;
break;
case 3:
if( typeof wfastDemoData3 !== "undefined" ) wDemoData = wfastDemoData3;
break;
case 4:
if( typeof wfastDemoData4 !== "undefined" ) wDemoData = wfastDemoData4;
break;
case 5:
if( typeof wfastDemoData5 !== "undefined" ) wDemoData = wfastDemoData5;
break;
}
for(var i = 0; i < wDemoData.length; i++) { //temp
playerDemoData[wDemoData[i].level-1] = wDemoData[i];
}
}
//===========================================================================
// Chrome 66 policy changes default mute autoplay, need resume it
// https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
//
// Reference: https://github.com/CreateJS/SoundJS/issues/297
//===========================================================================
function resumeAudioContext()
{
// handler for fixing suspended audio context in Chrome
//------------------------------------------------------------------
// Error Msgs:
// "The AudioContext was not allowed to start.
// It must be resume (or created) after a user gesture on the page.
// https://goo.gl/7K7WLu"
//------------------------------------------------------------------
try {
if (createjs.WebAudioPlugin.context.state === "suspended") {
createjs.WebAudioPlugin.context.resume();
console.log("Resume Web Audio context...");
}
} catch (e) {
// SoundJS context or web audio plugin may not exist
console.error("There was an error while trying to resume the SoundJS Web Audio context...");
console.error(e);
}
}