Skip to content

Instantly share code, notes, and snippets.

@TomMalbran
Created August 29, 2016 04:41
Show Gist options
  • Save TomMalbran/f43ba15b0b8b29f8af034f600707b378 to your computer and use it in GitHub Desktop.
Save TomMalbran/f43ba15b0b8b29f8af034f600707b378 to your computer and use it in GitHub Desktop.
public Sprite GetSpriteForFurniture(Furniture furn)
{
string spriteName = furn.GetSpriteName();
if (furn.LinksToNeighbour == false)
{
return SpriteManager.current.GetSprite("Furniture", spriteName);
}
// Otherwise, the sprite name is more complicated.
spriteName += "_";
// Check for neighbours North, East, South, West, Northeast, Southeast, Southwest, Northwest
int x = furn.Tile.X;
int y = furn.Tile.Y;
string suffix = string.Empty;
suffix += GetSuffixForNeighbour(furn, x, y + 1, "N");
suffix += GetSuffixForNeighbour(furn, x + 1, y, "E");
suffix += GetSuffixForNeighbour(furn, x, y - 1, "S");
suffix += GetSuffixForNeighbour(furn, x - 1, y, "W");
// Now we check if we have the neighbours in the cardinal directions next to the respective diagonals
// because pure diagonal checking would leave us with diagonal walls and stockpiles, which make no sense.
suffix += GetSuffixForDiagonalNeighbour(suffix, "N", "E", furn, x + 1, y + 1);
suffix += GetSuffixForDiagonalNeighbour(suffix, "S", "E", furn, x + 1, y - 1);
suffix += GetSuffixForDiagonalNeighbour(suffix, "S", "W", furn, x - 1, y - 1);
suffix += GetSuffixForDiagonalNeighbour(suffix, "N", "W", furn, x - 1, y + 1);
// For example, if this object has all eight neighbours of
// the same type, then the string will look like:
// Wall_NESWneseswnw
return SpriteManager.current.GetSprite("Furniture", spriteName + suffix);
}
private string GetSuffixForDiagonalNeighbour(string suffix, string coord1, string coord2, Furniture furn, int x, int y)
{
if (suffix.Contains(coord1) && suffix.Contains(coord2))
{
return GetSuffixForNeighbour(furn, x, y, coord1.ToLower() + coord2.ToLower());
}
return string.Empty;
}
private string GetSuffixForNeighbour(Furniture furn, int x, int y, string suffix)
{
Tile t = world.GetTileAt(x, y);
if (t != null && t.Furniture != null && t.Furniture.ObjectType == furn.ObjectType)
{
return suffix;
}
return string.Empty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment