blocks in a tile game

Hi all,

What is the best way of implimenting blocks into a tile oriented game? blocks like fences, sign posts, chairs maybe and generally things that have unique data to each block. for example each sign post has unique text attached to each one. The way I have it implimented now is giving me trouble with the unique block.

I’ve got it implimented in the same way my tiles are, the tile class initiates all the tiles. Heres a code snippit of what I mean.
FROM Block.class

public class Block{
	private static SpriteSheet spriteSheet = new SpriteSheet("/spriteSheets/block_sheet.png");

	public static Block[] blocks = new Block[256];
	public static Block empty = new blockVoid		(spriteSheet, 0xFF00FF00, 0);
	public static Block fence = new blockFence		(spriteSheet, 0xFFFF0000, 1);
	public static Block sign = new blockSign		(spriteSheet, 0xFF, 2);

this doesn’t really allow for individual setup because if you change it there then it changes for all of the objects.

Thanks for the help,
James

It changes everything because your variables are static-read more here. Block spritesheet could be static if all blocks have the same unchanging spritesheet, but things like x/y should not be static. I don’t really understand your code, however. An array of blocks within your block class, and three other blocks? “empty”, “fence”, and “sign” are not exactly descriptive variable names.

Have your world store 2 values in each tile position: id, metadata
id is the value to represent the block in that tile, metadata is to give it singularity.
Then have a single instance of each block to receive the metadata and tile position in order to administrate that tile through it’s methods (metadata and tile positions should be used as parameters in the Blocks’ methods.
For example, change the metadata of the tile x,y which represents a fence, to open/close it throught the Fence block instance’s method.
But the sign is a problem here. A sign can hold a lot of text, it’d be hard to hold all those characters in a single integer (metadata), so you could create some kind of tile-entity for the sign to hold the text, or a class to hold sign’s texts and access them via the sign’s metadata?

That doesn’t sound like a bad idea, I might need to make a blockManager class then to determine which block it is and what action to take when the player uses that block… like it would see that the block is a fence and then it would run a use method to open or close the fence but only change the data on that instance of fence… I’ll have to just try and see ;D thanks for the help I’ll let you know if I managed to get it working. I’m trying to keep it as simple as programmably possible.

well that didn’t work… and then it did work…

I removed “blocks” and just made the sign an entity and now everything is working as I wanted it too. Thanks Zhon for the help.