src/core/query.js in entityjs-0.3.2 vs src/core/query.js in entityjs-0.4.0
- old
+ new
@@ -27,11 +27,11 @@
//warning you cannot query for two ids
re('#bob player')
//takes values from all objects and returns an array
.pluck('width height');
- //find all bitmap entities with update bind.
+ //find all bitmap entities with update event.
re('bitmap ^update');
//add color comp to all text components
re('text').comp('color');
@@ -47,12 +47,11 @@
return false;
})
//calls reset method on all entities
.method('reset');
- //returns direct reference to first entity with id of player
- re('#player').health = 100;
+ re('#player')[0].health = 100;
*/
var q = function(selector){
if(selector){
@@ -133,25 +132,26 @@
while(++i < l && (e = re._e[i])){
if(select.call(e, i, l)) this.push(e);
}
+ } else if(re.is(select, 'array')){
+ this.push.apply(this, select);
}
return this;
}
/*
Calls the given method name on all entities.
- re('enemies').method('rest');
+ re('enemies').invoke('rest');
*/
- p.method = function(m){
+ p.invoke = function(m){
var b = Array.prototype.slice.call(arguments, 1);
- return this.each(function(){
-
- this[m].apply(this, b);
+ return this.each(function(e){
+ e[m].apply(e, b);
});
}
/*
@@ -160,26 +160,26 @@
});
returning false will break the loop
*/
- p.each = function(m){
- var l = this.length, i = -1, e;
+ p.each = function(m, context){
+ var l = this.length, i = -1, e;
+
+ while(++i < l && (e = this[i]) != null && m.call(context || this, e, i, this) !== false);
- while(++i < l && (e = this[i]) && m.call(e, i, l) !== false);
-
return this;
}
/*
The map method allows multidimensional loops.
//map through and increase y every 3 entities.
- re('draw').tilemap(3, function(x, y){
- this.x(x * width);
- this.y(Y * height);
+ re('draw').tilemap(3, function(e, x, y){
+ e.x(x * width);
+ e.y(Y * height);
});
//so instead of getting this back
[e,e,e,e,e...]
@@ -192,17 +192,17 @@
]
returning false will break the loop
*/
- p.tilemap = function(w, method){
+ p.tilemap = function(w, method, context){
var x = 0;
var y = 0;
return this.each(function(i, l){
- if(method.call(this[i], x, y, i, l) == false) return false;
+ if(method.call(context, this[i], x, y, i, this) == false) return false;
x++;
if(x == w){
x = 0;
@@ -215,12 +215,12 @@
Returns an array of all components in the query.
*/
p.comps = function(){
var l = [];
- this.each(function(){
- var k = this.comps();
+ this.each(function(e){
+ var k = e.comps();
for(var i=0; i<k.length; i++){
if(l.indexOf(k[i]) == -1){
l.push(k[i])
}
}
@@ -231,13 +231,54 @@
/*
Returns a random entity.
*/
p.random = function(){
- return this[~~(Math.random()*this.length)];
+ return this[(Math.random()*this.length)|0];
}
+ p.attr = function(obj, value){
+ return this.invoke('attr', obj, value);
+
+ }
+
+ p.def = function(obj, value){
+ return this.invoke('def', obj, value);
+ }
+
+ p.comp = function(c){
+ return this.invoke('comp', c);
+
+ }
+
+ p.removeComp = function(c){
+ return this.invoke('removeComp', c);
+ }
+
+ p.on = function(type, method){
+ return this.invoke('on', type, method);
+ }
+
+ p.off = function(type, method){
+ return this.invoke('off', type, method);
+ }
+
+ p.trigger = function(){
+ var p = arguments;
+ return this.each(function(e){
+ e.trigger.apply(e, p);
+ });
+ }
+
+ p.has = function(comp){
+ //return false if empty
+ if(!this.length) return false;
+ return this.every(function(e){
+ return e.has(comp);
+ });
+ }
+
/*
The pluck method returns all values from all entities in an array.
//will return all pos objs from all entities.
re('point').pluck('pos visible');
@@ -253,101 +294,200 @@
p.pluck = function(value){
var t = [];
var k = value.split(' ');
- this.each(function(){
+ this.each(function(e){
var o = {};
for(var p in k){
- if(k.hasOwnProperty(p)){
- o[k[p]] = this[k[p]];
- }
+ var name = k[p];
+ o[name] = e[name];
}
t.push(o);
});
return t;
}
- p.defines = function(){
- throw 'Deprecated use attr'
+ p.isEmpty = function(){
+ return !this.length;
}
- p.attr = function(obj, value){
- return this.each(function(){
- this.attr(obj,value);
- });
+ /*
+ Returns the first entity that passes the truth iterator method.
+
+ re('tile').find(function(e){
+ return e.tileX() == 0 && e.tileY() == 1;
+ });
+
+ */
+ p.find = function(method, context){
+ for(var i=0, l=this.length; i<l; i++){
+ if(method.call(context || this, this[i], i, this)) return this[i];
+ }
+
+ return null;
+ }
+
+ /*
+ Returns the lowest entity from the given iterator.
+
+ var weakestRat = re('rat').min(function(e){
+ return e.health;
+ });
+
+ */
+ p.min = function(method, context){
+ var lowest = Infinity, val;
+ this.each(function(e, i, l){
+ var next = method.call(context || this, e, i, l);
+ if(next < lowest){
+ lowest = next;
+ val = e;
+ }
+ });
+
+ return val;
}
- p.defaults = function(){
- throw 'Deprecated use def'
+ p.max = function(method, context){
+ var lowest = -Infinity, val;
+ this.each(function(e, i, l){
+ var next = method.call(context || this, e, i, l);
+ if(next > lowest){
+ lowest = next;
+ val = e;
+ }
+ });
+
+ return val;
}
- p.def = function(obj, value){
- return this.each(function(){
- this.def(obj, value);
- });
-
+ //without this filter would return a normal array.
+ p.filter = function(){
+ return re(Array.prototype.filter.apply(this, arguments));
}
- p.comp = function(c){
-
- return this.each(function(ref){
- this.comp(c);
- });
-
+ /*
+ Finds first entity with components
+
+ re('draw').findWith('circle !red');
+
+ */
+ p.findWith = function(comps, c){
+ return this.find(function(e){
+ return e.has(comps);
+ }, c);
}
- p.removeComp = function(c){
- return this.each(function(ref){
- this.removeComp(c);
- });
+ /*
+ Creates a new entity and pushes it into the array.
+ */
+ p.e = function(comps, count){
+ var e = re.e(comps, count);
+ if(count){
+ //add all in query
+ for(var i in e){
+ this.push(e[i]);
+ }
+ } else {
+ this.push(e);
+ }
+
+ return this;
}
- p.on = function(type, method){
-
- return this.each(function(){
- this.on(type,method);
- });
-
+ p.include = function(ref){
+ return this.indexOf(ref) != -1;
}
- p.off = function(type, method){
- return this.each(function(){
- this.off(type, method);
- });
+ /*
+ Removes first reference found from array.
+
+ var blah = re.e();
+
+ var q = re()
+ q.push(blah);
+
+ q.erase(blah);
+
+ q.include(blah) //false
+
+ Can also add in other in its place.
+
+ q.erase(blah, re.e());
+
+ */
+ p.erase = function(ref){
+ for(var i=this.length; i--;){
+ if(this[i] == ref) this.splice(i, 1);
+ }
+ return this;
}
- p.trigger = function(){
- var p = arguments;
- return this.each(function(){
- this.trigger.apply(this, p);
- });
- }
+ /*
+ Inserts an element after the other.
+ */
+ p.insertAfter = function(target, ref){
+ this.splice(this.indexOf(target)+1, 0, ref);
+ return this;
+ }
- p.has = function(comp){
-
- for(var i=0; i<this.length; i++){
- if(!this[i].has(comp)){
- return false;
- }
- }
-
- return this.length != 0;
+ /*
+ Inserts an element before the other.
+ */
+ p.insertBefore = function(target, ref){
+ this.splice(this.indexOf(target), 0, ref);
+ return this;
}
+ /*
+ Swaps the indexes of the given elements.
+ */
+ p.swap = function(ref1, ref2){
+ var ref1i = this.indexOf(ref1);
+ var ref2i = this.indexOf(ref2);
+
+ var t = this[ref1i];
+ this[ref1i] = ref2;
+ this[ref2i] = t;
+
+ return this;
+ }
+
p.dispose = function(){
- return this.each(function(){
+ return this.each(function(e){
- this.dispose();
+ e.dispose();
});
+ }
+
+ /*
+ returns first element or appends it to front
+
+ re().first(1).first(); //1
+ */
+ p.first = function(r){
+ if(arguments.length){
+ this.unshift.apply(this, arguments);
+ return this;
+ }
+ return this[0];
+ }
+
+ p.last = function(ref){
+ if(arguments.length){
+ this.push.apply(this, arguments);
+ return this;
+ }
+ return this[this.length-1];
}
re.query = q;
}(re));
\ No newline at end of file