entityjs 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. data/.gitignore +1 -0
  2. data/bin/entityjs +1 -1
  3. data/entityjs.gemspec +3 -2
  4. data/lib/entityjs.rb +13 -1
  5. data/lib/entityjs/command.rb +22 -16
  6. data/lib/entityjs/commands/build.rb +96 -48
  7. data/lib/entityjs/commands/eunit.rb +1 -1
  8. data/lib/entityjs/commands/html.rb +31 -0
  9. data/lib/entityjs/commands/min.rb +37 -0
  10. data/lib/entityjs/commands/new.rb +3 -0
  11. data/lib/entityjs/commands/server.rb +9 -3
  12. data/lib/entityjs/config.rb +163 -59
  13. data/lib/entityjs/dirc.rb +28 -0
  14. data/lib/entityjs/page.rb +73 -34
  15. data/lib/entityjs/version.rb +1 -1
  16. data/public/favicon.ico +0 -0
  17. data/public/play.html +33 -28
  18. data/public/qunit/qunit.entity.js +56 -6
  19. data/public/qunit/qunit.input.js +5 -5
  20. data/public/test.html +49 -0
  21. data/spec/javascripts/src/core/comp_spec.js +17 -6
  22. data/spec/javascripts/src/core/entity_spec.js +4 -5
  23. data/spec/javascripts/src/core/query_spec.js +25 -0
  24. data/spec/javascripts/src/core/re_spec.js +3 -3
  25. data/spec/javascripts/src/cycle/update_spec.js +8 -8
  26. data/spec/javascripts/src/display/align_spec.js +2 -2
  27. data/spec/javascripts/src/display/el_spec.js +17 -0
  28. data/spec/javascripts/src/input/keyboard_spec.js +4 -4
  29. data/spec/javascripts/src/input/mouse_spec.js +8 -7
  30. data/spec/javascripts/src/math/random_spec.js +8 -0
  31. data/spec/javascripts/src/util/clone_spec.js +20 -0
  32. data/spec/lib/entityjs/commands/build_spec.rb +3 -8
  33. data/spec/lib/entityjs/commands/html_spec.rb +27 -0
  34. data/spec/lib/entityjs/commands/min_spec.rb +30 -0
  35. data/spec/lib/entityjs/config_spec.rb +6 -0
  36. data/spec/lib/entityjs/page_spec.rb +4 -4
  37. data/src/core/comp.js +87 -31
  38. data/src/core/entity.js +38 -43
  39. data/src/core/load.js +2 -8
  40. data/src/core/query.js +17 -5
  41. data/src/core/re.js +1 -1
  42. data/src/core/system.js +8 -11
  43. data/src/cycle/drawlist.js +11 -11
  44. data/src/cycle/update.js +5 -5
  45. data/src/display/align.js +10 -3
  46. data/src/display/animate.js +26 -12
  47. data/src/display/el.js +99 -0
  48. data/src/input/keyboard.js +5 -5
  49. data/src/input/mouse.js +16 -11
  50. data/src/math/bisect.js +1 -1
  51. data/src/math/force.js +4 -4
  52. data/src/math/hitmap.js +2 -1
  53. data/src/math/iso.js +1 -1
  54. data/src/math/random.js +17 -2
  55. data/src/math/tile.js +1 -1
  56. data/src/media/sound.js +1 -1
  57. data/src/pattern/pathfind.js +2 -4
  58. data/src/save/storage.js +1 -1
  59. data/src/util/clone.js +12 -0
  60. data/src/util/scene.js +21 -16
  61. data/templates/circle/game.json +16 -0
  62. data/templates/circle/scripts/displays/circle.js +40 -0
  63. data/templates/circle/scripts/scenes/home.js +8 -17
  64. data/templates/circle/tests/scenes/home_test.js +3 -11
  65. data/templates/dom/game.json +6 -0
  66. data/templates/dom/scripts/els/btn.js +5 -0
  67. data/templates/dom/scripts/els/contain.js +5 -0
  68. data/templates/dom/scripts/els/header.js +5 -0
  69. data/templates/{animation → dom}/scripts/init.js +0 -0
  70. data/templates/dom/scripts/scenes/home.js +22 -0
  71. data/templates/{animation → dom}/scripts/scenes/load.js +0 -0
  72. data/templates/dom/tests/scenes/home_test.js +9 -0
  73. data/templates/{animation → dom}/tests/scenes/load_test.js +0 -0
  74. data/templates/isometric/game.json +16 -0
  75. data/templates/platform/game.json +18 -0
  76. data/templates/platform/scripts/displays/hero.js +1 -1
  77. data/templates/platform/scripts/items/coin.js +1 -1
  78. data/templates/platform/scripts/items/spring.js +1 -1
  79. data/templates/platform/tests/displays/hero_test.js +4 -4
  80. data/templates/platform/tests/factories.js +1 -1
  81. data/templates/platform/tests/items/coin_test.js +1 -1
  82. data/templates/pong/game.json +16 -0
  83. data/templates/tiltmaze/game.json +16 -0
  84. data/templates/tiltmaze/scripts/structs/level.js +1 -1
  85. data/templates/tiltmaze/scripts/tiles/walltile.js +1 -1
  86. metadata +45 -32
  87. data/.rspec +0 -2
  88. data/public/tests.html +0 -31
  89. data/src/cycle/worker.js +0 -9
  90. data/templates/animation/assets/images/hero.png +0 -0
  91. data/templates/animation/scripts/scenes/home.js +0 -41
  92. data/templates/animation/tests/init_test.js +0 -4
  93. data/templates/animation/tests/scenes/home_test.js +0 -15
  94. data/templates/arrow_keys/assets/images/arrow.png +0 -0
  95. data/templates/arrow_keys/config.yml +0 -22
  96. data/templates/arrow_keys/readme.txt +0 -9
  97. data/templates/arrow_keys/scripts/displays/arrow.js +0 -69
  98. data/templates/arrow_keys/scripts/init.js +0 -10
  99. data/templates/arrow_keys/scripts/inputs/controls.js +0 -35
  100. data/templates/arrow_keys/scripts/scenes/home.js +0 -20
  101. data/templates/arrow_keys/scripts/scenes/load.js +0 -57
  102. data/templates/arrow_keys/tests/displays/arrow_test.js +0 -29
  103. data/templates/arrow_keys/tests/init_test.js +0 -4
  104. data/templates/arrow_keys/tests/inputs/controls_test.js +0 -32
  105. data/templates/arrow_keys/tests/scenes/home_test.js +0 -0
  106. data/templates/arrow_keys/tests/scenes/load_test.js +0 -18
  107. data/templates/circle/config.yml +0 -22
  108. data/templates/isometric/config.yml +0 -22
  109. data/templates/platform/config.yml +0 -23
  110. data/templates/pong/config.yml +0 -22
  111. data/templates/tiltmaze/config.yml +0 -25
