New setBody component for setting and change body shape

This commit is contained in:
Richard Davey 2017-11-27 03:45:03 +00:00
parent ff316be41e
commit f8fe81dfa1
4 changed files with 110 additions and 30 deletions

View file

@ -18,6 +18,7 @@ var MatterImage = new Class({
Components.Gravity, Components.Gravity,
Components.Mass, Components.Mass,
Components.Sensor, Components.Sensor,
Components.SetBody,
Components.Sleep, Components.Sleep,
Components.Static, Components.Static,
Components.Transform, Components.Transform,
@ -35,28 +36,26 @@ var MatterImage = new Class({
this.setSizeToFrame(); this.setSizeToFrame();
this.setOrigin(); this.setOrigin();
this._tempVec2 = new Vector2(); this.world = world;
var isCircle = GetFastValue(options, 'isCircle', false); this._tempVec2 = new Vector2(x, y);
if (isCircle) var shape = GetFastValue(options, 'shape', null);
if (!shape)
{ {
var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2); this.body = Bodies.rectangle(x, y, this.width, this.height, options);
this.body = Bodies.circle(x, y, radius, options); this.body.gameObject = this;
if (GetFastValue(options, 'addToWorld', true))
{
world.add(this.body);
}
} }
else else
{ {
this.body = Bodies.rectangle(x, y, this.width, this.height, options); this.setBody(shape, options);
}
this.body.gameObject = this;
this.world = world;
if (GetFastValue(options, 'addToWorld', true))
{
world.add(this.body);
} }
this.setPosition(x, y); this.setPosition(x, y);

View file

@ -19,6 +19,7 @@ var MatterSprite = new Class({
Components.Gravity, Components.Gravity,
Components.Mass, Components.Mass,
Components.Sensor, Components.Sensor,
Components.SetBody,
Components.Sleep, Components.Sleep,
Components.Static, Components.Static,
Components.Transform, Components.Transform,
@ -38,28 +39,26 @@ var MatterSprite = new Class({
this.setSizeToFrame(); this.setSizeToFrame();
this.setOrigin(); this.setOrigin();
this._tempVec2 = new Vector2(); this.world = world;
var isCircle = GetFastValue(options, 'isCircle', false); this._tempVec2 = new Vector2(x, y);
if (isCircle) var shape = GetFastValue(options, 'shape', null);
if (!shape)
{ {
var radius = GetFastValue(options, 'radius', Math.max(this.width, this.height) / 2); this.body = Bodies.rectangle(x, y, this.width, this.height, options);
this.body = Bodies.circle(x, y, radius, options); this.body.gameObject = this;
if (GetFastValue(options, 'addToWorld', true))
{
world.add(this.body);
}
} }
else else
{ {
this.body = Bodies.rectangle(x, y, this.width, this.height, options); this.setBody(shape, options);
}
this.body.gameObject = this;
this.world = world;
if (GetFastValue(options, 'addToWorld', true))
{
world.add(this.body);
} }
this.setPosition(x, y); this.setPosition(x, y);

View file

@ -0,0 +1,81 @@
var Bodies = require('../lib/factory/Bodies');
var Body = require('../lib/body/Body');
var GetFastValue = require('../../../utils/object/GetFastValue');
var SetBody = {
setBody: function (config, options)
{
// Existing body? Remove it.
if (this.body)
{
this.world.remove(this.body);
}
if (!config)
{
return this;
}
else
{
var shapeType = GetFastValue(config, 'type', 'rectangle');
var bodyX = GetFastValue(config, 'x', this._tempVec2.x);
var bodyY = GetFastValue(config, 'y', this._tempVec2.y);
var bodyWidth = GetFastValue(config, 'width', this.width);
var bodyHeight = GetFastValue(config, 'height', this.height);
switch (shapeType)
{
case 'rectangle':
this.body = Bodies.rectangle(bodyX, bodyY, bodyWidth, bodyHeight, options);
break;
case 'circle':
var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
var maxSides = GetFastValue(config, 'maxSides', 25);
this.body = Bodies.circle(bodyX, bodyY, radius, options, maxSides);
break;
case 'trapezoid':
var slope = GetFastValue(config, 'slope', 0.5);
this.body = Bodies.trapezoid(bodyX, bodyY, bodyWidth, bodyHeight, slope, options);
break;
case 'polygon':
var sides = GetFastValue(config, 'sides', 5);
var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
this.body = Bodies.polygon(bodyX, bodyY, sides, radius, options);
break;
case 'fromVertices':
case 'fromVerts':
var verts = GetFastValue(config, 'verts', []);
if (this.body)
{
Body.setVertices(this.body, verts);
}
else
{
var flagInternal = GetFastValue(config, 'flagInternal', false);
var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
var minimumArea = GetFastValue(config, 'minimumArea', 10);
this.body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
}
break;
}
}
this.body.gameObject = this;
if (GetFastValue(config, 'addToWorld', true))
{
this.world.add(this.body);
}
return this;
}
};
module.exports = SetBody;

View file

@ -10,6 +10,7 @@ module.exports = {
Mass: require('./Mass'), Mass: require('./Mass'),
Static: require('./Static'), Static: require('./Static'),
Sensor: require('./Sensor'), Sensor: require('./Sensor'),
SetBody: require('./SetBody'),
Sleep: require('./Sleep'), Sleep: require('./Sleep'),
Transform: require('./Transform'), Transform: require('./Transform'),
Velocity: require('./Velocity') Velocity: require('./Velocity')