Slick2d |渲染和更新的最佳方式

问题描述 投票:0回答:1

问题:

您认为渲染和更新实体的最佳方式是什么?
目前我正在使用世界一流的技术来做到这一点。

友情链接:

游戏(已完成)

代码(来自World.java):

List<Entity> entities = new ArrayList<Entity>();

public void render(GameContainer gc, Graphics g, Camera camera) {
    // Draw background
    background.draw(0 - camera.getX(), 0 - camera.getY(), 2100, 2100);

    // Loop through all entities and render
    for (int i = 0; i < entities.size(); i++) {
        Entity entity = entities.get(i);

        // A very long if... render only if visible
        if (entity.getX() < player.getX() + ((gc.getWidth() / 2) + entity.getWidth() + 50) &&
            entity.getX() > player.getX() - ((gc.getWidth() / 2) + entity.getWidth() + 50) &&
            entity.getY() < player.getY() + ((gc.getHeight() / 2) + entity.getHeight() + 50) &&
            entity.getY() > player.getY() - ((gc.getHeight() / 2) + entity.getHeight() + 50)) {
                entity.render(gc, g);
            }
        }
    }


    public void update(GameContainer gc, int delta) {
        // Loop through entities and update all time
        for (int i = 0; i < entities.size(); i++) {
            Entity entity = entities.get(i);
            entity.update(gc, delta);
    }
}
java render slick2d
1个回答
2
投票
List<Entity> entities = new ArrayList<Entity>();

public void render(GameContainer gc, Graphics g, Camera camera) {
   background.draw(0 - camera.getX(), 0 - camera.getY(), 2100, 2100);
   for( Entity entity : entities ) {
      if( entity.isVisible( player, gc )) {
         entity.render( gc, g );
      }
   }
}

public void update(GameContainer gc, int delta) {
   for( Entity entity : entities ) {
      entity.update( gc, delta );
   }
}

Entity.update()
不应修改列表,就地使用:

private final List< Entity > toBeRemoved = new LinkedList<>();

public void update(GameContainer gc, int delta) {
   toBeRemoved.clear();
   for( Entity entity : entities ) {
      if( entity.update( gc, delta )) {
         toBeRemoved.add( entity );
      }
   }
   entities.removeAll( toBeRemoved );
}

Entity.update()
当实体需要删除时返回 true。

© www.soinside.com 2019 - 2024. All rights reserved.