entityjs 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.gitignore +1 -1
  2. data/lib/entityjs/config.rb +4 -4
  3. data/lib/entityjs/dirc.rb +2 -2
  4. data/lib/entityjs/page.rb +7 -1
  5. data/lib/entityjs/version.rb +1 -1
  6. data/public/qunit/qunit.entity.js +27 -12
  7. data/spec/javascripts/src/core/comp_spec.js +11 -0
  8. data/spec/javascripts/src/core/entity_spec.js +88 -5
  9. data/spec/javascripts/src/core/query_spec.js +196 -15
  10. data/spec/javascripts/src/cycle/drawlist_spec.js +36 -0
  11. data/spec/javascripts/src/cycle/tween_spec.js +1 -1
  12. data/spec/javascripts/src/cycle/wait_spec.js +31 -0
  13. data/spec/javascripts/src/{cycle → display}/draw_spec.js +25 -9
  14. data/spec/javascripts/src/display/sprite_spec.js +18 -0
  15. data/spec/javascripts/src/display/text_spec.js +8 -0
  16. data/spec/javascripts/src/input/mouse_spec.js +24 -36
  17. data/spec/javascripts/src/math/bisect_spec.js +4 -4
  18. data/spec/javascripts/src/math/distance_spec.js +11 -0
  19. data/spec/javascripts/src/math/iso_spec.js +131 -0
  20. data/spec/javascripts/src/math/point_spec.js +3 -3
  21. data/spec/javascripts/src/{util → math}/random_spec.js +0 -0
  22. data/spec/javascripts/src/math/range_spec.js +9 -0
  23. data/spec/javascripts/src/math/tile_spec.js +22 -7
  24. data/spec/javascripts/src/pattern/automap_spec.js +2 -2
  25. data/spec/javascripts/src/pattern/flicker_spec.js +34 -22
  26. data/spec/javascripts/src/pattern/pathfind_spec.js +55 -0
  27. data/src/core/comp.js +16 -16
  28. data/src/core/entity.js +52 -51
  29. data/src/core/load.js +5 -5
  30. data/src/core/query.js +214 -74
  31. data/src/core/system.js +16 -6
  32. data/src/cycle/drawlist.js +79 -0
  33. data/src/cycle/tween.js +7 -20
  34. data/src/cycle/wait.js +10 -19
  35. data/src/{cycle → display}/draw.js +24 -34
  36. data/src/display/image.js +1 -1
  37. data/src/display/screen.js +3 -3
  38. data/src/display/sprite.js +3 -3
  39. data/src/display/text.js +3 -1
  40. data/src/input/mouse.js +35 -12
  41. data/src/math/bisect.js +11 -10
  42. data/src/math/distance.js +7 -0
  43. data/src/math/iso.js +147 -0
  44. data/src/math/point.js +1 -5
  45. data/src/math/random.js +21 -0
  46. data/src/math/range.js +14 -0
  47. data/src/math/tile.js +40 -27
  48. data/src/pattern/automap.js +12 -11
  49. data/src/pattern/flicker.js +87 -135
  50. data/src/pattern/pathfind.js +168 -0
  51. data/src/pattern/timestep.js +4 -1
  52. data/src/util/polyfill.js +1 -1
  53. data/templates/isometric/assets/images/isotiles.png +0 -0
  54. data/templates/isometric/config.yml +22 -0
  55. data/templates/isometric/readme.txt +79 -0
  56. data/templates/isometric/scripts/displays/cursor.js +34 -0
  57. data/templates/isometric/scripts/displays/isoimage.js +32 -0
  58. data/templates/isometric/scripts/init.js +7 -0
  59. data/templates/isometric/scripts/levels/level.js +55 -0
  60. data/templates/isometric/scripts/levels/level1.js +11 -0
  61. data/templates/isometric/scripts/scenes/home.js +10 -0
  62. data/templates/isometric/scripts/scenes/load.js +11 -0
  63. data/templates/isometric/tests/scenes/load_test.js +15 -0
  64. metadata +42 -21
  65. data/src/net/socket.js +0 -52
  66. data/src/util/random.js +0 -24
data/src/core/load.js CHANGED
@@ -140,12 +140,12 @@
140
140
  });
141
141
 
142
142
  img.onload = function(){
143
- re.c(a).defaults({
143
+ re.c(a).defines({
144
144
  sizeX:img.width,
145
- sizeY:img.height
146
- })
147
- .defines('bisect', img.width);
148
-
145
+ sizeY:img.height,
146
+ bisect:img.width
147
+ });
148
+
149
149
  that._loaded();
150
150
  };
