Java游戏碰撞系统冻结窗口

时间:2017-04-01 19:22:10

标签: java collision-detection slick2d code-organization

我正在尝试为游戏测试创建一个基于消息的碰撞系统,其中所有对象都存储在迭代的ArrayList中,并且所有对象都会针对所有其他对象进行检查。如果两个对象发生冲突,则会向两个对象发送消息,并且当发生这种情况时,它会执行sysout(打印)。到目前为止我的代码在下面,我已经包含了我认为相关的类,但如果涉及另一个类,我也可以发布它。 (我正在使用Slick2D和LWJGL)

public class MyGame extends BasicGame {

private ArrayList<GameObject> objects = new ArrayList<GameObject>();
MessageQueue messageQueue = new MessageQueue();
CollisionSystem collisionSystem = new CollisionSystem(objects, messageQueue);

public MyGame() {
    super("My Game");
}

public static void main(String[] arguments) {

    // Start up the game window
    try {
        AppGameContainer app = new AppGameContainer(new MyGame());
        app.setDisplayMode(500, 400, false);
        app.start();
        app.setVSync(false);
        app.setTargetFrameRate(60);
    } catch (SlickException e) {
        e.printStackTrace();
    }

}

@Override
public void init(GameContainer container) throws SlickException {
    Image img = new Image("myimage.png", false, Image.FILTER_NEAREST);

    objects.add(new ObjSpriteObject(20, 20, img));
    objects.add(new ObjSpriteObject(15, 20, img));
}

@Override
public void update(GameContainer container, int delta) throws SlickException {
    collisionSystem.update();
    messageQueue.update();

    for (GameObject object : objects) {
        object.update(container, delta);
    }
}

@Override
public void render(GameContainer container, Graphics g) throws SlickException {
    g.scale(3, 3);

    for (GameObject object : objects) {
        object.render(container, g);
    }
}

}

的MessageQueue

public class MessageQueue {

public ArrayList<Message> messages;

public MessageQueue() {
    messages = new ArrayList<Message>();
}

public void update() {
    while (messages.iterator().hasNext()) {
        Message m = messages.iterator().next();
        process(m);
    }
}

private void process(Message m) {
    switch (m.getType()) {
    case 'p':
        System.out.println("MESSAGE: " + m.getData());
        break;
    case 'c':
        m.getReciever().processMessage(m);
    }
}

}

ObjSpriteObject

public class ObjSpriteObject extends GameObject {

private Image sprite;
protected float vspeed = 0f;
protected float hspeed = 0f;
public boolean hasBB = true;

public ObjSpriteObject(int x, int y, Image sprite) {
    super(x, y);
    this.sprite = sprite;
    this.boundingBox = new BoundingBox(this, x, y, 16, 16);
}

@Override
public void update(GameContainer container, int delta) {
    super.update(container, delta);
}

@Override
public void render(GameContainer container, Graphics g) {
    sprite.draw(x, y);
    g.setColor(Color.red);
    g.draw(getBoundingBox());
}

@Override
public void processMessage(Message m) {
    if (m.getType() == 'c') {
        delete = true;
    }
}

}

CollisionSystem

public class CollisionSystem {

private ArrayList<GameObject> objects;
private MessageQueue messageQueue;

public CollisionSystem(ArrayList<GameObject> objects, MessageQueue messageQueue) {
    this.objects = objects;
    this.messageQueue = messageQueue;
}

public void update() {

    // Iterate through each BB with each BB
    for (int i = 0; i < objects.size(); i++) {

        // If object has bounding box
        GameObject ob1 = objects.get(i);
        if (ob1.getBoundingBox() != null) {
            for (int k = 0; k < objects.size(); k++) {

                // If second object has bounding box
                GameObject ob2 = objects.get(i);
                if (ob2.getBoundingBox() != null) {

                    // Check both bounding boxes and send messages if they collide
                    BoundingBox bb1 = objects.get(i).getBoundingBox();
                    BoundingBox bb2 = objects.get(k).getBoundingBox();
                    // System.out.println(bb1 + " " + bb2);
                    if (bb1.collides(bb2) && bb1 != bb2) {
                        messageQueue.messages.add(new Message(bb1.getParent(), bb2.getParent(), 'p', "Collision!"));
                    }
                }
            }
        }
    }
}

}

当我运行此代码时,游戏会冻结。我觉得它与它的运行就像一个永不退出的while循环有关,但我不明白为什么如果主游戏循环确保事情在计时器上运行它会这样做。为什么这段代码会冻结程序?

0 个答案:

没有答案