Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/js/jgame.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 170 - (show annotations) (download) (as text)
Mon May 4 03:43:41 2015 UTC (9 years ago) by tsugehara
File MIME type: application/x-javascript
File size: 237679 byte(s)


1 ///<reference path="all.ts"/>
2 ///<reference path="all.ts"/>
3 var jg;
4 (function (jg) {
5 (function (Angle) {
6 Angle[Angle["Unknown"] = 0] = "Unknown";
7 Angle[Angle["Left"] = 1] = "Left";
8 Angle[Angle["Right"] = 2] = "Right";
9 Angle[Angle["Up"] = 3] = "Up";
10 Angle[Angle["Down"] = 4] = "Down";
11 })(jg.Angle || (jg.Angle = {}));
12 var Angle = jg.Angle;
13 (function (RenderTransferMode) {
14 RenderTransferMode[RenderTransferMode["Unknown"] = 0] = "Unknown";
15 RenderTransferMode[RenderTransferMode["Transfer"] = 1] = "Transfer";
16 RenderTransferMode[RenderTransferMode["Flip"] = 2] = "Flip";
17 RenderTransferMode[RenderTransferMode["Direct"] = 3] = "Direct";
18 })(jg.RenderTransferMode || (jg.RenderTransferMode = {}));
19 var RenderTransferMode = jg.RenderTransferMode;
20 (function (InputEventType) {
21 InputEventType[InputEventType["Unknown"] = 0] = "Unknown";
22 InputEventType[InputEventType["Keyboard"] = 1] = "Keyboard";
23 InputEventType[InputEventType["Point"] = 2] = "Point";
24 })(jg.InputEventType || (jg.InputEventType = {}));
25 var InputEventType = jg.InputEventType;
26 (function (InputEventAction) {
27 InputEventAction[InputEventAction["Unknown"] = 0] = "Unknown";
28 InputEventAction[InputEventAction["Down"] = 1] = "Down";
29 InputEventAction[InputEventAction["Move"] = 2] = "Move";
30 InputEventAction[InputEventAction["Up"] = 3] = "Up";
31 })(jg.InputEventAction || (jg.InputEventAction = {}));
32 var InputEventAction = jg.InputEventAction;
33 (function (Keytype) {
34 Keytype[Keytype["Unknown"] = 0] = "Unknown";
35 Keytype[Keytype["Left"] = 1] = "Left";
36 Keytype[Keytype["Right"] = 2] = "Right";
37 Keytype[Keytype["Up"] = 3] = "Up";
38 Keytype[Keytype["Down"] = 4] = "Down";
39 Keytype[Keytype["Enter"] = 5] = "Enter";
40 Keytype[Keytype["Esc"] = 6] = "Esc";
41 })(jg.Keytype || (jg.Keytype = {}));
42 var Keytype = jg.Keytype;
43 (function (ShapeStyle) {
44 ShapeStyle[ShapeStyle["Unknown"] = 0] = "Unknown";
45 ShapeStyle[ShapeStyle["Stroke"] = 1] = "Stroke";
46 ShapeStyle[ShapeStyle["Fill"] = 2] = "Fill";
47 })(jg.ShapeStyle || (jg.ShapeStyle = {}));
48 var ShapeStyle = jg.ShapeStyle;
49 (function (ShapeType) {
50 ShapeType[ShapeType["Unknown"] = 0] = "Unknown";
51 ShapeType[ShapeType["Rect"] = 1] = "Rect";
52 ShapeType[ShapeType["Arc"] = 2] = "Arc";
53 })(jg.ShapeType || (jg.ShapeType = {}));
54 var ShapeType = jg.ShapeType;
55 (function (EffectType) {
56 EffectType[EffectType["None"] = 0] = "None";
57 EffectType[EffectType["Fade"] = 1] = "Fade";
58 EffectType[EffectType["Mosaic"] = 2] = "Mosaic";
59 EffectType[EffectType["Blur"] = 3] = "Blur";
60 EffectType[EffectType["SlideUp"] = 4] = "SlideUp";
61 EffectType[EffectType["SlideDown"] = 5] = "SlideDown";
62 EffectType[EffectType["SlideLeft"] = 6] = "SlideLeft";
63 EffectType[EffectType["SlideRight"] = 7] = "SlideRight";
64 EffectType[EffectType["WipeUp"] = 8] = "WipeUp";
65 EffectType[EffectType["WipeDown"] = 9] = "WipeDown";
66 EffectType[EffectType["WipeLeft"] = 10] = "WipeLeft";
67 EffectType[EffectType["WipeRight"] = 11] = "WipeRight";
68 EffectType[EffectType["WipeFadeUp"] = 12] = "WipeFadeUp";
69 EffectType[EffectType["WipeFadeDown"] = 13] = "WipeFadeDown";
70 EffectType[EffectType["WipeFadeLeft"] = 14] = "WipeFadeLeft";
71 EffectType[EffectType["WipeFadeRight"] = 15] = "WipeFadeRight";
72 EffectType[EffectType["BoxOut"] = 16] = "BoxOut";
73 EffectType[EffectType["BoxOut45"] = 17] = "BoxOut45";
74 EffectType[EffectType["BoxIn"] = 18] = "BoxIn";
75 EffectType[EffectType["BoxIn45"] = 19] = "BoxIn45";
76 EffectType[EffectType["ArcOut"] = 20] = "ArcOut";
77 EffectType[EffectType["ArcIn"] = 21] = "ArcIn";
78 })(jg.EffectType || (jg.EffectType = {}));
79 var EffectType = jg.EffectType;
80 })(jg || (jg = {}));
81 ///<reference path="all.ts"/>
82 if (!window.requestAnimationFrame) {
83 window.requestAnimationFrame =
84 window["mozRequestAnimationFrame"] ||
85 window["webkitRequestAnimationFrame"] ||
86 window["msRequestAnimationFrame"] ||
87 (function () {
88 var lastTime = Date.now ? Date.now() : new Date().getTime();
89 var frame = 1000 / 60;
90 return function (func) {
91 var currentTime = Date.now ? Date.now() : new Date().getTime();
92 var _id = setTimeout(function () {
93 func(Date.now ? Date.now() : new Date().getTime());
94 }, Math.max(0, lastTime + frame - currentTime));
95 lastTime = currentTime;
96 return _id;
97 };
98 }());
99 }
100 window.createCanvas = function (width, height) {
101 var canvas = document.createElement("canvas");
102 canvas.width = width;
103 canvas.height = height;
104 if ("imageSmoothingEnabled" in window) {
105 var browser = jg.JGUtil.getBrowser();
106 if (browser.opera) {
107 canvas.style["imageRendering"] = "-o-crisp-edges";
108 }
109 else if (browser.msie) {
110 canvas.style["msInterpolationMode"] = "nearest-neighbor";
111 }
112 else if (browser.safari) {
113 canvas.style["imageRendering"] = "-webkit-optimize-contrast";
114 }
115 var context = canvas.getContext("2d");
116 if (context["imageSmoothingEnabled"])
117 context["imageSmoothingEnabled"] = false;
118 if (context["webkitImageSmoothingEnabled"])
119 context["webkitImageSmoothingEnabled"] = false;
120 if (context["mozImageSmoothingEnabled"])
121 context["mozImageSmoothingEnabled"] = false;
122 }
123 return canvas;
124 };
125 ///<reference path="all.ts"/>
126 var jg;
127 (function (jg) {
128 var Rectangle = (function () {
129 function Rectangle(left, top, right, bottom) {
130 this.left = left;
131 this.top = top;
132 this.right = right;
133 this.bottom = bottom;
134 }
135 Rectangle.prototype.hitTest = function (point) {
136 return (this.left <= point.x && this.right >= point.x && this.top <= point.y && this.bottom >= point.y);
137 };
138 Rectangle.prototype.fit = function (point) {
139 if (point.y < this.top)
140 point.y = this.top;
141 else if (point.y > this.bottom)
142 point.y = this.bottom;
143 if (point.x < this.left)
144 point.x = this.left;
145 else if (point.x > this.right)
146 point.x = this.right;
147 };
148 Rectangle.prototype.width = function () {
149 return Math.abs(this.right - this.left);
150 };
151 Rectangle.prototype.height = function () {
152 return Math.abs(this.bottom - this.top);
153 };
154 return Rectangle;
155 })();
156 jg.Rectangle = Rectangle;
157 })(jg || (jg = {}));
158 ///<reference path="all.ts"/>
159 var jg;
160 (function (jg) {
161 var Easing = (function () {
162 function Easing() {
163 }
164 Easing.LINEAR = function (t, b, c, d) {
165 return c * t / d + b;
166 };
167 Easing.SWING = function (t, b, c, d) {
168 return c * (0.5 - Math.cos(((t / d) * Math.PI)) / 2) + b;
169 };
170 Easing.QUAD_EASEIN = function (t, b, c, d) {
171 return c * (t /= d) * t + b;
172 };
173 Easing.QUAD_EASEOUT = function (t, b, c, d) {
174 return -c * (t /= d) * (t - 2) + b;
175 };
176 Easing.QUAD_EASEINOUT = function (t, b, c, d) {
177 if ((t /= d / 2) < 1) {
178 return c / 2 * t * t + b;
179 }
180 return -c / 2 * ((--t) * (t - 2) - 1) + b;
181 };
182 Easing.CUBIC_EASEIN = function (t, b, c, d) {
183 return c * (t /= d) * t * t + b;
184 };
185 Easing.CUBIC_EASEOUT = function (t, b, c, d) {
186 return c * ((t = t / d - 1) * t * t + 1) + b;
187 };
188 Easing.CUBIC_EASEINOUT = function (t, b, c, d) {
189 if ((t /= d / 2) < 1) {
190 return c / 2 * t * t * t + b;
191 }
192 return c / 2 * ((t -= 2) * t * t + 2) + b;
193 };
194 Easing.QUART_EASEIN = function (t, b, c, d) {
195 return c * (t /= d) * t * t * t + b;
196 };
197 Easing.QUART_EASEOUT = function (t, b, c, d) {
198 return -c * ((t = t / d - 1) * t * t * t - 1) + b;
199 };
200 Easing.QUART_EASEINOUT = function (t, b, c, d) {
201 if ((t /= d / 2) < 1) {
202 return c / 2 * t * t * t * t + b;
203 }
204 return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
205 };
206 Easing.QUINT_EASEIN = function (t, b, c, d) {
207 return c * (t /= d) * t * t * t * t + b;
208 };
209 Easing.QUINT_EASEOUT = function (t, b, c, d) {
210 return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
211 };
212 Easing.QUINT_EASEINOUT = function (t, b, c, d) {
213 if ((t /= d / 2) < 1) {
214 return c / 2 * t * t * t * t * t + b;
215 }
216 return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
217 };
218 Easing.SIN_EASEIN = function (t, b, c, d) {
219 return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
220 };
221 Easing.SIN_EASEOUT = function (t, b, c, d) {
222 return c * Math.sin(t / d * (Math.PI / 2)) + b;
223 };
224 Easing.SIN_EASEINOUT = function (t, b, c, d) {
225 return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
226 };
227 Easing.CIRC_EASEIN = function (t, b, c, d) {
228 return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
229 };
230 Easing.CIRC_EASEOUT = function (t, b, c, d) {
231 return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
232 };
233 Easing.CIRC_EASEINOUT = function (t, b, c, d) {
234 if ((t /= d / 2) < 1) {
235 return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
236 }
237 return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
238 };
239 Easing.ELASTIC_EASEIN = function (t, b, c, d, a, p) {
240 if (t === 0) {
241 return b;
242 }
243 if ((t /= d) === 1) {
244 return b + c;
245 }
246 if (!p) {
247 p = d * 0.3;
248 }
249 var s;
250 if (!a || a < Math.abs(c)) {
251 a = c;
252 s = p / 4;
253 }
254 else {
255 s = p / (2 * Math.PI) * Math.asin(c / a);
256 }
257 return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
258 };
259 Easing.ELASTIC_EASEOUT = function (t, b, c, d, a, p) {
260 if (t === 0) {
261 return b;
262 }
263 if ((t /= d) === 1) {
264 return b + c;
265 }
266 if (!p) {
267 p = d * 0.3;
268 }
269 var s;
270 if (!a || a < Math.abs(c)) {
271 a = c;
272 s = p / 4;
273 }
274 else {
275 s = p / (2 * Math.PI) * Math.asin(c / a);
276 }
277 return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
278 };
279 Easing.ELASTIC_EASEINOUT = function (t, b, c, d, a, p) {
280 if (t === 0) {
281 return b;
282 }
283 if ((t /= d / 2) === 2) {
284 return b + c;
285 }
286 if (!p) {
287 p = d * (0.3 * 1.5);
288 }
289 var s;
290 if (!a || a < Math.abs(c)) {
291 a = c;
292 s = p / 4;
293 }
294 else {
295 s = p / (2 * Math.PI) * Math.asin(c / a);
296 }
297 if (t < 1) {
298 return -0.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
299 }
300 return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * 0.5 + c + b;
301 };
302 Easing.BOUNCE_EASEOUT = function (t, b, c, d) {
303 if ((t /= d) < (1 / 2.75)) {
304 return c * (7.5625 * t * t) + b;
305 }
306 else if (t < (2 / 2.75)) {
307 return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b;
308 }
309 else if (t < (2.5 / 2.75)) {
310 return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b;
311 }
312 else {
313 return c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b;
314 }
315 };
316 Easing.BOUNCE_EASEIN = function (t, b, c, d) {
317 return c - Easing.BOUNCE_EASEOUT(d - t, 0, c, d) + b;
318 };
319 Easing.BOUNCE_EASEINOUT = function (t, b, c, d) {
320 if (t < d / 2) {
321 return Easing.BOUNCE_EASEIN(t * 2, 0, c, d) * 0.5 + b;
322 }
323 else {
324 return Easing.BOUNCE_EASEOUT(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
325 }
326 };
327 Easing.BACK_EASEIN = function (t, b, c, d, s) {
328 if (s === undefined) {
329 s = 1.70158;
330 }
331 return c * (t /= d) * t * ((s + 1) * t - s) + b;
332 };
333 Easing.BACK_EASEOUT = function (t, b, c, d, s) {
334 if (s === undefined) {
335 s = 1.70158;
336 }
337 return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
338 };
339 Easing.BACK_EASEINOUT = function (t, b, c, d, s) {
340 if (s === undefined) {
341 s = 1.70158;
342 }
343 if ((t /= d / 2) < 1) {
344 return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
345 }
346 return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
347 };
348 Easing.EXPO_EASEIN = function (t, b, c, d) {
349 return (t === 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
350 };
351 Easing.EXPO_EASEOUT = function (t, b, c, d) {
352 return (t === d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
353 };
354 Easing.EXPO_EASEINOUT = function (t, b, c, d) {
355 if (t === 0) {
356 return b;
357 }
358 if (t === d) {
359 return b + c;
360 }
361 if ((t /= d / 2) < 1) {
362 return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
363 }
364 return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
365 };
366 return Easing;
367 })();
368 jg.Easing = Easing;
369 })(jg || (jg = {}));
370 ///<reference path="all.ts"/>
371 var jg;
372 (function (jg) {
373 jg.ENTITY_OPTIONS_DEFAULT_VALUES = {
374 rotate: 0,
375 translate: { x: 0, y: 0 },
376 transform: { m11: 1, m12: 0, m21: 0, m22: 1, dx: 0, dy: 0 },
377 scale: { x: 1, y: 1 },
378 purescale: { x: 1, y: 1 },
379 globalAlpha: undefined,
380 font: undefined,
381 fillStyle: undefined,
382 strokeStyle: undefined,
383 lineCap: undefined,
384 lineJoin: undefined,
385 lineWidth: undefined,
386 miterLimit: undefined,
387 shadowBlur: undefined,
388 shadowColor: undefined,
389 shadowOffsetX: undefined,
390 shadowOffsetY: undefined,
391 textAlign: undefined,
392 textBaseline: undefined,
393 globalCompositeOperation: undefined
394 };
395 var E = (function () {
396 function E() {
397 this.opacity = 1;
398 this.x = 0;
399 this.y = 0;
400 }
401 E.prototype.enablePointingEvent = function () {
402 this.pointCapture = true;
403 if (!this.pointDown)
404 this.pointDown = new jg.Trigger();
405 if (!this.pointUp)
406 this.pointUp = new jg.Trigger();
407 if (!this.pointMove)
408 this.pointMove = new jg.Trigger();
409 };
410 E.prototype.disablePointingEvent = function () {
411 delete this.pointCapture;
412 };
413 E.prototype.removeDrawOption = function (name) {
414 if (!this.options)
415 return;
416 if (this.options[name] !== undefined)
417 delete this.options[name];
418 var cnt = 0;
419 for (var i in this.options) {
420 cnt++;
421 break;
422 }
423 if (!cnt)
424 delete this.options;
425 this.updated();
426 };
427 E.prototype.setDrawOption = function (name, value) {
428 if (!this.options)
429 this.options = {};
430 this.options[name] = value;
431 this.updated();
432 };
433 E.prototype.getDrawOption = function (name) {
434 if (!this.options || this.options[name] == undefined)
435 return jg.ENTITY_OPTIONS_DEFAULT_VALUES[name];
436 return this.options[name];
437 };
438 E.prototype.moveTo = function (x, y) {
439 this.x = x;
440 this.y = y;
441 this.updated();
442 };
443 E.prototype.moveBy = function (x, y) {
444 this.x += x;
445 this.y += y;
446 this.updated();
447 };
448 E.prototype.scrollTo = function (x, y) {
449 this.scroll = {
450 x: x,
451 y: y
452 };
453 this.updated();
454 };
455 E.prototype.scrollBy = function (x, y) {
456 if (!this.scroll)
457 this.scroll = { x: 0, y: 0 };
458 this.scroll.x += x;
459 this.scroll.y += y;
460 this.updated();
461 };
462 E.prototype.activate = function () {
463 if (this.active_queue) {
464 var f;
465 while (f = this.active_queue.shift())
466 f.call(this);
467 delete this.active_queue;
468 }
469 if (this.entities) {
470 for (var i = 0; i < this.entities.length; i++) {
471 if (!this.entities[i].scene) {
472 this.entities[i].scene = this.scene;
473 this.entities[i].activate();
474 }
475 }
476 }
477 };
478 E.prototype.addActiveQueue = function (f) {
479 if (this.scene) {
480 f.call(this);
481 return;
482 }
483 if (!this.active_queue)
484 this.active_queue = [];
485 this.active_queue.push(f);
486 };
487 E.prototype.appendTo = function (scene, layerName) {
488 scene.append(this, layerName);
489 };
490 E.prototype.remove = function () {
491 if (this.parent)
492 this.parent.removeChild(this);
493 else
494 throw "Can not remove layer. (use scene.deleteLayer)";
495 };
496 E.prototype.insert = function (entity, index) {
497 if (!this.entities)
498 throw "Can not call append of non-container entity";
499 entity.scene = this.scene;
500 entity.parent = this;
501 if (typeof index != "number") {
502 for (var i = 0; i < this.entities.length; i++) {
503 if (this.entities[i] == index) {
504 index = i;
505 break;
506 }
507 }
508 }
509 this.entities.splice(index, 0, entity);
510 entity.activate();
511 this.updated();
512 };
513 E.prototype.append = function (entity) {
514 if (!this.entities)
515 throw "Can not call append of non-container entity";
516 entity.scene = this.scene;
517 entity.parent = this;
518 this.entities.push(entity);
519 if (this.scene)
520 entity.activate();
521 this.updated();
522 };
523 E.prototype.removeChild = function (entity) {
524 if (!this.entities)
525 throw "Can not call removeChild of non-container entity";
526 for (var i = 0; i < this.entities.length; i++) {
527 if (this.entities[i] == entity) {
528 if (entity.entities) {
529 var childEntity;
530 while (childEntity = entity.entities.pop())
531 entity.removeChild(childEntity);
532 }
533 this.entities.splice(i, 1);
534 entity.destroy();
535 this.updated();
536 return true;
537 }
538 }
539 return false;
540 };
541 E.prototype.start = function () {
542 if (this.started)
543 return;
544 this.started = true;
545 if (this.scene)
546 this.scene.game.update.handle(this, this.update);
547 else
548 this.addActiveQueue(function () { this.scene.game.update.handle(this, this.update); });
549 };
550 E.prototype.stop = function () {
551 if (!this.started)
552 return;
553 this.started = false;
554 if (this.scene)
555 this.scene.game.update.remove(this, this.update);
556 else
557 this.addActiveQueue(function () { this.scene.game.update.remove(this, this.update); });
558 };
559 E.prototype.startTimer = function (wait, method) {
560 if (this.scene)
561 this.scene.game.addTimer(wait, this, method ? method : this.interval);
562 else
563 this.addActiveQueue(function () { this.scene.game.addTimer(wait, this, method ? method : this.interval); });
564 };
565 E.prototype.stopTimer = function (wait, method) {
566 if (this.scene)
567 this.scene.game.removeTimer(wait, this, method ? method : this.interval);
568 else
569 this.addActiveQueue(function () { this.scene.game.removeTimer(wait, this, method ? method : this.interval); });
570 };
571 E.prototype.updated = function () {
572 var p = this;
573 while (p.parent)
574 p = p.parent;
575 p.isUpdated = true;
576 };
577 E.prototype.tl = function () {
578 if (!this._tl)
579 this._tl = new jg.Timeline(this);
580 return this._tl;
581 };
582 E.prototype.destroy = function () {
583 if (this._tl) {
584 this._tl.clear();
585 delete this._tl;
586 }
587 this.stop();
588 if (this.scene) {
589 this.scene.game.removeTimerAll(this);
590 this.scene = null;
591 }
592 delete this.parent;
593 if (this.entities) {
594 var childEntity;
595 while (childEntity = this.entities.pop())
596 childEntity.destroy();
597 }
598 if (this.pointDown) {
599 this.pointDown.destroy();
600 delete this.pointDown;
601 }
602 if (this.pointUp) {
603 this.pointUp.destroy();
604 delete this.pointUp;
605 }
606 if (this.pointMove) {
607 this.pointMove.destroy();
608 delete this.pointMove;
609 }
610 };
611 E.prototype.offset = function () {
612 var parent_offset = this.parent ? this.parent.offset() : { x: this.scroll ? this.scroll.x : 0, y: this.scroll ? this.scroll.y : 0 };
613 return {
614 x: this.x + parent_offset.x,
615 y: this.y + parent_offset.y
616 };
617 };
618 E.prototype.rect = function () {
619 var offset = this.offset();
620 return new jg.Rectangle(offset.x, offset.y, offset.x + this.width, offset.y + this.height);
621 };
622 E.prototype.hitTest = function (point) {
623 return this.rect().hitTest(point);
624 };
625 E.prototype.getDistance = function (point) {
626 var area = point;
627 if (area.width && area.height) {
628 return {
629 x: Math.abs((area.x + area.width / 2) - (this.x + this.width / 2)),
630 y: Math.abs((area.y + area.height / 2) - (this.y + this.height / 2))
631 };
632 }
633 else {
634 return {
635 x: Math.abs(point.x - (this.x + this.width / 2)),
636 y: Math.abs(point.y - (this.y + this.height / 2))
637 };
638 }
639 };
640 E.prototype.getEntityByPoint = function (point, force) {
641 if (this.entities) {
642 for (var i = this.entities.length - 1; i >= 0; i--) {
643 if (force || this.entities[i].pointCapture) {
644 var p = this.entities[i].getEntityByPoint(point);
645 if (p)
646 return p;
647 }
648 }
649 }
650 if ((force || this.pointCapture) && this.hitTest(point))
651 return this;
652 return null;
653 };
654 E.prototype.createSprite = function () {
655 var buffer = new jg.BufferedRenderer({ width: this.width, height: this.height });
656 var x = this.x;
657 var y = this.y;
658 this.x = 0;
659 this.y = 0;
660 buffer.renderUnit(this);
661 this.x = x;
662 this.y = y;
663 return buffer.createSprite();
664 };
665 E.prototype.update = function (t) { };
666 E.prototype.interval = function () { };
667 E.prototype.draw = function (context) { };
668 E.prototype.show = function () {
669 this.opacity = 1;
670 this.updated();
671 };
672 E.prototype.hide = function () {
673 this.opacity = 0;
674 this.updated();
675 };
676 return E;
677 })();
678 jg.E = E;
679 })(jg || (jg = {}));
680 (function () {
681 var canvas = document.createElement("canvas");
682 var context = canvas.getContext("2d");
683 for (var p in jg.ENTITY_OPTIONS_DEFAULT_VALUES)
684 if (jg.ENTITY_OPTIONS_DEFAULT_VALUES[p] === undefined)
685 jg.ENTITY_OPTIONS_DEFAULT_VALUES[p] = context[p];
686 })();
687 var __extends = this.__extends || function (d, b) {
688 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
689 function __() { this.constructor = d; }
690 __.prototype = b.prototype;
691 d.prototype = new __();
692 };
693 ///<reference path="all.ts"/>
694 var jg;
695 (function (jg) {
696 var Shape = (function (_super) {
697 __extends(Shape, _super);
698 function Shape(width, height, style, color, type) {
699 _super.call(this);
700 this.x = 0;
701 this.y = 0;
702 this.width = width;
703 this.height = height;
704 this.style = style ? style : jg.ShapeStyle.Stroke;
705 if (color)
706 this.setColor(color);
707 this.type = type ? type : jg.ShapeType.Rect;
708 }
709 Shape.prototype.setClip = function (value) {
710 this.clip = value;
711 if (this.clip)
712 this.disableTransform = true;
713 else
714 delete this.disableTransform;
715 };
716 Shape.prototype.setStyle = function (style) {
717 this.style = style;
718 this.setColor(this.getColor());
719 };
720 Shape.prototype.setLineWidth = function (width) {
721 this.setDrawOption("lineWidth", width);
722 };
723 Shape.prototype.getLineWidth = function () {
724 return this.getDrawOption("lineWidth");
725 };
726 Shape.prototype.setColor = function (color) {
727 if (this.style == jg.ShapeStyle.Stroke)
728 this.setDrawOption("strokeStyle", color);
729 else
730 this.setDrawOption("fillStyle", color);
731 };
732 Shape.prototype.getColor = function () {
733 if (this.style == jg.ShapeStyle.Stroke)
734 return this.getDrawOption("strokeStyle");
735 else
736 return this.getDrawOption("filltyle");
737 };
738 Shape.prototype.synchronize = function (syncObj, syncFunc) {
739 this.syncObj = syncObj;
740 this.syncFunc = syncFunc;
741 };
742 Shape.prototype.draw = function (context) {
743 if (this.syncObj)
744 this.syncFunc.call(this.syncObj, this);
745 if (this.clip) {
746 context.save();
747 context.translate(this.x, this.y);
748 if (this.options)
749 this.scene.game.renderer.useDrawOption(this, context);
750 }
751 context.beginPath();
752 switch (this.type) {
753 case jg.ShapeType.Rect:
754 context.rect(0, 0, this.width, this.height);
755 break;
756 case jg.ShapeType.Arc:
757 var w2 = this.width / 2;
758 context.arc(w2, w2, w2, 0, Shape.PI_200_PER, false);
759 break;
760 }
761 if (this.clip) {
762 context.restore();
763 context.clip();
764 }
765 else if (this.style == jg.ShapeStyle.Fill)
766 context.fill();
767 else
768 context.stroke();
769 };
770 Shape.PI_200_PER = Math.PI * 2;
771 return Shape;
772 })(jg.E);
773 jg.Shape = Shape;
774 })(jg || (jg = {}));
775 ///<reference path="all.ts"/>
776 var jg;
777 (function (jg) {
778 var Trigger = (function () {
779 function Trigger() {
780 this.handlers = [];
781 }
782 Trigger.prototype.handleInsert = function (index, owner, handler) {
783 if (!handler)
784 this.handlers.splice(index, 0, { owner: window, handler: owner });
785 else
786 this.handlers.splice(index, 0, { owner: owner, handler: handler });
787 };
788 Trigger.prototype.handle = function (owner, handler) {
789 if (!handler)
790 this.handlers.push({ owner: window, handler: owner });
791 else
792 this.handlers.push({ owner: owner, handler: handler });
793 };
794 Trigger.prototype.destroy = function () {
795 this.handlers = [];
796 };
797 Trigger.prototype.removeAll = function (owner) {
798 var ret = [];
799 var tmp;
800 while (tmp = this.handlers.shift())
801 if (tmp.owner != owner)
802 ret.push(tmp);
803 this.handlers = ret;
804 };
805 Trigger.prototype.removeAllByHandler = function (handler) {
806 var ret = [];
807 var tmp;
808 while (tmp = this.handlers.shift())
809 if (tmp.handler != handler)
810 ret.push(tmp);
811 this.handlers = ret;
812 };
813 Trigger.prototype.remove = function (owner, handler) {
814 var ret = [];
815 var tmp;
816 if (!handler) {
817 handler = owner;
818 owner = window;
819 }
820 while (tmp = this.handlers.shift())
821 if (tmp.handler != handler || tmp.owner != owner)
822 ret.push(tmp);
823 this.handlers = ret;
824 };
825 Trigger.prototype.fire = function (param) {
826 if (this.handlers.length == 0)
827 return;
828 var handlers = this.handlers.concat();
829 for (var i = 0; i < handlers.length; i++)
830 handlers[i].handler.call(handlers[i].owner, param);
831 };
832 Trigger.prototype.fastFire = function (param) {
833 for (var i = 0; i < this.handlers.length; i++)
834 this.handlers[i].handler.call(this.handlers[i].owner, param);
835 };
836 return Trigger;
837 })();
838 jg.Trigger = Trigger;
839 })(jg || (jg = {}));
840 ///<reference path="all.ts"/>
841 var jg;
842 (function (jg) {
843 var SimpleSound = (function () {
844 function SimpleSound() {
845 }
846 SimpleSound.play = function (sound, loop) {
847 var _sound = new Audio(null);
848 _sound.src = sound.src;
849 _sound.loop = loop ? loop : false;
850 ;
851 _sound.play();
852 return _sound;
853 };
854 SimpleSound.hasBgm = function () {
855 return SimpleSound.bgm !== undefined;
856 };
857 SimpleSound.playBgm = function (sound, loop) {
858 if (SimpleSound.bgm)
859 SimpleSound.stopBgm();
860 sound.load();
861 sound.loop = loop ? loop : false;
862 sound.play();
863 SimpleSound.bgm = sound;
864 return SimpleSound.bgm;
865 };
866 SimpleSound.stop = function (source) {
867 if (!source)
868 return;
869 source.pause();
870 };
871 SimpleSound.stopBgm = function () {
872 SimpleSound.stop(SimpleSound.bgm);
873 delete SimpleSound.bgm;
874 };
875 return SimpleSound;
876 })();
877 jg.SimpleSound = SimpleSound;
878 })(jg || (jg = {}));
879 ///<reference path="all.ts"/>
880 var jg;
881 (function (jg) {
882 var ResourceStructure = (function () {
883 function ResourceStructure(img, sound) {
884 this.img = img;
885 this.sound = sound;
886 }
887 ResourceStructure.prototype.isAbsolute = function (url) {
888 var white = [".", "/"];
889 if (white.indexOf(url.charAt(0)) >= 0 || url.match(/^(http:|https:|data:)/))
890 return url;
891 };
892 ResourceStructure.prototype.imageUrl = function (url) {
893 if (this.isAbsolute(url))
894 return url;
895 return this.img + url;
896 };
897 ResourceStructure.prototype.soundUrl = function (url) {
898 if (this.isAbsolute(url))
899 return url;
900 return this.sound + url;
901 };
902 ResourceStructure.Default = new ResourceStructure("img/", "sound/");
903 ResourceStructure.Plain = new ResourceStructure("", "");
904 return ResourceStructure;
905 })();
906 jg.ResourceStructure = ResourceStructure;
907 })(jg || (jg = {}));
908 ///<reference path="all.ts"/>
909 var jg;
910 (function (jg) {
911 var ResourceLoader = (function () {
912 function ResourceLoader(resource) {
913 this.resource = resource;
914 }
915 ResourceLoader.prototype.load = function (url, identifier) {
916 };
917 return ResourceLoader;
918 })();
919 jg.ResourceLoader = ResourceLoader;
920 var ImageResourceLoader = (function (_super) {
921 __extends(ImageResourceLoader, _super);
922 function ImageResourceLoader() {
923 _super.apply(this, arguments);
924 }
925 ImageResourceLoader.prototype.load = function (url, identifier) {
926 var image = new Image();
927 image.src = this.resource.structure.imageUrl(url);
928 var caller = this;
929 var callback = this.completed;
930 image.onerror = function () {
931 callback.call(caller, identifier, image, false);
932 };
933 image.onload = function () {
934 callback.call(caller, identifier, image, true);
935 };
936 };
937 ImageResourceLoader.prototype.completed = function (name, image, is_success) {
938 if (!is_success)
939 console.log("error: " + name);
940 else
941 this.resource.images[name] = image;
942 this.resource.requestCompleted(name);
943 };
944 return ImageResourceLoader;
945 })(ResourceLoader);
946 jg.ImageResourceLoader = ImageResourceLoader;
947 var ScriptResourceLoader = (function (_super) {
948 __extends(ScriptResourceLoader, _super);
949 function ScriptResourceLoader() {
950 _super.apply(this, arguments);
951 }
952 ScriptResourceLoader.prototype.load = function (url, identifier) {
953 var _this = this;
954 var callback = this.completed;
955 var waitLoader = function () {
956 if (ScriptResourceLoader.loading) {
957 window.setTimeout(waitLoader, 100);
958 return;
959 }
960 ScriptResourceLoader.loading = true;
961 var script = document.createElement("script");
962 var heads = document.getElementsByTagName("head");
963 if (heads.length == 0)
964 throw "can not find head tag";
965 script.src = url + "?" + (new Date()).getTime();
966 script.onload = function () {
967 callback.call(_this, identifier, script, true);
968 ScriptResourceLoader.loading = false;
969 };
970 script.onerror = function () {
971 callback.call(_this, identifier, script, false);
972 ScriptResourceLoader.loading = false;
973 };
974 heads[0].appendChild(script);
975 };
976 waitLoader();
977 };
978 ScriptResourceLoader.prototype.completed = function (name, script, is_success) {
979 if (!is_success)
980 console.log("error: " + name);
981 else
982 this.resource.scripts[name] = script;
983 this.resource.requestCompleted(name);
984 };
985 return ScriptResourceLoader;
986 })(ResourceLoader);
987 jg.ScriptResourceLoader = ScriptResourceLoader;
988 var SoundResourceLoader = (function (_super) {
989 __extends(SoundResourceLoader, _super);
990 function SoundResourceLoader() {
991 _super.apply(this, arguments);
992 }
993 SoundResourceLoader.prototype.load = function (url, identifier) {
994 var _this = this;
995 var audio = new Audio(null);
996 audio.autoplay = false;
997 audio.preload = "auto";
998 audio.src = this.resource.structure.soundUrl(url);
999 audio.load();
1000 audio.addEventListener("canplaythrough", function () {
1001 _this.completed(identifier, audio, true);
1002 }, true);
1003 audio.addEventListener("error", function () {
1004 _this.completed(identifier, audio, false);
1005 }, true);
1006 };
1007 SoundResourceLoader.prototype.completed = function (name, audio, is_success) {
1008 if (!is_success)
1009 console.log("error: " + name);
1010 else
1011 this.resource.sounds[name] = audio;
1012 this.resource.requestCompleted(name);
1013 };
1014 return SoundResourceLoader;
1015 })(ResourceLoader);
1016 jg.SoundResourceLoader = SoundResourceLoader;
1017 })(jg || (jg = {}));
1018 ///<reference path="all.ts"/>
1019 var jg;
1020 (function (jg) {
1021 var Resource = (function () {
1022 function Resource() {
1023 this.requests = [];
1024 this.loaded = new jg.Trigger();
1025 this.added = new jg.Trigger();
1026 this.clear();
1027 this.loaders = {};
1028 this.loaders["js"] = new jg.ScriptResourceLoader(this);
1029 this.loaders["default"] = new jg.ImageResourceLoader(this);
1030 this.loaders["mp3"] = new jg.SoundResourceLoader(this);
1031 this.loaders["ogg"] = this.loaders["mp3"];
1032 this.loaders["wav"] = this.loaders["mp3"];
1033 this.loaders["mid"] = this.loaders["mp3"];
1034 this.structure = jg.ResourceStructure.Default;
1035 }
1036 Resource.getInstance = function () {
1037 if (!Resource.instance)
1038 Resource.instance = new Resource();
1039 return Resource.instance;
1040 };
1041 Resource.prototype.clear = function () {
1042 this.images = {};
1043 this.scripts = {};
1044 this.sounds = {};
1045 };
1046 Resource.prototype.get = function (name) {
1047 return this.images[name];
1048 };
1049 Resource.prototype.sound = function (name) {
1050 return this.sounds[name];
1051 };
1052 Resource.prototype.requestCompleted = function (name) {
1053 for (var i = 0; i < this.requests.length; i++) {
1054 if (this.requests[i] == name) {
1055 this.requests.splice(i, 1);
1056 break;
1057 }
1058 }
1059 this.loaded.fire(this.requests.length);
1060 };
1061 Resource.prototype.load = function (name, url) {
1062 if (!url)
1063 url = name;
1064 this.requests.push(name);
1065 var dot = url.split(/\./g);
1066 var ext;
1067 if (dot.length == 0)
1068 ext = "";
1069 else
1070 ext = dot[dot.length - 1];
1071 ext = ext.toLowerCase();
1072 var loader = this.loaders[ext] ? this.loaders[ext] : this.loaders["default"];
1073 loader.load(url, name);
1074 this.added.fire({
1075 name: name,
1076 url: url,
1077 loader: loader
1078 });
1079 };
1080 Resource.prototype.loadManual = function (name) {
1081 this.requests.push(name);
1082 this.added.fire({
1083 name: name,
1084 url: null,
1085 loader: null
1086 });
1087 };
1088 Resource.prototype.completeManual = function (name) {
1089 this.requestCompleted(name);
1090 };
1091 return Resource;
1092 })();
1093 jg.Resource = Resource;
1094 })(jg || (jg = {}));
1095 ///<reference path="all.ts"/>
1096 var jg;
1097 (function (jg) {
1098 var Scene = (function () {
1099 function Scene(game) {
1100 this.game = game;
1101 this.layers = {};
1102 this.root = new jg.Layer(this);
1103 this.layers["root"] = this.root;
1104 this.layerCount = 1;
1105 this.mode = [];
1106 this.started = new jg.Trigger();
1107 this.ended = new jg.Trigger();
1108 this.showed = new jg.Trigger();
1109 this.hid = new jg.Trigger();
1110 }
1111 Scene.prototype.currentMode = function () {
1112 return this.mode.length == 0 ? null : this.mode[this.mode.length - 1];
1113 };
1114 Scene.prototype.getLayerArray = function () {
1115 var ret = [];
1116 for (var i in this.layers)
1117 ret.push(this.layers[i]);
1118 return ret;
1119 };
1120 Scene.prototype.enablePointingEvent = function () {
1121 this.root.enablePointingEvent();
1122 if (!this.pointDown)
1123 this.pointDown = new jg.Trigger();
1124 if (!this.pointMove)
1125 this.pointMove = new jg.Trigger();
1126 if (!this.pointUp)
1127 this.pointUp = new jg.Trigger();
1128 };
1129 Scene.prototype.disablePointingEvent = function () {
1130 this.root.disablePointingEvent();
1131 };
1132 Scene.prototype.changeMode = function (mode) {
1133 var linkMode = this.currentMode();
1134 if (linkMode && this[linkMode + "Hide"])
1135 this[linkMode + "Hide"]();
1136 this.mode.push(mode);
1137 if (mode) {
1138 if (this[mode + "Start"])
1139 this[mode + "Start"]();
1140 if (this[mode + "Show"])
1141 this[mode + "Show"]();
1142 }
1143 };
1144 Scene.prototype.endCurrentMode = function (newMode) {
1145 if (this.mode.length == 0) {
1146 this.end();
1147 return;
1148 }
1149 var mode = this.mode.pop();
1150 if (mode)
1151 if (this[mode + "End"])
1152 this[mode + "End"]();
1153 var linkMode = this.currentMode();
1154 if (newMode !== undefined)
1155 this.changeMode(newMode);
1156 else if (linkMode && this[linkMode + "Show"])
1157 this[linkMode + "Show"]();
1158 };
1159 Scene.prototype.createLayer = function (name, size) {
1160 for (var i in this.layers)
1161 if (!this.layers[i].hasBuffer())
1162 this.layers[i].createBuffer();
1163 if (size && size instanceof jg.Layer) {
1164 this.layers[name] = size;
1165 this.layers[name].scene = this;
1166 }
1167 else {
1168 this.layers[name] = new jg.Layer(this);
1169 if (size) {
1170 this.layers[name].width = size.width;
1171 this.layers[name].height = size.height;
1172 }
1173 }
1174 this.layers[name].createBuffer();
1175 this.layerCount++;
1176 return this.layers[name];
1177 };
1178 Scene.prototype.deleteLayer = function (name) {
1179 if (name == "root")
1180 throw "can not delete root layer";
1181 this.layers[name].destroy();
1182 delete this.layers[name];
1183 this.layerCount--;
1184 if (this.layerCount == 1)
1185 this.root.deleteBuffer();
1186 };
1187 Scene.prototype.destroy = function () {
1188 for (var i in this.layers)
1189 this.layers[i].destroy();
1190 if (this.keyDown)
1191 this.keyDown.destroy();
1192 if (this.keyUp)
1193 this.keyUp.destroy();
1194 if (this.pointDown)
1195 this.pointDown.destroy();
1196 if (this.pointUp)
1197 this.pointUp.destroy();
1198 if (this.pointMove)
1199 this.pointMove.destroy();
1200 };
1201 Scene.prototype.end = function () {
1202 this.game.endScene();
1203 };
1204 Scene.prototype.refresh = function () {
1205 for (var i in this.layers) {
1206 this.layers[i].refresh();
1207 this.layers[i].updated();
1208 }
1209 };
1210 Scene.prototype.scrollTo = function (x, y, layerName) {
1211 if (!layerName)
1212 layerName = "root";
1213 this.layers[layerName].scrollTo(x, y);
1214 };
1215 Scene.prototype.scrollBy = function (x, y, layerName) {
1216 if (x == 0 && y == 0)
1217 return;
1218 if (!layerName)
1219 layerName = "root";
1220 this.layers[layerName].scrollBy(x, y);
1221 };
1222 Scene.prototype.append = function (entity, layerName) {
1223 if (!layerName)
1224 this.root.append(entity);
1225 else
1226 this.layers[layerName].append(entity);
1227 };
1228 Scene.prototype.removeEntity = function (entity) {
1229 for (var i in this.layers)
1230 this.layers[i].removeChild(entity);
1231 };
1232 return Scene;
1233 })();
1234 jg.Scene = Scene;
1235 })(jg || (jg = {}));
1236 ///<reference path="all.ts"/>
1237 var jg;
1238 (function (jg) {
1239 var Sprite = (function (_super) {
1240 __extends(Sprite, _super);
1241 function Sprite(image, srcWidth, srcHeight) {
1242 _super.call(this);
1243 this.x = 0;
1244 this.y = 0;
1245 this.width = srcWidth ? srcWidth : image.width;
1246 this.height = srcHeight ? srcHeight : image.height;
1247 this.srcWidth = this.width;
1248 this.srcHeight = this.height;
1249 this.srcX = 0;
1250 this.srcY = 0;
1251 this.image = image;
1252 }
1253 Sprite.prototype.draw = function (context) {
1254 context.drawImage(this.image, this.srcX, this.srcY, this.srcWidth, this.srcHeight, 0, 0, this.width, this.height);
1255 };
1256 return Sprite;
1257 })(jg.E);
1258 jg.Sprite = Sprite;
1259 })(jg || (jg = {}));
1260 ///<reference path="all.ts"/>
1261 var jg;
1262 (function (jg) {
1263 var FrameSprite = (function (_super) {
1264 __extends(FrameSprite, _super);
1265 function FrameSprite(image, srcWidth, srcHeight, wait) {
1266 _super.call(this, image, srcWidth, srcHeight);
1267 this.sep = Math.floor(this.image.width / this.width);
1268 var frameCnt = Math.ceil(image.width * image.height) / (srcWidth * srcHeight);
1269 this.frame = [];
1270 for (var i = 0; i < frameCnt; i++)
1271 this.frame.push(i);
1272 this.fno = 0;
1273 this.animation = false;
1274 this.wait = wait === undefined ? 200 : wait;
1275 this.loop = true;
1276 }
1277 FrameSprite.prototype.changeFrame = function () {
1278 this.srcX = (this.frame[this.fno] % this.sep) * this.srcWidth;
1279 this.srcY = Math.floor(this.frame[this.fno] / this.sep) * this.srcHeight;
1280 this.updated();
1281 };
1282 FrameSprite.prototype.animate = function (noClear) {
1283 if (this.animation)
1284 return true;
1285 if (!noClear)
1286 this.fno = 0;
1287 this.animation = true;
1288 this.startTimer(this.wait);
1289 };
1290 FrameSprite.prototype.inanimate = function () {
1291 if (!this.animation)
1292 return;
1293 this.stopTimer(this.wait);
1294 this.animation = false;
1295 };
1296 FrameSprite.prototype.interval = function () {
1297 this.fno++;
1298 if (this.fno >= this.frame.length) {
1299 if (this.loop) {
1300 this.fno = 0;
1301 }
1302 else {
1303 if (this.frameEnded) {
1304 this.inanimate();
1305 this.frameEnded.fire();
1306 return;
1307 }
1308 }
1309 }
1310 this.changeFrame();
1311 };
1312 return FrameSprite;
1313 })(jg.Sprite);
1314 jg.FrameSprite = FrameSprite;
1315 })(jg || (jg = {}));
1316 ///<reference path="all.ts"/>
1317 var jg;
1318 (function (jg) {
1319 var Character = (function (_super) {
1320 __extends(Character, _super);
1321 function Character(image, width, height, wait) {
1322 _super.call(this, image, width, height, wait);
1323 this.moving = false;
1324 if (!wait)
1325 wait = 200;
1326 this.animeCnt = 2;
1327 this.charaSeq = 0;
1328 this.charaCol = 1;
1329 this.movePixel = 64;
1330 this.moveTime = 300;
1331 this.animate();
1332 }
1333 Character.prototype.moveLeft = function (stackNext) {
1334 if (this.move(-this.movePixel, 0, this.moveTime)) {
1335 this.angle(jg.Angle.Left);
1336 return true;
1337 }
1338 if (stackNext && this.moveInfo.t * 2 >= this.moveInfo.f)
1339 this.nextMove = "Left";
1340 return false;
1341 };
1342 Character.prototype.moveRight = function (stackNext) {
1343 if (this.move(this.movePixel, 0, this.moveTime)) {
1344 this.angle(jg.Angle.Right);
1345 return true;
1346 }
1347 if (stackNext && this.moveInfo.t * 2 >= this.moveInfo.f)
1348 this.nextMove = "Right";
1349 return false;
1350 };
1351 Character.prototype.moveUp = function (stackNext) {
1352 if (this.move(0, -this.movePixel, this.moveTime)) {
1353 this.angle(jg.Angle.Up);
1354 return true;
1355 }
1356 if (stackNext && this.moveInfo.t * 2 >= this.moveInfo.f)
1357 this.nextMove = "Up";
1358 return false;
1359 };
1360 Character.prototype.moveDown = function (stackNext) {
1361 if (this.move(0, this.movePixel, this.moveTime)) {
1362 this.angle(jg.Angle.Down);
1363 return true;
1364 }
1365 if (stackNext && this.moveInfo.t * 2 >= this.moveInfo.f)
1366 this.nextMove = "Down";
1367 return false;
1368 };
1369 Character.prototype.move = function (x, y, f) {
1370 if (this.moving)
1371 return false;
1372 this.moving = true;
1373 this.moveInfo = {
1374 x: this.x,
1375 y: this.y,
1376 dx: this.x + x,
1377 dy: this.y + y,
1378 f: f,
1379 t: 0
1380 };
1381 if (this.beginMove)
1382 this.beginMove.fire(this.moveInfo);
1383 if (this.moving)
1384 this.start();
1385 return true;
1386 };
1387 Character.prototype.update = function (t) {
1388 if (this.moving) {
1389 this.moveInfo.t += t;
1390 if (this.moveInfo.t < this.moveInfo.f) {
1391 this.moveTo(this.moveInfo.x + Math.round((this.moveInfo.dx - this.moveInfo.x) / this.moveInfo.f * this.moveInfo.t), this.moveInfo.y + Math.round((this.moveInfo.dy - this.moveInfo.y) / this.moveInfo.f * this.moveInfo.t));
1392 }
1393 else {
1394 this.moveTo(this.moveInfo.dx, this.moveInfo.dy);
1395 this.endMove();
1396 }
1397 }
1398 };
1399 Character.prototype.endMove = function () {
1400 this.moving = false;
1401 this.stop();
1402 var e = {};
1403 if (this.nextMove) {
1404 e.nextMove = this.nextMove;
1405 delete this.nextMove;
1406 }
1407 if (this.moved)
1408 this.moved.fire(e);
1409 if (e.nextMove)
1410 this["move" + e.nextMove]();
1411 };
1412 Character.prototype.angle = function (angle) {
1413 this.currentAngle = angle;
1414 var rowP = Math.floor(this.charaSeq / this.charaCol) * 4;
1415 switch (angle) {
1416 case jg.Angle.Up:
1417 rowP += (this.angleSeq ? this.angleSeq[jg.Angle.Up] : 3);
1418 break;
1419 case jg.Angle.Down:
1420 rowP += (this.angleSeq ? this.angleSeq[jg.Angle.Down] : 0);
1421 break;
1422 case jg.Angle.Left:
1423 rowP += (this.angleSeq ? this.angleSeq[jg.Angle.Left] : 1);
1424 break;
1425 case jg.Angle.Right:
1426 rowP += (this.angleSeq ? this.angleSeq[jg.Angle.Right] : 2);
1427 break;
1428 }
1429 var f = this.animeCnt * (this.charaSeq % this.charaCol) + this.charaCol * this.animeCnt * rowP;
1430 this.frame = [];
1431 if (this.animeCnt % 2 == 1) {
1432 for (var i = 0; i < this.animeCnt; i++)
1433 this.frame.push(i + f);
1434 for (var i = this.animeCnt - 2; i > 0; i--)
1435 this.frame.push(i + f);
1436 }
1437 else {
1438 for (var i = 0; i < this.animeCnt; i++)
1439 this.frame.push(i + f);
1440 }
1441 this.changeFrame();
1442 };
1443 return Character;
1444 })(jg.FrameSprite);
1445 jg.Character = Character;
1446 })(jg || (jg = {}));
1447 ///<reference path="all.ts"/>
1448 var jg;
1449 (function (jg) {
1450 var CharacterFactory = (function () {
1451 function CharacterFactory(image, width, height) {
1452 this.width = width;
1453 this.height = height;
1454 this.image = image;
1455 this.wait = 200;
1456 this.charaCol = 1;
1457 this.animeCnt = 2;
1458 this.moveTime = 300;
1459 this.movePixel = 64;
1460 this.angle = jg.Angle.Down;
1461 this.createClass = jg.Character;
1462 }
1463 CharacterFactory.prototype.create = function (charaSeq, offset, angle) {
1464 var c = new this.createClass(this.image, this.width, this.height, this.wait);
1465 var black = ["width", "height", "image", "wait", "angle"];
1466 for (var i in this) {
1467 if (typeof this[i] == "function")
1468 continue;
1469 if (black.indexOf(i) >= 0)
1470 continue;
1471 c[i] = this[i];
1472 }
1473 c.charaSeq = charaSeq;
1474 c.angle(angle == undefined ? this.angle : angle);
1475 if (offset)
1476 c.moveTo(offset.x, offset.y);
1477 return c;
1478 };
1479 return CharacterFactory;
1480 })();
1481 jg.CharacterFactory = CharacterFactory;
1482 })(jg || (jg = {}));
1483 ///<reference path="all.ts"/>
1484 var jg;
1485 (function (jg) {
1486 var Label = (function (_super) {
1487 __extends(Label, _super);
1488 function Label(text, fontSize, fontColor, baseline) {
1489 _super.call(this);
1490 this.x = 0;
1491 this.y = 0;
1492 if (text) {
1493 this.setText(text);
1494 }
1495 else {
1496 this.setText("");
1497 this.width = 0;
1498 this.height = 0;
1499 }
1500 this.setTextBaseline(baseline ? baseline : "top");
1501 this.setFontSize(fontSize ? fontSize : 14);
1502 this.setColor(fontColor ? fontColor : "black");
1503 }
1504 Label.prototype.setMaxWidth = function (maxWidth) {
1505 this.maxWidth = maxWidth;
1506 this.updateSize();
1507 };
1508 Label.prototype.updateSize = function () {
1509 var canvas = window.createCanvas(10, 10);
1510 var ctx = canvas.getContext("2d");
1511 ctx.font = this.getFont();
1512 var metrix = ctx.measureText(this.text);
1513 this.width = metrix.width;
1514 this.height = this.getFontSize();
1515 };
1516 Label.prototype.addShadow = function (color) {
1517 this.setDrawOption("shadowBlur", 1);
1518 this.setDrawOption("shadowColor", color ? color : "rgba(0,0,0,0.8)");
1519 this.setDrawOption("shadowOffsetX", 1);
1520 this.setDrawOption("shadowOffsetY", 1);
1521 };
1522 Label.prototype.removeShadow = function () {
1523 this.removeDrawOption("shadowBlur");
1524 this.removeDrawOption("shadowColor");
1525 };
1526 Label.prototype.setText = function (text) {
1527 this.text = text;
1528 this.updateSize();
1529 this.updated();
1530 };
1531 Label.prototype.setFont = function (fontString) {
1532 this.setDrawOption("font", fontString);
1533 this.updateSize();
1534 };
1535 Label.prototype.getFont = function () {
1536 return this.getDrawOption("font");
1537 };
1538 Label.prototype.setFontSize = function (size) {
1539 var dom = document.createElement("div");
1540 dom.style.font = this.getFont();
1541 dom.style.fontSize = size + "px";
1542 this.setFont(dom.style.font);
1543 };
1544 Label.prototype.getFontSize = function () {
1545 var dom = document.createElement("div");
1546 dom.style.font = this.getFont();
1547 return Number(dom.style.fontSize.substr(0, dom.style.fontSize.length - 2));
1548 };
1549 Label.prototype.getFontFamily = function () {
1550 var dom = document.createElement("div");
1551 dom.style.font = this.getFont();
1552 return dom.style.fontFamily;
1553 };
1554 Label.prototype.setFontFamily = function (family) {
1555 var dom = document.createElement("div");
1556 dom.style.font = this.getFont();
1557 dom.style.fontFamily = family;
1558 this.setFont(dom.style.font);
1559 };
1560 Label.prototype.setTextAlign = function (align) {
1561 this.setDrawOption("textAlign", align);
1562 };
1563 Label.prototype.getTextAlign = function () {
1564 return this.getDrawOption("textAlign");
1565 };
1566 Label.prototype.setTextBaseline = function (baseline) {
1567 this.setDrawOption("textBaseline", baseline);
1568 };
1569 Label.prototype.getTextBaseline = function () {
1570 return this.getDrawOption("textBaseline");
1571 };
1572 Label.prototype.setColor = function (color) {
1573 this.setDrawOption("fillStyle", color);
1574 };
1575 Label.prototype.getColor = function () {
1576 return this.getDrawOption("fillStyle");
1577 };
1578 Label.prototype.synchronize = function (obj, prop, round) {
1579 this.syncObj = obj;
1580 this.syncProp = prop;
1581 this.syncRound = round;
1582 };
1583 Label.prototype.draw = function (context) {
1584 if (this.syncObj) {
1585 var val;
1586 if (typeof this.syncObj[this.syncProp] == "function")
1587 val = this.syncObj[this.syncProp](this);
1588 else
1589 val = this.syncObj[this.syncProp];
1590 this.text = this.syncRound ? Math.round(val) : val;
1591 }
1592 if (this.maxWidth) {
1593 context.fillText(this.text, 0, 0, this.maxWidth);
1594 }
1595 else {
1596 context.fillText(this.text, 0, 0);
1597 }
1598 };
1599 return Label;
1600 })(jg.E);
1601 jg.Label = Label;
1602 })(jg || (jg = {}));
1603 ///<reference path="all.ts"/>
1604 var jg;
1605 (function (jg) {
1606 var TextLineInfo = (function () {
1607 function TextLineInfo(offsetY) {
1608 this.width = 0;
1609 this.height = 0;
1610 this.offsetY = offsetY;
1611 }
1612 return TextLineInfo;
1613 })();
1614 jg.TextLineInfo = TextLineInfo;
1615 var MultilineScriptAnalyzer = (function () {
1616 function MultilineScriptAnalyzer() {
1617 }
1618 MultilineScriptAnalyzer.prototype.init = function (owner, context, pos) {
1619 this.mode = 0;
1620 this.owner = owner;
1621 this.context = context;
1622 this.pos = pos;
1623 };
1624 MultilineScriptAnalyzer.prototype.next = function (c) {
1625 if (this.mode) {
1626 if (c == " ") {
1627 this.mode = 0;
1628 if (this.buf == "page")
1629 return -1;
1630 }
1631 else
1632 this.buf += c;
1633 return 1;
1634 }
1635 if (c == "#") {
1636 this.mode = 1;
1637 this.buf = "";
1638 return 1;
1639 }
1640 return 0;
1641 };
1642 return MultilineScriptAnalyzer;
1643 })();
1644 jg.MultilineScriptAnalyzer = MultilineScriptAnalyzer;
1645 var MultilineText = (function (_super) {
1646 __extends(MultilineText, _super);
1647 function MultilineText(size, offset) {
1648 _super.call(this);
1649 this.scriptAnalyzer = new MultilineScriptAnalyzer();
1650 this.width = size.width;
1651 this.height = size.height;
1652 if (offset)
1653 this.moveTo(offset.x, offset.y);
1654 else
1655 this.moveTo(0, 0);
1656 this.defaultStyle = "#fff";
1657 this.defaultFont = "18px sans-serif";
1658 this.defaultBlur = 1;
1659 this.defaultShadowColor = "rgba(0,0,0,0.8)";
1660 this.defaultShadowOffsetX = 1;
1661 this.defaultShadowOffsetY = 1;
1662 this.clip = new jg.Line({ x: 0, y: 0 });
1663 this.clip.addLine(this.width, 0);
1664 this.clip.addLine(this.width, this.height);
1665 this.clip.addLine(0, this.height);
1666 this.clip.addLine(0, this.height);
1667 this.clip.addLine(0, this.height);
1668 this.clip.closePath = true;
1669 this.clip.setClip(true);
1670 this.entities = [];
1671 this.entities.push(this.clip);
1672 this.animeSpeed = 400;
1673 this.animated = new jg.Trigger();
1674 }
1675 MultilineText.prototype.setText = function (text, offset) {
1676 var plainScript = text;
1677 return this.setScript(plainScript, offset);
1678 };
1679 MultilineText.prototype.setScript = function (script, offset) {
1680 this.script = script.replace(/\r\n?/g, "\n");
1681 this.updated();
1682 return this.createBuffer(offset);
1683 };
1684 MultilineText.prototype.getLineHeight = function (c) {
1685 var font = c.font;
1686 var firstPos = font.indexOf("px");
1687 var lastPos = font.lastIndexOf(" ", firstPos);
1688 if (lastPos < 0)
1689 lastPos = 0;
1690 if (firstPos < 0)
1691 return 16;
1692 var fontSize = parseInt(font.substring(lastPos, firstPos));
1693 var line_height = Math.round(fontSize * MultilineText.LINE_HEIGHT_NORMAL);
1694 return line_height;
1695 };
1696 MultilineText.prototype.createBuffer = function (offset) {
1697 var _this = this;
1698 if (!this.buffer)
1699 this.buffer = window.createCanvas(this.width, this.height);
1700 if (offset === undefined)
1701 offset = 0;
1702 var script = this.script;
1703 var len = script.length;
1704 var pos = { x: 0, y: 0 };
1705 var c = this.buffer.getContext("2d");
1706 var s;
1707 var m = MultilineText.BROWSER_BASELINE_MARGIN;
1708 this.lines = [];
1709 if (this.bufferBg)
1710 c.putImageData(this.bufferBg, 0, 0);
1711 else
1712 c.clearRect(0, 0, this.width, this.height);
1713 c.fillStyle = this.defaultStyle;
1714 c.font = this.defaultFont;
1715 c.textBaseline = "top";
1716 if (!this.disableShadow) {
1717 c.shadowBlur = this.defaultBlur;
1718 c.shadowColor = this.defaultShadowColor;
1719 c.shadowOffsetX = this.defaultShadowOffsetX;
1720 c.shadowOffsetY = this.defaultShadowOffsetY;
1721 }
1722 var lineHeight = this.getLineHeight(c);
1723 var lineInfo = new TextLineInfo(0);
1724 lineInfo.height = lineHeight;
1725 this.lines.push(lineInfo);
1726 var _newLine = function () {
1727 pos.x = 0;
1728 pos.y += lineInfo.height;
1729 if ((pos.y + lineInfo.height) > _this.height)
1730 return false;
1731 lineInfo = new TextLineInfo(pos.y);
1732 lineInfo.height = lineHeight;
1733 _this.lines.push(lineInfo);
1734 return true;
1735 };
1736 this.scriptAnalyzer.init(this, c, pos);
1737 while (offset < len) {
1738 s = script.substr(offset, 1);
1739 var script_ret = this.scriptAnalyzer.next(s);
1740 if (script_ret) {
1741 lineHeight = lineInfo.height;
1742 if (script_ret < 0) {
1743 offset -= script_ret;
1744 break;
1745 }
1746 offset += script_ret;
1747 continue;
1748 }
1749 if (s == "\n") {
1750 offset++;
1751 if (!_newLine())
1752 break;
1753 continue;
1754 }
1755 var metric = c.measureText(s);
1756 if ((pos.x + metric.width) > this.width) {
1757 if (!_newLine())
1758 break;
1759 }
1760 c.fillText(s, pos.x, pos.y + m);
1761 pos.x += metric.width;
1762 lineInfo.width += metric.width;
1763 offset++;
1764 }
1765 this.sprite = new jg.Sprite(this.buffer);
1766 this.sprite.moveTo(0, 0);
1767 if (this.entities.length == 1)
1768 this.entities.push(this.sprite);
1769 else
1770 this.entities[1] = this.sprite;
1771 return offset == len ? -1 : offset;
1772 };
1773 MultilineText.prototype.refresh = function () {
1774 delete this.buffer;
1775 this.createBuffer();
1776 };
1777 MultilineText.prototype.startAnimation = function (animeSpeed) {
1778 this.start();
1779 this.animeLine = 0;
1780 this.animePos = { x: 0, y: this.lines[this.animeLine].height };
1781 if (animeSpeed !== undefined)
1782 this.animeSpeed = animeSpeed;
1783 this.hideAll();
1784 this.clip.p[4].y = this.animePos.y;
1785 this.clip.p[5].y = this.animePos.y;
1786 };
1787 MultilineText.prototype.update = function (t) {
1788 this.animePos.x += this.animeSpeed / 1000 * t;
1789 if (this.animePos.x >= this.lines[this.animeLine].width) {
1790 this.animePos.x = 0;
1791 this.animePos.y += this.lines[this.animeLine].height;
1792 this.animeLine++;
1793 if (this.animeLine < this.lines.length) {
1794 this.clip.p[2].y = this.lines[this.animeLine].offsetY;
1795 this.clip.p[3].y = this.clip.p[2].y;
1796 this.clip.p[4].y = this.animePos.y;
1797 this.clip.p[5].y = this.animePos.y;
1798 }
1799 }
1800 if (this.animeLine >= this.lines.length) {
1801 this.showAll();
1802 }
1803 else {
1804 this.clip.p[3].x = this.animePos.x;
1805 this.clip.p[4].x = this.clip.p[3].x;
1806 }
1807 this.updated();
1808 };
1809 MultilineText.prototype.hideAll = function () {
1810 this.clip.p[0] = { x: 0, y: 0 };
1811 this.clip.p[1] = { x: this.width, y: 0 };
1812 this.clip.p[2] = { x: this.width, y: 0 };
1813 this.clip.p[3] = { x: 0, y: 0 };
1814 this.clip.p[4] = { x: 0, y: 0 };
1815 this.clip.p[5] = { x: 0, y: 0 };
1816 };
1817 MultilineText.prototype.showAll = function () {
1818 this.clip.p[0] = { x: 0, y: 0 };
1819 this.clip.p[1] = { x: this.width, y: 0 };
1820 this.clip.p[2] = { x: this.width, y: this.height };
1821 this.clip.p[3] = { x: 0, y: this.height };
1822 this.clip.p[4] = { x: 0, y: this.height };
1823 this.clip.p[5] = { x: 0, y: this.height };
1824 this.stop();
1825 this.animated.fire();
1826 };
1827 MultilineText.LINE_HEIGHT_NORMAL = 1.2;
1828 MultilineText.BROWSER_BASELINE_MARGIN = 0;
1829 return MultilineText;
1830 })(jg.E);
1831 jg.MultilineText = MultilineText;
1832 })(jg || (jg = {}));
1833 ///<reference path="all.ts"/>
1834 var jg;
1835 (function (jg) {
1836 var ChipSet = (function () {
1837 function ChipSet(image) {
1838 this.image = image;
1839 }
1840 ChipSet.prototype.count = function (tile) {
1841 return Math.round((this.image.width * this.image.height) / (tile.tileWidth * tile.tileHeight));
1842 };
1843 ChipSet.prototype.draw = function (tile, c, x, y, chip) {
1844 var tw = tile.tileWidth;
1845 var th = tile.tileHeight;
1846 var sep = Math.floor(this.image.width / tile.tileWidth);
1847 c.drawImage(this.image, (chip % sep) * tw, Math.floor(chip / sep) * th, tw, th, x * tw, y * th, tw, th);
1848 };
1849 ChipSet.prototype.getChips = function (tile) {
1850 var len = this.count(tile);
1851 var sprite = new jg.Sprite(this.image);
1852 var buf = new jg.BufferedRenderer(sprite);
1853 var ret = [];
1854 var w = tile.tileWidth;
1855 var h = tile.tileHeight;
1856 var area = {
1857 x: 0,
1858 y: 0,
1859 width: w,
1860 height: h
1861 };
1862 buf.renderUnit(sprite);
1863 var sep = Math.floor(this.image.width / tile.tileWidth);
1864 for (var i = 0; i < len; i++) {
1865 ret.push(buf.createSprite({
1866 x: (i % sep) * w,
1867 y: Math.floor(i / sep) * h,
1868 width: w,
1869 height: h
1870 }, area, area));
1871 }
1872 return ret;
1873 };
1874 return ChipSet;
1875 })();
1876 jg.ChipSet = ChipSet;
1877 var AutoTileChipSet = (function (_super) {
1878 __extends(AutoTileChipSet, _super);
1879 function AutoTileChipSet() {
1880 _super.apply(this, arguments);
1881 }
1882 AutoTileChipSet.prototype.map = function (tile, x, y, out_value) {
1883 if (x < 0 || y < 0 || x >= tile.size.width || y >= tile.size.height)
1884 return out_value !== undefined ? out_value : -1;
1885 return tile.data[x][y];
1886 };
1887 AutoTileChipSet.prototype.count = function (tile) {
1888 return 1;
1889 };
1890 AutoTileChipSet.prototype.draw = function (tile, c, x, y, chip) {
1891 var tw = tile.tileWidth;
1892 var th = tile.tileHeight;
1893 var tw2 = Math.floor(tw / 2);
1894 var th2 = Math.floor(th / 2);
1895 var sep = Math.floor(this.image.width / tile.tileWidth);
1896 chip += this.chipOffset;
1897 for (var i = 0; i < 2; i++) {
1898 for (var j = 0; j < 2; j++) {
1899 var tx = x + (i == 0 ? -1 : 1);
1900 var ty = y + (j == 0 ? -1 : 1);
1901 var v = this.map(tile, tx, y, chip);
1902 var h = this.map(tile, x, ty, chip);
1903 var vh = this.map(tile, tx, ty, chip);
1904 var sel = 0;
1905 if (h == chip)
1906 sel++;
1907 if (v == chip)
1908 sel += 2;
1909 if (sel == 3 && vh == chip)
1910 sel++;
1911 c.drawImage(this.image, (sel % sep) * tw + tw2 * i, Math.floor(sel / sep) * th + th2 * j, tw2, th2, x * tw + tw2 * i, y * th + th2 * j, tw2, th2);
1912 }
1913 }
1914 };
1915 AutoTileChipSet.prototype.getChips = function (tile) {
1916 var len = this.count(tile);
1917 var sprite = new jg.Sprite(this.image, tile.tileWidth, tile.tileHeight);
1918 return [sprite.createSprite()];
1919 };
1920 return AutoTileChipSet;
1921 })(ChipSet);
1922 jg.AutoTileChipSet = AutoTileChipSet;
1923 var Tile = (function (_super) {
1924 __extends(Tile, _super);
1925 function Tile(image, tileWidth, tileHeight) {
1926 _super.call(this);
1927 this.tileWidth = tileWidth;
1928 this.tileHeight = tileHeight;
1929 this.chips = [];
1930 this.chipMap = [];
1931 this.chipCount = 0;
1932 if (image)
1933 this.addChipSet(image);
1934 this.x = 0;
1935 this.y = 0;
1936 this.disableTransform = true;
1937 }
1938 Tile.prototype.addChipSet = function (image, opt) {
1939 var chipset;
1940 if (opt) {
1941 if (opt.autoTile) {
1942 chipset = new AutoTileChipSet(image);
1943 }
1944 }
1945 if (!chipset)
1946 chipset = new ChipSet(image);
1947 chipset.chipOffset = this.chipCount;
1948 this.chips.push(chipset);
1949 var cnt = chipset.count(this);
1950 var cnt2 = this.chipCount + cnt;
1951 for (var i = this.chipCount; i < cnt2; i++)
1952 this.chipMap[i] = chipset;
1953 this.chipCount = cnt2;
1954 };
1955 Tile.prototype.copyChips = function (tile) {
1956 tile.chips = this.chips;
1957 tile.chipCount = this.chipCount;
1958 tile.chipMap = this.chipMap;
1959 };
1960 Tile.prototype._clear = function (width, height) {
1961 this.size = {
1962 width: width,
1963 height: height
1964 };
1965 this.width = this.tileWidth * width;
1966 this.height = this.tileHeight * height;
1967 };
1968 Tile.prototype.clear = function (width, height, value) {
1969 if (!width)
1970 width = this.size.width;
1971 if (!height)
1972 height = this.size.height;
1973 if (value === undefined)
1974 value = -1;
1975 this._clear(width, height);
1976 this.data = [];
1977 for (var x = 0; x < width; x++) {
1978 this.data[x] = [];
1979 for (var y = 0; y < height; y++)
1980 this.data[x][y] = value;
1981 }
1982 this.refresh();
1983 };
1984 Tile.prototype.generate = function (data, width, height, transpose) {
1985 if (width === true) {
1986 transpose = true;
1987 width = data.length;
1988 height = data[0].length;
1989 }
1990 if (!width)
1991 width = data.length;
1992 if (!height)
1993 height = data[0].length;
1994 this.data = transpose ? jg.JGUtil.transpose(data) : data;
1995 this._clear(width, height);
1996 this.refresh();
1997 };
1998 Tile.prototype.refresh = function () {
1999 this.canvas = window.createCanvas(this.width, this.height);
2000 var c = this.canvas.getContext("2d");
2001 var chipset;
2002 var w = this.size.width;
2003 var h = this.size.height;
2004 var d = this.data;
2005 var cm = this.chipMap;
2006 for (var x = 0; x < w; x++)
2007 for (var y = 0; y < h; y++)
2008 this.drawChip(x, y, false, c);
2009 this.updated();
2010 };
2011 Tile.prototype.drawChip = function (x, y, clear, context) {
2012 if (context === undefined)
2013 context = this.canvas.getContext("2d");
2014 if (clear)
2015 context.clearRect(x * this.tileWidth, y * this.tileHeight, this.tileWidth, this.tileHeight);
2016 if (this.data[x][y] < 0)
2017 return;
2018 var cs = this.chipMap[this.data[x][y]];
2019 cs.draw(this, context, x, y, this.data[x][y] - cs.chipOffset);
2020 };
2021 Tile.prototype.draw = function (context) {
2022 if (!this.canvas)
2023 return;
2024 var parent = this.parent ? this.parent : this;
2025 var scroll = parent.scroll ? parent.scroll : { x: 0, y: 0 };
2026 var src = {
2027 x: -scroll.x,
2028 y: -scroll.y,
2029 width: parent.width,
2030 height: parent.height
2031 };
2032 var dist = {
2033 x: -scroll.x,
2034 y: -scroll.y,
2035 width: parent.width,
2036 height: parent.height
2037 };
2038 if (src.x < 0) {
2039 src.width += src.x;
2040 if (src.width <= 0)
2041 return;
2042 dist.x -= src.x;
2043 dist.width += src.x;
2044 src.x = 0;
2045 }
2046 else if ((src.x + src.width) > this.width) {
2047 var p = ((src.x + src.width) - this.width);
2048 src.width -= p;
2049 if (src.width <= 0)
2050 return;
2051 dist.width -= p;
2052 }
2053 if (src.y < 0) {
2054 src.height += src.y;
2055 if (src.height <= 0)
2056 return;
2057 dist.y -= src.y;
2058 dist.height += src.y;
2059 src.y = 0;
2060 }
2061 else if ((src.y + src.height) > this.height) {
2062 var p = ((src.y + src.height) - this.height);
2063 src.height -= p;
2064 if (src.height <= 0)
2065 return;
2066 dist.height -= p;
2067 }
2068 context.drawImage(this.canvas, src.x, src.y, src.width, src.height, dist.x, dist.y, dist.width, dist.height);
2069 };
2070 Tile.prototype.getChips = function () {
2071 var ret = [];
2072 var len = this.chips.length;
2073 for (var i = 0; i < len; i++)
2074 ret = ret.concat(this.chips[i].getChips(this));
2075 return ret;
2076 };
2077 return Tile;
2078 })(jg.E);
2079 jg.Tile = Tile;
2080 })(jg || (jg = {}));
2081 ///<reference path="all.ts"/>
2082 var jg;
2083 (function (jg) {
2084 var Layer = (function (_super) {
2085 __extends(Layer, _super);
2086 function Layer(scene) {
2087 _super.call(this);
2088 this.entities = [];
2089 this.x = 0;
2090 this.y = 0;
2091 this.scene = scene;
2092 if (this.scene) {
2093 this.width = this.scene.game.width;
2094 this.height = this.scene.game.height;
2095 }
2096 this.isUpdated = true;
2097 }
2098 Layer.prototype.hasBuffer = function () {
2099 if (this.canvas)
2100 return true;
2101 return false;
2102 };
2103 Layer.prototype.createBuffer = function () {
2104 this.refresh(true);
2105 };
2106 Layer.prototype.refresh = function (must) {
2107 if (must || this.hasBuffer()) {
2108 this.canvas = window.createCanvas(this.width, this.height);
2109 this.context = this.canvas.getContext("2d");
2110 this.updated();
2111 }
2112 if (this.entities) {
2113 for (var i = 0; i < this.entities.length; i++) {
2114 if (this.entities[i]["refresh"])
2115 this.entities[i]["refresh"]();
2116 }
2117 }
2118 };
2119 Layer.prototype.deleteBuffer = function () {
2120 delete this.context;
2121 delete this.canvas;
2122 };
2123 Layer.prototype.destroy = function () {
2124 _super.prototype.destroy.call(this);
2125 if (this.hasBuffer())
2126 this.deleteBuffer();
2127 };
2128 return Layer;
2129 })(jg.E);
2130 jg.Layer = Layer;
2131 })(jg || (jg = {}));
2132 ///<reference path="all.ts"/>
2133 var jg;
2134 (function (jg) {
2135 var LoadingScene = (function (_super) {
2136 __extends(LoadingScene, _super);
2137 function LoadingScene(game, resource) {
2138 _super.call(this, game);
2139 this.resource = resource;
2140 this.resource.loaded.handle(this, this.complete);
2141 this.resource.added.handle(this, this.added);
2142 this.requestCount = this.resource.requests.length;
2143 this.finished = new jg.Trigger();
2144 this.init();
2145 }
2146 LoadingScene.prototype.init = function () {
2147 this.shape = new jg.Shape(this.game.width, 32);
2148 this.shape.moveTo(0, this.game.height / 2 - 16);
2149 this.shapeP = new jg.Shape(1, 32, jg.ShapeStyle.Fill);
2150 this.shapeP.moveTo(0, this.game.height / 2 - 16);
2151 this.append(this.shape);
2152 this.append(this.shapeP);
2153 };
2154 LoadingScene.prototype.animate = function (per) {
2155 this.shapeP.width = this.game.width * per;
2156 this.shapeP.updated();
2157 };
2158 LoadingScene.prototype.complete = function (cnt) {
2159 var per = (this.requestCount - cnt) / this.requestCount;
2160 this.animate(per);
2161 if (per == 1) {
2162 this.resource.loaded.remove(this, this.complete);
2163 this.resource.added.remove(this, this.added);
2164 this.end();
2165 this.finished.fire();
2166 }
2167 };
2168 LoadingScene.prototype.added = function (e) {
2169 this.requestCount++;
2170 };
2171 return LoadingScene;
2172 })(jg.Scene);
2173 jg.LoadingScene = LoadingScene;
2174 })(jg || (jg = {}));
2175 ///<reference path="all.ts"/>
2176 var jg;
2177 (function (jg) {
2178 var InputEvent = (function () {
2179 function InputEvent(type, action, param) {
2180 this.type = type;
2181 this.action = action;
2182 this.param = param;
2183 }
2184 return InputEvent;
2185 })();
2186 jg.InputEvent = InputEvent;
2187 var InputKeyboardEvent = (function (_super) {
2188 __extends(InputKeyboardEvent, _super);
2189 function InputKeyboardEvent(action, key, e) {
2190 _super.call(this, jg.InputEventType.Keyboard, action, e);
2191 this.key = key;
2192 }
2193 return InputKeyboardEvent;
2194 })(InputEvent);
2195 jg.InputKeyboardEvent = InputKeyboardEvent;
2196 var InputPointEvent = (function (_super) {
2197 __extends(InputPointEvent, _super);
2198 function InputPointEvent(action, e, point) {
2199 _super.call(this, jg.InputEventType.Point, action, e);
2200 this.point = point;
2201 }
2202 InputPointEvent.prototype.set = function (entity) {
2203 var entityOffset = entity.offset();
2204 this.entity = entity;
2205 this.x = this.point.x - entityOffset.x;
2206 this.y = this.point.y - entityOffset.y;
2207 };
2208 return InputPointEvent;
2209 })(InputEvent);
2210 jg.InputPointEvent = InputPointEvent;
2211 })(jg || (jg = {}));
2212 ///<reference path="all.ts"/>
2213 var jg;
2214 (function (jg) {
2215 var Renderer = (function () {
2216 function Renderer() {
2217 var _this = this;
2218 this.radian = Math.PI / 180;
2219 this.drawOptionFunctions = {
2220 transform: function (c, entity, params) {
2221 c.transform(params.m11, params.m12, params.m21, params.m22, params.dx, params.dy);
2222 },
2223 translate: function (c, entity, params) {
2224 c.translate(params.x, params.y);
2225 },
2226 scale: function (c, entity, params) {
2227 c.transform.apply(c, _this.getMatrix(entity.width, entity.height, params.x, params.y, 0));
2228 },
2229 purescale: function (c, entity, params) {
2230 c.scale(params.x, params.y);
2231 },
2232 rotate: function (c, entity, params) {
2233 c.transform.apply(c, _this.getMatrix(entity.width, entity.height, 1, 1, params));
2234 }
2235 };
2236 }
2237 Renderer.prototype.getMatrix = function (width, height, scaleX, scaleY, angle) {
2238 var r = angle * this.radian;
2239 var _cos = Math.cos(r);
2240 var _sin = Math.sin(r);
2241 var a = _cos * scaleX;
2242 var b = _sin * scaleX;
2243 var c = _sin * scaleY;
2244 var d = _cos * scaleY;
2245 var w = width / 2;
2246 var h = height / 2;
2247 return [
2248 a,
2249 b,
2250 -c,
2251 d,
2252 (-a * w + c * h + w),
2253 (-b * w - d * h + h)
2254 ];
2255 };
2256 Renderer.prototype.renderParent = function (parent, c) {
2257 if (parent.orderDraw)
2258 parent.orderDraw(parent);
2259 c.save();
2260 if (parent.opacity != 1)
2261 c.globalAlpha = parent.opacity;
2262 if (parent.options) {
2263 if (this.useDrawOption(parent, c)) {
2264 c.restore();
2265 return;
2266 }
2267 }
2268 if (parent.scroll)
2269 c.translate(parent.scroll.x, parent.scroll.y);
2270 for (var i = 0; i < parent.entities.length; i++)
2271 this.renderEntity(parent.entities[i], c);
2272 c.restore();
2273 };
2274 Renderer.prototype.renderEntity = function (entity, c) {
2275 if (!entity.opacity)
2276 return;
2277 if (entity.disableTransform) {
2278 entity.draw(c);
2279 }
2280 else {
2281 c.save();
2282 c.translate(entity.x, entity.y);
2283 if (entity.opacity != 1)
2284 c.globalAlpha *= entity.opacity;
2285 if (entity.options) {
2286 if (this.useDrawOption(entity, c)) {
2287 c.restore();
2288 return;
2289 }
2290 }
2291 if (entity.filter) {
2292 this.filterDraw(entity, c);
2293 }
2294 else {
2295 entity.draw(c);
2296 if (entity.entities) {
2297 if (entity.scroll)
2298 c.translate(entity.scroll.x, entity.scroll.y);
2299 for (var i = 0; i < entity.entities.length; i++)
2300 this.renderEntity(entity.entities[i], c);
2301 }
2302 }
2303 c.restore();
2304 }
2305 };
2306 Renderer.prototype.renderPure = function (entity, c) {
2307 entity.draw(c);
2308 if (entity.entities) {
2309 for (var i = 0; i < entity.entities.length; i++)
2310 this.renderEntity(entity.entities[i], c);
2311 }
2312 };
2313 Renderer.prototype.filterDraw = function (entity, c) {
2314 var buffer = new jg.BufferedRenderer(entity);
2315 buffer.filter = entity.filter;
2316 buffer.renderPure(entity, buffer.c);
2317 buffer.applyFilter(buffer.c, entity);
2318 buffer.draw(c);
2319 };
2320 Renderer.prototype.useDrawOption = function (entity, c) {
2321 for (var p in entity.options) {
2322 if (this.drawOptionFunctions[p])
2323 this.drawOptionFunctions[p].call(this, c, entity, entity.options[p]);
2324 else {
2325 c[p] = entity.options[p];
2326 }
2327 }
2328 };
2329 return Renderer;
2330 })();
2331 jg.Renderer = Renderer;
2332 })(jg || (jg = {}));
2333 ///<reference path="all.ts"/>
2334 var jg;
2335 (function (jg) {
2336 var GameRenderer = (function (_super) {
2337 __extends(GameRenderer, _super);
2338 function GameRenderer(game, container, transferMode, disableBg) {
2339 _super.call(this);
2340 this.game = game;
2341 this.container = container ? container : document.getElementById("jgame");
2342 if (!this.container) {
2343 var div = document.createElement("div");
2344 div.id = "jgame";
2345 var bodies = document.getElementsByTagName("body");
2346 if (bodies.length == 0)
2347 throw "can not initialize game engine";
2348 bodies[0].appendChild(div);
2349 this.container = div;
2350 }
2351 this.handler = document.createElement("div");
2352 this.handler.style.display = "inline-block";
2353 this.handler.setAttribute("tabindex", "1");
2354 this.handler.className = "input-handler";
2355 this.handler.style.outline = 'none';
2356 this.container.appendChild(this.handler);
2357 this.handler.focus();
2358 this.changeTransferMode(transferMode ? transferMode : jg.RenderTransferMode.Transfer);
2359 if (!disableBg) {
2360 this.bg = this.fc.getImageData(0, 0, this.game.width, this.game.height);
2361 for (var i = 0; i < this.bg.data.length; i++)
2362 this.bg.data[i] = 255;
2363 }
2364 }
2365 GameRenderer.prototype.changeFrontCanvasSize = function (size, offset) {
2366 this.frontCanvasSize = size;
2367 this.frontCanvasOffset = offset;
2368 this.refresh();
2369 };
2370 GameRenderer.prototype.changeTransferMode = function (mode) {
2371 this.transferMode = mode;
2372 if (this.transferMode == jg.RenderTransferMode.Flip) {
2373 this.handler.style.position = "relative";
2374 this.handler.style.width = this.game.width + "px";
2375 this.handler.style.height = this.game.height + "px";
2376 }
2377 this.refresh();
2378 };
2379 GameRenderer.prototype.changeScene = function (scene) {
2380 this.scene = scene;
2381 };
2382 GameRenderer.prototype.flip = function () {
2383 var c = this.fc;
2384 this.fc = this.bc;
2385 this.bc = this.fc;
2386 this.flipNo = this.flipNo ? 0 : 1;
2387 this.buffer[this.flipNo].style.zIndex = "1";
2388 this.buffer[this.flipNo ? 0 : 1].style.zIndex = "0";
2389 };
2390 GameRenderer.prototype.render = function () {
2391 var hasUpdate = false;
2392 if (this.scene.layerCount == 1) {
2393 var layer = this.scene.root;
2394 if (!layer.isUpdated) {
2395 }
2396 else {
2397 hasUpdate = true;
2398 if (!this.disableClear)
2399 this.bc.putImageData(this.bg, 0, 0);
2400 this.renderParent(layer, this.bc);
2401 layer.isUpdated = false;
2402 }
2403 }
2404 else {
2405 for (var i in this.scene.layers) {
2406 if (this.scene.layers[i].isUpdated) {
2407 hasUpdate = true;
2408 break;
2409 }
2410 }
2411 if (hasUpdate) {
2412 if (!this.disableClear)
2413 this.bc.putImageData(this.bg, 0, 0);
2414 for (var i in this.scene.layers) {
2415 var layer = this.scene.layers[i];
2416 if (layer.isUpdated) {
2417 layer.context.clearRect(0, 0, layer.width, layer.height);
2418 this.renderParent(layer, layer.context);
2419 }
2420 this.bc.drawImage(layer.canvas, layer.x, layer.y);
2421 layer.isUpdated = false;
2422 }
2423 }
2424 }
2425 if (hasUpdate) {
2426 if (this.filter) {
2427 var imageData = this.bc.getImageData(0, 0, this.game.width, this.game.height);
2428 this.filter.filter(imageData);
2429 this.fc.putImageData(imageData, 0, 0);
2430 }
2431 else {
2432 if (this.bc != this.fc) {
2433 this.fc.drawImage(this.buffer[1], 0, 0);
2434 }
2435 else if (this.transferMode == jg.RenderTransferMode.Flip) {
2436 this.flip();
2437 }
2438 }
2439 }
2440 };
2441 GameRenderer.prototype.refresh = function () {
2442 delete this.buffer;
2443 this.buffer = [];
2444 if (this.transferMode == jg.RenderTransferMode.Flip) {
2445 this.handler.innerHTML = "";
2446 for (var i = 0; i < 2; i++) {
2447 this.buffer[i] = window.createCanvas(this.game.width, this.game.height);
2448 ;
2449 this.buffer[i].style.position = "absolute";
2450 this.buffer[i].style.zIndex = i.toString();
2451 this.handler.appendChild(this.buffer[i]);
2452 }
2453 this.fc = this.buffer[1].getContext("2d");
2454 this.bc = this.buffer[0].getContext("2d");
2455 this.flipNo = 1;
2456 if (this.frontCanvasSize) {
2457 jg.JGUtil.scaleCanvas(this.buffer[1], this.frontCanvasSize);
2458 }
2459 }
2460 else if (this.transferMode == jg.RenderTransferMode.Transfer) {
2461 this.handler.innerHTML = "";
2462 for (var i = 0; i < 2; i++) {
2463 this.buffer[i] = window.createCanvas(this.game.width, this.game.height);
2464 ;
2465 }
2466 this.handler.appendChild(this.buffer[0]);
2467 this.fc = this.buffer[0].getContext("2d");
2468 this.bc = this.buffer[1].getContext("2d");
2469 }
2470 else {
2471 this.handler.innerHTML = "";
2472 this.buffer[0] = window.createCanvas(this.game.width, this.game.height);
2473 ;
2474 this.handler.appendChild(this.buffer[0]);
2475 this.fc = this.buffer[0].getContext("2d");
2476 this.bc = this.fc;
2477 }
2478 if (this.frontCanvasSize) {
2479 jg.JGUtil.scaleCanvas(this.buffer[0], this.frontCanvasSize);
2480 if (this.frontCanvasOffset) {
2481 this.handler.style.position = "relative";
2482 this.handler.style.<