151
151
 
data/src/core/query.js CHANGED
@@ -29,7 +29,7 @@
29
29
  //takes values from all objects and returns an array
30
30
  .pluck('width height');
31
31
 
32
- //find all bitmap entities with update bind.
32
+ //find all bitmap entities with update event.
33
33
  re('bitmap ^update');
34
34
 
35
35
  //add color comp to all text components
@@ -49,8 +49,7 @@
49
49
  //calls reset method on all entities
50
50
  .method('reset');
51
51
 
52
- //returns direct reference to first entity with id of player
53
- re('#player').health = 100;
52
+ re('#player')[0].health = 100;
54
53
 
55
54
  */
56
55
  var q = function(selector){
@@ -135,6 +134,8 @@
135
134
  if(select.call(e, i, l)) this.push(e);
136
135
  }
137
136
 
137
+ } else if(re.is(select, 'array')){
138
+ this.push.apply(this, select);
138
139
  }
139
140
 
140
141
  return this;
@@ -143,13 +144,12 @@
143
144
  /*
144
145
  Calls the given method name on all entities.
145
146
 
146
- re('enemies').method('rest');
147
+ re('enemies').invoke('rest');
147
148
  */
148
- p.method = function(m){
149
+ p.invoke = function(m){
149
150
  var b = Array.prototype.slice.call(arguments, 1);
150
- return this.each(function(){
151
-
152
- this[m].apply(this, b);
151
+ return this.each(function(e){
152
+ e[m].apply(e, b);
153
153
  });
154
154
 
155
155
  }
@@ -162,10 +162,10 @@
162
162
 
163
163
  returning false will break the loop
164
164
  */
165
- p.each = function(m){
166
- var l = this.length, i = -1, e;
167
-
168
- while(++i < l && (e = this[i]) && m.call(e, i, l) !== false);
165
+ p.each = function(m, context){
166
+ var l = this.length, i = -1, e;
167
+
168
+ while(++i < l && (e = this[i]) != null && m.call(context || this, e, i, this) !== false);
169
169
 
170
170
  return this;
171
171
  }
@@ -175,9 +175,9 @@
175
175
 
176
176
  //map through and increase y every 3 entities.
177
177
 
178
- re('draw').tilemap(3, function(x, y){
179
- this.x(x * width);
180
- this.y(Y * height);
178
+ re('draw').tilemap(3, function(e, x, y){
179
+ e.x(x * width);
180
+ e.y(Y * height);
181
181
  });
182
182
 
183
183
  //so instead of getting this back
@@ -194,13 +194,13 @@
194
194
  returning false will break the loop
195
195
 
196
196
  */
197
- p.tilemap = function(w, method){
197
+ p.tilemap = function(w, method, context){
198
198
  var x = 0;
199
199
  var y = 0;
200
200
 
201
201
  return this.each(function(i, l){
202
202
 
203
- if(method.call(this[i], x, y, i, l) == false) return false;
203
+ if(method.call(context, this[i], x, y, i, this) == false) return false;
204
204
 
205
205
  x++;
206
206
 
@@ -217,8 +217,8 @@
217
217
  p.comps = function(){
218
218
  var l = [];
219
219
 
220
- this.each(function(){
221
- var k = this.comps();
220
+ this.each(function(e){
221
+ var k = e.comps();
222
222
  for(var i=0; i<k.length; i++){
223
223
  if(l.indexOf(k[i]) == -1){
224
224
  l.push(k[i])
@@ -233,7 +233,48 @@
233
233
  Returns a random entity.
234
234
  */
235
235
  p.random = function(){
236
- return this[~~(Math.random()*this.length)];
236
+ return this[(Math.random()*this.length)|0];
237
+ }
238
+
239
+ p.attr = function(obj, value){
240
+ return this.invoke('attr', obj, value);
241
+
242
+ }
243
+
244
+ p.def = function(obj, value){
245
+ return this.invoke('def', obj, value);
246
+ }
247
+
248
+ p.comp = function(c){
249
+ return this.invoke('comp', c);
250
+
251
+ }
252
+
253
+ p.removeComp = function(c){
254
+ return this.invoke('removeComp', c);
255
+ }
256
+
257
+ p.on = function(type, method){
258
+ return this.invoke('on', type, method);
259
+ }
260
+
261
+ p.off = function(type, method){
262
+ return this.invoke('off', type, method);
263
+ }
264
+
265
+ p.trigger = function(){
266
+ var p = arguments;
267
+ return this.each(function(e){
268
+ e.trigger.apply(e, p);
269
+ });
270
+ }
271
+
272
+ p.has = function(comp){
273
+ //return false if empty
274
+ if(!this.length) return false;
275
+ return this.every(function(e){
276
+ return e.has(comp);
277
+ });
237
278
  }
238
279
 
239
280
  /*
@@ -255,13 +296,12 @@
255
296
 
256
297
  var k = value.split(' ');
257
298
 
258
- this.each(function(){
299
+ this.each(function(e){
259
300
  var o = {};
260
301
 
261
302
  for(var p in k){
262
- if(k.hasOwnProperty(p)){
263
- o[k[p]] = this[k[p]];
264
- }
303
+ var name = k[p];
304
+ o[name] = e[name];
265
305
  }
266
306
 
267
307
  t.push(o);
@@ -271,84 +311,184 @@
271
311
  return t;
272
312
  }
273
313
 
274
- p.defines = function(){
275
- throw 'Deprecated use attr'
314
+ p.isEmpty = function(){
315
+ return !this.length;
276
316
  }
277
317
 
278
- p.attr = function(obj, value){
279
- return this.each(function(){
280
- this.attr(obj,value);
281
- });
318
+ /*
319
+ Returns the first entity that passes the truth iterator method.
320
+
321
+ re('tile').find(function(e){
322
+ return e.tileX() == 0 && e.tileY() == 1;
323
+ });
324
+
325
+ */
326
+ p.find = function(method, context){
327
+ for(var i=0, l=this.length; i<l; i++){
328
+ if(method.call(context || this, this[i], i, this)) return this[i];
329
+ }
330
+
331
+ return null;
332
+ }
333
+
334
+ /*
335
+ Returns the lowest entity from the given iterator.
336
+
337
+ var weakestRat = re('rat').min(function(e){
338
+ return e.health;
339
+ });
340
+
341
+ */
342
+ p.min = function(method, context){
343
+ var lowest = Infinity, val;
344
+ this.each(function(e, i, l){
345
+ var next = method.call(context || this, e, i, l);
346
+ if(next < lowest){
347
+ lowest = next;
348
+ val = e;
349
+ }
282
350
 
351
+ });
352
+
353
+ return val;
283
354
  }
284
355
 
285
- p.defaults = function(){
286
- throw 'Deprecated use def'
356
+ p.max = function(method, context){
357
+ var lowest = -Infinity, val;
358
+ this.each(function(e, i, l){
359
+ var next = method.call(context || this, e, i, l);
360
+ if(next > lowest){
361
+ lowest = next;
362
+ val = e;
363
+ }
364
+ });
365
+
366
+ return val;
287
367
  }
288
368
 
289
- p.def = function(obj, value){
290
- return this.each(function(){
291
- this.def(obj, value);
292
- });
293
-
369
+ //without this filter would return a normal array.
370
+ p.filter = function(){
371
+ return re(Array.prototype.filter.apply(this, arguments));
294
372
  }
295
373
 
296
- p.comp = function(c){
297
-
298
- return this.each(function(ref){
299
- this.comp(c);
300
- });
301
-
374
+ /*
375
+ Finds first entity with components
376
+
377
+ re('draw').findWith('circle !red');
378
+
379
+ */
380
+ p.findWith = function(comps, c){
381
+ return this.find(function(e){
382
+ return e.has(comps);
383
+ }, c);
302
384
  }
303
385
 
304
- p.removeComp = function(c){
305
- return this.each(function(ref){
306
- this.removeComp(c);
307
- });
386
+ /*
387
+ Creates a new entity and pushes it into the array.
388
+ */
389
+ p.e = function(comps, count){
390
+ var e = re.e(comps, count);
391
+ if(count){
392
+ //add all in query
393
+ for(var i in e){
394
+ this.push(e[i]);
395
+ }
396
+ } else {
397
+ this.push(e);
398
+ }
399
+
400
+ return this;
308
401
  }
309
402
 
310
- p.on = function(type, method){
311
-
312
- return this.each(function(){
313
- this.on(type,method);
314
- });
315
-
403
+ p.include = function(ref){
404
+ return this.indexOf(ref) != -1;
316
405
  }
317
406
 
318
- p.off = function(type, method){
319
- return this.each(function(){
320
- this.off(type, method);
321
- });
407
+ /*
408
+ Removes first reference found from array.
409
+
410
+ var blah = re.e();
411
+
412
+ var q = re()
413
+ q.push(blah);
414
+
415
+ q.erase(blah);
416
+
417
+ q.include(blah) //false
418
+
419
+ Can also add in other in its place.
420
+
421
+ q.erase(blah, re.e());
422
+
423
+ */
424
+ p.erase = function(ref){
425
+ for(var i=this.length; i--;){
426
+ if(this[i] == ref) this.splice(i, 1);
427
+ }
428
+ return this;
322
429
  }
323
430
 
324
- p.trigger = function(){
325
- var p = arguments;
326
- return this.each(function(){
327
- this.trigger.apply(this, p);
328
- });
329
- }
431
+ /*
432
+ Inserts an element after the other.
433
+ */
434
+ p.insertAfter = function(target, ref){
435
+ this.splice(this.indexOf(target)+1, 0, ref);
436
+ return this;
437
+ }
330
438
 
331
- p.has = function(comp){
332
-
333
- for(var i=0; i<this.length; i++){
334
- if(!this[i].has(comp)){
335
- return false;
336
- }
337
- }
338
-
339
- return this.length != 0;
439
+ /*
440
+ Inserts an element before the other.
441
+ */
442
+ p.insertBefore = function(target, ref){
443
+ this.splice(this.indexOf(target), 0, ref);
444
+ return this;
445
+ }
446
+
447
+ /*
448
+ Swaps the indexes of the given elements.
449
+ */
450
+ p.swap = function(ref1, ref2){
451
+ var ref1i = this.indexOf(ref1);
452
+ var ref2i = this.indexOf(ref2);
453
+
454
+ var t = this[ref1i];
455
+ this[ref1i] = ref2;
456
+ this[ref2i] = t;
457
+
458
+ return this;
340
459
  }
341
460
 
342
461
  p.dispose = function(){
343
462
 
344
- return this.each(function(){
463
+ return this.each(function(e){
345
464
 
346
- this.dispose();
465
+ e.dispose();
347
466
 
348
467
  });
349
468
 
350
469
  }
351
470
 
471
+ /*
472
+ returns first element or appends it to front
473
+
474
+ re().first(1).first(); //1
475
+ */
476
+ p.first = function(r){
477
+ if(arguments.length){
478
+ this.unshift.apply(this, arguments);
479
+ return this;
480
+ }
481
+ return this[0];
482
+ }
483
+
484
+ p.last = function(ref){
485
+ if(arguments.length){
486
+ this.push.apply(this, arguments);
487
+ return this;
488
+ }
489
+ return this[this.length-1];
490
+ }
491
+
352
492
  re.query = q;
353
493
 
354
494
  }(re));
data/src/core/system.js CHANGED
@@ -44,6 +44,7 @@ re.c('system')
44
44
  (function m(){
45
45
 
46
46
  that.system_loop();
47
+
47
48
  if(that.running){
48
49
  that.requestAnimationFrame(m, that.canvas);
49
50
  }
@@ -67,7 +68,7 @@ re.c('system')
67
68
  },
68
69
 
69
70
  //scale is currently not implemented!
70
- init:function(canvasId, scale, contextType){
71
+ init:function(canvasId, contextType){
71
72
 
72
73
  //add comps here because system is defined earlier than other comps
73
74
  this.comp('polyfill tick timestep');
@@ -79,8 +80,6 @@ re.c('system')
79
80
  this.canvas = re.$(canvasId);
80
81
  }
81
82
 
82
- this.scale = scale || 1;
83
-
84
83
  this.context = this.canvas.getContext(contextType || '2d');
85
84
 
86
85
  var s = re.screen = re.e('screen');
@@ -98,6 +97,8 @@ re.c('system')
98
97
  if(re._c.touch){
99
98
  re._c.touch.i();
100
99
  }
100
+ this.system_loop = this.defaultLoop;
101
+ this.second = this.stepSize * 30;
101
102
 
102
103
  return this;
103
104
  },
@@ -105,16 +106,25 @@ re.c('system')
105
106
  /*
106
107
  Default main loop
107
108
  */
108
- system_loop:function(){
109
+ defaultLoop:function(){
109
110
 
110
111
  this.timestep(Math.min(this.tick() / 1000, this.maxTick), function(){
111
112
  //update
112
- re._c.update.update(this.stepSize);
113
+ this.update();
113
114
  });
114
115
 
115
116
  //clear
116
117
  this.clear(this.clearColor);
117
- re._c.draw.draw(this.context);
118
+ this.draw();
119
+ },
120
+
121
+ update:function(){
122
+ re._c.update.update(this.stepSize);
123
+ },
124
+
125
+ draw:function(){
126
+ //renders default drawlist
127
+ re.drawlist().drawlist(this.context);
118
128
  }
119
129
 
120
130