Skip to content

Instantly share code, notes, and snippets.

@JoshuaCLee
Created August 28, 2014 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaCLee/fb74e1a59751733cb570 to your computer and use it in GitHub Desktop.
Save JoshuaCLee/fb74e1a59751733cb570 to your computer and use it in GitHub Desktop.
public class EntityConstructor extends Constructor
{
World world;
PhysicsSystem physics;
public EntityConstructor(World world, PhysicsSystem physics)
{
this.world = world;
this.physics = physics;
this.yamlConstructors.put(new Tag("!entity"), new ConstructEntity());
this.yamlConstructors.put(new Tag("!body"), new ConstructBody());
this.yamlConstructors.put(new Tag("!fixture"), new ConstructFixture());
}
private class ConstructEntity implements Construct
{
@Override
public Object construct(Node node)
{
Entity entity = world.createEntity();
Map<Object, Object> map = constructMapping((MappingNode)node);
if(!(Boolean)map.get("enabled"))
entity.disable();
List<Component> components = (List<Component>)map.get("components");
for(Component c : components)
{
if (c instanceof PhysicsComponent)
((PhysicsComponent) c).getBody().setUserData(entity);
entity.addComponent(c);
}
String tag = (String)map.get("tag");
if(tag != null)
{
world.getManager(TagManager.class).unregister(tag);
world.getManager(TagManager.class).register(tag, entity);
}
List<String> groups = (List<String>)map.get("groups");
for(String s : groups) {
world.getManager(GroupManager.class).add(entity, s);
}
return entity;
}
@Override
public void construct2ndStep(Node node, Object o)
{
}
}
private class ConstructBody implements Construct
{
@Override
public Object construct(Node node)
{
BodyDef bodyDef = new BodyDef();
Map<Object, Object> map = constructMapping((MappingNode)node);
bodyDef.type = (BodyType)map.get("m_type");
bodyDef.linearVelocity = (Vec2)map.get("m_linearVelocity");
bodyDef.angularVelocity = ((Double)map.get("m_angularVelocity")).floatValue();
bodyDef.linearDamping = ((Double)map.get("m_linearDamping")).floatValue();
bodyDef.angularDamping = ((Double)map.get("m_angularDamping")).floatValue();
bodyDef.gravityScale = ((Double)map.get("m_gravityScale")).floatValue();
Body body = physics.getPhysicsWorld().createBody(bodyDef);
body.m_flags = (Integer)map.get("m_flags");
body.m_islandIndex = (Integer)map.get("m_islandIndex");
Transform transform = (Transform)map.get("m_xf");
body.setTransform(transform.p, transform.q.getAngle());
body.applyForceToCenter((Vec2)map.get("m_force"));
body.applyTorque(((Double)map.get("m_torque")).floatValue());
body.m_sleepTime = ((Double)map.get("m_sleepTime")).floatValue();
List<FixtureDef> fixtures = (List<FixtureDef>)map.get("m_fixtureList");
for(FixtureDef fixture : fixtures)
body.createFixture(fixture);
return body;
}
@Override
public void construct2ndStep(Node node, Object o)
{
}
}
private class ConstructFixture implements Construct
{
@Override
public Object construct(Node node)
{
FixtureDef fixtureDef = new FixtureDef();
Map<Object, Object> map = constructMapping((MappingNode)node);
fixtureDef.density = ((Double)map.get("m_density")).floatValue();
fixtureDef.shape = (Shape)map.get("m_shape");
fixtureDef.friction = ((Double)map.get("m_friction")).floatValue();
fixtureDef.restitution = ((Double)map.get("m_restitution")).floatValue();
fixtureDef.isSensor = (Boolean)map.get("m_isSensor");
return fixtureDef;
}
@Override
public void construct2ndStep(Node node, Object o)
{
}
}
}
public class EntityRepresenter extends Representer
{
public EntityRepresenter()
{
this.representers.put(Entity.class, new RepresentEntity());
this.representers.put(Body.class, new RepresentBody());
this.representers.put(Fixture.class, new RepresentFixture());
}
private class RepresentEntity implements Represent
{
@Override
public Node representData(Object o)
{
Entity entity = (Entity)o;
Map<String, Object> map = new HashMap<String, Object>();
map.put("enabled", entity.isEnabled());
Bag<Component> bag = new Bag<Component>();
bag = entity.getComponents(bag);
List<Component> components = new ArrayList<Component>();
for(int i = 0; i < bag.size(); i++)
components.add(bag.get(i));
map.put("components", components);
Collection<String> tags = entity.getWorld().getManager(TagManager.class).getRegisteredTags();
for(String s : tags)
{
if(entity.getWorld().getManager(TagManager.class).getEntity(s).equals(entity))
{
map.put("tag", s);
break;
}
}
ImmutableBag<String> groupBag = entity.getWorld().getManager(GroupManager.class).getGroups(entity);
if(groupBag != null)
{
List<String> groups = new ArrayList<String>();
for(int i = 0; i < groupBag.size(); i++)
groups.add(groupBag.get(i));
map.put("groups", groups);
}
return representMapping(new Tag("!entity"), map, false);
}
}
public class RepresentBody implements Represent
{
@Override
public Node representData(Object o)
{
Body body = (Body)o;
Map<String, Object> map = new HashMap<String, Object>();
map.put("m_type", body.m_type);
map.put("m_flags", body.m_flags);
map.put("m_islandIndex", body.m_islandIndex);
map.put("m_xf", body.m_xf);
map.put("m_linearVelocity", body.m_linearVelocity);
map.put("m_angularVelocity", body.m_angularVelocity);
map.put("m_force", body.m_force);
map.put("m_torque", body.m_torque);
map.put("m_linearDamping", body.m_linearDamping);
map.put("m_angularDamping", body.m_angularDamping);
map.put("m_gravityScale", body.m_gravityScale);
map.put("m_sleepTime", body.m_sleepTime);
List<Fixture> fixtures = new ArrayList<Fixture>();
Fixture fixture = body.m_fixtureList;
while(fixture != null)
{
fixtures.add(fixture);
fixture = fixture.getNext();
}
map.put("m_fixtureList", fixtures);
return representMapping(new Tag("!body"), map, false);
}
}
public class RepresentFixture implements Represent
{
@Override
public Node representData(Object o)
{
Fixture fixture = (Fixture)o;
Map<String, Object> map = new HashMap<String, Object>();
map.put("m_density", fixture.m_density);
map.put("m_shape", fixture.m_shape);
map.put("m_friction", fixture.m_friction);
map.put("m_restitution", fixture.m_restitution);
map.put("m_isSensor", fixture.m_isSensor);
return representMapping(new Tag("!fixture"), map, false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment