Web tiled layer:libGDX:如何处理来自平铺对象层的精灵的冲突

我正在使用libGDXTiled制作 RPG。我已经有很多东西在工作:标题屏幕,测试屏幕,上面加载了我的地图。我可以读取我放在地图和某些图块上的属性。我也可以在地图上移动所有内容,但是我现在想弄清楚的是:

如何从对象层渲染地图对象并处理冲突?

我想使用平铺的对象层为我的碰撞层。IE:将形状放在我不希望角色能够通过的某些图块 / 区域周围。

这是我到目前为止:

package rawct.awakening;
import java.lang.reflect.Method;
import java.util.Iterator;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapObjects;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TiledMapTileSet;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
public class GameMap extends TiledMap {
    private String TAG = "GameMap";
    private TmxMapLoader loader = new TmxMapLoader();
    private OrthogonalTiledMapRenderer mRender;
    private TiledMap gamemap;
    private TiledMapTileLayer mapTiles;
    private ObjectMap<TiledMapTile, Boolean> Blocked;
    private ObjectMap<TiledMapTile, Boolean> Event;
    private MapObjects mObjects = new MapObjects();
    private MapObject mObj;
    public void draw(OrthographicCamera cam){
        mRender.setView(cam);
        mRender.render();
    // Should render my map object?
        mRender.renderObject(mObj);
    }
    public GameMap(String Map){
        Blocked = new ObjectMap<TiledMapTile, Boolean>();
        Event = new ObjectMap<TiledMapTile, Boolean>();
        gamemap = loader.load("maps/"+Map+".tmx");
        mRender = new OrthogonalTiledMapRenderer(gamemap);
        loadMap(gamemap);
    }
    private Cell getCellAt(float x, float y){
        return mapTiles.getCell((int)x, (int)y);
    }
    private TiledMapTile getTileAt(float x, float y){
        Cell cell = getCellAt(x, y);
        return cell != null ? cell.getTile() : null;
    }
    public boolean isTileBlocked(float x, float y){
        try {
            return Blocked.get(getTileAt(x, y));
        } catch (Exception e) {
            Gdx.app.log(TAG, e.toString());
            return false;
        }
    }
    private void loadMap(TiledMap map) {
        String sI = null;
        Blocked.clear();
        Event.clear();
        try{
            mapTiles = (TiledMapTileLayer)map.getLayers().get(0);
            mObjects = map.getLayers().get("Testing").getObjects();
        } catch (Exception e) {
            Gdx.app.log(TAG, e.toString());
        }
        Gdx.app.log(TAG, "Objects:"+mObjects.getCount());
        for(Iterator<MapObject> mObjs = mObjects.iterator(); mObjs.hasNext();){
        // I have set just about everything possible(I only have one object at the moment so mObj only gets set once.
            mObj = mObjs.next();
            Gdx.app.log(TAG, "Obj:"+mObj.getName());
            mObj.setColor(Color.GREEN);
            mObj.setOpacity(1f);
            mObj.setVisible(true);
//          try {
//              Method Test = getClass().getDeclaredMethod((String) mObj.getProperties().get("Func"));
//              Test.invoke(this);
//          } catch (Exception e) {
//              Gdx.app.log(TAG, e.toString());
//          }
        }
        Array<String> sTilesets = new Array<String>();
        TiledMapTile tile;
        try {
            for(Iterator<TiledMapTileSet> tilesets = map.getTileSets().iterator(); tilesets.hasNext();){
                    sI = tilesets.next().getName();
                    sTilesets.add(sI);
            }
            int tCount = sTilesets.size;
            for(int i = 0; i < tCount; i++){
                for(Iterator<TiledMapTile> tiles = map.getTileSets().getTileSet(sTilesets.get(i)).iterator(); tiles.hasNext();){
                tile = tiles.next();
                if(tile.getProperties().containsKey("blocked")){
                    //Gdx.app.log(TAG, "Tile:" + tile.getId() + " blocked!");
                }
                if(tile.getProperties().containsKey("name")){
                    //Gdx.app.log(TAG, "Name:" + tile.getProperties().get("name"));
                }
                boolean blocked = Boolean.pBoolean(tile.getProperties().get("blocked", "false", String.class));
                boolean event = Boolean.pBoolean(tile.getProperties().get("event", "false", String.class));
                Blocked.put(tile, blocked);
                Event.put(tile, event);
            }
        }
            Gdx.app.log(TAG, "Map Loaded!");
        } catch (Exception e) {
            Gdx.app.log(TAG, "Error:" + e.toString());
        }
    }
    public TiledMap getMap(){
        return gamemap;
    }
}
6

