0

如何管理 slick 中的碰撞?

我创建了地图,每一个墙的瓷砖都有选项blocked = true;

如何实现碰撞,如果我撞到墙,我可以通过它?此外,如果我到达地图边缘,请阻止移动。

package javagame;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState {

    Animation movingUp, movingDown, movingLeft, movingRight;
    Animation player;
    int[] duration = {200, 200, 200};
    float playerX = 0;
    float playerY = 0;
    float cameraX;
    float cameraY;
    float screenWidth;
    float screenHeight;
    private TiledMap map = null;
    private static final float SPEED = 0.1f;

    private boolean[][] blocked;

    public Play(int state, int x, int y) {
        playerX = (float) x;
        playerY = (float) y;
    }

    private boolean blocked(float x, float y) {
        return blocked[(int) x][(int) y];
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

        map = new TiledMap("map/map.tmx");

        // build a collision map based on tile properties in the TileD map 
        blocked = new boolean[map.getWidth()][map.getHeight()];
        for (int x = 0; x < map.getWidth(); x++) {
            for (int y = 0; y < map.getHeight(); y++) {
                int tileID = map.getTileId(x, y, 0);
                String value = map.getTileProperty(tileID, "blocked", "false");
                if ("true".equals(value)) {
                    blocked[x][y] = true;
                }
            }
        }

        Image[] walkUp = {
            new Image("graphics/player/up0.png"),
            new Image("graphics/player/up1.png"),
            new Image("graphics/player/up2.png")
        };

        Image[] walkDown = {
            new Image("graphics/player/down0.png"),
            new Image("graphics/player/down1.png"),
            new Image("graphics/player/down2.png")
        };
        Image[] walkLeft = {
            new Image("graphics/player/left0.png"),
            new Image("graphics/player/left1.png"),
            new Image("graphics/player/left2.png")
        };
        Image[] walkRight = {
            new Image("graphics/player/right0.png"),
            new Image("graphics/player/right1.png"),
            new Image("graphics/player/right2.png")
        };

        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);

        player = movingDown;

    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

        screenWidth = gc.getWidth();
        screenHeight = gc.getHeight();

        cameraX = (screenWidth / 2) - (playerX / 2);
        cameraY = (screenHeight / 2) - (playerY / 2);

        map.render((int) playerX, (int) playerY);

        player.draw(cameraX, cameraY);

        g.drawString("X: " + playerX + "\nY: " + playerY, 520, 20);
        g.resetTransform();

    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
            player = movingUp;
            playerY += delta * SPEED;
            player.update(delta);
        } else if (input.isKeyDown(Input.KEY_DOWN) || input.isKeyDown(Input.KEY_S)) {
            player = movingDown;
            playerY -= delta * SPEED;
            player.update(delta);
        } else if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A)) {
            player = movingLeft;
            playerX += delta * SPEED;
            player.update(delta);
        } else if (input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_D)) {
            player = movingRight;
            playerX -= delta * SPEED;
            player.update(delta);
        }

    }

}

这是示例源代码(NetBeans 项目):

https://www.box.com/s/9nicp0n067de632kcpj3

4

2 回答 2

2

在更新时,您可以简单地检查阵列。如果是墙,请不要更新。如果是边界,请不要更新。

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    Input input = gc.getInput();
    float yChange = 0, xChange=0;
    boolean capturedInput = false;
    if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
        player = movingUp;
        yChange += delta * SPEED;
        capturedInput = true;
    } else if (input.isKeyDown(Input.KEY_DOWN) || input.isKeyDown(Input.KEY_S)) {
        player = movingDown;
        yChange -= delta * SPEED;
        capturedInput = true;
    } else if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A)) {
        player = movingLeft;
        xChange += delta * SPEED;
        capturedInput = true;
    } else if (input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_D)) {
        player = movingRight;
        xChange -= delta * SPEED;
        capturedInput = true;
    }
    if(capturedInput==true && !blocked(playerX+xChange,playerY+yChange)){
        playerX += xChange;
        playerY += yChange;
        player.update(delta);
    }
}

private boolean blocked(float x, float y) {
    return blocked[(int) x][(int) y];
}

几乎不要更改值,直到您先检查它,然后在一切正常时更改播放器值。

编辑:我添加了一个变量 captureInput,这会让我知道我们实际上有一个 keyDown 事件。对此有两条评论。我认为这是使您的代码工作的“黑客”,而不是最漂亮的。我个人会尝试在窗口中添加一个事件侦听器以侦听按键点击,当点击按键时,我会更改位置信息,一直刷新框架并将更新,或者仅在发生更改后刷新通过调用重绘()。

有很多教程可以听 onKeyDown。您的代码现在应该可以更好地工作,并且不需要像您所做的那样手动捕获边缘,您应该能够在布尔块数组中的边缘周围画一堵墙,如果没有,您可以继续使用您的方法。

于 2012-11-22T00:19:16.117 回答
0

尝试从XY位置乘以瓷砖尺寸创建块的矩形,然后使用该intersects()方法验证它是否与您的实体发生冲突,如wiki Slick中所述。

我希望这对你有用。

于 2012-11-25T06:46:09.723 回答