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

时间:2016-09-01 16:35:35

标签: java libgdx platform inventory

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

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

我目前在我的地图中基于对象图块加载我的项目,这些项目位于名为“项目”的图层上,如下所示:

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);
}

我的Item类如下:

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中实现这一点。

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

此致

詹姆斯

1 个答案:

答案 0 :(得分:2)

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

将所有会议室的项目列表存储在地图对象中,并在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中删除项目,该列表将反映您下次进入会议室时的内容。