answered a similar question最近在 GameDev 上,已经被同样的问题困扰了一段时间。一个彻底的 Googling 最终导致我a tutorial关于这个主题。而且我的意思是彻底。它似乎是整个互联网上这个问题的唯一解决方案。奇怪

无论如何。我给出的答案和它基于的教程都使用 Box2d 来处理碰撞检测。如果你不是太深入制作你的游戏,我会全心全意地建议走这条路:如果你使用 Box2d,它会为你处理很多现有的代码,但它确实意味着重新思考很多与运动有关的东西,基本上。如果这是你选择做的,那么好的旧的0

如果你不想去那个兔子洞,那么上面的链接仍然建议一个解决方案。我认为你错的地方是试图直接绘制MapObject。试试这个:

MapObjects objects = map.getLayers().get("Obstacles").getObjects();
for(MapObject object : objects) {
    if (object instanceof RectangleMapObject) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();
        // do something with rect...
    }
    else if (object instanceof PolygonMapObject) {
        Polygon polygon = ((PolygonMapObject) object).getPolygon();
        // do something with polygon...
    }
    else if (object instanceof PolylineMapObject) {
        Polyline chain = ((PolylineMapObject) object).getPolyline();
        // do something with chain...
    }
    else if (object instanceof CircleMapObject) {
        Circle circle = ((CircleMapObject) object).getCircle();
        // do something with circle...
    }
}

从那里您处理生成的形状(Rectangle或其他形状),而不是MapObject。它们具有用于碰撞检测的方法,例如overlaps()contains()等。如果您只处理一种形状,则在非 Box2d 游戏中可以使生活更轻松。仅使用3

-1

private void setBlocked() {
    for (int i = 0; i < this.map.getLayers().getCount(); i++) {
        TiledMapTileLayer layer = (TiledMapTileLayer) this.map.getLayers()
                .get(i);
        for (int y = 0; y < layer.getHeight(); y++) {
            for (int x = 0; x < layer.getWidth(); x++) {
                if (layer.getCell(x, y) != null
                        && layer.getCell(x, y).getTile().getProperties()
                                .containsKey("blocked")) {
                    this.mapArray[x][y] = Config.CANTMOVEONPOSITION;
                }
            }
        }
    }
}

要调试我渲染正方形,并将它们填充在“mapState”的颜色中,如下所示:

if (this.map != null) {
            for (int i = 0; i < this.map.width; i++) {
                for (int j = 0; j < this.map.height; j++) {
                    switch (this.map.mapArray[i][j]) {
                    case Config.CHARSTATE:
                        // green
                        de.setColor(0, 1f, 0, Config.DEBUG_ALPHA);
                        break;
                    case Config.CANTMOVEONPOSITION:
                        // black
                        de.setColor(1f, 1f, 1f, Config.DEBUG_ALPHA + 0.1f);
                        break;
                    case Config.MONSTERSTATE:
                        // red
                        de.setColor(1f, 0, 0, Config.DEBUG_ALPHA);
                        break;
                    case Config.MOVETONEXTMAP:
                        // yellow
                        de.setColor(1f, 1f, 0, Config.DEBUG_ALPHA);
                        break;
                    default:
                        de.setColor(0, 0, 0, 0);
                        break;
                    }
                    de.rect(i * Config.TILE_SIZE, j * Config.TILE_SIZE,
                            Config.TILE_SIZE, Config.TILE_SIZE);
                }
            }
            de.end();
        }

这不是优化的!因此,如果您有一张大地图,只显示一个小框架,它仍然会绘制每个正方形。在这种情况下,height and width是高度和宽度的 tilecount。
我希望这确实回答了您的问题,或者您可以在此帮助下找到自己的方式。
问候

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(391)
新时达电梯故障代码大全:Arduino电梯
上一篇
虚幻4能用python编程嘛:用虚幻引擎4编程的platformer游戏中的偏转问题
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(80条)