Java/LibGDX跟踪从一个房间到另一个房间收集的项目
我正在为一个个人项目写一个基于Jet Set Willy的游戏。如你所知,角色可以从一个房间移动到另一个房间,边走边收集物品
我正在使用LibGDX和平铺地图编辑器
目前,我根据地图中的对象平铺加载项目,这些平铺位于名为“项目”的图层上,如下所示:
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);
}
我的项目类别如下:
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 楼答案
有很多方法可以做到这一点。这里有一个建议:
将所有房间的项目列表存储在地图对象中,并在适当的情况下从
loadItems()
中的地图中读取。此外,我会避免使用static
,除非确实有必要,如果您对Java还是有点陌生,而且它们通常不是很好的面向对象实践,那么很容易导致潜在的错误然后你可以安全地从
items
中删除项目,列表将在你下次进入房间时反映出这一点