i'm working on a little RTS Game. Now
i wan't to build special buildings
like a church or something, just in
certain places. The Problem is now
that i don't know how to do this.
I assume you have some sort of object that describes your RTS map layout. Wether it be based on tiles or other methods, I guess the code that want to place the church ask the map if it is possible to do so.
Something similar to:
if (map.CanPlace(church))
{
map.Place(church);
}
Maybe there's some way to make some
texture or something for the terrain
and check if the building is on a
specific field of the texture.specific field of the texture.
This is another part of the puzzle, the data representation. I guess you want a way to design areas where you can place these churches. One way to do this with a texture could simply be to assign each pixel a color value based on a known table of color mappings.
Note that if you are using a simple tile based map, then you can probably do this much easier with a simple space delimited text file, where each character represent a tile of the map. The character value can then apply different meaning to the map.
Anyhow, I'll continue down the texture path you seem to aim for. In some place of the program you need to link color-to-attribute to make sense of the color. This can easily be done with a Dictionary (System.Collections.Generic.Dictionary). We want to map color to something meaningful, so we can make a map between Color and string (Dictionary). Then your map can probe the attribute at any given position by sampling the texture to obtain a color, and then looking up that string in your dictionary.
You probably want to create a simple configuration file or something similar to that where you define what different colors should mean.
So here is a suggestion on how Map.CanPlace(Item item) could look like:
bool CanPlace(Item item)
{
string allowedType = GetAllowedTypeForPosition(item.position);
if (string.IsNullOrEmpty(allowedType)) return true;
if (item.TileType == allowedType) return true;
else return false;
}
I don't have time to dig down into the documentation to find out how to read a pixel from a texture but basically you just need to do that. Figure out where on the texture item.position is going to be, get that color, and look the attribute up in the dictionary based on that color, or return null or string.Empty if it is not found.
I don't know your experience level, but just to make sure you get it across, these functions I wrote here doesn't exist yet of course. You have to create them yourself.