entityjs 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +4 -0
  3. data/README.md +49 -0
  4. data/Rakefile +1 -0
  5. data/bin/entityjs +14 -0
  6. data/build/build_debug.bat +1 -0
  7. data/build/build_min.bat +1 -0
  8. data/build/config.php +64 -0
  9. data/build/debug.php +48 -0
  10. data/build/files.php +74 -0
  11. data/build/jsmin.php +376 -0
  12. data/build/min.php +50 -0
  13. data/changelog.txt +53 -0
  14. data/entityjs.gemspec +25 -0
  15. data/examples/keys/arrow.png +0 -0
  16. data/examples/keys/keys.html +59 -0
  17. data/examples/keys/keys.js +148 -0
  18. data/examples/mouse/mouse.html +58 -0
  19. data/examples/mouse/mouse.js +60 -0
  20. data/examples/scenes/home.png +0 -0
  21. data/examples/scenes/scenes.html +55 -0
  22. data/examples/scenes/scenes.js +134 -0
  23. data/examples/scenes/win.png +0 -0
  24. data/examples/sounds/sound1.mp3 +0 -0
  25. data/examples/sounds/sound1.ogg +0 -0
  26. data/examples/sounds/sounds.html +56 -0
  27. data/examples/sounds/sounds.js +44 -0
  28. data/examples/style/background.png +0 -0
  29. data/examples/style/sheet.css +762 -0
  30. data/examples/tiles/tiles.html +56 -0
  31. data/examples/tiles/tiles.js +85 -0
  32. data/examples/tiles/tiles.png +0 -0
  33. data/lib/blank/config.js +4 -0
  34. data/lib/blank/config.yml +21 -0
  35. data/lib/blank/home.js +11 -0
  36. data/lib/blank/init.js +7 -0
  37. data/lib/blank/load.js +21 -0
  38. data/lib/blank/play.html +29 -0
  39. data/lib/entity.debug.js +52 -0
  40. data/lib/entity.min.js +184 -0
  41. data/lib/entityjs/command.rb +37 -0
  42. data/lib/entityjs/comp.rb +11 -0
  43. data/lib/entityjs/game.rb +93 -0
  44. data/lib/entityjs/min.rb +11 -0
  45. data/lib/entityjs/refresh.rb +14 -0
  46. data/lib/entityjs/version.rb +3 -0
  47. data/lib/entityjs.rb +6 -0
  48. data/license.txt +1 -0
  49. data/spec/lib/entityjs/game_spec.rb +11 -0
  50. data/spec/spec_helper.rb +3 -0
  51. data/src/entityjs/core/component.js +306 -0
  52. data/src/entityjs/core/entity.js +516 -0
  53. data/src/entityjs/core/load.js +224 -0
  54. data/src/entityjs/core/query.js +410 -0
  55. data/src/entityjs/core/re.js +70 -0
  56. data/src/entityjs/core/system.js +125 -0
  57. data/src/entityjs/cycle/draw.js +185 -0
  58. data/src/entityjs/cycle/ticker.js +27 -0
  59. data/src/entityjs/cycle/tween.js +61 -0
  60. data/src/entityjs/cycle/update.js +86 -0
  61. data/src/entityjs/cycle/wait.js +22 -0
  62. data/src/entityjs/cycle/worker.js +9 -0
  63. data/src/entityjs/display/anchor.js +53 -0
  64. data/src/entityjs/display/bitfont.js +93 -0
  65. data/src/entityjs/display/bitmap.js +37 -0
  66. data/src/entityjs/display/circle.js +30 -0
  67. data/src/entityjs/display/font.js +41 -0
  68. data/src/entityjs/display/group.js +63 -0
  69. data/src/entityjs/display/rect.js +19 -0
  70. data/src/entityjs/display/screen.js +46 -0
  71. data/src/entityjs/display/sprite.js +37 -0
  72. data/src/entityjs/input/keyboard.js +150 -0
  73. data/src/entityjs/input/mouse.js +123 -0
  74. data/src/entityjs/input/pressed.js +81 -0
  75. data/src/entityjs/input/touch.js +28 -0
  76. data/src/entityjs/math/bind.js +76 -0
  77. data/src/entityjs/math/bisect.js +69 -0
  78. data/src/entityjs/math/body.js +39 -0
  79. data/src/entityjs/math/drag.js +39 -0
  80. data/src/entityjs/math/hitmap.js +165 -0
  81. data/src/entityjs/math/hittest.js +26 -0
  82. data/src/entityjs/math/physics.js +142 -0
  83. data/src/entityjs/math/point.js +57 -0
  84. data/src/entityjs/math/tile.js +91 -0
  85. data/src/entityjs/media/channel.js +93 -0
  86. data/src/entityjs/media/playlist.js +5 -0
  87. data/src/entityjs/media/sound.js +110 -0
  88. data/src/entityjs/net/socket.js +52 -0
  89. data/src/entityjs/pattern/arraymap.js +89 -0
  90. data/src/entityjs/pattern/flicker.js +214 -0
  91. data/src/entityjs/pattern/timestep.js +34 -0
  92. data/src/entityjs/save/database.js +7 -0
  93. data/src/entityjs/save/storage.js +57 -0
  94. data/src/entityjs/util/log.js +25 -0
  95. data/src/entityjs/util/polyfill.js +25 -0
  96. data/src/entityjs/util/random.js +38 -0
  97. data/src/entityjs/util/scene.js +101 -0
  98. data/src/entityjs/util/sheet.js +51 -0
  99. data/src/entityjs/util/support.js +132 -0
  100. metadata +156 -0
