【问题标题】:libgdx - handling objects outside cam [closed]libgdx - 处理凸轮外的对象[关闭]
【发布时间】:2017-05-16 20:57:10
【问题描述】:

我看过一些 libgdx 教程并学习了如何使用 cam 和使用 tile 制作地图。但是我遇到了一个小问题,我想要一些想法。 我想做RPG游戏,但我想让游戏记住每个怪物的信息,比如位置。但是地图非常大,所以我需要减少渲染量。如何在不占用大量资源的情况下保留每个怪物的信息,包括摄像头外的怪物?

【问题讨论】:

  • 对于大地图,您需要使用一种称为裁剪的方法。至于存储信息,请使用您需要的信息(例如位置、纹理区域等)创建一个 java 对象。

标签: java dictionary libgdx render


【解决方案1】:

我从您的问题中假设您已经在 libGDX 中使用过TiledMaps。您可以使用OrthogonalTiledMapRenderer 来绘制地图,这将使用您传递给它的Camera 对象为您呈现屏幕的正确部分:

OrthogonalTiledMapRenderer renderer =
    new OrthogonalTiledMapRenderer(myMap, 1 / 16f); // Change this to your render scale
                                                  //e.g. 1/32f means your tiles are 32px wide

OrthographicCamera camera = new OrthographicCamera(16, 9); //viewport size: 16x9

...

//Inside your class that implements Screen or extends Game
public void render(float delta) {
    ...
    renderer.setView(camera);
    renderer.render();
    ...
}

要管理您的实体,请查看the Ashley framework。您可以为所有实体提供一个包含其位置的Component,然后创建一个渲染系统来绘制它们。

您必须自己绘制实体,因为它们不是 TiledMap 的一部分,但对于初学者来说,system using Box2dDebugRenderer 的这种实现可能会有所帮助。

如果您使用 Box2D,以下是使用 EntitySystem 的示例(未测试):

public class RenderingSystem extends IteratingSystem {

    public static final Family MONSTERS = Family.all(BoxComponent.class).get();
    //Assumes you've made a BoxComponent class

    private ShapeRenderer shapes;
    private Camera camera;

    public RenderingSystem(Camera camera) {
        super(MONSTERS);
        this.camera = camera;
        this.shapes = new ShapeRenderer();
    }

    @Override
    public void update(float deltaTime) {
        shapes.begin(ShapeType.Line);
        shapes.setColor(Color.RED);
        super.update(deltaTime);
        shapes.end();
    }

    @Override
    protected void processEntity(Entity entity, float deltaTime) {
        Rectangle box = entity.getComponent(BoxComponent.class);
        // ^ Could be done faster using ComponentMappers
        shapes.rect(box.x, box.y, box.width, box.height);
    }

}

【讨论】:

  • 感谢您提供的信息,但我不知道如何使用 ashley 框架...我对编程很陌生。如何在我的项目中实现该库?
  • 你使用 Gradle 来构建你的项目吗?如果是,您可以将这些行 here 添加到您的 gradle 文件并重建。如果不是,libGDX 使用 Gradle,因此您可以使用 this(如果您使用 Eclipse)或 this(如果您使用 IntelliJ)将其导入您的 IDE(Eclipse/IntelliJ)。还有一个 NetBeans 部分here
  • 哦,我没注意到阿什利在那里。我没有生成任何东西,因为我仍在构建我的大地图。我想我会在生成项目时包含它。非常感谢您提供的信息!
猜你喜欢
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
相关资源
最近更新 更多