gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:23:41 25 4
gpt4 key购买 nike

我正在为个人项目编写一款基于 Jet Set Willy 的游戏。如您所知,角色可以从一个房间移动到另一个房间,并在移动过程中收集元素。

我正在使用 LibGDX 和平铺 map 编辑器。

我目前根据 map 中的对象图 block 加载项目,这些项目位于名为“项目”的图层上,如下所示:

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

这些项目存储在我的游戏屏幕上的数组中,如下所示:

public static Array<Item> items;

收集元素后,我只需将它们从屏幕上删除即可。

要切换房间,我基本上会加载一张新 map ,获取该级别的元素等。问题是,如果我移回原来的房间,我需要再次获取元素,这会再次绘制它们。

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

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

问候

詹姆斯

最佳答案

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

将所有房间的项目列表存储在 map 对象中,并在适当的情况下在 loadItems() 中从 map 中读取。另外,我会避免使用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 中删除项目,列表将在您下次进入房间时反射(reflect)出来。

关于Java/LibGDX - 跟踪从一个房间到另一个房间收集的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39276705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com