Help on tiles, in my game !

Hi. I’m trying to recreate mojang’s Catacomb Snatch, in slick… so I can learn a bit more about game programming. And I have a few problems right now.

I created Tile classes ( Tile, WallTile, FloorTile etc ). Also created Level Class to setTile( initialize tile )… and other stuff. Right now, in my Play class, I’m creating tiles based on color from level image, and store them in array Tile[][]. Then in floortile class I need to determine does the tile next to it cast shadow, if so, set the floortile to be the one with shadow. It works picking up left and top tile from level, but right tile doesn’t. Here is the code :

Play class stuff :

tiles = new Tile[level.getWidth()][level.getHeight()];

		for (int y = 0; y < level.getHeight(); y++) {
			for (int x = 0; x < level.getWidth(); x++) {
				int col;

				col = level.getColor(x, y);

				if (col == 0xFF0000)
					tile = new WallTile();
				else if (col == 0x000000) {
					if (y > 0) {
						if (tiles[x][y - 1] instanceof FloorTile) {
							tile = new EdgeTile();
						} else
							tile = new HoleTile();
					}
				} else if (col == 0xff7777)
					tile = new TreasureTile();
				else
					tile = new FloorTile();

				tiles[x][y] = tile;
				level.setTile(tiles, x, y);
			}
		}

FloorTile:

public void init(Level level, int x, int y) {
		super.init(level, x, y);
		shadowateTile();
	}

	private void shadowateTile() {
		Tile w = level.getTile(x - 1, y);
		Tile n = level.getTile(x, y - 1);
		Tile s = level.getTile(x, y + 1);
		Tile e = level.getTile(x + 1, y);

		tile = randomTile(floortiles, 4, TileSize, TileSize, 0, 0);

		if (w != null && w.castShadow())
			tile = cutTile(floortiles, TileSize, TileSize, 64, 32);
		if (n != null && n.castShadow())
			tile = cutTile(floortiles, TileSize, TileSize, 32, 64);
		if(e != null && e.castShadow())
			tile = cutTile(floortiles, TileSize, TileSize, 96,32);
	}

	public void render() {
		tile.draw(x * 32, y * 32);
	}

Level class :

public void setTile(Tile[][] tiles, int x, int y) throws SlickException {
		this.tiles = tiles;
		this.tiles[x][y].init(this, x, y);
	}

	public Tile getTile(int x, int y) {
		if (x < 0 || y < 0 || x > 47 || y > 47)
			return null;
		return tiles[x][y];
	}

I saw mojang team uses different way of setting the tiles, like Tile[x + y * width]; … I don’t know how to set that up unfortunately…

I don’t know this game you’re speaking of or what you’re trying to do. But I can explain what the mysterious ‘Tile[x + y * width]’ is.

Tile[x][y] == Tile[x + y * width]

These are the same thing if we assume they have the same width. Is it better to use one over each other? No, it’s all a matter of preference - I think the latter approach just stuck on since in early C (programming language) IIRC you HAD to use it since you couldn’t make multi-dimentional arrays straight up but had to juggle it with a long array.

Consider A) and B)

A:
[0][1][2]
[3][4][5]
[6][7][8]

width = 3;
height = 3;
size = 9;
B:
[0][1][2][3][4][5][6][7][8]
size = 9;

If we consider B with the same length as A (I.e 3) then

A[0][1] == [0 + 1 * width] == 3

Oh ok. The thing I want to achieve is to get the north, west and east tile, and see if they cast shadow, if so put the shadow tile next to them. It works for left one, and top one, but the right one ( east - e tile ) doesn’t work, and I don’t know why…

To make it a little bit more clear, in every Tile class, there is method castShadow() which simply returns true or false.

Since I don’t know how cutTile() works exactly I just take a guess looking at your paramaters:


if (w != null && w.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 64, 32); // last 2 parameters are x and y?
if (n != null && n.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 32, 64);
if(e != null && e.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 96,32); // 96 seems wrong looking at the other parameters, should be 0 if I follow the logic for the other two

Maybe I’m completely off. Hard to tell since I can’t see the code for it and it seems to work for the other two tiles.

Already started a debugger and stepped through the processing of eastern tiles ?

cutTile gets the specific tile from image, last 2 paramateres are x and y, the 64, 32, is not based really logicaly, it’s coincidence :slight_smile:

Here you go full project, so you can see the pictures and stuff :

Slick Snatch

The problem is, that it seems to not return me tiles[x+1][y].

Also, I just found out that it doesn’t work if I add on the x or y position… so x + 1 or y + 1 doesn’t work, but x - 1, or y - 1 work…

This is really weird for me.

Unbelievable !

For some reason level.getTile(x + 1, y - 1) works, but not the way I want it to ( makes tiles below wall also ), but level.getTile(x + 1, y) will not work, not at all, it even says there is no tile at that position…

   private void shadowateTile() {
      Tile w = level.getTile(x - 1, y);
      Tile n = level.getTile(x, y - 1);
      Tile s = level.getTile(x, y + 1);
      Tile e = level.getTile(x + 1, y);

      Tile here = level.getTile(x, y);
      System.out.println(x + ", " + y);
      System.out.println(n + " " + e + " " + s + " " + w);
      System.out.println(here == this);
      System.out.println();

      tile = randomTile(floortiles, 4, TileSize, TileSize, 0, 0);

      if (w != null && w.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 64, 32);
      if (n != null && n.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 32, 64);
      if(e != null && e.castShadow())
         tile = cutTile(floortiles, TileSize, TileSize, 96,32);
   }

What does this print?

I think I almost got the error. If I try to use shadowate tile method, in the tile that is just created, then it tries to sample tile from ahead of it, which is still not created !

Yeah, I’ve just been reading through this, and was about to say just that.
You should do these things separately.

First create the tiles from the image, then initialize them all.

Got it to work ( using some mojang’s code ) !