Crafty.map

Functions related with querying entities.

Methods

Back to top

Crafty.map.boundaries()

public Object Crafty.map.boundaries()
[Returns]

An object with the following structure, which represents an MBR which contains all entities

Note that the returned object is a reference to the internally used object. Use Crafty.clone to get a copy instead.

{
  min: {
    x: val_x,
    y: val_y
  },
  max: {
    x: val_x,
    y: val_y
  }
}
Back to top

Crafty.map.insert()

public Object Crafty.map.insert(Object obj)
obj

An entity to be inserted.

[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
}
Back to top

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);
Back to top

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);
Back to top

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 and y.

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;
});