Repository navigation
Expand file tree
/
Copy pathquintus-all.js
More file actions
4918 lines (4030 loc) · 135 KB
/
Copy pathquintus-all.js
File metadata and controls
4918 lines (4030 loc) · 135 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Quintus Game Engine
// (c) 2012 Pascal Rettig, Cykod LLC
// Quintus may be freely distributed under the MIT license or GPLv2 License.
// For all details and documentation:
// http://html5quintus.com
//
// Quintus HTML5 Game Engine
// =========================
//
// The code in `quintus.js` defines the base `Quintus()` method
// which create an instance of the engine. The basic engine doesn't
// do a whole lot - it provides an architecture for extension, a
// game loop, and a method for creating or binding to an exsiting
// canvas context. The engine has dependencies on Underscore.js and jQuery,
// although the jQuery dependency will be removed in the future.
//
// Most of the game-specific functionality is in the
// various other modules:
//
// * `quintus_input.js` - `Input` module, which allows for user input via keyboard and touchscreen
// * `quintus_sprites.js` - `Sprites` module, which defines a basic `Q.Sprite` class along with spritesheet support in `Q.SpriteSheet`.
// * `quintus_scenes.js` - `Scenes` module. It defines the `Q.Scene` class, which allows creation of reusable scenes, and the `Q.Stage` class, which handles managing a number of sprites at once.
// * `quintus_anim.js` - `Anim` module, which adds in support for animations on sprites along with a `viewport` component to follow the player around and a `Q.Repeater` class that can create a repeating, scrolling background.
// Engine Bootstrapping
// ====================
// Top-level Quintus engine factory wrapper,
// creates new instances of the engine by calling:
//
// var Q = Quintus({ ... });
//
// Any initial setup methods also all return the `Q` object, allowing any initial
// setup calls to be chained together.
//
// var Q = Quintus()
// .include("Input, Sprites, Scenes")
// .setup('quintus', { maximize: true })
// .controls();
//
// `Q` is used internally as the object name, and is used in most of the examples,
// but multiple instances of the engine on the same page can have different names.
//
// var Game1 = Quintus(), Game2 = Quintus();
//
var Quintus = function Quintus(opts) {
// A la jQuery - the returned `Q` object is actually
// a method that calls `Q.select`. `Q.select` doesn't do anything
// initially, but can be overridden by a module to allow
// selection of game objects. The `Scenes` module adds in
// the select method which selects from the default stage.
//
// var Q = Quintus().include("Sprites, Scenes");
// ... Game Code ...
// // Set the angry property on all Enemy1 class objects to true
// Q("Enemy1").p({ angry: true });
//
var Q = function(selector,scope,options) {
return Q.select(selector,scope,options);
};
Q.select = function() { /* No-op */ };
// Syntax for including other modules into quintus, can accept a comma-separated
// list of strings, an array of strings, or an array of actual objects. Example:
//
// Q.include("Input, Sprites, Scenes")
//
Q.include = function(mod) {
Q._each(Q._normalizeArg(mod),function(name) {
var m = Quintus[name] || name;
if(!Q._isFunction(m)) { throw "Invalid Module:" + name; }
m(Q);
});
return Q;
};
// Utility Methods
// ===============
//
// Most of these utility methods are a subset of Underscore.js,
// Most are pulled directly from underscore and some are
// occasionally optimized for speed and memory usage in lieu of flexibility.
// Underscore.js is (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Underscore is freely distributable under the MIT license.
// http://underscorejs.org
// An internal utility method (utility methods are prefixed with underscores)
// It's used to take a string of comma separated names and turn it into an `Array`
// of names. If an array of names is passed in, it's left as is. Example usage:
//
// Q._normalizeArg("Sprites, Scenes, Physics ");
// // returns [ "Sprites", "Scenes", "Physics" ]
//
// Used by `Q.include` and `Q.Sprite.add` to add modules and components, respectively.
Q._normalizeArg = function(arg) {
if(Q._isString(arg)) {
arg = arg.replace(/\s+/g,'').split(",");
}
if(!Q._isArray(arg)) {
arg = [ arg ];
}
return arg;
};
// Extends a destination object
// with a source object
Q._extend = function(dest,source) {
if(!source) { return dest; }
for (var prop in source) {
dest[prop] = source[prop];
}
return dest;
};
// Return a shallow copy of an object. Sub-objects (and sub-arrays) are not cloned.
Q._clone = function(obj) {
return Q._extend({},obj);
};
// Method that adds default properties onto
// an object only if the key is undefined
Q._defaults = function(dest,source) {
if(!source) { return dest; }
for (var prop in source) {
if(dest[prop] === void 0) {
dest[prop] = source[prop];
}
}
return dest;
};
// Shortcut for hasOwnProperty
Q._has = function(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
};
// Check if something is a string
// NOTE: this fails for non-primitives
Q._isString = function(obj) {
return typeof obj === "string";
};
Q._isNumber = function(obj) {
return Object.prototype.toString.call(obj) === '[object Number]';
};
// Check if something is a function
Q._isFunction = function(obj) {
return Object.prototype.toString.call(obj) === '[object Function]';
};
// Check if something is a function
Q._isObject = function(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
};
// Check if something is a function
Q._isArray = function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
// Check if something is undefined
Q._isUndefined = function(obj) {
return obj === void 0;
};
// Removes a property from an object and returns it
Q._popProperty = function(obj,property) {
var val = obj[property];
delete obj[property];
return val;
};
// Basic iteration method. This can often be a performance
// handicap when the callback iterator is created inline,
// as this leads to lots of functions that need to be GC'd.
// Better is to define the iterator as a private method so
// it is only created once.
Q._each = function(obj,iterator,context) {
if (obj == null) { return; }
if (obj.forEach) {
obj.forEach(iterator,context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
iterator.call(context, obj[i], i, obj);
}
} else {
for (var key in obj) {
iterator.call(context, obj[key], key, obj);
}
}
};
// Invoke the named property on each element of the array
Q._invoke = function(arr,property,arg1,arg2) {
if (arr == null) { return; }
for (var i = 0, l = arr.length; i < l; i++) {
arr[i][property](arg1,arg2);
}
};
// Basic detection method, returns the first instance where the
// iterator returns truthy.
Q._detect = function(obj,iterator,context,arg1,arg2) {
var result;
if (obj == null) { return; }
if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
result = iterator.call(context, obj[i], i, arg1,arg2);
if(result) { return result; }
}
return false;
} else {
for (var key in obj) {
result = iterator.call(context, obj[key], key, arg1,arg2);
if(result) { return result; }
}
return false;
}
};
// Returns a new Array with entries set to the return value of the iterator.
Q._map = function(obj, iterator, context) {
var results = [];
if (obj == null) { return results; }
if (obj.map) { return obj.map(iterator, context); }
Q._each(obj, function(value, index, list) {
results[results.length] = iterator.call(context, value, index, list);
});
if (obj.length === +obj.length) { results.length = obj.length; }
return results;
};
// Returns a sorted copy of unique array elements with null remove
Q._uniq = function(arr) {
arr = arr.slice().sort();
var output = [];
var last = null;
for(var i=0;i<arr.length;i++) {
if(arr[i] !== void 0 && last !== arr[i]) {
output.push(arr[i]);
}
last = arr[i];
}
return output;
};
// returns a new array with the same entries as the source but in a random order.
Q._shuffle = function(obj) {
var shuffled = [], rand;
Q._each(obj, function(value, index, list) {
rand = Math.floor(Math.random() * (index + 1));
shuffled[index] = shuffled[rand];
shuffled[rand] = value;
});
return shuffled;
};
// Return an object's keys
Q._keys = Object.keys || function(obj) {
if(Q._isObject(obj)) { throw new TypeError('Invalid object'); }
var keys = [];
for (var key in obj) { if (Q._has(obj, key)) { keys[keys.length] = key; } }
return keys;
};
Q._range = function(start,stop,step) {
step = step || 1;
var len = Math.max(Math.ceil((stop - start) / step), 0);
var idx = 0;
var range = new Array(len);
while(idx < len) {
range[idx++] = start;
start += step;
}
return range;
};
var idIndex = 0;
// Return a unique identifier
Q._uniqueId = function() {
return idIndex++;
};
// Options
// ========
// Default engine options defining the paths
// where images, audio and other data files should be found
// relative to the base HTML file. As well as a couple of other
// options.
//
// These can be overriden by passing in options to the `Quintus()`
// factory method, for example:
//
// // Override the imagePath to default to /assets/images/
// var Q = Quintus({ imagePath: "/assets/images/" });
//
// If you follow the default convention from the examples, however,
// you should be able to call `Quintus()` without any options.
Q.options = {
imagePath: "images/",
audioPath: "audio/",
dataPath: "data/",
audioSupported: [ 'mp3','ogg' ],
sound: true,
frameTimeLimit: 100
};
if(opts) { Q._extend(Q.options,opts); }
// Game Loop support
// =================
// By default the engine doesn't start a game loop until you actually tell it to.
// Usually the loop is started the first time you call `Q.stageScene`, but if you
// aren't using the `Scenes` module you can explicitly start the game loop yourself
// and control **exactly** what the engine does each cycle. For example:
//
// var Q = Quintus().setup();
//
// var ball = new Q.Sprite({ .. });
//
// Q.gameLoop(function(dt) {
// Q.clear();
// ball.step(dt);
// ball.draw(Q.ctx);
// });
//
// The callback will be called with fraction of a second that has elapsed since
// the last call to the loop method.
Q.gameLoop = function(callback) {
Q.lastGameLoopFrame = new Date().getTime();
// Short circuit the loop check in case multiple scenes
// are staged immediately
Q.loop = true;
// Keep track of the frame we are on (so that animations can be synced
// to the next frame)
Q._loopFrame = 0;
// Wrap the callback to save it and standardize the passed
// in time.
Q.gameLoopCallbackWrapper = function() {
var now = new Date().getTime();
Q._loopFrame++;
Q.loop = window.requestAnimationFrame(Q.gameLoopCallbackWrapper);
var dt = now - Q.lastGameLoopFrame;
/* Prevent fast-forwarding by limiting the length of a single frame. */
if(dt > Q.options.frameTimeLimit) { dt = Q.options.frameTimeLimit; }
callback.apply(Q,[dt / 1000]);
Q.lastGameLoopFrame = now;
};
window.requestAnimationFrame(Q.gameLoopCallbackWrapper);
return Q;
};
// Pause the entire game by canceling the requestAnimationFrame call. If you use setTimeout or
// setInterval in your game, those will, of course, keep on rolling...
Q.pauseGame = function() {
if(Q.loop) {
window.cancelAnimationFrame(Q.loop);
}
Q.loop = null;
};
// Unpause the game by restarting the requestAnimationFrame-based loop.
Q.unpauseGame = function() {
if(!Q.loop) {
Q.lastGameLoopFrame = new Date().getTime();
Q.loop = window.requestAnimationFrame(Q.gameLoopCallbackWrapper);
}
};
// The base Class object
// ===============
//
// Quintus uses the Simple JavaScript inheritance Class object, created by
// John Resig and described on his blog:
//
// [http://ejohn.org/blog/simple-javascript-inheritance/](http://ejohn.org/blog/simple-javascript-inheritance/)
//
// The class is used wholesale, with the only differences being that instead
// of appearing in a top-level namespace, the `Class` object is available as
// `Q.Class` and a second argument on the `extend` method allows for adding
// class level methods and the class name is passed in a parameter for introspection
// purposes.
//
// Classes can be created by calling `Q.Class.extend(name,{ .. })`, although most of the time
// you'll want to use one of the derivitive classes, `Q.Evented` or `Q.GameObject` which
// have a little bit of functionality built-in. `Q.Evented` adds event binding and
// triggering support and `Q.GameObject` adds support for components and a destroy method.
//
// The main things Q.Class get you are easy inheritance, a constructor method called `init()`,
// dynamic addition of a this._super method when a method is overloaded (be careful with
// this as it adds some overhead to method calls.) Calls to `instanceof` also all
// work as you'd hope.
//
// By convention, classes should be added onto to the `Q` object and capitalized, so if
// you wanted to create a new class for your game, you'd write:
//
// Q.Class.extend("MyClass",{ ... });
//
// Examples:
//
// Q.Class.extend("Bird",{
// init: function(name) { this.name = name; },
// speak: function() { console.log(this.name); },
// fly: function() { console.log("Flying"); }
// });
//
// Q.Bird.extend("Penguin",{
// speak: function() { console.log(this.name + " the penguin"); },
// fly: function() { console.log("Can't fly, sorry..."); }
// });
//
// var randomBird = new Q.Bird("Frank"),
// pengy = new Q.Penguin("Pengy");
//
// randomBird.fly(); // Logs "Flying"
// pengy.fly(); // Logs "Can't fly,sorry..."
//
// randomBird.speak(); // Logs "Frank"
// pengy.speak(); // Logs "Pengy the penguin"
//
// console.log(randomBird instanceof Q.Bird); // true
// console.log(randomBird instanceof Q.Penguin); // false
// console.log(pengy instanceof Q.Bird); // true
// console.log(pengy instanceof Q.Penguin); // true
/* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* MIT Licensed.
*
* Inspired by base2 and Prototype
*/
(function(){
var initializing = false,
fnTest = /xyz/.test(function(){ var xyz;}) ? /\b_super\b/ : /.*/;
/* The base Class implementation (does nothing) */
Q.Class = function(){};
// See if a object is a specific class
Q.Class.prototype.isA = function(className) {
return this.className === className;
};
/* Create a new Class that inherits from this class */
Q.Class.extend = function(className, prop, classMethods) {
/* No name, don't add onto Q */
if(!Q._isString(className)) {
classMethods = prop;
prop = className;
className = null;
}
var _super = this.prototype,
ThisClass = this;
/* Instantiate a base class (but only create the instance, */
/* don't run the init constructor) */
initializing = true;
var prototype = new ThisClass();
initializing = false;
function _superFactory(name,fn) {
return function() {
var tmp = this._super;
/* Add a new ._super() method that is the same method */
/* but on the super-class */
this._super = _super[name];
/* The method only need to be bound temporarily, so we */
/* remove it when we're done executing */
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
}
/* Copy the properties over onto the new prototype */
for (var name in prop) {
/* Check if we're overwriting an existing function */
prototype[name] = typeof prop[name] === "function" &&
typeof _super[name] === "function" &&
fnTest.test(prop[name]) ?
_superFactory(name,prop[name]) :
prop[name];
}
/* The dummy class constructor */
function Class() {
/* All construction is actually done in the init method */
if ( !initializing && this.init ) {
this.init.apply(this, arguments);
}
}
/* Populate our constructed prototype object */
Class.prototype = prototype;
/* Enforce the constructor to be what we expect */
Class.prototype.constructor = Class;
/* And make this class extendable */
Class.extend = Q.Class.extend;
/* If there are class-level Methods, add them to the class */
if(classMethods) {
Q._extend(Class,classMethods);
}
if(className) {
/* Save the class onto Q */
Q[className] = Class;
/* Let the class know its name */
Class.prototype.className = className;
Class.className = className;
}
return Class;
};
}());
// Event Handling
// ==============
// The `Q.Evented` class adds event handling onto the base `Q.Class`
// class. Evented objects can trigger events and other objects can
// bind to those events.
Q.Class.extend("Evented",{
// Binds a callback to an event on this object. If you provide a
// `target` object, that object will add this event to it's list of
// binds, allowing it to automatically remove it when it is destroyed.
on: function(event,target,callback) {
if(Q._isArray(event) || event.indexOf(",") !== -1) {
event = Q._normalizeArg(event);
for(var i=0;i<event.length;i++) {
this.on(event[i],target,callback);
}
return;
}
// Handle the case where there is no target provided,
// swapping the target and callback parameters.
if(!callback) {
callback = target;
target = null;
}
// If there's still no callback, default to the event name
if(!callback) {
callback = event;
}
// Handle case for callback that is a string, this will
// pull the callback from the target object or from this
// object.
if(Q._isString(callback)) {
callback = (target || this)[callback];
}
// To keep `Q.Evented` objects from needing a constructor,
// the `listeners` object is created on the fly as needed.
// `listeners` keeps a list of callbacks indexed by event name
// for quick lookup.
this.listeners = this.listeners || {};
this.listeners[event] = this.listeners[event] || [];
this.listeners[event].push([ target || this, callback]);
// With a provided target, the target object keeps track of
// the events it is bound to, which allows for automatic
// unbinding on destroy.
if(target) {
if(!target.binds) { target.binds = []; }
target.binds.push([this,event,callback]);
}
},
// Triggers an event, passing in some optional additional data about
// the event.
trigger: function(event,data) {
// First make sure there are any listeners, then check for any listeners
// on this specific event, if not, early out.
if(this.listeners && this.listeners[event]) {
// Call each listener in the context of either the target passed into
// `on` or the object itself.
for(var i=0,len = this.listeners[event].length;i<len;i++) {
var listener = this.listeners[event][i];
listener[1].call(listener[0],data);
}
}
},
// Unbinds an event. Can be called with 1, 2, or 3 parameters, each
// of which unbinds a more specific listener.
off: function(event,target,callback) {
// Without a target, remove all teh listeners.
if(!target) {
if(this.listeners[event]) {
delete this.listeners[event];
}
} else {
// If the callback is a string, find a method of the
// same name on the target.
if(Q._isString(callback) && target[callback]) {
callback = target[callback];
}
var l = this.listeners && this.listeners[event];
if(l) {
// Loop from the end to the beginning, which allows us
// to remove elements without having to affect the loop.
for(var i = l.length-1;i>=0;i--) {
if(l[i][0] === target) {
if(!callback || callback === l[i][1]) {
this.listeners[event].splice(i,1);
}
}
}
}
}
},
// `debind` is called to remove any listeners an object had
// on other objects. The most common case is when an object is
// destroyed you'll want all the event listeners to be removed
// for you.
debind: function() {
if(this.binds) {
for(var i=0,len=this.binds.length;i<len;i++) {
var boundEvent = this.binds[i],
source = boundEvent[0],
event = boundEvent[1];
source.off(event,this);
}
}
}
});
// Components
// ==============
//
// Components are self-contained pieces of functionality that can be added onto and removed
// from objects. The allow for a more dynamic functionality tree than using inheritance (i.e.
// by favoring composition over inheritance) and are added and removed on the fly at runtime.
// (yes, I know everything in JS is at runtime, but you know what I mean, geez)
//
// Combining components with events makes it easy to create reusable pieces of
// functionality that can be decoupled from each other.
// The master list of registered components, indexed in an object by name.
Q.components = {};
// The base class for components. These are usually not derived directly but are instead
// created by calling `Q.register` to register a new component given a set of methods the
// component supports. Components are created automatically when they are added to a
// `Q.GameObject` with the `add` method.
//
// Many components also define an `added` method, which is called automatically by the
// `init` constructor after a component has been added to an object. This is a good time
// to add event listeners on the object.
Q.Evented.extend("Component",{
// Components are created when they are added onto a `Q.GameObject` entity. The entity
// is directly extended with any methods inside of an `extend` property and then the
// component itself is added onto the entity as well.
init: function(entity) {
this.entity = entity;
if(this.extend) { Q._extend(entity,this.extend); }
entity[this.name] = this;
entity.activeComponents.push(this.componentName);
if(entity.stage && entity.stage.addToList) {
entity.stage.addToList(this.componentName,entity);
}
if(this.added) { this.added(); }
},
// `destroy` is called automatically when a component is removed from an entity. It is
// not called, however, when an entity is destroyed (for performance reasons).
//
// It's job is to remove any methods that were added with `extend` and then remove and
// debind itself from the entity. It will also call `destroyed` if the component has
// a method by that name.
destroy: function() {
if(this.extend) {
var extensions = Q._keys(this.extend);
for(var i=0,len=extensions.length;i<len;i++) {
delete this.entity[extensions[i]];
}
}
delete this.entity[this.name];
var idx = this.entity.activeComponents.indexOf(this.componentName);
if(idx !== -1) {
this.entity.activeComponents.splice(idx,1);
if(this.entity.stage && this.entity.stage.addToList) {
this.entity.stage.addToLists(this.componentName,this.entity);
}
}
this.debind();
if(this.destroyed) { this.destroyed(); }
}
});
// This is the base class most Quintus objects are derived from, it extends
// `Q.Evented` and adds component support to an object, allowing components to
// be added and removed from an object. It also defines a destroyed method
// which will debind the object, remove it from it's parent (usually a scene)
// if it has one, and trigger a destroyed event.
Q.Evented.extend("GameObject",{
// Simple check to see if a component already exists
// on an object by searching for a property of the same name.
has: function(component) {
return this[component] ? true : false;
},
// Adds one or more components to an object. Accepts either
// a comma separated string or an array of strings that map
// to component names.
//
// Instantiates a new component object of the correct type
// (if the component exists) and then triggers an addComponent
// event.
//
// Returns the object to allow chaining.
add: function(components) {
components = Q._normalizeArg(components);
if(!this.activeComponents) { this.activeComponents = []; }
for(var i=0,len=components.length;i<len;i++) {
var name = components[i],
Comp = Q.components[name];
if(!this.has(name) && Comp) {
var c = new Comp(this);
this.trigger('addComponent',c);
}
}
return this;
},
// Removes one or more components from an object. Accepts the
// same style of parameters as `add`. Triggers a delComponent event
// and and calls destroy on the component.
//
// Returns the element to allow chaining.
del: function(components) {
components = Q._normalizeArg(components);
for(var i=0,len=components.length;i<len;i++) {
var name = components[i];
if(name && this.has(name)) {
this.trigger('delComponent',this[name]);
this[name].destroy();
}
}
return this;
},
// Destroys the object by calling debind and removing the
// object from it's parent. Will trigger a destroyed event
// callback.
destroy: function() {
if(this.isDestroyed) { return; }
this.trigger('destroyed');
this.debind();
if(this.stage && this.stage.remove) {
this.stage.remove(this);
}
this.isDestroyed = true;
if(this.destroyed) { this.destroyed(); }
}
});
// This registers a component with the engine, making it available to `Q.GameObject`'s
// This creates a new descendent class of `Q.Component` with new methods added in.
Q.component = function(name,methods) {
if(!methods) { return Q.components[name]; }
methods.name = name;
methods.componentName = "." + name;
return (Q.components[name] = Q.Component.extend(name + "Component",methods));
};
// Generic Game State object that can be used to
// track of the current state of the Game, for example when the player starts
// a new game you might want to keep track of their score and remaining lives:
//
// Q.reset({ score: 0, lives: 2 });
//
// Then in your game might want to add to the score:
//
// Q.state.inc("score",50);
//
// In your hud, you can listen for change events on the state to update your
// display:
//
// Q.state.on("change.score",function() { .. update the score display .. });
//
Q.GameObject.extend("GameState",{
init: function(p) {
this.p = Q._extend({},p);
this.listeners = {};
},
// Resets the state to value p
reset: function(p) { this.init(p); this.trigger("reset"); },
// Internal helper method to set an individual property
_triggerProperty: function(value,key) {
if(this.p[key] !== value) {
this.p[key] = value;
this.trigger("change." + key,value);
}
},
// Set one or more properties, trigger events on those
// properties changing
set: function(properties,value) {
if(Q._isObject(properties)) {
Q._each(properties,this._triggerProperty,this);
} else {
this._triggerProperty(value,properties);
}
this.trigger("change");
},
// Increment an individual property by amount
inc: function(property,amount) {
this.set(property,this.get(property) + amount);
},
// Increment an individual property by amount
dec: function(property,amount) {
this.set(property,this.get(property) - amount);
},
// Return an individual property
get: function(property) {
return this.p[property];
}
});
// The instance of the Q.stage property
Q.state = new Q.GameState();
// Reset the game state and unbind all state events
Q.reset = function() { Q.state.reset(); };
// Canvas Methods
// ==============
//
// The `setup` and `clear` method are the only two canvas-specific methods in
// the core of Quintus. `imageData` also uses canvas but it can be used in
// any type of game.
// Setup will either create a new canvas element and append it
// to the body of the document or use an existing one. It will then
// pull out the width and height of the canvas for engine use.
//
// It also adds a wrapper container around the element.
//
// If the `maximize` is set to true, the canvas element is maximized
// on the page and the scroll trick is used to try to get the address bar away.
//
// The engine will also resample the game to CSS dimensions at twice pixel
// dimensions if the `resampleWidth` or `resampleHeight` options are set.
//
// TODO: add support for auto-resize w/ engine event notifications, remove
// jQuery.
Q.touchDevice = ('ontouchstart' in document);
Q.setup = function(id, options) {
if(Q._isObject(id)) {
options = id;
id = null;
}
options = options || {};
id = id || "quintus";
if(Q._isString(id)) {
Q.el = document.getElementById(id);
} else {
Q.el = id;
}
if(!Q.el) {
Q.el = document.createElement("canvas");
Q.el.width = options.width || 320;
Q.el.height = options.height || 420;
Q.el.id = id;
document.body.appendChild(Q.el);
}
var w = parseInt(Q.el.width,10),
h = parseInt(Q.el.height,10);
var maxWidth = options.maxWidth || 5000,
maxHeight = options.maxHeight || 5000,
resampleWidth = options.resampleWidth,
resampleHeight = options.resampleHeight,
upsampleWidth = options.upsampleWidth,
upsampleHeight = options.upsampleHeight;
if(options.maximize === true || (Q.touchDevice && options.maximize === 'touch')) {
document.body.style.padding = 0;
document.body.style.margin = 0;
w = Math.min(window.innerWidth,maxWidth);
h = Math.min(window.innerHeight - 5,maxHeight);
if(Q.touchDevice) {
Q.el.style.height = (h*2) + "px";
window.scrollTo(0,1);
w = Math.min(window.innerWidth,maxWidth);
h = Math.min(window.innerHeight,maxHeight);
}
} else if(Q.touchDevice) {
window.scrollTo(0,1);
}
if((upsampleWidth && w <= upsampleWidth) ||
(upsampleHeight && h <= upsampleHeight)) {
Q.el.style.height = h + "px";
Q.el.style.width = w + "px";
Q.el.width = w * 2;
Q.el.height = h * 2;
}
else if(((resampleWidth && w > resampleWidth) ||
(resampleHeight && h > resampleHeight)) &&
Q.touchDevice) {
Q.el.style.height = h + "px";
Q.el.style.width = w + "px";
Q.el.width = w / 2;
Q.el.height = h / 2;
} else {
Q.el.style.height = h + "px";
Q.el.style.width = w + "px";
Q.el.width = w;
Q.el.height = h;
}
var elParent = Q.el.parentNode;
if(elParent) {
Q.wrapper = document.createElement("div");
Q.wrapper.id = id + '_container';
Q.wrapper.style.width = w + "px";
Q.wrapper.style.margin = "0 auto";
Q.wrapper.style.position = "relative";
elParent.insertBefore(Q.wrapper,Q.el);
Q.wrapper.appendChild(Q.el);
}
Q.el.style.position = 'relative';
Q.ctx = Q.el.getContext &&
Q.el.getContext("2d");
Q.width = parseInt(Q.el.width,10);
Q.height = parseInt(Q.el.height,10);
Q.cssWidth = w;
Q.cssHeight = h;
window.addEventListener('orientationchange',function() {