Skip to content

Commit 235b6c8

Browse files
committed
added Body.set
1 parent 8dea166 commit 235b6c8

1 file changed

Lines changed: 77 additions & 12 deletions

File tree

src/body/Body.js

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,26 @@ var Body = {};
9898
*/
9999
var _initProperties = function(body, options) {
100100
// init required properties
101-
body.bounds = body.bounds || Bounds.create(body.vertices);
102-
body.positionPrev = body.positionPrev || Vector.clone(body.position);
103-
body.anglePrev = body.anglePrev || body.angle;
104-
105-
// must use setters for the more complicated properties
106-
Body.setVertices(body, body.vertices);
107-
Body.setStatic(body, body.isStatic);
108-
Sleeping.set(body, body.isSleeping);
101+
Body.set(body, {
102+
bounds: body.bounds || Bounds.create(body.vertices),
103+
positionPrev: body.positionPrev || Vector.clone(body.position),
104+
anglePrev: body.anglePrev || body.angle,
105+
vertices: body.vertices,
106+
isStatic: body.isStatic,
107+
isSleeping: body.isSleeping
108+
});
109+
109110
Vertices.rotate(body.vertices, body.angle, body.position);
110111
Axes.rotate(body.axes, body.angle);
111112
Bounds.update(body.bounds, body.vertices, body.velocity);
112113

113114
// allow options to override the automatically calculated properties
114-
body.axes = options.axes || body.axes;
115-
body.area = options.area || body.area;
116-
Body.setMass(body, options.mass || body.mass);
117-
Body.setInertia(body, options.inertia || body.inertia);
115+
Body.set(body, {
116+
axes: options.axes || body.axes,
117+
area: options.area || body.area,
118+
mass: options.mass || body.mass,
119+
inertia: options.inertia || body.inertia
120+
});
118121

119122
// render properties
120123
var defaultFillStyle = (body.isStatic ? '#eeeeee' : Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58'])),
@@ -123,6 +126,68 @@ var Body = {};
123126
body.render.strokeStyle = body.render.strokeStyle || defaultStrokeStyle;
124127
};
125128

129+
/**
130+
* Given a property and a value (or map of), sets the property(s) on the body, using the appropriate setter functions if they exist.
131+
* Prefer to use the actual setter functions in performance critical situations.
132+
* @method set
133+
* @param {body} body
134+
* @param {} settings A property name (or map of properties and values) to set on the body.
135+
* @param {} value The value to set if `settings` is a single property name.
136+
*/
137+
Body.set = function(body, settings, value) {
138+
var property;
139+
140+
if (typeof settings === 'string') {
141+
property = settings;
142+
settings = {};
143+
settings[property] = value;
144+
}
145+
146+
for (property in settings) {
147+
value = settings[property];
148+
149+
if (!settings.hasOwnProperty(property))
150+
continue;
151+
152+
switch (property) {
153+
154+
case 'isStatic':
155+
Body.setStatic(body, value);
156+
break;
157+
case 'isSleeping':
158+
Sleeping.set(body, value);
159+
break;
160+
case 'mass':
161+
Body.setMass(body, value);
162+
break;
163+
case 'density':
164+
Body.setDensity(body, value);
165+
break;
166+
case 'inertia':
167+
Body.setInertia(body, value);
168+
break;
169+
case 'vertices':
170+
Body.setVertices(body, value);
171+
break;
172+
case 'position':
173+
Body.setPosition(body, value);
174+
break;
175+
case 'angle':
176+
Body.setAngle(body, value);
177+
break;
178+
case 'velocity':
179+
Body.setVelocity(body, value);
180+
break;
181+
case 'angularVelocity':
182+
Body.setAngularVelocity(body, value);
183+
break;
184+
default:
185+
body[property] = value;
186+
187+
}
188+
}
189+
};
190+
126191
/**
127192
* Sets the body as static, including isStatic flag and setting mass and inertia to Infinity.
128193
* @method setStatic

0 commit comments

Comments
 (0)