denki_guy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,611 @@
1
+ // ---
2
+ // Copyright (c) 2010 Francesco Cottone, http://www.kesiev.com/
3
+ // ---
4
+
5
+ var dynalist={
6
+ create:function() {
7
+ return {
8
+ first:null,
9
+ last:null,
10
+ data:[],
11
+ dl:0,
12
+ gar:[],
13
+ disconnect:function(obd) {
14
+ if (this.data[obd].__first!=null) this.data[this.data[obd].__first].__next=this.data[obd].__next; else this.first=this.data[obd].__next;
15
+ if (this.data[obd].__next!=null) this.data[this.data[obd].__next].__first=this.data[obd].__first; else this.last=this.data[obd].__first;
16
+ },
17
+ addObject:function(obj,prio) {
18
+ var nid=this.gar.pop();
19
+ if (nid==null) {
20
+ nid=this.dl;
21
+ this.dl++;
22
+ }
23
+ if (this.first==null) { // First element
24
+ obj.__next=null;
25
+ obj.__first=null;
26
+ this.first=nid;
27
+ this.last=nid;
28
+ } else { // Chain next
29
+ var i=this.first;
30
+ while (i!=null)
31
+ if (this.data[i].__prio>prio) break; else i=this.data[i].__next;
32
+ if (i==null) { // if last, chain in queue
33
+ obj.__next=null;
34
+ obj.__first=this.last;
35
+ this.data[this.last].__next=nid;
36
+ this.last=nid;
37
+ } else { // else reconnect objects
38
+ obj.__first=this.data[i].__first;
39
+ obj.__next=i;
40
+ this.data[i].__first=nid;
41
+ if (obj.__first!=null) this.data[obj.__first].__next=nid; else this.first=nid;
42
+ }
43
+
44
+ }
45
+ obj.__prio=prio;
46
+ obj.__id=nid;
47
+ this.data[nid]=obj;
48
+ return nid;
49
+ },
50
+ setPrio:function(obd,prio) {
51
+ if (this.data[obd].__prio==prio) return;
52
+ if (this.first!=this.last)
53
+ if (this.data[obd].__prio<prio) {
54
+ if (this.data[obd].__id!=this.last) {
55
+ var i=this.data[obd].__next;
56
+ while (i!=null)
57
+ if (this.data[i].__prio>=prio) break; else i=this.data[i].__next;
58
+ if ((i==null)||(this.data[i].__first!=this.data[obd].__id)) {
59
+ // disconnect
60
+ this.disconnect(obd);
61
+ // Reconnect
62
+ if (i==null) {
63
+ this.data[this.last].__next=this.data[obd].__id;
64
+ this.data[obd].__first=this.last;
65
+ this.data[obd].__next=null;
66
+ this.last=this.data[obd].__id;
67
+ } else {
68
+ this.data[obd].__first=this.data[i].__first;
69
+ this.data[obd].__next=i;
70
+ this.data[i].__first=this.data[obd].__id;
71
+ if (this.data[obd].__first!=null) this.data[this.data[obd].__first].__next=this.data[obd].__id; else this.first=this.data[obd].__id;
72
+ }
73
+ }
74
+ }
75
+ } else {
76
+ if (this.data[obd].__id!=this.first) {
77
+ var i=this.data[obd].__first;
78
+ while (i!=null)
79
+ if (this.data[i].__prio<=prio) break; else i=this.data[i].__first;
80
+ if ((i==null)||(this.data[i].__next!=this.data[obd].__id)) {
81
+ // disconnect
82
+ this.disconnect(obd);
83
+ if (i==null) {
84
+ this.data[this.first].__first=this.data[obd].__id;
85
+ this.data[obd].__first=null;
86
+ this.data[obd].__next=this.first;
87
+ this.first=this.data[obd].__id;
88
+ } else {
89
+ this.data[obd].__first=i;
90
+ this.data[obd].__next=this.data[i].__next;
91
+ this.data[i].__next=this.data[obd].__id;
92
+ if (this.data[obd].__next!=null) this.data[this.data[obd].__next].__first=this.data[obd].__id; else this.last=this.data[obd].__id;
93
+ }
94
+ }
95
+ }
96
+ }
97
+ this.data[obd].__prio=prio;
98
+ },
99
+ remove:function(obd) {
100
+ this.disconnect(obd);
101
+ this.gar.push(this.data[obd].__id);
102
+ delete this.data[this.data[obd].__id];
103
+ }
104
+ }
105
+ }
106
+
107
+ }
108
+
109
+ var gbox={
110
+ // CONSTANTS
111
+ ALIGN_CENTER:0,
112
+ ALIGN_MIDDLE:0,
113
+ ALIGN_RIGHT:1,
114
+ ALIGN_BOTTOM:1,
115
+ COLOR_BLACK:'rgb(0,0,0)',
116
+ COLOR_WHITE:'rgb(255,255,255)',
117
+ ZINDEX_LAYER:-1,
118
+
119
+ // VARS
120
+ _autoid:0,
121
+ _keyboard:[],
122
+ _keymap:{
123
+ up:38,
124
+ down:40,
125
+ right:39,
126
+ left:37,
127
+ a:90,
128
+ b:88,
129
+ c:67
130
+ },
131
+ _fonts:{},
132
+ _count:0,
133
+ _countloaded:0,
134
+ _tiles:{},
135
+ _images:{},
136
+ _loaded:function() {
137
+ gbox._countloaded++;
138
+ },
139
+ _camera:{},
140
+ _screen:0,
141
+ _screenposition:0,
142
+ _keyboardpicker:0,
143
+ _screenh:0,
144
+ _screenw:0,
145
+ _screenhh:0,
146
+ _screenhw:0,
147
+ _zoom:1,
148
+ _canvas:{},
149
+ _objects:{},
150
+ _groups:[],
151
+ _renderorder:[],
152
+ _groupplay:{},
153
+ _actionqueue:["first","then","blit","after"], // initialize is executed once
154
+ _mspf:0,
155
+ _fps:0,
156
+ _gametimer:0,
157
+ _frameskip:0,
158
+ _autoskip:{min:0,max:5,lowidle:0,hiidle:5}, // minimum frameskip, maximum frameskip, minimum idle time allowed for increasing frameskip, maximum idle time allowed for decreasing frameskip
159
+ _fskid:0,
160
+ _statbar:0,
161
+ _border:0,
162
+ _garbage:[],
163
+ _zindexch:[],
164
+ _framestart:0,
165
+ _zindex:dynalist.create(),
166
+ _db:false,
167
+ _safedrawimage:function(tox,img,sx,sy,sw,sh,dx,dy,dw,dh) {
168
+ if (sx<0) { dx-=(dw/sw)*sx;sw+=sx; sx=0; }
169
+ if (sy<0) { dy-=(dh/sh)*sy;sh+=sy; sy=0; }
170
+ if (sx+sw>img.width) { dw=(dw/sw)*(img.width-sx);sw=img.width-sx;}
171
+ if (sy+sh>img.height) { dh=(dh/sh)*(img.height-sy);sh=img.height-sy;}
172
+ if ((sh>0)&&(sw>0)&&(sx<img.width)&&(sy<img.height)) tox.drawImage(img, sx,sy,sw,sh,dx,dy,dw,dh);
173
+ },
174
+ _keydown:function(e){
175
+ var key=(e.fake||window.event?e.keyCode:e.which);
176
+ if (!gbox._keyboard[key]) gbox._keyboard[key]=1;
177
+ },
178
+ _keyup:function(e){
179
+ var key=(e.fake||window.event?e.keyCode:e.which);
180
+ gbox._keyboard[key]=-1;
181
+ },
182
+ _resetkeys:function() {
183
+ for (var key in gbox._keymap)
184
+ gbox._keyup({fake:1,keyCode:gbox._keymap[key]});
185
+ },
186
+ _showkeyboardpicker:function(){
187
+ gbox._keyboardpicker.value="Click/Tap here to enable the keyboard";
188
+ gbox._keyboardpicker.style.left=(gbox._screenposition.x+5)+"px";
189
+ gbox._keyboardpicker.style.top=(gbox._screenposition.y+5)+"px";
190
+ gbox._keyboardpicker.style.width=(gbox._screenposition.w-10)+"px";
191
+ gbox._keyboardpicker.style.height="30px";
192
+ this._keyboardpicker.style.border="1px dashed white";
193
+ this._keyboardpicker.readOnly=null;
194
+ },
195
+ _hidekeyboardpicker:function(){
196
+ this._keyboardpicker.style.zIndex=100;
197
+ this._keyboardpicker.readOnly="yes";
198
+ this._keyboardpicker.style.position="absolute";
199
+ this._keyboardpicker.style.textAlign="center";
200
+ this._keyboardpicker.style.backgroundColor="#000000";
201
+ this._keyboardpicker.style.color="#fefefe";
202
+ this._keyboardpicker.style.cursor="pointer";
203
+ this._keyboardpicker.value="";
204
+ this._keyboardpicker.style.left="0px";
205
+ this._keyboardpicker.style.top="0px";
206
+ this._keyboardpicker.style.height="0px";
207
+ this._keyboardpicker.style.width="0px";
208
+ this._keyboardpicker.style.border="0px";
209
+ this._keyboardpicker.style.padding="0px";
210
+ this._keyboardpicker.style.margin="0px";
211
+ },
212
+ _domgetabsposition:function(oElement) {
213
+ var sizes={x:0,y:0,h:0,w:0};
214
+ sizes.h=oElement.offsetHeight;
215
+ sizes.w=oElement.offsetWidth;
216
+ while( oElement != null) {
217
+ sizes.y += oElement.offsetTop;
218
+ sizes.x += oElement.offsetLeft;
219
+ oElement = oElement.offsetParent;
220
+ }
221
+ return sizes;
222
+ },
223
+ setStatusBar:function(a) { this._statbar=a },
224
+ setScreenBorder:function(a) { this._border=a},
225
+ initScreen:function(w,h) {
226
+ document.body.style.textAlign="center";
227
+ var container=document.createElement("div");
228
+ container.style.width="100%";
229
+ container.style.height="100%";
230
+ container.style.display="table";
231
+ this._box=document.createElement("div");
232
+ this._box.style.display="table-cell";
233
+ this._box.style.width="100%";
234
+ this._box.style.textAlign="center";
235
+ this._box.style.verticalAlign="middle";
236
+
237
+ this._screen=document.createElement("canvas");
238
+ if (this._border) this._screen.style.border="1px solid black";
239
+ this._screen.setAttribute('height',h);
240
+ this._screen.setAttribute('width',w);
241
+ this._screen.style.width=(w*this._zoom)+"px";
242
+ this._screen.style.height=(h*this._zoom)+"px";
243
+ this._screenh=h;
244
+ this._screenw=w;
245
+ this._screenhh=Math.floor(h/2);
246
+ this._screenhw=Math.floor(w/2);
247
+ this._camera.x=0;
248
+ this._camera.y=0;
249
+ this._camera.h=h;
250
+ this._camera.w=w;
251
+ this._box.appendChild(this._screen);
252
+ container.appendChild(this._box);
253
+ document.body.appendChild(container);
254
+
255
+ this.createCanvas("_buffer");
256
+ window.addEventListener('keydown', this._keydown,false);
257
+ window.addEventListener('keyup', this._keyup,false);
258
+ if (this._statbar) {
259
+ this._statbar=document.createElement("div");
260
+ if (this._border) this._statbar.style.border="1px solid black";
261
+ this._statbar.style.margin="auto";
262
+ this._statbar.style.backgroundColor="#ffffff";
263
+ this._statbar.style.fontSize="10px";
264
+ this._statbar.style.fontFamily="sans-serif";
265
+ this._statbar.style.width=(w*this._zoom)+"px";
266
+ this._box.appendChild(this._statbar);
267
+ }
268
+ // Keyboard support on devices that needs focus (like iPad) - actually is not working for a bug on WebKit's "focus" command.
269
+ this._keyboardpicker=document.createElement("input");
270
+ this._keyboardpicker.onclick=function(evt) { gbox._hidekeyboardpicker();evt.preventDefault();evt.stopPropagation();};
271
+ this._hidekeyboardpicker(this._keyboardpicker);
272
+
273
+ gbox._box.appendChild(this._keyboardpicker);
274
+ gbox._screen.ontouchstart=function(evt) { gbox._screenposition=gbox._domgetabsposition(gbox._screen);if (evt.touches[0].pageY-gbox._screenposition.y<30) gbox._showkeyboardpicker();else gbox._hidekeyboardpicker();evt.preventDefault();evt.stopPropagation();};
275
+ gbox._screen.ontouchend=function(evt) {evt.preventDefault();evt.stopPropagation();};
276
+ gbox._screen.ontouchmove=function(evt) { evt.preventDefault();evt.stopPropagation();};
277
+ gbox._screen.onmousedown=function(evt) {gbox._screenposition=gbox._domgetabsposition(gbox._screen);if (evt.pageY-gbox._screenposition.y<30) gbox._showkeyboardpicker(); else gbox._hidekeyboardpicker();evt.preventDefault();evt.stopPropagation();};
278
+ },
279
+ setDoubleBuffering:function(db){this._db=db},
280
+ setStatBar:function(txt){ if (gbox._statbar) this._statbar.innerHTML=(txt?txt:"&nbsp")},
281
+ setFps:function(f){
282
+ this._fps=f;
283
+ this._mspf=Math.floor(1000/f)
284
+ },
285
+ getFps:function() { return this._fps },
286
+ setAutoskip:function(f){this._autoskip=f},
287
+ setFrameskip:function(f){this._frameskip=f},
288
+ getScreenH:function(){return this._screenh},
289
+ getScreenW:function(){return this._screenw},
290
+ getScreenHH:function(){return this._screenhh},
291
+ getScreenHW:function(){return this._screenhw},
292
+ setZoom:function(z) { this._zoom=z},
293
+ setCallback:function(cb) { this._cb=cb; },
294
+ _playobject:function(g,obj,a) {
295
+ if (gbox._objects[g][obj].initialize) {
296
+ gbox._objects[g][obj].initialize(obj);
297
+ delete gbox._objects[g][obj].initialize;
298
+ }
299
+ if (gbox._objects[g][obj][a]) gbox._objects[g][obj][a](obj,a);
300
+ },
301
+ go:function() {
302
+ gbox._framestart=new Date().getTime();
303
+ var gr="";
304
+ for (var g=0;g<gbox._renderorder.length;g++)
305
+ if (gbox._groupplay[gbox._renderorder[g]])
306
+ if (gbox._renderorder[g]==gbox.ZINDEX_LAYER) {
307
+ var id;
308
+ for (var i=0;i<gbox._actionqueue.length;i++) {
309
+ id=gbox._zindex.first;
310
+ while (id!=null) {
311
+ if (gbox._groupplay[gbox._zindex.data[id].g])
312
+ gbox._playobject(gbox._zindex.data[id].g,gbox._zindex.data[id].o,gbox._actionqueue[i]);
313
+ id=gbox._zindex.data[id].__next;
314
+ }
315
+ }
316
+ } else
317
+ for (var i=0;i<gbox._actionqueue.length;i++)
318
+ for (var obj in gbox._objects[gbox._renderorder[g]])
319
+ gbox._playobject(gbox._renderorder[g],obj,gbox._actionqueue[i]);
320
+ if (gbox._fskid>=gbox._frameskip) {
321
+ if (gbox._db) gbox.blitImageToScreen(gbox.getBuffer());
322
+ gbox._fskid=0;
323
+ } else gbox._fskid++;
324
+
325
+ gbox.purgeGarbage();
326
+
327
+ if (gbox._zindexch.length) {
328
+
329
+ for (var i=0;i<gbox._zindexch.length;i++) {
330
+ if (gbox._objects[gbox._zindexch[i].o.g][gbox._zindexch[i].o.o])
331
+ if (gbox._objects[gbox._zindexch[i].o.g][gbox._zindexch[i].o.o].__zt==null)
332
+ gbox._objects[gbox._zindexch[i].o.g][gbox._zindexch[i].o.o].__zt=gbox._zindex.addObject(gbox._zindexch[i].o,gbox._zindexch[i].z);
333
+ else
334
+ gbox._zindex.setPrio(gbox._objects[gbox._zindexch[i].o.g][gbox._zindexch[i].o.o].__zt,gbox._zindexch[i].z);
335
+ }
336
+ gbox._zindexch=[];
337
+ }
338
+
339
+
340
+ // Handle holding
341
+ for (var key in gbox._keymap)
342
+ if (gbox._keyboard[gbox._keymap[key]]==-1) gbox._keyboard[gbox._keymap[key]]=0; else
343
+ if (gbox._keyboard[gbox._keymap[key]]&&(gbox._keyboard[gbox._keymap[key]]<100)) gbox._keyboard[gbox._keymap[key]]++;
344
+
345
+ gbox._framestart=gbox._mspf-(new Date().getTime()-gbox._framestart);
346
+ if (gbox._autoskip)
347
+ if ((gbox._framestart<gbox._autoskip.lowidle)&&(gbox._frameskip<gbox._autoskip.max)) gbox.setFrameskip(gbox._frameskip+1); else
348
+ if ((gbox._framestart>gbox._autoskip.hiidle)&&(gbox._frameskip>gbox._autoskip.min)) gbox.setFrameskip(gbox._frameskip-1);
349
+ if (gbox._statbar) gbox.debugGetstats();
350
+ this._gametimer=setTimeout(gbox.go,(gbox._framestart<=0?1:gbox._framestart));
351
+ },
352
+ debugGetstats:function() {
353
+ var statline="Idle: "+gbox._framestart+"/"+gbox._mspf+(gbox._frameskip>0?" ("+gbox._frameskip+"skip)":"")+" | ";
354
+ var cnt=0;
355
+ for (var g=0;g<gbox._groups.length;g++)
356
+ if (gbox._groupplay[gbox._groups[g]]) {
357
+ cnt=0;
358
+ for (var obj in gbox._objects[gbox._groups[g]]) cnt++;
359
+ if (cnt) statline+=gbox._groups[g]+"["+cnt+"] ";
360
+ }
361
+ /*
362
+ statline+="<br><br>";
363
+ var id=gbox._zindex.first;
364
+ while (id!=null) {
365
+ if (gbox._groupplay[gbox._zindex.data[id].g]) statline+=gbox._zindex.data[id].g+" | "+gbox._zindex.data[id].o+" ("+gbox._zindex.data[id].__prio+")<br>";
366
+ id=gbox._zindex.data[id].__next;
367
+ }
368
+ */
369
+ gbox.setStatBar(statline);
370
+ },
371
+ setZindex:function(th,z) {
372
+ if ((th.__zt==null)||(th.zindex!=z)) {
373
+ th.zindex=z;
374
+ this._zindexch.push({o:{g:th.group,o:th.id},z:z});
375
+ }
376
+ },
377
+ keyIsHit:function(id) { return this._keyboard[this._keymap[id]]==1},
378
+ keyIsPressed:function(id) { return this._keyboard[this._keymap[id]]>0},
379
+ keyIsHold:function(id) { return this._keyboard[this._keymap[id]]>1},
380
+ keyIsReleased:function(id) { return this._keyboard[this._keymap[id]]==-1},
381
+ getCamera:function() { return this._camera; },
382
+ setCameraY:function(y,viewdata) {
383
+ this._camera.y=y;
384
+ if (this._camera.y+this._camera.h>viewdata.h) this._camera.y=viewdata.h-this._screenh;
385
+ if (this._camera.y<0) this._camera.y=0;
386
+ },
387
+ setCameraX:function(x,viewdata) {
388
+ this._camera.x=x;
389
+ if (this._camera.x+this._camera.w>viewdata.w) this._camera.x=viewdata.w-this._screenw;
390
+ if (this._camera.x<0) this._camera.x=0;
391
+ },
392
+ centerCamera:function(data,viewdata) {
393
+ this.setCameraX(data.x-this._screenhw,viewdata);
394
+ this.setCameraY(data.y-this._screenhh,viewdata);
395
+ },
396
+ getGroups:function() { return this._groups; },
397
+ setGroups:function(g){
398
+ this._groups=g;
399
+ this._groupplay[gbox.ZINDEX_LAYER]=true;
400
+ for (var i=0;i<g.length;i++)
401
+ if (!this._objects[g[i]]) {
402
+ this._objects[g[i]]={};
403
+ this._groupplay[g[i]]=true;
404
+ this._renderorder[i]=g[i];
405
+ }
406
+ },
407
+ setRenderOrder:function(g) { this._renderorder=g; },
408
+ playGroup:function(gid){this._groupplay[gid]=true;},
409
+ stopGroup:function(gid){this._groupplay[gid]=false;},
410
+ toggleGroup:function(gid){this._groupplay[gid]=!this._groupplay[gid];},
411
+ soloGroup:function(gid) {
412
+ for (var i=0;i<this._groups.length;i++)
413
+ if (this._groups[i]==gid) this.playGroup(this._groups[i]); else this.stopGroup(this._groups[i]);
414
+ },
415
+ playAllGroups:function() { for (var i=0;i<this._groups.length;i++) this.playGroup(this._groups[i]); },
416
+ clearGroup:function(group) {
417
+ for (var obj in this._objects[group]) {
418
+ if (this._objects[group][obj].__zt!=null) this._zindex.remove(this._objects[group][obj].__zt);
419
+ delete this._objects[group][obj];
420
+ }
421
+ },
422
+ playGroups:function(gid){for (var i=0;i<gid.length;i++)this.playGroup(gid[i])},
423
+ stopGroups:function(gid){for (var i=0;i<gid.length;i++)this.stopGroup(gid[i])},
424
+ toggleGroups:function(gid){for (var i=0;i<gid.length;i++)this.toggleGroup(gid[i])},
425
+ getObject:function(group,id) {return this._objects[group][id]},
426
+ addFont:function(data) {
427
+ data.tilehh=Math.floor(data.tileh/2);
428
+ data.tilehw=Math.floor(data.tilew/2);
429
+ this._fonts[data.id]=data;
430
+ this._fonts[data.id].firstascii=data.firstletter.charCodeAt(0);
431
+ },
432
+ getFont:function(id) {
433
+ return this._fonts[id];
434
+ },
435
+ trashObject:function(obj) {
436
+ if (!this._garbage[obj.group]) this._garbage[obj.group]={};
437
+ this._garbage[obj.group][obj.id]=1;
438
+ obj.__trashing=true;
439
+ },
440
+ purgeGarbage:function() {
441
+ for (var group in this._garbage)
442
+ for (var id in this._garbage[group]) {
443
+ if (this._objects[group][id].__zt!=null)
444
+ this._zindex.remove(this._objects[group][id].__zt)
445
+ delete this._objects[group][id];
446
+ }
447
+ gbox._garbage={};
448
+ },
449
+ trashGroup:function(group) {
450
+ if (!this._garbage[group]) this._garbage[group]={};
451
+ for (var obj in this._objects[group])
452
+ this._garbage[group][obj]=1;
453
+ },
454
+ objectIsTrash:function(o) { return o.__trashing },
455
+ addObject:function(data) {
456
+ // Extras
457
+ if (!data.id) {
458
+ data.id="obj-"+this._autoid;
459
+ this._autoid=(this._autoid+1)%1000;
460
+ }
461
+ if (data.tileset) {
462
+ if (data.h==null) data.h=this._tiles[data.tileset].tileh;
463
+ if (data.w==null) data.w=this._tiles[data.tileset].tilew;
464
+ if (data.hw==null) data.hw=this._tiles[data.tileset].tilehw;
465
+ if (data.hh==null) data.hh=this._tiles[data.tileset].tilehh;
466
+ }
467
+ this._objects[data.group][data.id]=data;
468
+ if (data.zindex!=null)
469
+ this.setZindex(this._objects[data.group][data.id],data.zindex);
470
+ return this._objects[data.group][data.id];
471
+ },
472
+ groupIsEmpty:function(gid) { for (var i in this._objects[gid]) return false; return true; },
473
+ createCanvas:function(id,data) {
474
+ if (this._canvas[id]) delete this._canvas[id];
475
+ this._canvas[id]=document.createElement("canvas");
476
+ this._canvas[id].setAttribute('height',(data&&data.h?data.h:this._screenh));
477
+ this._canvas[id].setAttribute('width',(data&&data.w?data.w:this._screenw));
478
+ },
479
+ getImage:function(id){return this._images[id]},
480
+ getBuffer:function(id){return this.getCanvas("_buffer")},
481
+ getBufferContext:function(id){ return (gbox._fskid>=gbox._frameskip?(this._db?this.getCanvasContext("_buffer"):this._screen.getContext("2d")):null) },
482
+ getCanvas:function(id){return this._canvas[id]},
483
+ getCanvasContext:function(id){return this.getCanvas(id).getContext("2d");},
484
+ addImage:function(id,filename) {
485
+ this._count++;
486
+ this._images[id]=new Image();
487
+ this._images[id].addEventListener('load', this._loaded,false);
488
+ this._images[id].src=filename;
489
+ this._images[id].setAttribute('id',id);
490
+ },
491
+ addTiles:function(t) {
492
+ t.tilehh=Math.floor(t.tileh/2);
493
+ t.tilehw=Math.floor(t.tilew/2);
494
+ this._tiles[t.id]=t;
495
+ },
496
+ getTiles:function(t) { return this._tiles[t] },
497
+ loadAll:function() {
498
+ if (gbox._count!=gbox._countloaded) {
499
+ gbox.setStatBar("Loading... ("+gbox._count+"/"+gbox._countloaded+")");
500
+ setTimeout(gbox.loadAll,50);
501
+ } else {
502
+ // Calculate half heights
503
+ for (var id in gbox._images) {
504
+ gbox._images[id].hheight=Math.floor(gbox._images[id].height/2);
505
+ gbox._images[id].hwidth=Math.floor(gbox._images[id].width/2);
506
+ }
507
+ gbox.setStatBar();
508
+ gbox._cb();
509
+ }
510
+ },
511
+ _implicitsargs:function(data) {
512
+ if (data.camera) {
513
+ data.dx-=this._camera.x;
514
+ data.dy-=this._camera.y;
515
+ }
516
+ if (data.sourcecamera) {
517
+ data.x=this._camera.x*(data.parallaxx?data.parallaxx:1);
518
+ data.y=this._camera.y*(data.parallaxy?data.parallaxy:1);
519
+ }
520
+ },
521
+ blitTile:function(tox,data) {
522
+ if (tox==null) return;
523
+ var ts=this._tiles[data.tileset];
524
+ var img=this.getImage(ts.image);
525
+ this._implicitsargs(data);
526
+ tox.save();
527
+ tox.globalAlpha=(data.alpha?data.alpha:1);
528
+ tox.translate((data.fliph?ts.tilew:0), (data.flipv?ts.tileh:0)); tox.scale((data.fliph?-1:1), (data.flipv?-1:1));
529
+ this._safedrawimage(tox,img, ts.gapx+(ts.tilew*(data.tile%ts.tilerow)),ts.gapy+(ts.tileh*Math.floor(data.tile/ts.tilerow)),(data.w==null?ts.tilew:data.w),(data.h==null?ts.tileh:data.h),data.dx*(data.fliph?-1:1),data.dy*(data.flipv?-1:1),(data.w?data.w:ts.tilew),(data.h?data.h:ts.tileh));
530
+ tox.restore();
531
+ },
532
+ blitAll:function(tox,image,data) {
533
+ if (tox==null) return;
534
+ this._implicitsargs(data);
535
+ tox.save();
536
+ tox.globalAlpha=(data.alpha?data.alpha:1);
537
+ tox.translate((data.fliph?image.width:0), (data.flipv?image.height:0)); tox.scale((data.fliph?-1:1), (data.flipv?-1:1));
538
+ tox.drawImage(image, data.dx*(data.fliph?-1:1),data.dy*(data.flipv?-1:1));
539
+ tox.restore();
540
+ },
541
+ blit:function(tox,image,data) {
542
+ if (tox==null) return;
543
+ this._implicitsargs(data);
544
+ tox.save();
545
+ tox.globalAlpha=(data.alpha?data.alpha:1);
546
+ tox.translate((data.fliph?data.dw:0), (data.flipv?data.dh:0)); tox.scale((data.fliph?-1:1), (data.flipv?-1:1));
547
+ this._safedrawimage(tox,image,(data.x?data.x:0), (data.y?data.y:0),(data.w?data.w:data.dw),(data.h?data.h:data.dh),data.dx*(data.fliph?-1:1),data.dy*(data.flipv?-1:1),data.dw,data.dh);
548
+ tox.restore();
549
+ },
550
+ blitTilemap:function(tox,data) {
551
+ if (tox==null) return;
552
+ var ts=this._tiles[data.tileset];
553
+ for (var y=0;y<data.map.length;y++)
554
+ for (var x=0;x<data.map[y].length;x++)
555
+ if (data.map[y][x]!=null) this.blitTile(tox,{tileset:data.tileset,tile:data.map[y][x],dx:x*ts.tilew,dy:y*ts.tilew});
556
+ },
557
+ blitText:function(tox,data) {
558
+ if (tox==null) return;
559
+ data.text+=""; // Convert to string.
560
+ var fn=this._fonts[data.font];
561
+ var tile=0;
562
+ this._implicitsargs(data);
563
+ var dx=data.dx;
564
+ var dy=data.dy;
565
+ if (data.valign==gbox.ALIGN_BOTTOM) dy = dy+data.dh-fn.tileh;
566
+ else if (data.valign==gbox.ALIGN_MIDDLE) dy = dy+Math.floor(data.dh/2)-fn.tileh;
567
+ if (data.halign==gbox.ALIGN_RIGHT) dx = dx+data.dw-(data.text.length*fn.tilew);
568
+ else if (data.halign==gbox.ALIGN_CENTER) dx = dx+Math.floor((data.dw-(data.text.length*fn.tilew))/2);
569
+ tox.save();
570
+ tox.globalAlpha=(data.alpha?data.alpha:1);
571
+ for (var y=0;y<data.text.length;y++) {
572
+ tile=data.text.charCodeAt(y)-fn.firstascii;
573
+ if (tile>=0) {
574
+ if (data.clear) tox.clearRect(dx+(y*fn.tilew),dy,(data.w?data.w:fn.tilew),(data.h?data.h:fn.tileh));
575
+ this._safedrawimage(tox,this.getImage(fn.image), fn.gapx+(fn.tilew*(tile%fn.tilerow)),
576
+ fn.gapy+(fn.tileh*Math.floor(tile/fn.tilerow)),fn.tilew,fn.tileh,dx+(y*fn.tilew),dy,(data.w?data.w:fn.tilew),(data.h?data.h:fn.tileh));
577
+ }
578
+ }
579
+ tox.restore();
580
+ },
581
+ blitClear:function(image,data) {
582
+ if (image==null) return;
583
+ if (data==null) data={x:0,y:0};
584
+ this._implicitsargs(data);
585
+ image.clearRect(data.x,data.y,(data.w==null?image.canvas.width:data.w),(data.h==null?image.canvas.height:data.h));
586
+ },
587
+ blitImageToScreen:function(image) {
588
+ this._screen.getContext("2d").drawImage(image,0,0);
589
+ },
590
+ blitFade:function(tox,data) {
591
+ if (tox) this.blitRect(tox,{x:0,y:0,w:tox.canvas.width,h:tox.canvas.height,alpha:data.alpha,color:data.color});
592
+ },
593
+ blitRect:function(tox,data) {
594
+ if (tox==null) return;
595
+ tox.save();
596
+ tox.globalAlpha=(data.alpha?data.alpha:1);
597
+ tox.fillStyle = (data.color?data.color:gbox.COLOR_BLACK);
598
+ tox.fillRect(data.x,data.y,data.w,data.h);
599
+ tox.restore();
600
+ },
601
+ collides:function(o1,o2,t) {
602
+ if (!t) t=0;
603
+ return !((o1.y+o1.h-1-t<o2.y+t) || (o1.y+t> o2.y+o2.h-1-t) || (o1.x+o1.w-1-t<o2.x+t) || (o1.x+t>o2.x+o2.w-1-t));
604
+ },
605
+ pixelcollides:function(o1,o2,t) {
606
+ if (!t) t=0;
607
+ return !((o1.y<o2.y+t) || (o1.y> o2.y+o2.h-1-t) || (o1.x<o2.x+t) || (o1.x>o2.x+o2.w-1-t));
608
+ },
609
+ objectIsVisible:function(obj) { return this.collides(obj,this._camera,0); }
610
+ };
611
+