Ok, as I understand it you want to make a custom grid system to store structures at specific grid coordinates (X, Y)? so that you can query them and see if and what ‘type’ of structure is at any given location? If this is the case you can use a Dictionary for it and then do all sorts of magic with that, let’s explore =)
Let’s start by making a custom key type for the Dictionary, you can do something like this, your milage may vary and this is untested code:
public struct GridCoordinate
{
public int X;
public int Y;
public GridCoordinate(int x, int y)
{
X = x;
Y = y;
}
public override bool Equals(object obj)
{
if (obj is GridCoordinate other)
{
return X == other.X && Y == other.Y;
}
return false;
}
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
}
We will probably need some sort of enumeration to detect the type of structure:
public enum StructureType
{
None,
ShortGun,
LongGun,
// etc.. whatever you need =-)
}
Our Dictionary:
Dictionary<GridCoordinate, StructureType> grid = new Dictionary<GridCoordinate, StructureType>();
So now we have the basic system set up, here’s how we can use it. To add a structure to your new grid system you can do something like this (again I haven’t really tested this code):
public void AddStructure(int x, int y, StructureType structureType)
{
GridCoordinate coordinate = new GridCoordinate(x, y);
if (!grid.ContainsKey(coordinate))
{
grid.Add(coordinate, structureType);
}
else
{
Debug.Log("A structure already exists at this location.");
}
}
To check if a structure exists at a specific location, you can use:
public bool HasStructureAt(int x, int y)
{
GridCoordinate coordinate = new GridCoordinate(x, y);
return grid.ContainsKey(coordinate);
}
To get the structure type at a specific location:
public StructureType GetStructureAt(int x, int y)
{
GridCoordinate coordinate = new GridCoordinate(x, y);
if (grid.TryGetValue(coordinate, out StructureType structureType))
{
return structureType;
}
return StructureType.None;
}
Finally, to check if a specific structure is adjacent to another structure, you can use a method like this:
public bool IsStructureAdjacent(int x, int y, StructureType targetStructure)
{
GridCoordinate[] adjacentCoordinates = new GridCoordinate[]
{
new GridCoordinate(x - 1, y),
new GridCoordinate(x + 1, y),
new GridCoordinate(x, y - 1),
new GridCoordinate(x, y + 1)
};
foreach (GridCoordinate coordinate in adjacentCoordinates)
{
if (grid.TryGetValue(coordinate, out StructureType structureType) && structureType == targetStructure)
{
return true;
}
}
return false;
}
Had a bit of fun with this, I’m not sure if this is what you exactly wanted, but in the very least it should give you a good idea for how to go about implemented what you want.