I'm working on a game in LibGDX, and have a map set up with Tiled. I added a custom String property to objects in a specific layer to get further information, what object it represents.
I have a ContactListener set up, that calls a method in the abstract class of the map object. The listener looks like this:
@Override
public void beginContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
if (fixtureA.getUserData() == "player" || fixtureB.getUserData() == "player") {
// Get either fixture A or B, depending on which of them is the player fixture
Fixture player = fixtureA.getUserData() == "player" ? fixtureA : fixtureB;
// Get the colliding object, depending on which of them is the player fixture
Fixture collidedObject = player == fixtureA ? fixtureB : fixtureA;
// Determine what kind of object the player collided with and trigger the respectable method
if (collidedObject.getUserData() instanceof InteractiveMapTileObject) {
((InteractiveMapTileObject) collidedObject.getUserData()).onPlayerBeginContact();
} else if (collidedObject.getUserData() instanceof Enemy) {
((Enemy) collidedObject.getUserData()).onPlayerBeginContact();
}
}
}
When the player hits an object of an instance of InteractiveMapTileObject, the onPlayerBeginContact() method gets called, which looks like this:
@Override
public void onPlayerBeginContact() {
MapObjects objects = playScreen.getMap().getLayers().get("weapon").getObjects();
for (MapObject object : objects) {
if (object.getProperties().containsKey("weapon_name")) {
String weaponName = object.getProperties().get("weapon_name", String.class);
Gdx.app.log("Picked up weapon", weaponName);
}
}
}
Here, I am getting the objects of the "weapon" layer in the map, and then iterate through it to find the correct property and it's value. This works fine as it is.
The problem now is, that I obviously have multiple objects in the layer, and therefore multiple MapObjects. I need a way to identify the object, that the player collided with and then get it's property.
Is it possible to do that with the ContactListener, or do I need to implement something else? I already searched through a ton of posts, but didn't get lucky.