1+ /**
2+ * @author Georgios Kaleadis https://github.com/georgiee
3+ * @author Richard Davey <rich@photonstorm.com>
4+ * @copyright 2014 Photon Storm Ltd.
5+ * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License }
6+ */
7+
8+ /**
9+ * Allow to access a list of created fixture (coming from Body#addPhaserPolygon)
10+ * which itself parse the input from PhysicsEditor with the custom phaser exporter.
11+ * You can access fixtures of a Body by a group index or even by providing a fixture Key.
12+
13+ * You can set the fixture key and also the group index for a fixture in PhysicsEditor.
14+ * This gives you the power to create a complex body built of many fixtures and modify them
15+ * during runtime (to remove parts, set masks, categories & sensor properties)
16+ *
17+ * @class Phaser.Physics.P2.FixtureList
18+ * @classdesc Collection for generated P2 fixtures
19+ * @constructor
20+ * @param {Array } list - A list of fixtures (from Phaser.Physics.P2.Body#addPhaserPolygon)
21+ */
22+
23+ Phaser . Physics . P2 . FixtureList = function ( list ) {
24+ if ( ! ( list instanceof Array ) ) {
25+ list = [ list ] ;
26+ }
27+ this . rawList = list ;
28+ this . init ( ) ;
29+ this . parse ( this . rawList ) ;
30+ }
31+
32+ Phaser . Physics . P2 . FixtureList . prototype = {
33+
34+ init : function ( ) {
35+ /**
36+ * @property {object } namedFixtures - Collect all fixtures with a key
37+ * @private
38+ */
39+ this . namedFixtures = { } ;
40+
41+ /**
42+ * @property {Array } groupedFixtures - Collect all given fixtures per group index.
43+ * Notice: Every fixture with a key also belongs to a group
44+ * @private
45+ */
46+ this . groupedFixtures = [ ] ;
47+
48+ /**
49+ * @property {Array } allFixtures - This is a list of everything in this collection
50+ * @private
51+ */
52+ this . allFixtures = [ ] ;
53+ } ,
54+
55+ /**
56+ * @method Phaser.Physics.P2#setCategory
57+ * @param {Number } bit - The bit to set as the collision group
58+ * @param {String } fixtureKey - Only apply to the fixture with the given key
59+ */
60+ setCategory : function ( bit , fixtureKey ) {
61+ var setter = function ( fixture ) {
62+ fixture . collisionGroup = bit ;
63+ } ;
64+
65+ fixtures = this . getFixtures ( fixtureKey )
66+ fixtures . forEach ( setter ) ;
67+ } ,
68+
69+ /**
70+ * @method Phaser.Physics.P2#setMask
71+ * @param {Number } bit - The bit to set as the collision mask
72+ * @param {String } fixtureKey - Only apply to the fixture with the given key
73+ */
74+ setMask : function ( bit , fixtureKey ) {
75+ var setter = function ( fixture ) {
76+ fixture . collisionMask = bit ;
77+ } ;
78+
79+ fixtures = this . getFixtures ( fixtureKey )
80+ fixtures . forEach ( setter ) ;
81+ } ,
82+
83+
84+ /**
85+ * @method Phaser.Physics.P2#setSensor
86+ * @param {Boolean } value - sensor true or false
87+ * @param {String } fixtureKey - Only apply to the fixture with the given key
88+ */
89+ setSensor : function ( value , fixtureKey ) {
90+ var setter = function ( fixture ) {
91+ fixture . sensor = value ;
92+ } ;
93+
94+ fixtures = this . getFixtures ( fixtureKey )
95+ fixtures . forEach ( setter ) ;
96+ } ,
97+
98+ /**
99+ * @method Phaser.Physics.P2#setMaterial
100+ * @param {Object } material - The contact material for a fixture
101+ * @param {String } fixtureKey - Only apply to the fixture with the given key
102+ */
103+ setMaterial : function ( material , fixtureKey ) {
104+ var setter = function ( fixture ) {
105+ fixture . material = material ;
106+ } ;
107+
108+ fixtures = this . getFixtures ( fixtureKey )
109+ fixtures . forEach ( setter ) ;
110+ } ,
111+
112+ /**
113+ * Accessor to get either a list of specified fixtures by key or the whole fixture list
114+ *
115+ * @method Phaser.Physics.P2#getFixtures
116+ * @param {Array } keys - A list of fixture keys
117+ */
118+ getFixtures : function ( keys ) {
119+ var fixtures = [ ]
120+
121+ if ( keys ) {
122+ if ( ! ( keys instanceof Array ) ) {
123+ keys = [ keys ]
124+ }
125+ var self = this
126+ keys . forEach ( function ( key ) {
127+ if ( self . namedFixtures [ key ] ) {
128+ fixtures . push ( self . namedFixtures [ key ] )
129+ }
130+ } )
131+ return this . flatten ( fixtures ) ;
132+
133+ } else {
134+ return this . allFixtures ;
135+ }
136+ } ,
137+
138+ /**
139+ * Accessor to get either a single fixture by its key
140+ *
141+ * @method Phaser.Physics.P2#getFixtureByKey
142+ * @param {String } key - The key of the fixture
143+ */
144+ getFixtureByKey : function ( key ) {
145+ return this . namedFixtures [ key ] ;
146+ } ,
147+
148+ /**
149+ * Accessor to get a group of fixtures by its group index
150+ *
151+ * @method Phaser.Physics.P2#getGroup
152+ * @param {Number } groupID - The group index
153+ */
154+ getGroup : function ( groupID ) {
155+ return this . groupedFixtures [ groupID ] ;
156+ } ,
157+
158+
159+ /**
160+ * Parser for the output of Phaser.Physics.P2.Body#addPhaserPolygon
161+ *
162+ * @method Phaser.Physics.P2#getGroup
163+ * @param {Number } groupID - The group index
164+ */
165+ parse : function ( ) {
166+ var key , value , _ref , _results ;
167+ _ref = this . rawList ;
168+ _results = [ ] ;
169+ for ( key in _ref ) {
170+ value = _ref [ key ] ;
171+ if ( ! isNaN ( key - 0 ) ) {
172+ this . groupedFixtures [ key ] = this . groupedFixtures [ key ] || [ ] ;
173+ this . groupedFixtures [ key ] = this . groupedFixtures [ key ] . concat ( value ) ;
174+ } else {
175+ this . namedFixtures [ key ] = this . flatten ( value ) ;
176+ }
177+ _results . push ( this . allFixtures = this . flatten ( this . groupedFixtures ) ) ;
178+ }
179+ _results ;
180+ } ,
181+
182+ /**
183+ * A helper to flatten arrays
184+ * This is very useful in this class as the fixtures a nested from time to time
185+ * Due to the way P2 creates and splits polygons
186+ *
187+ * @method Phaser.Physics.P2#flatten
188+ * @param {Array } array - The array to flatten.
189+ * Notice: This will happen recursive not shallow.
190+ */
191+ flatten : function ( array ) {
192+ var result , self ;
193+ result = [ ] ;
194+ self = arguments . callee ;
195+ array . forEach ( function ( item ) {
196+ return Array . prototype . push . apply ( result , ( Array . isArray ( item ) ? self ( item ) : [ item ] ) ) ;
197+ } ) ;
198+ return result ;
199+ }
200+ }
0 commit comments