LibGDX Tiled在碰撞时获取对象属性

时间:2017-11-12 19:00:56

标签: java libgdx collision-detection tiled

我正在使用LibGDX开发游戏,并使用Tiled设置地图。我为特定图层中的对象添加了一个自定义String属性,以获取更多信息,它代表什么对象。

我设置了一个ContactListener,它调用map对象的抽象类中的方法。听众看起来像这样:

@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();
        }
    }
}

当玩家点击InteractiveMapTileObject实例的对象时,会调用onPlayerBeginContact()方法,如下所示:

@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);
        }
    }
}

在这里,我得到了"武器的对象"地图中的图层,然后遍历它以查找正确的属性及其值。这样可以正常工作。

现在的问题是,我显然在图层中有多个对象,因此有多个MapObject。我需要一种方法来识别玩家碰撞的对象然后获得它的属性。

是否可以使用ContactListener执行此操作,还是需要实现其他功能?我已经搜索了很多帖子,但没有成功。

2 个答案:

答案 0 :(得分:1)

我认为不是从MapObject获取武器名称而是从Weapon类获取它。

每件武器都可以从InteractiveTileMapObject继承。当玩家与灯具发生碰撞时,您可以从用户数据中获取参考,并使用参考获取武器名称。

我希望这个答案对您有所帮助,如果您有任何其他问题,请随时在下面发表评论!

答案 1 :(得分:0)

我通过在抽象类中编写自定义方法来检查特定对象层中每个对象的交集来解决我的问题。

protected MapObject getCellProperties(int layerIndex) {
    MapObjects mapObjects = map.getLayers().get(layerIndex).getObjects();

    for (MapObject mapObject : mapObjects) {
        MapProperties mapProperties = mapObject.getProperties();

        float width, height, x, y;
        Rectangle objectRectangle = new Rectangle();
        Rectangle playerRectangle = new Rectangle();

        if (mapProperties.containsKey("width") && mapProperties.containsKey("height") && mapProperties.containsKey("x") && mapProperties.containsKey("y")) {
            width = (float) mapProperties.get("width");
            height = (float) mapProperties.get("height");
            x = (float) mapProperties.get("x");
            y = (float) mapProperties.get("y");
            objectRectangle.set(x, y, width, height);
        }

        playerRectangle.set(
                playScreen.getPlayer().getX() * MainGameClass.PPM,
                playScreen.getPlayer().getY() * MainGameClass.PPM,
                playScreen.getPlayer().getWidth() * MainGameClass.PPM,
                playScreen.getPlayer().getHeight() * MainGameClass.PPM
        );

        // If the player rectangle and the object rectangle is colliding, return the object
        if (Intersector.overlaps(objectRectangle, playerRectangle)) {
            return mapObject;
        }
    }

    // If no colliding object was found in that layer
    return null;
}

这可能不是最好的解决方案,但效果非常好。