@@ -11,9 +11,14 @@
11
11
  });
12
12
 
13
13
  */
14
- var q = function(c, count){
14
+ var q = function(c, count, alone){
15
15
  if(!count){
16
- return new re.entity.init(c);
16
+ var r = new re.entity.init(c);
17
+
18
+ if(!alone){
19
+ re._e.push(r);
20
+ }
21
+ return r;
17
22
  }
18
23
 
19
24
  //optimize for multiple calls
@@ -21,42 +26,32 @@
21
26
 
22
27
  //create entity by number of count
23
28
  for(var i=0; i<count; i++){
24
- q.push(re.e(c));
29
+ q.push(re.e(c, 0, alone));
25
30
  }
26
31
 
27
32
  return q;
28
33
  };
29
34
 
30
- q.id = 0;
31
-
32
35
  var e = function(c){
33
36
 
34
37
  this._re_comps = [];
35
- this._re_signals = {};
36
-
37
- this.id = q.id+'';
38
-
39
- q.id++;
40
-
41
- re._e.push(this);
38
+ this._re_listens = {};
42
39
 
43
40
  this.comp(c);
44
41
  };
45
42
 
46
43
  var p = e.prototype;
47
44
 
48
- p.id = '';
49
-
50
45
  p.comp = function(com){
51
46
 
52
47
  this._re_comp(com);
53
48
 
54
49
  //check implement
55
- if(this._re_implements){
50
+ if(this._re_interfaces){
56
51
 
57
- for(var i in this._re_implements){
58
- if(!this.hasOwnProperty(this._re_implements[i])){
59
- throw 'Interface: '+this._re_implements[i]+' missing';
52
+ for(var i in this._re_interfaces){
53
+ if(!this.hasOwnProperty(this._re_interfaces[i])){
54
+ throw 'Interface: '+this._re_interfaces[i]+' missing';
60
55
  }
61
56
  }
62
57
 
@@ -155,16 +150,17 @@
155
150
  //component reference
156
151
  var c;
157
152
 
153
+ //will be sent to init function
158
154
  var vals = com.split(':');
159
155
 
160
156
  com = vals[0];
161
157
 
158
+ //remove comp string
159
+ vals.shift();
160
+
162
161
  //add component
163
162
  c = re._c[com];
164
-
165
- //swap values
166
- vals[0] = c;
167
-
163
+
168
164
  //if already has component
169
165
  if(!this.has(com)){
170
166
 
@@ -177,11 +173,11 @@
177
173
  this._re_comp(c._re_requires);
178
174
 
179
175
  //add interface of component
180
- if(c._re_implements){
181
- if(!this._re_implements){
182
- this._re_implements = [];
176
+ if(c._re_interfaces){
177
+ if(!this._re_interfaces){
178
+ this._re_interfaces = [];
183
179
  }
184
- this._re_implements = this._re_implements.concat(c._re_implements);
180
+ this._re_interfaces = this._re_interfaces.concat(c._re_interfaces);
185
181
  }
186
182
 
187
183
  if(c._re_asserts){
@@ -191,8 +187,8 @@
191
187
  this._re_asserts = this._re_asserts.concat(c._re_asserts);
192
188
  }
193
189
 
194
- if(c._re_inherits){
195
- this.def(c._re_inherits);
190
+ if(c._re_defaults){
191
+ this.def(c._re_defaults);
196
192
  }
197
193
 
198
194
  if(c._re_defines){
@@ -223,8 +219,8 @@
223
219
  return this._re_comps.slice();
224
220
  }
225
221
 
226
- p.clone = function(count){
227
- return re.e(this._re_comps, count);
222
+ p.clone = function(count, a){
223
+ return re.e(this._re_comps, count, a);
228
224
  }
229
225
 
230
226
  /*
@@ -251,7 +247,7 @@
251
247
  return c._re_defines[method].apply(this, a);
252
248
  }
253
249
 
254
- return c._re_inherits[method].apply(this, a);
250
+ return c._re_defaults[method].apply(this, a);
255
251
  }
256
252
 
257
253
  /*
@@ -306,7 +302,7 @@
306
302
  //check if entity contains signals
307
303
  for(p=0; p<comp.on.length; p++){
308
304
  s = comp.on[p];
309
- if(!this._re_signals[s] || this._re_signals[s].length == 0){
305
+ if(!this._re_listens[s] || this._re_listens[s].length == 0){
310
306
  return false;
311
307
  }
312
308
  }
@@ -343,12 +339,12 @@
343
339
 
344
340
  } else {
345
341
 
346
- if(!this._re_signals[type]){
347
- this._re_signals[type] = [];
342
+ if(!this._re_listens[type]){
343
+ this._re_listens[type] = [];
348
344
  }
349
345
  if(!re.is(method)) throw 'Method is null'
350
346
  //save context
351
- this._re_signals[type].push({c:context || this, f:method});
347
+ this._re_listens[type].push({c:context || this, f:method});
352
348
 
353
349
  }
354
350
 
@@ -385,7 +381,7 @@
385
381
  } else {
386
382
 
387
383
  if(method){
388
- var i = this._re_signals[type];
384
+ var i = this._re_listens[type];
389
385
  for(var k in i){
390
386
 
391
387
  if(i[k].f == method){
@@ -396,11 +392,11 @@
396
392
  } else if(type){
397
393
 
398
394
  //no method was passed. Remove all signals
399
- this._re_signals[type] = [];
395
+ this._re_listens[type] = [];
400
396
 
401
397
  } else {
402
398
  //remove all signals
403
- this._re_signals = {};
399
+ this._re_listens = {};
404
400
  }
405
401
  }
406
402
 
@@ -417,12 +413,12 @@
417
413
  */
418
414
  p.trigger = function(type){
419
415
 
420
- if(!this._re_signals[type]) return this;
416
+ if(!this._re_listens[type]) return this;
421
417
  var b;
422
418
 
423
- for(var i=0, l = this._re_signals[type].length; i<l; i++){
419
+ for(var i=0, l = this._re_listens[type].length; i<l; i++){
424
420
 
425
- b = this._re_signals[type];
421
+ b = this._re_listens[type];
426
422
 
427
423
  if(!b) break;
428
424
  if(!b[i]) continue;
@@ -465,8 +461,7 @@
465
461
  };
466
462
 
467
463
  p.get = function(v){
468
- var l = this[v];
469
- return (re.is(l,'function'))? l() : l;
464
+ return (re.is(this[v], 'function'))? this[v]() : this[v];
470
465
  };
471
466
 
472
467
  p.def = function(obj, value){
@@ -131,13 +131,10 @@
131
131
  //create new image component
132
132
  re.c(a)
133
133
  .alias(n+re.load.imageExt)
134
- .statics({
135
- image:img
136
- })
137
134
  .defines({
138
135
  //save image for other components to copy or use
139
136
  _image:img
140
- });
137
+ }).image = img;
141
138
 
142
139
  img.onload = function(){
143
140
  re.c(a).defines({
@@ -236,12 +233,9 @@
236
233
  re.c(a)
237
234
  //create statics codec for easy use
238
235
  .alias(n+re.load.soundExt)
239
- .statics({
240
- sound:s
241
- })
242
236
  .defines({
243
237
  _sound:s
244
- });
238
+ }).sound = s;
245
239
  }
246
240
 
247
241
  p.progress = function(m){
@@ -63,6 +63,7 @@
63
63
  Sort a query string into objects
64
64
  */
65
65
  q._toObj = function(query){
66
+ //check if its caught
66
67
  if(q.c[query]){
67
68
  return q.c[query];
68
69
  }
@@ -99,6 +100,7 @@
99
100
  id:id
100
101
  };
101
102
 
103
+ //catch it
102
104
  q.c[query] = comp;
103
105
 
104
106
  return comp;
@@ -168,7 +170,7 @@
168
170
  while(++i < l && (e = this[i]) != null && m.call(context || this, e, i, this) !== false);
169
171
 
170
172
  return this;
171
- }
173
+ };
172
174
 
173
175
  /*
174
176
  The map method allows multidimensional loops.
@@ -371,23 +373,33 @@
371
373
  return re(Array.prototype.filter.apply(this, arguments));
372
374
  }
373
375
 
376
+ //Removes all elements in the array that pass the truth test
377
+ p.reject = function(it, c){
378
+ for(var i=0; i<this.length; i++){
379
+ if(it.call(c || this, this[i], i, this)){
380
+ this.splice(i, 1);
381
+ }
382
+ }
383
+ return this;
384
+ };
385
+
374
386
  /*
375
387
  Finds first entity with components
376
388
 
377
389
  re('draw').findWith('circle !red');
378
390
 
379
391
  */
380
- p.findWith = function(comps, c){
392
+ p.findWith = function(comps){
381
393
  return this.find(function(e){
382
394
  return e.has(comps);
383
- }, c);
395
+ });
384
396
  }
385
397
 
386
398
  /*
387
399
  Creates a new entity and pushes it into the array.
388
400
  */
389
- p.e = function(comps, count){
390
- var e = re.e(comps, count);
401
+ p.e = function(comps, count, alone){
402
+ var e = re.e(comps, count, alone);
391
403
  if(count){
392
404
  //add all in query
393
405
  for(var i in e){
@@ -11,7 +11,7 @@ re = function(selector){
11
11
  };
12
12
 
13
13
  //automatically filled when compiled
14
- re.version = "$VERSION";
14
+ re.version = "RE_VERSION";
15
15
 
16
16
  //contains all entities
17
17
  re._e = [];
@@ -88,14 +88,14 @@ re.c('system')
88
88
  this.sizeY = s.sizeY = this.canvas.height;
89
89
 
90
90
  //init listeners
91
- if(re._c.keyboard){
92
- re._c.keyboard.i();
91
+ if(re.keyboard){
92
+ re.keyboard.i();
93
93
  }
94
- if(re._c.mouse){
95
- re._c.mouse.i();
94
+ if(re.mouse){
95
+ re.mouse.i();
96
96
  }
97
- if(re._c.touch){
98
- re._c.touch.i();
97
+ if(re.touch){
98
+ re.touch.i();
99
99
  }
100
100
  this.system_loop = this.defaultLoop;
101
101
  this.second = this.stepSize * 30;
@@ -119,7 +119,7 @@ re.c('system')
119
119
  },
120
120
 
121
121
  update:function(){
122
- re._c.update.update(this.stepSize);
122
+ re.update.update(this.stepSize);
123
123
  },
124
124
 
125
125
  draw:function(){
@@ -129,9 +129,6 @@ re.c('system')
129
129
 
130
130
 
131
131
  })
132
- .run(function(){
133
132
 
134
133
  //create default system
135
- re.system = re.sys = re.e('system');
136
-
137
- });
134
+ re.system = re.sys = re.e('system');
@@ -10,20 +10,20 @@ For example:
10
10
  In a case of an isometric game, certain tiles should appear behind or in
11
11
 
12
12
  */
13
- re.drawlist = function(name){
14
- var d = re.c('drawlist'), name = name || '';
13
+
14
+ re.c('drawlist')
15
+ .method(function(name){
16
+ var name = name || '';
15
17
 
16
18
  //name default to '' and returns default drawlist
17
19
 
18
- if(!d._lists[name]){
20
+ if(!re.drawlist._lists[name]){
19
21
  //add new list
20
22
  re.e('drawlist:'+name);
21
23
  }
22
24
 
23
- return d._lists[name];
24
- };
25
-
26
- re.c('drawlist')
25
+ return re.drawlist._lists[name];
26
+ })
27
27
  .statics({
28
28
  _lists:{}
29
29
  })
@@ -69,11 +69,11 @@ re.c('drawlist')
69
69
  }
70
70
 
71
71
  })
72
- .init(function(c, name){
73
- c._lists[name] = this;
72
+ .init(function(name){
73
+ re.drawlist._lists[name] = this;
74
74
  this.listName = name;
75
75
  this.list = re();
76
76
  })
77
- .dispose(function(c){
78
- delete c._lists[this.listName];
77
+ .dispose(function(){
78
+ delete re.drawlist._lists[this.listName];
79
79
  });
@@ -23,7 +23,7 @@ re.c('update')
23
23
  })
24
24
  .defines(function(){
25
25
 
26
- var l = re.c('update').l;
26
+ var l = re.update.l;
27
27
 
28
28
  return {
29
29
 
@@ -78,10 +78,10 @@ re.c('update')
78
78
  };
79
79
 
80
80
  }())
81
- .init(function(c){
82
- c.l.push(this);
81
+ .init(function(){
82
+ re.update.l.push(this);
83
83
  })
84
- .dispose(function(c){
84
+ .dispose(function(){
85
85
 
86
- c.l.splice(c.l.indexOf(this), 1);
86
+ re.update.l.splice(re.update.l.indexOf(this), 1);
87
87
  });
@@ -5,13 +5,20 @@ The align component contains helper methods for positioning entities relative to
5
5
 
6
6
  */
7
7
  re.c('align')
8
- .requires('draw')
8
+ .defaults({
9
+ regX:0,
10
+ regY:0,
11
+ sizeX:1,
12
+ sizeY:1,
13
+ posX:0,
14
+ posY:0
15
+ })
9
16
  .defines({
10
17
 
11
- align:function(x, y){
18
+ align:function(x, y){
12
19
  this.alignHor(x);
13
20
  return this.alignVer(y);
14
- },
21
+ },
15
22
 
16
23
  alignHor:function(o){
17
24
  o = o || 0;
@@ -5,7 +5,7 @@ The animate comp defines a simple interface for animating sprites.
5
5
  var apple = re.e('apple.png animate sprite');
6
6
 
7
7
  //setup animations to play
8
- apple.anis = {
8
+ apple.animates = {
9
9
  //time, frames, loops
10
10
  explode:[1000, [0, 1, 2], 1]
11
11
  //seconds, frames, loops defaults to once
@@ -25,26 +25,40 @@ apple.flickering(); //this comes from flicker comp
25
25
  re.c('animate')
26
26
  .requires('flicker')
27
27
  .defines({
28
-
29
- animate:function(name){
30
- //ignore if calling the same frame
28
+
29
+ //overload flickers method
30
+ flicker_stop:function(){
31
+ this._super('flicker', 'flicker_stop');
32
+ if(this.animate_finish) this.animate_finish();
33
+ },
34
+
35
+ animate:function(name, onFinish, onUpdate){
36
+ //ignore if calling the same animation
31
37
  if(this.flickering() != name){
32
-
33
- var a = this.anis[name] || [];
38
+
39
+ this.animate_finish = onFinish;
40
+ this.animate_update = onUpdate;
41
+
42
+ var a = this.animates[name];
34
43
  //flicker interface
35
44
  //(duration:int, frames:array, loops:int, id:string)
36
45
  this.flicker(a[0], a[1], a[2], name);
37
46
 
38
47
  //only run if a is defined
39
- if(a.length)
40
- this.flicker_run(); //run first frame
48
+ if(a.push)
49
+ this.flicker_run(); //run first frame
41
50
  }
42
51
  return this;
43
52
  },
44
53
 
45
- //implement for flicker
46
- flick:function(c){
47
- this.frame(c);
48
- }
54
+ animating:function(t){
55
+ return this.flickering(t);
56
+ },
57
+
58
+ //implement for flicker
59
+ flick:function(c){
60
+ this.frame(c);
61
+ if(this.animate_update) this.animate_update.apply(this, arguments);
62
+ }
49
63
 
50
64
  });