@@ -0,0 +1,516 @@
1
+ (function(re){
2
+
3
+ /*
4
+ Main function for re.e
5
+
6
+ //create multiple entities
7
+ re.e('spider', 10)
8
+ //returns a query with all entities
9
+ .each(function(index){
10
+ this.posX = index * 10;
11
+ });
12
+
13
+ */
14
+ var q = function(c, count){
15
+ if(!count){
16
+ return new re.entity.init(c);
17
+ }
18
+
19
+ //optimize for multiple calls
20
+ var q = re();
21
+
22
+ //create entity by number of count
23
+ for(var i=0; i<count; i++){
24
+ q._e.push(re.e(c));
25
+ }
26
+
27
+ return q;
28
+ };
29
+
30
+ q.id = 0;
31
+
32
+ var e = function(c){
33
+
34
+ this._re_comps = [];
35
+ this._re_signals = {};
36
+
37
+ this.id = q.id+'';
38
+
39
+ q.id++;
40
+
41
+ re._e.push(this);
42
+
43
+ this.addComp(c);
44
+ };
45
+
46
+ var p = e.prototype;
47
+
48
+ p.id = '';
49
+
50
+ p.addComp = function(com){
51
+
52
+ this._re_comp(com);
53
+
54
+ //check implement
55
+ if(this._re_interface){
56
+
57
+ for(var i in this._re_interface){
58
+ if(!this.hasOwnProperty(this._re_interface[i])){
59
+ throw 'implementation: '+this._re_interface[i]+' missing';
60
+ }
61
+ }
62
+
63
+ }
64
+
65
+ //check asserts
66
+ if(this._re_asserts){
67
+ for(var t in this._re_asserts){
68
+ if(this._re_comps.indexOf(c._re_asserts[t]) != -1){
69
+ throw 'assert: '+c.name+' cannot be coupled with '+c._re_asserts[t];
70
+ }
71
+ }
72
+ }
73
+
74
+ return this;
75
+ };
76
+
77
+ p.removeComp = function(com){
78
+
79
+ var pieces;
80
+
81
+ //handle string or array?
82
+ if(typeof com == 'object'){
83
+ pieces = com;
84
+
85
+ com = com[0];
86
+ } else {
87
+ pieces = com.split(' ');
88
+ }
89
+
90
+ if(pieces.length > 1){
91
+
92
+ for(var k in pieces){
93
+ this._re_comp(pieces[k]);
94
+ }
95
+
96
+ return this;
97
+ }
98
+
99
+ var c = re._c[com];
100
+
101
+ if(!this.has(com)) return this;
102
+
103
+ //remove from array
104
+ this._re_comps.splice(this._re_comps.indexOf(com), 1);
105
+
106
+ //only remove if it exists
107
+ if(c){
108
+
109
+ if(c._re_dispose){
110
+ c._re_dispose.call(this, c);
111
+ }
112
+
113
+ c.signal('dispose', this);
114
+
115
+ }
116
+ };
117
+
118
+ /*
119
+ //add components
120
+ this.comp('point text');
121
+
122
+ //add health component with 100 health
123
+ this.comp('health:100 physics');
124
+
125
+ //remove components
126
+ this.comp('-point');
127
+ */
128
+ /*p.comp = function(com){
129
+
130
+ this._re_comp(com);
131
+
132
+ //check implement
133
+ if(this._re_interface){
134
+
135
+ for(var i in this._re_interface){
136
+ if(!this.hasOwnProperty(this._re_interface[i])){
137
+ throw 'implementation: '+this._re_interface[i]+' missing';
138
+ }
139
+ }
140
+
141
+ }
142
+
143
+ //check asserts
144
+ if(this._re_asserts){
145
+ for(var t in this._re_asserts){
146
+ if(this._re_comps.indexOf(c._re_asserts[t]) != -1){
147
+ throw 'assert: '+c.name+' cannot be coupled with '+c._re_asserts[t];
148
+ }
149
+ }
150
+ }
151
+
152
+ return this;
153
+ }*/
154
+
155
+ p._re_comp = function(com){
156
+ if(!com) return this;
157
+
158
+ //split a multi word string into smaller one word function calls
159
+ var pieces;
160
+
161
+ //handle array or string?
162
+ if(typeof com == 'object'){
163
+ pieces = com;
164
+ //set in case length is 1
165
+ com = com[0];
166
+ } else {
167
+ pieces = com.split(' ');
168
+ }
169
+
170
+ if(pieces.length > 1){
171
+ for(var k in pieces){
172
+ this._re_comp(pieces[k]);
173
+ }
174
+
175
+ return this;
176
+ }
177
+
178
+ //component reference
179
+ var c;
180
+
181
+ var vals = com.split(':');
182
+
183
+ com = vals[0];
184
+
185
+ //add component
186
+ c = re._c[com];
187
+
188
+ //swap values
189
+ vals[0] = c;
190
+
191
+ //if already has component
192
+ if(this.has(com)) return this;
193
+
194
+ //add comp first thing, to avoid dupe requirement calls
195
+ //and this lets the init remove the comp too.
196
+ this._re_comps.push(com);
197
+
198
+ //init component only if it exists
199
+ if(c){
200
+ this._re_comp(c._re_requires);
201
+
202
+ //add interface of component
203
+ if(c._re_implements){
204
+ if(!this._re_implements){
205
+ this._re_implements = [];
206
+ }
207
+ this._re_implements = this._re_implements.concat(c._re_implements);
208
+ }
209
+
210
+ if(c._re_asserts){
211
+ if(!this._re_asserts){
212
+ this._re_asserts = [];
213
+ }
214
+ this._re_asserts = this._re_asserts.concat(c._re_asserts);
215
+ }
216
+
217
+ if(c._re_inherits){
218
+ this.inherit(c._re_inherits);
219
+ }
220
+
221
+ if(c._re_extends){
222
+ this.extend(c._re_extends);
223
+ }
224
+
225
+ if(c._re_init){
226
+ c._re_init.apply(this, vals);
227
+ }
228
+
229
+ c.signal('init', this);
230
+ }
231
+
232
+
233
+
234
+
235
+ return this;
236
+ }
237
+
238
+ /*
239
+ Returns component array
240
+ */
241
+ p.getComps = function(){
242
+ return this._re_comps.slice();
243
+ }
244
+
245
+ p.clone = function(count){
246
+ return re.e(this._re_comps, count);
247
+ }
248
+
249
+ /*
250
+ Calls methods of parent components.
251
+
252
+ Use '' to call entities components when overriding
253
+ */
254
+ p.parent = function(comp, method){
255
+
256
+ var a = Array.prototype.slice.call(arguments, 2);
257
+
258
+ if(comp == ''){
259
+ //call entity parent methods
260
+ re.e.init[method].apply(this, a);
261
+ }
262
+
263
+ var c = re._c[comp];
264
+
265
+ if(c._re_extends[method]){
266
+ return c._re_extends[method].apply(this, a);
267
+ }
268
+
269
+ if(c._re_inherits[method]){
270
+ return c._re_inherits[method].apply(this, a);
271
+ }
272
+
273
+ return this;
274
+ }
275
+
276
+ /*
277
+ TODO extend has to multiple item query
278
+
279
+ //returns true if both present
280
+ this.has('draw update');
281
+
282
+ //return true if bitmap not present but has update
283
+ this.has('update -bitmap');
284
+
285
+ //returns true if has asset id and update signal
286
+ this.has('#asset ^update');
287
+
288
+ //expanded
289
+ this.has({
290
+ 'comp':['draw'],
291
+ 'id':'bob',
292
+ 'signal':['draw'],
293
+ 'not':['update']
294
+ });
295
+ */
296
+ p.has = function(comp){
297
+
298
+ if(typeof comp == 'string'){
299
+
300
+ comp = re.query._toObj(comp);
301
+ }
302
+
303
+ comp.comp = comp.comp || [];
304
+ comp.id = comp.id || '';
305
+ comp.signal = comp.signal || [];
306
+ comp.not = comp.not || [];
307
+
308
+ //check if entitiy contains the correct components
309
+ for(p=0; p<comp.comp.length; p++){
310
+
311
+ //check if not containing components
312
+ if(this._re_comps.indexOf(comp.comp[p]) == -1){
313
+ return false;
314
+ }
315
+ }
316
+
317
+ //check if entity doesn't contain components
318
+ for(p=0; p<comp.not.length; p++){
319
+ if(this._re_comps.indexOf(comp.not[p]) != -1){
320
+ return false;
321
+ }
322
+ }
323
+
324
+ var s;
325
+ //check if entity contains signals
326
+ for(p=0; p<comp.signal.length; p++){
327
+ s = comp.signal[p];
328
+ if(!this._re_signals[s] || this._re_signals[s].length == 0){
329
+ return false;
330
+ }
331
+ }
332
+
333
+ if(comp.id != '' && this.id != comp.id){
334
+ return false;
335
+ }
336
+
337
+
338
+ return true;
339
+ };
340
+
341
+ /*
342
+ New way to add signals version 0.2.1.
343
+
344
+ //single
345
+ addSignal('draw', function(){});
346
+
347
+ //multiple
348
+ addSignal({
349
+
350
+ draw:function(){},
351
+
352
+ update:function(){}
353
+
354
+ });
355
+ */
356
+ p.addSignal = function(type, method){
357
+
358
+ if(typeof type == 'object'){
359
+
360
+ for(var k in type){
361
+ this.addSignal(k, type[k]);
362
+ }
363
+
364
+ } else {
365
+
366
+ if(!this._re_signals[type]){
367
+ this._re_signals[type] = [];
368
+ }
369
+
370
+ this._re_signals[type].push(method);
371
+
372
+ }
373
+
374
+ return this;
375
+ };
376
+
377
+ /*
378
+ Added in V0.2.1
379
+
380
+ //remove single
381
+ removeSignal('draw', this.draw);
382
+
383
+ //remove multiple
384
+ removeSignal({
385
+
386
+ draw:this.draw,
387
+ update:this.update
388
+
389
+ });
390
+ */
391
+ p.removeSignal = function(type, method){
392
+
393
+ if(typeof type == 'object'){
394
+
395
+ for(var k in type){
396
+ this.removeSignal(k, type[k]);
397
+ }
398
+
399
+ } else {
400
+
401
+ if(typeof method == 'function'){
402
+
403
+ for(var k in this._re_signals[type]){
404
+
405
+ if(this._re_signals[type][k] == method){
406
+ this._re_signals[type].splice(k, 1);
407
+ }
408
+
409
+ }
410
+ } else {
411
+
412
+ //no method was passed. Remove all signals
413
+ this._re_signals[type] = [];
414
+
415
+ }
416
+ }
417
+
418
+ return this;
419
+ };
420
+
421
+ /*
422
+ Signal dispatches events to entities.
423
+ Modified V0.2.1
424
+
425
+
426
+ -dispatch signals
427
+ this.signal('click');
428
+ this.signal('click draw');
429
+ this.signal('click', {data:0});
430
+
431
+ */
432
+ p.signal = function(type){
433
+
434
+ if(!this._re_signals[type]) return this;
435
+ var b;
436
+
437
+ for(var i=0, l = this._re_signals[type].length; i<l; i++){
438
+
439
+ b = this._re_signals[type];
440
+
441
+ if(!b[i]) continue;
442
+
443
+ //return false remove?
444
+ if(b[i].apply( (b[i].c)?b[i].c : this , Array.prototype.slice.call(arguments, 1)) === false){
445
+ b.splice(i, 1);
446
+ }
447
+
448
+ }
449
+
450
+ return this;
451
+ };
452
+
453
+ p.extend = function(obj, value){
454
+ var a = typeof obj;
455
+ if(a == 'object'){
456
+
457
+ for(var key in obj){
458
+ if(!obj.hasOwnProperty(key)) continue;
459
+
460
+ this.extend(key, obj[key]);
461
+ }
462
+
463
+ }else {
464
+ //extend property
465
+
466
+ this[obj] = value;
467
+ }
468
+
469
+ return this;
470
+ }
471
+
472
+ p.inherit = function(obj, value){
473
+
474
+ if(typeof obj == 'object'){
475
+
476
+ for(var key in obj){
477
+ if(!obj.hasOwnProperty(key)) continue;
478
+
479
+ this.inherit(key, obj[key]);
480
+
481
+ }
482
+
483
+ } else {
484
+ //extend property
485
+
486
+ if(!this.hasOwnProperty(obj) || typeof this[obj] != typeof value){
487
+
488
+ this[obj] = value;
489
+
490
+ }
491
+ }
492
+
493
+ return this;
494
+ }
495
+
496
+ p.dispose = function(){
497
+ //delete from global array
498
+ re._e.splice(re._e.indexOf(this), 1);
499
+
500
+ for(var i in this._re_comps){
501
+ var k = re.c(this._re_comps[i]);
502
+ if(k._re_dispose){
503
+ k._re_dispose.call(this, k);
504
+ }
505
+ k.signal('dispose', this);
506
+ }
507
+
508
+ this.signal('dispose');
509
+
510
+ return this;
511
+ }
512
+
513
+ re.entity = re.e = q;
514
+ re.entity.init = e;
515
+
516
+ }(re));
@@ -0,0 +1,224 @@
1
+ (function(re){
2
+
3
+ var b = function(assets){
4
+ return new re.load.init(assets);
5
+ }
6
+
7
+ b.path = "";
8
+
9
+ b.imageExt = 'img';
10
+ b.soundExt = 'sfx';
11
+ b.images = ['gif', 'jpg', 'jpeg', 'png'];
12
+ b.sounds = ['wav', 'mp3', 'aac', 'ogg'];
13
+
14
+ /*
15
+ Loads images, sounds and other files into components.
16
+
17
+ All loaded assets will be put into a component with a ref to the asset.
18
+
19
+ //example of loading assets
20
+ re.load('tiles.png add.js attack.mp3')
21
+ .complete(function(arrayOfAssets){
22
+ //create new bitmap of tiles.png
23
+ re.e('bitmap tiles.png');
24
+
25
+ //new sound
26
+ re.e('sound attack.mp3');
27
+
28
+ //access image staticaly or localy
29
+ re.comp('tiles.png').image;
30
+ re.entity('tiles.png').image;
31
+
32
+ })
33
+ .error(function(assetThatCausedError){
34
+ //error
35
+ })
36
+ .progress(function(current, total, assetLoaded){
37
+ //called on loads
38
+ });
39
+
40
+ @warning only supports loading images and sounds
41
+
42
+ //load sound
43
+
44
+ //re.support will return the supported codec
45
+ re.load('run.'+re.support('ogg', 'aac'));
46
+
47
+ FUTURE remove directories from calls
48
+
49
+ */
50
+ var l = function(assets){
51
+
52
+ if(typeof assets == 'string'){
53
+ this.assets = assets.split(' ');
54
+ } else {
55
+ this.assets = assets;
56
+ }
57
+
58
+ var a;
59
+ for(var i=0; i<this.assets.length; i++){
60
+
61
+ this.total++;
62
+
63
+ a = this.assets[i];
64
+
65
+ //find file extension
66
+ var j = a.lastIndexOf('.')+1;
67
+ var ext = a.substr(j).toLowerCase();
68
+
69
+ //copy full source path
70
+ var s = a;
71
+
72
+ //remove directories
73
+ var d = a.lastIndexOf('/');
74
+ if(d != -1){
75
+ a = a.substr(d+1, a.length);
76
+ }
77
+
78
+ //find name
79
+ var n = a.substr(0, j);
80
+
81
+ if(re.load.images.indexOf(ext) != -1){
82
+
83
+ this._loadImg(s, a, n);
84
+
85
+ } else if(re.load.sounds.indexOf(ext) != -1){
86
+
87
+ //check if support component exists first
88
+ if(!re.support || re.support(ext)){
89
+ this._loadSound(s, a, n);
90
+ }
91
+
92
+ }
93
+
94
+
95
+ }
96
+
97
+ return this;
98
+ }
99
+
100
+ var p = l.prototype;
101
+
102
+ p.current = 0;
103
+ p.total = 0;
104
+
105
+ p._loadImg = function(src, a, n){
106
+
107
+ var that = this;
108
+ var img = new Image();
109
+
110
+ //create new image component
111
+ re.c(a)
112
+ .alias(n+re.load.imgExt)
113
+ .global({
114
+ bitmap:img
115
+ })
116
+ .extend({
117
+ //save image for other components to copy or use
118
+ bitmap:img
119
+ });
120
+
121
+ img.onload = function(){
122
+
123
+ re.c(a).extend({
124
+
125
+ bisect:img.width,
126
+ sizeX:img.width,
127
+ sizeY:img.height
128
+ });
129
+
130
+ that.current++;
131
+
132
+ if(that._p){
133
+ that._p.call(that, that.current, that.total, a);
134
+ }
135
+
136
+ if(that.current >= that.total){
137
+
138
+ if(that._s){
139
+ that._s.call(that, that.assets);
140
+ }
141
+
142
+ }
143
+ };
144
+
145
+ img.onerror = function(){
146
+
147
+ if(that._e){
148
+ that._e.call(that, a);
149
+ }
150
+
151
+ };
152
+
153
+ img.src = re.load.path+src;
154
+
155
+ return this;
156
+ }
157
+
158
+ p._loadSound = function(src, a, n){
159
+ var that = this;
160
+
161
+ var s = new Audio(re.load.path+src);
162
+ s.preload = "auto";
163
+ s.load();
164
+
165
+ re.c(a)
166
+ //create global codec for easy use
167
+ .alias(n+re.load.soundExt)
168
+ .global({
169
+ sound:s
170
+ })
171
+ .extend({
172
+ sound:s
173
+ });
174
+
175
+ s.addEventListener('canplaythrough',function(){
176
+ that.current++;
177
+
178
+ if(that._p){
179
+ that._p.call(that, that.current, that.total, a);
180
+ }
181
+
182
+ if(that.current >= that.total){
183
+ if(that._s){
184
+ that._s.call(that, that.assets);
185
+ }
186
+
187
+ }
188
+
189
+ },false);
190
+
191
+ s.addEventListener('error',function(){
192
+
193
+ if(that._e){
194
+ that._e.call(that, a);
195
+ }
196
+ },false);
197
+
198
+ }
199
+
200
+ p.progress = function(m){
201
+
202
+ this._p = m;
203
+
204
+ return this;
205
+ }
206
+
207
+ p.complete = function(m){
208
+
209
+ this._s = m;
210
+
211
+ return this;
212
+ }
213
+
214
+ p.error = function(m){
215
+
216
+ this._e = m;
217
+
218
+ return this;
219
+ }
220
+
221
+ re.load = b;
222
+ re.load.init = l;
223
+
224
+ }(re));