Sorry for the confusing title, I dont know how to resume it that much.
Im making a game, and I have a simple class Mob which is extended by Warrior and Enemy.
A Mob has a Body, and that body usually has one main Fixture.
Im setting the Body’s userdata to the class object (Warrior’s Body has a pointer to Warrior in its UserData) and im setting the Fixture’s userdata to a String which identifies what the fixture is. For example, the Warrior’s Body’s Fixture has the String “warrior” in its UserData.
The problem is, when a contact happens, i have common logic for a Mob, and i have specific logic for Warrior and Enemy.
Still, I cant find a good way to identify the collided fixtures at such a level.
For example, say I have these methods for code readability, since a Contact’s Fixture order is hard to guess and I dislike writing the same code for both Fixtures:
public boolean isPartOfContact(Contact c, String userdata)
{
return (c.getFixtureA().getUserData().equals(userdata) ||
c.getFixtureB().getUserData().equals(userdata));
}
public Fixture extractFixture(Contact c, String userdata)
{
if(c.getFixtureA().getUserData().equals(userdata))
return c.getFixtureA();
if(c.getFixtureB().getUserData().equals(userdata))
return c.getFixtureB();
return null;
}
public Body extractBody(Contact c, String userdata)
{
return extractFixture(c, userdata).getBody();
}
With these, I can write something like:
if(isPartOfContact(contact, "warrior"))
{
if(isPartOfContact(contact, "map")) //warrior vs terrain
{
//Get warrior object
ServerWarrior warrior = (ServerWarrior) extractBody(contact, "warrior").getUserData();
//...
}
}
But what if I want to check for mob? warrior’s userdata is “warrior” and enemy’s userdata is “enemy”. Unless I check for class instances I cant think of a smart way of identifying them correctly.
I realise this really looks like fancy details which shouldnt really matter so soon in development but Im curious about how to create a Box2D ContactListener in a way which doesnt seem “hacked”.