0
\$\begingroup\$

So I've been making this game recently in java w/ LWJGL (2) and Slick2d. I've done some stuff like the map; it's a simple 2d array it goes through it and based on what id it is its gonna draw that tile on the screen. I've also made the camera move by drawing the 'world' with an offset (Im trying to start explaining the code here) so when I move I go xOff += speed; (where xOff is the offset on the x).

So that all works now except I cant, just cant, figure out the collision. I've made a "system" that takes the location of a stone tile and then checks if you can move e.g. left or right. It works sort of decent. But... Then I noticed my stupidity when I put more stone tiles in the map. Baisicly I made a loop go through the map and if it runs into a stone tile it'll set the same location in another array to be 1 so to mark it as solid, that other array then I would loop through and get the x and y of the stone tile. It's just that I made this work with only the last generated stone (only 1 stone tile in the map). So that's when I just gave up. Here's the code for the Play state class (a lwjgl thing if you didn't know):

        public int mapw = 30; // 31 column
    public int maph = 30; // 31 row

    public int[][] map = new int[mapw][maph];
    public int[][] bmap = new int[mapw][maph];

    public int tileh = 32;

    public int xOff = 0;
    public int yOff = 0;

    public int x;
    public int y;

    public int bg;


    public int tx;
    public int ty;

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        // grass=1; dirt=2; stone=3
        ball = new Image("res/ball.png");
        grass = new Image("res/grass.png");
        dirt = new Image("res/dirt.png");
        stone = new Image("res/stone.png");

        for (int d = 0; d < maph; d++) {
            for (int c = 0; c < mapw; c++) {
                map[c][d] = 1;
            }
        }

        for (int d = 0; d < maph; d++) {
            for (int c = 0; c < mapw; c++) {
                bmap[c][d] = 0;
            }
        }

        cu = false;
        cd = false;
        cl = false;
        cr = false;

        map[3][4] = 3;
        map[4][4] = 3;
        map[5][4] = 3;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.setBackground(Color.black);
        // tmap.render(xOff, yOff);

        for (int i = 0; i < maph; i++) {
            for (int j = 0; j < mapw; j++) {
                if (map[i][j] == 1) {
                    g.drawImage(grass, j * tileh + xOff, i * tileh + yOff);
                }
                if (map[i][j] == 2) {
                    g.drawImage(dirt, j * tileh + xOff, i * tileh + yOff);
                }
                if (map[i][j] == 3) {
                    g.drawImage(stone, j * tileh + xOff, i * tileh + yOff);
                    bmap[i][j] = 1;
                }

            }

        }

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

        x = xOff + 516;
        y = yOff + 336;

        for (int i = 0; i < maph; i++) {
            for (int j = 0; j < mapw; j++) {
                if (bmap[i][j] == 1) {
                    // This killed me. It only saves the last coordinates I need
                    // There might be an obvious sollution but its like 12 pm.
                    tx = j * 32;
                    ty = i * 32;
                    System.out.println(tx + " - " + ty);
                }
            }
        }

        // !((ty + yOff + 32) > 336 - speed) && 336 < (ty + yOff)
        // Movement
        if (!menuui) {
            if (input.isKeyDown(Input.KEY_W)) {
                // This is the collision checking. I dont even remember what is what, but I hope you get baisicly what this is.
                if ((((tx + xOff) > 516) || ((tx + xOff + 32) < 516))
                        || !((ty + yOff + 32) > 336 - speed && (ty + yOff) < 336)) {
                    yOff += speed;
                }
            }
            if (input.isKeyDown(Input.KEY_A)) {

                if (!((336 + 32) < (ty + yOff) && (336 + 32) > (ty + yOff + 32))) {
                    xOff += speed;
                }

            }
            if (input.isKeyDown(Input.KEY_S)) {

                if ((((tx + xOff) > 516) || ((tx + xOff + 32) < 516))
                        || (((ty + yOff) > 336 + 75 + speed) && !(336 > (ty + yOff))) || 336 > (ty + yOff)) {
                    yOff -= speed;
                }

            }
            if (input.isKeyDown(Input.KEY_D)) {

                xOff -= speed;

            }
        }

        // if(yOff > 0){
        // yOff = 0;
        // }

        // Escape menu
        if (input.isKeyDown(Input.KEY_ESCAPE)) {
            if (!menuui) {
                menuui = true;
            }
        }

        // If inside of the menu
        if (menuui) {
            if (input.isKeyDown(Input.KEY_B)) {
                askq = true;
            } else if (input.isKeyDown(Input.KEY_M)) {
                sbg.enterState(0);
            } else if (input.isKeyDown(Input.KEY_Q)) {
                menuui = false;
            }
        }

        if (askq) {
            if (input.isKeyDown(Input.KEY_Y)) {
                System.exit(0);
            }
            if (input.isKeyPressed(Input.KEY_N)) {
                askq = false;
                menuui = false;
            }
        }

    }

    public int getID() {
        return 1;
    }

    public Play(int state) {

    }

}
\$\endgroup\$
  • \$\begingroup\$ Welcome to GameDev. Would you mind editing your post to only include the relevant code (including class variables so we know what they are) that is causing your problems? It makes it easier for people to read and understand your code. \$\endgroup\$ – Seta Oct 4 '15 at 21:10
  • \$\begingroup\$ I pretty much cut the code down to a minimum, but if you dont see what im saying how do i make the tx and ty store the x and y of the stone tiles, so that i can later find the closest tx and ty to the player \$\endgroup\$ – Ivan Jerza Oct 5 '15 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.