Skip to content

Commit

Permalink
[#1008] move some initialization "logic" from Entity to Body
Browse files Browse the repository at this point in the history
  • Loading branch information
obiot committed Sep 6, 2021
1 parent e17cca1 commit 15e0630
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
20 changes: 5 additions & 15 deletions src/entity/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Renderable from "./../renderable/renderable.js";
import Sprite from "./../renderable/sprite.js";
import Body from "./../physics/body.js";
import Polygon from "./../shapes/poly.js";
import collision from "./../physics/collision.js";


/**
* a Generic Object Entity<br>
* a Generic Object Entity
* @class
* @extends me.Renderable
* @memberOf me
* @see me.Renderable
* @constructor
* @param {Number} x the x coordinates of the entity object
* @param {Number} y the y coordinates of the entity object
Expand Down Expand Up @@ -126,19 +126,9 @@ var Entity = Renderable.extend({
this.resize(this.body.getBounds().width, this.body.getBounds().height);
}

// set the collision mask if defined
if (typeof(settings.collisionMask) !== "undefined") {
this.body.setCollisionMask(settings.collisionMask);
}

// set the collision mask if defined
if (typeof(settings.collisionType) !== "undefined") {
if (typeof collision.types[settings.collisionType] !== "undefined") {
this.body.collisionType = collision.types[settings.collisionType];
} else {
throw new Error("Invalid value for the collisionType property");
}
}
// set the collision mask and type (if defined)
this.body.setCollisionMask(settings.collisionMask);
this.body.setCollisionType(settings.collisionType);

// disable for entities
this.autoTransform = false;
Expand Down
28 changes: 25 additions & 3 deletions src/physics/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,47 @@ class Body {

/**
* By default all entities are able to collide with all other entities, <br>
* but it's also possible to specificy 'collision filters' to provide a finer <br>
* but it's also possible to specify 'collision filters' to provide a finer <br>
* control over which entities can collide with each other.
* @name setCollisionMask
* @memberOf me.Body
* @public
* @function
* @see me.collision.types
* @param {Number} bitmask the collision mask
* @param {Number} [bitmask = me.collision.types.ALL_OBJECT] the collision mask
* @example
* // filter collision detection with collision shapes, enemies and collectables
* myEntity.body.setCollisionMask(me.collision.types.WORLD_SHAPE | me.collision.types.ENEMY_OBJECT | me.collision.types.COLLECTABLE_OBJECT);
* ...
* // disable collision detection with all other objects
* myEntity.body.setCollisionMask(me.collision.types.NO_OBJECT);
*/
setCollisionMask(bitmask) {
setCollisionMask(bitmask = collision.types.ALL_OBJECT) {
this.collisionMask = bitmask;
}

/**
* define the collision type of the body for collision filtering
* @name setCollisionType
* @memberOf me.Body
* @public
* @function
* @see me.collision.types
* @param {Number} type the collision type
* @example
* // set the body collision type
* myEntity.body.collisionType = me.collision.types.PLAYER_OBJECT;
*/
setCollisionType(type) {
if (typeof type !== "undefined") {
if (typeof collision.types[type] !== "undefined") {
this.collisionType = collision.types[type];
} else {
throw new Error("Invalid value for the collisionType property");
}
}
}

/**
* the built-in function to solve the collision response
* @protected
Expand Down

0 comments on commit 15e0630

Please sign in to comment.