Skip to content

Commit

Permalink
added a p2 fixture collections helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiee committed Apr 10, 2014
1 parent 1213cae commit 13e5cac
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 0 deletions.
200 changes: 200 additions & 0 deletions src/physics/p2/FixtureList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/**
* @author Georgios Kaleadis https://github.com/georgiee
* @author Richard Davey <[email protected]>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/

/**
* Allow to access a list of created fixture (coming from Body#addPhaserPolygon)
* which itself parse the input from PhysicsEditor with the custom phaser exporter.
* You can access fixtures of a Body by a group index or even by providing a fixture Key.
* You can set the fixture key and also the group index for a fixture in PhysicsEditor.
* This gives you the power to create a complex body built of many fixtures and modify them
* during runtime (to remove parts, set masks, categories & sensor properties)
*
* @class Phaser.Physics.P2.FixtureList
* @classdesc Collection for generated P2 fixtures
* @constructor
* @param {Array} list - A list of fixtures (from Phaser.Physics.P2.Body#addPhaserPolygon)
*/

Phaser.Physics.P2.FixtureList = function(list) {
if (!(list instanceof Array)) {
list = [list];
}
this.rawList = list;
this.init();
this.parse(this.rawList);
}

Phaser.Physics.P2.FixtureList.prototype = {

init: function() {
/**
* @property {object} namedFixtures - Collect all fixtures with a key
* @private
*/
this.namedFixtures = {};

/**
* @property {Array} groupedFixtures - Collect all given fixtures per group index.
* Notice: Every fixture with a key also belongs to a group
* @private
*/
this.groupedFixtures = [];

/**
* @property {Array} allFixtures - This is a list of everything in this collection
* @private
*/
this.allFixtures = [];
},

/**
* @method Phaser.Physics.P2#setCategory
* @param {Number} bit - The bit to set as the collision group
* @param {String} fixtureKey - Only apply to the fixture with the given key
*/
setCategory: function(bit, fixtureKey) {
var setter = function(fixture){
fixture.collisionGroup = bit;
};

fixtures = this.getFixtures(fixtureKey)
fixtures.forEach(setter);
},

/**
* @method Phaser.Physics.P2#setMask
* @param {Number} bit - The bit to set as the collision mask
* @param {String} fixtureKey - Only apply to the fixture with the given key
*/
setMask: function(bit, fixtureKey) {
var setter = function(fixture) {
fixture.collisionMask = bit;
};

fixtures = this.getFixtures(fixtureKey)
fixtures.forEach(setter);
},


/**
* @method Phaser.Physics.P2#setSensor
* @param {Boolean} value - sensor true or false
* @param {String} fixtureKey - Only apply to the fixture with the given key
*/
setSensor: function(value, fixtureKey) {
var setter = function(fixture) {
fixture.sensor = value;
};

fixtures = this.getFixtures(fixtureKey)
fixtures.forEach(setter);
},

/**
* @method Phaser.Physics.P2#setMaterial
* @param {Object} material - The contact material for a fixture
* @param {String} fixtureKey - Only apply to the fixture with the given key
*/
setMaterial: function(material, fixtureKey) {
var setter = function(fixture) {
fixture.material = material;
};

fixtures = this.getFixtures(fixtureKey)
fixtures.forEach(setter);
},

/**
* Accessor to get either a list of specified fixtures by key or the whole fixture list
*
* @method Phaser.Physics.P2#getFixtures
* @param {Array} keys - A list of fixture keys
*/
getFixtures: function(keys) {
var fixtures = []

if (keys) {
if(!(keys instanceof Array)){
keys = [keys]
}
var self = this
keys.forEach(function(key){
if(self.namedFixtures[key]){
fixtures.push(self.namedFixtures[key])
}
})
return this.flatten(fixtures);

} else {
return this.allFixtures;
}
},

/**
* Accessor to get either a single fixture by its key
*
* @method Phaser.Physics.P2#getFixtureByKey
* @param {String} key - The key of the fixture
*/
getFixtureByKey: function(key) {
return this.namedFixtures[key];
},

/**
* Accessor to get a group of fixtures by its group index
*
* @method Phaser.Physics.P2#getGroup
* @param {Number} groupID - The group index
*/
getGroup: function(groupID) {
return this.groupedFixtures[groupID];
},


/**
* Parser for the output of Phaser.Physics.P2.Body#addPhaserPolygon
*
* @method Phaser.Physics.P2#getGroup
* @param {Number} groupID - The group index
*/
parse: function() {
var key, value, _ref, _results;
_ref = this.rawList;
_results = [];
for (key in _ref) {
value = _ref[key];
if (!isNaN(key - 0)) {
this.groupedFixtures[key] = this.groupedFixtures[key] || [];
this.groupedFixtures[key] = this.groupedFixtures[key].concat(value);
} else {
this.namedFixtures[key] = this.flatten(value);
}
_results.push(this.allFixtures = this.flatten(this.groupedFixtures));
}
_results;
},

/**
* A helper to flatten arrays
* This is very useful in this class as the fixtures a nested from time to time
* Due to the way P2 creates and splits polygons
*
* @method Phaser.Physics.P2#flatten
* @param {Array} array - The array to flatten.
* Notice: This will happen recursive not shallow.
*/
flatten: function(array) {
var result, self;
result = [];
self = arguments.callee;
array.forEach(function(item) {
return Array.prototype.push.apply(result, (Array.isArray(item) ? self(item) : [item]));
});
return result;
}
}
28 changes: 28 additions & 0 deletions src/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,31 @@ if (!Array.isArray)
return Object.prototype.toString.call(arg) == '[object Array]';
};
}


/**
* A polyfill for Array.forEach
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
*/
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisArg */)
{
"use strict";

if (this === void 0 || this === null)
throw new TypeError();

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();

var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
if (i in t)
fun.call(thisArg, t[i], i, t);
}
};
}

0 comments on commit 13e5cac

Please sign in to comment.