简体   繁体   English

获取平铺播放器站立(播放器不根据(等距)TiledMap LibGDX移动

[英]Get tile player is standing on (player doesn't move according to (isometric) TiledMap LibGDX

I'm developing a game resembling the popular 'Warlock' custom map from WC3. 我正在开发一款类似于WC3中流行的“ Warlock”自定义地图的游戏。 For the game I am using a tiledmap, however the player does not (and is not supposed to) move using the LibGDX tiledmap api. 对于游戏,我使用的是tiledmap,但是玩家没有(也不应该)使用LibGDX tiledmap api移动。 Instead the player moves by vectors. 而是玩家按矢量移动。 I'm trying to register which tile the player is standing on, by taking in the player's world coordinates and finding the respective Row and Column for that exact tile. 我正在尝试通过获取玩家的世界坐标并为该确切的瓷砖找到相应的行和列来注册玩家站在哪个瓷砖上。

I seem to have it properly working for an orthographic tiledmap with the following piece of code: 我似乎可以使用以下代码对正交的tiledmap正常工作:

  private boolean checkIfOnLava()
    {
        for (Entity e : world.getEntities(PLAYER)) {

            float height = currentLayer.getTileHeight() * currentLayer.getHeight();
            float width = currentLayer.getTileWidth() * currentLayer.getWidth();

            float playerX = e.get(Position.class).getX();
            float playerY = e.get(Position.class).getY();

            int tileRow = (int) (playerX / currentLayer.getTileWidth());
            int tileCol = (int) Math.abs((playerY - (height / 2)) / currentLayer.getTileHeight());
            System.out.println("Row: " + tileRow);
            System.out.println("Col: " + tileCol);
            if (currentLayer.getCell(tileRow, tileCol).getTile().getId() == 3) {
                System.out.println("Walking on lava");
                return true;
            }
        }
        System.out.println("walking on ground");

        return false;
    }

However, when working with an isometric map it seems a bit more tricky to find the respective column and row of the tile. 但是,在使用等轴测图时,查找图块的相应列和行似乎有些棘手。 I think I'll have to use some math for Rombus' instead of squares, but I'm not sure if thats correct, and if so, which math to use. 我认为我必须对Rombus使用一些数学运算而不是平方,但是我不确定这是否正确,如果可以,则不确定使用哪种数学。

正交和等距平铺图

I found a solution to my problem, with the use of the following method, it can now identify the Row and Column of the tile the player is standing on, based on its position in pixels. 通过使用以下方法,我找到了解决问题的方法,它现在可以根据播放器所在的像素位置来确定播放器所站立的拼贴的行和列。

private boolean OnLava()
{
    for (Entity e : world.getEntities(PLAYER)) {

        float playerX = e.get(Position.class).getX();
        float playerY = e.get(Position.class).getY();

        int tileRow = (int) (playerX / currentLayer.getTileWidth() - (playerY / currentLayer.getTileHeight()));
        int tileCol = (int) Math.abs((tileRow * currentLayer.getTileHeight() / 2 + playerY) / (currentLayer.getTileHeight() / 2));
        if (currentLayer.getCell(tileRow, tileCol).getTile().getId() == 3) {
            return true;
        }
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM