Java/LibGDX - 从一个房间到另一个房间跟踪收集的物品

Java/LibGDX - Track collected items from room to room

我正在为个人项目编写基于 Jet Set Willy 的游戏。如您所知,角色可以从一个房间移动到另一个房间,边走边收集物品。

我正在使用 LibGDX 和 Tiled Map 编辑器。

我目前根据地图中的对象图块加载我的项目,它们位于名为 'Items' 的图层上,如下所示:

public void loadItems() {
    //////////////////////////////////////////////////////////////////////////
    //create all Items
    for(MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)){
        Rectangle rect = ((RectangleMapObject) object).getRectangle();
        //new Item(screen, object);
        items.add(new Item(this, object, (rect.getX() + rect.getWidth() / 2) / Engine.PPM, (rect.getY() + rect.getHeight() / 2) / Engine.PPM));
    }
}

这些项目存储在我的 Playscreen 上的数组中,如下所示:

public static Array<Item> items;

收集物品后,我只是将它们从屏幕上移除。

为了切换房间,我基本上加载了一张新地图,获取该级别的物品等。问题是,如果我回到原来的房间,我需要再次获取物品,这会再次绘制它们。

//////////////////////////////////////////////////////////////////////////////
/**
 * Load the next Level
 */
public void changeMap(int roomNumber, float x, float y) {
    map.dispose();
    loadMap(roomNumber);


    this.current_level = roomNumber;

    renderer.getMap().dispose(); //dispose the old map
    renderer.setMap(map); //set the map in your renderer

    world = new World(new Vector2(0,-4 ), true);
    world.setContactListener(new WorldContactListener());

    creator = new B2WorldCreator(this);
    loadItems();

    //Reposition Player
    player = new Player(world, this, x * Engine.TILE_WIDTH, y * Engine.TILE_HEIGHT);
}

我的物品class如下:

public class Item extends Sprite {

protected World world;
protected PlayScreen screen;

private float stateTime;
protected TiledMap map;
protected MapObject object;

private Animation animation;
private Array<TextureRegion> frames;
private boolean setToDestroy;
private boolean destroyed;
float angle;
public Body b2body;

FixtureDef fdef;

private Texture tex;
private Texture blank_texture;
private int item_number;

////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Constructor
 * @param screen
 * @param object
 * @param x
 * @param y
 */
public Item(PlayScreen screen, MapObject object, float x, float y){
    this.world = screen.getWorld();
    this.screen = screen;
    this.map = screen.getMap();
    //this.item_number = item_number;

    setPosition(x, y);

    Random rn = new Random();
    int max = 2;
    int min = 1;
    int random = rn.nextInt(5) + 1;

    tex = new Texture(Gdx.files.internal("sprites/item" + random + ".png"));

    frames = new Array<TextureRegion>();

    for(int i = 0; i < 4; i++) {
        frames.add(new TextureRegion(tex, i * 16, 0, 16, 16));
    }

    animation = new Animation(0.1f, frames);

    blank_texture = new Texture(Gdx.files.internal("sprites/blank_item.png"));

    setBounds(getX(), getY(), 15 / Engine.PPM, 15 / Engine.PPM);
    setToDestroy = false;
    destroyed = false;
    angle = 0;

    stateTime = 0;

    define_item();
}


////////////////////////////////////////////////////////////////////////////////////////////
/**
 *Define the Box2D body for the item
 */
public void define_item() {


    BodyDef bdef = new BodyDef();
    bdef.position.set(getX(), getY());
    bdef.type = BodyDef.BodyType.StaticBody;
    b2body = world.createBody(bdef);

    fdef = new FixtureDef();
    fdef.filter.categoryBits = Engine.ITEM_BIT;
    fdef.filter.maskBits = Engine.PLAYER_BIT;

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(7 / Engine.PPM, 7 / Engine.PPM);

    fdef.shape = shape;

    b2body.createFixture(fdef).setUserData(this);
    b2body.setGravityScale(0);
    b2body.setActive(true);
}


public void redefineItem() {

    Gdx.app.log("redefineItem", "Item");

    Vector2 position = b2body.getPosition();
    world.destroyBody(b2body);

    BodyDef bdef = new BodyDef();
    bdef.position.set(position);
    bdef.type = BodyDef.BodyType.StaticBody;
    b2body = world.createBody(bdef);

    fdef = new FixtureDef();
    fdef.filter.categoryBits = Engine.ITEM_BIT;
    fdef.filter.maskBits = Engine.PLAYER_BIT;

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(7 / Engine.PPM, 7 / Engine.PPM);

    fdef.shape = shape;

    b2body.createFixture(fdef).setUserData(this);
    b2body.setGravityScale(0);
    b2body.setActive(true);
}


////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Draw Method
 * @param batch
 */
@Override
public void draw(Batch batch) {
    if(!destroyed) {
        super.draw(batch);
    }
}


////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Update the Items
 * @param dt
 */
public void update(float dt){

    setRegion(getFrame(dt));
    stateTime += dt;

    if(setToDestroy && !destroyed){
        world.destroyBody(b2body);
        destroyed = true;
        setRegion(blank_texture);
        stateTime = 0;
    }
    else if(!destroyed) {
        setRegion(animation.getKeyFrame(stateTime, true));
        setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
    }
}


////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Get the Texture
 * @param dt
 * @return
 */
public TextureRegion getFrame(float dt){
    TextureRegion region;
    region = animation.getKeyFrame(stateTime, true);
    return region;
}


////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Item has been collected
 * @param player
 */
public void collected(Player player) {
    if(Engine.bPlaySounds) {
        Sound sound = Gdx.audio.newSound(Gdx.files.internal("audio/sounds/collect_item.wav"));
        sound.play(1.0f);
    }

    //Change the Category Bit, so that it is no longer collidable
    fdef.filter.categoryBits = Engine.COLLECTED_BIT;
    this.setToDestroy = true;

    Gdx.app.log("Collected Item ", "" + this.item_number + " from room " + screen.getCurrentLevel() );

    //Increment the counter on the HUD
    screen.incrementItemCounter();
}


////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Set the category Filter
 * @param filterBit
 */
public void setCategoryFilter(short filterBit){
    Filter filter = new Filter();
    filter.categoryBits = filterBit;
}


////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Get the Tilemap cell
 * @return
 */
public TiledMapTileLayer.Cell getCell(){
    TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0);
    return layer.getCell((int)(b2body.getPosition().x * Engine.PPM / 16), (int)(b2body.getPosition().y * Engine.PPM / 16));
}

}

我想将我收集的每件物品存储在某种数组中,其中包括房间号和物品的 X/Y 位置。当我重绘项目时,它会跳过收集列表中的任何项目。问题是,我不确定如何在 Java.

中实现这一点

有人对我如何实现这一目标有任何建议吗?

此致

詹姆斯

有多种方法可以做到这一点。这是一个建议:

将所有房间的项目列表存储在地图对象中,并在适当时从 loadItems() 中的地图读取。另外,我会避免使用 static 除非真的有必要——如果你对 Java 还是有点陌生​​,那很容易导致偷偷摸摸的错误,而且它们通常不是好的对象-面向实践。

private final IntMap<Array<Item>> roomsToItems = new IntMap();
private Array<Item> items;

//...

public void loadItems(int roomNumber) {
    items = roomsToItems.get(roomNumber); //get this room's previous list if it exists
    if (items == null) { //this room hasn't been loaded yet
        items = new Array<>();

        //TODO: Load the items into "items"

        //store the items list so it can be retrieved instead of loaded next time:
        roomsToItems.put(roomNumber, items);
    }
}

然后您可以安全地从 items 中删除项目,列表将在您下次进入房间时反映出来。