LibGDX使用属性和图层进行平铺地图碰撞检测

时间:2015-08-03 05:27:16

标签: java libgdx

我正在用libGDX构建一个简单的自上而下的RPG游戏,但是没有找到关于如何在平铺地图上检测碰撞的精确指针。

我有一个看起来像这样的字符类

public class Son{

    SpriteBatch batch;
    Texture texture;
    Sprite sprite;

    private TiledMapTileLayer collisionLayer;


    public Son(float x, float y, TiledMapTileLayer collisionLayer){
        this.collisionLayer = collisionLayer;
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("characters/xter1.png"));
        sprite = new Sprite(texture);
        sprite.setX(x);
        sprite.setY(y);

    }

    public void render(){

        processKeys();
        batch.begin();
        sprite.draw(batch);
        batch.end();

    }


    private void processKeys(){
        if(Gdx.input.isTouched()){
           //Gdx.app.exit();
            Gdx.app.log("x touched", Gdx.input.getX() + "");
            Gdx.app.log("y touched", Gdx.input.getY() + "");

            //480 by 840

            //left
            if(Gdx.input.getX() >= 0 && Gdx.input.getX() <= 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/6 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/6 + 80){
                    sprite.setPosition(sprite.getX() - 10, sprite.getY());
                }
            }

            //right
            if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/8.4) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/8.4) + 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/6 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/6 + 80) {
                    sprite.setPosition(sprite.getX() + 10, sprite.getY());
                }
            }


            //up
            if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/16.8) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/16.8) + 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/3.2 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/3.2 + 80){
                    sprite.setPosition(sprite.getX(), sprite.getY() + 10);
                }
            }
            //down
            if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/16.8) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/16.8) + 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/48 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/48 + 80){
                    sprite.setPosition(sprite.getX(), sprite.getY() - 10);

                }
            }
        }
    }

}

此代码只是根据用户按下的屏幕部分更改精灵的位置。

包含平铺地图的类看起来像这样

public class StageOneScreen implements Screen {
    TiledMap tiledMap;
    OrthographicCamera camera;
    FitViewport viewPort;
    TiledMapRenderer tiledMapRenderer;
    Stage stage;
    ControllerRenderer controllerRenderer;
    Son son;
    private Music music;

    final float GAME_WORLD_HEIGHT = 100;
    final float GAME_WORLD_WIDTH = 50;

    private TiledMap collisionMap;
    private TiledMapTileLayer collisionLayer;


    public StageOneScreen(){
        float aspectRatio = (float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth();

        camera = new OrthographicCamera(GAME_WORLD_HEIGHT * aspectRatio, GAME_WORLD_HEIGHT);
        camera.position.set(GAME_WORLD_WIDTH/2, GAME_WORLD_HEIGHT/2, 0);
        //viewPort = new FitViewport(800, 400, camera);
        camera.setToOrtho(false, 800, 400);
        camera.update();


        tiledMap = new TmxMapLoader().load("stages/stage1.tmx");
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);

        stage = new Stage(new ScreenViewport());
        stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void show() {
       // playMusic();
        controllerRenderer = new ControllerRenderer();
        son = new Son(100, 100, collisionLayer);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glBlendFunc(GL20.GL_BLEND_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        tiledMapRenderer.setView(camera);

        tiledMapRenderer.render();
        controllerRenderer.render(camera);
        son.render();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        controllerRenderer.dispose();

    }

    public void playMusic(){
        // load music asset and check if the loop state before playing it
        music =  SoundAsset.loadMusic(SoundAsset.MUSIC_NAME);
        music.setLooping(true);
        if (music.isLooping())
            music.play();
    }
}

在我正在渲染的平铺地图上,我已经给出了某些瓷砖属性&#34; wall&#34;。我也使用两层。瓷砖我不想要&#34; Son&#34;能够穿过的角色在第二层。如何阻止角色通过某些瓷砖?

1 个答案:

答案 0 :(得分:0)

如果您使用的是TiledMap编辑器:http://www.mapeditor.org/, 您可以使用编辑器中的某些标记设置碰撞属性,并从代码中检索值。

当我使用Libgdx进行小型2D游戏时,我有一个策略模式来绘制和移动对象。 假设您已经设置了一些属性,只需阅读它并执行您的逻辑。像“玩家移动,如果nextCase == wall,nextCase = currentCase”