Crafty.map
Methods
Crafty.map.boundaries()
public Object Crafty.map.boundaries()
- [Returns]
An object with the following structure, which represents an MBR which contains all entities
Return a copy of the minimum bounding rectangle encompassing all entities.
{
min: {
x: val_x,
y: val_y
},
max: {
x: val_x,
y: val_y
}
}
Crafty.map.insert()
public Object Crafty.map.insert(Object obj[, Object entry])
- obj
An entity to be inserted.
- entry
An existing entry object to reuse. (Optional)
- [Returns]
An object representing this object's entry in the HashMap
obj
is inserted in '.map' of the corresponding broad phase cells. An object of the following fields is returned.
{
keys: the object that keep track of cells
obj: The inserted object
map: the HashMap object
}
Crafty.map.refresh()
public void Crafty.map.refresh(Entry entry)
- entry
An entry to update
Update an entry's keys, and its position in the broad phrase map.
Example
Crafty.map.refresh(e);
Crafty.map.remove()
public void Crafty.map.remove(Entry entry)
- entry
An entry to remove from the hashmap
Remove an entry from the broad phase map.
Example
Crafty.map.remove(e);
Crafty.map.search()
public Array Crafty.map.search(Object rect[, Array results])
- rect
the rectangular region to search for entities. This object must contain the properties
_x
,_y
,_w
,_h
.
- results
If passed, entities found will be appended to this array.
- [Returns]
a (possibly empty) array of entities that have been found in the given region
Do a search for entities in the given region. Returned entities are guaranteed to overlap with the given region.
The easier usage is with filter == true
. For performance reason, you may use filter == false
, and filter the result yourself. See examples in drawing.js and collision.js.
Example
// search for entities located in the current visible region of the viewport
var results = Crafty.map.search(Crafty.viewport.rect());
// iterate over all those entities
var ent;
for (var i = 0, l = results.length; i < l; ++i) {
// do something with an entity
ent = results[i];
Crafty.log('Found entity with id', ent.getId());
}
Crafty.map.traverseRay()
public void Crafty.map.traverseRay(Object origin, Object direction, Function callback)
- origin
the point of origin from which the ray will be cast. The object must contain the properties
_x
and_y
.
- direction
the direction the ray will be cast. It must be normalized. The object must contain the properties
x
andy
.
- callback
a callback that will be called for each object that is encountered along the ray. This function is called with two arguments: The first one represents the object encountered; the second one represents the distance up to which all objects have been reported so far. The callback can return a truthy value in order to stop the traversal early.
Traverse the spatial map in the direction of the supplied ray.
Given the origin
and direction
the ray is cast and the callback
is called
for each object encountered in map cells traversed by the ray.
The callback is called for each object that may be intersected by the ray. Whether an actual intersection occurs shall be determined by the callback's implementation.
Example
Crafty.e("2D")
.setName('First entity')
.attr({x: 0, y: 0, w: 10, h: 10});
Crafty.e("2D")
.setName('Second entity')
.attr({x: 20, y: 20, w: 10, h: 10});
var origin = {_x: -25, _y: -25};
var direction = new Crafty.math.Vector2D(1, 1).normalize();
Crafty.map.traverseRay(origin, direction, function(ent, processedDistance) {
Crafty.log('Encountered entity named', ent.getName()); // logs 'First entity'
Crafty.log('All entities up to', processedDistance, 'px away have been reported thus far.');
Crafty.log('Stopping traversal after encountering the first entity.');
return true;
});
Crafty.map.unfilteredSearch()
public Array Crafty.map.search(Object rect[, Array results])
- rect
the rectangular region to search for entities. This object must contain the properties
_x
,_y
,_w
,_h
.
- results
If passed, entities found will be appended to this array.
- [Returns]
a (possibly empty) array of entities that have been found in the given region
Do a search for entities in the given region. Returned entities are not guaranteed to overlap with the given region, and the results may contain duplicates.
This method is intended to be used as the first step of a more complex search. More common use cases should use Crafty.map.search, which filters the results.