Getting fancy with tiles

So far I have drawn the background using sprites that were set as public variables. It was convenient for getting things to work fast. However there are things that I don’t like about it.

Managing the sprites manually in Unity sounds really tedious to me with all the clicking and dragging. Furthermore, I’m not working with final assets right now so I expect my tileset to change. So I feel an incentive to  write code that does the boring part for me.

In fact, I want to minimize the work of adding new tiles as much as possible. Its with that desire in mind that I came up with my new scheme for handling the tilesmaps. It works like this:

  1. I created a new class called TerrainData. This new class only holds 3 bits of data: An identifier, the sprites index on the spritesheet, and if it collides with the player (example being water)
  2. In the Tilemap Manager I added a dictionary of TerrainData and filled the dictionary from a JSON file with all my data on my sprites.
  3. From the dictionary I created another collection of Tiles with all the correct sprites and if any of my tilemaps need a tile they can easily pull a reference to it.

Overall, I got all of that working and I’m pretty satisfied with how its going. I’m fully anticipating some unanticipated problems to crop from it that I need to fix. Also spent a good hour on a bug where everything compiled fine but on closer inspection the problem was hiding in plain sight. It’s in the section below.


public void AddPrototype(string key, TerrainTile tt)
{
if (prototypes.ContainsKey(key) == false)
{
prototypes.Add("key", tt);
}
else
{
Debug.Log("Invalid Prototype");
//throw new ArgumentException();
}
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s