The Problem:
I'm building one of those empire/kingdom building games and so I am trying to create an efficient way of making a "ResourceManager" class handle the games resources (wood, food, iron, stone). I currently repeat a function 4 times to do the same thing.
Attempt 1:
I created a "Resource" class (abstract) and derived the four resources from it. In each resource there are multiple methods which are similar:
Example Food.CS:
new public static void AddResources(int amount) {
Player.currentFoodSupply += amount;
foodText.text = Player.currentFoodSupply.ToString();
}
Compare to Iron.CS:
new public static void AddResources(int amount)
{
Player.currentIronSupply += amount;
ironText.text = Player.currentIronSupply.ToString();
}
The AddResource() function is static because I have other scripts (like buildings, when they get upgraded) needing to communicate with each resource. The base building class currently has a method like this:
public void DeductResources()
{
Food.DeductResources(FoodCost);
Wood.DeductResources(WoodCost);
Iron.DeductResources(IronCost);
Stone.DeductResources(StoneCost);
}
This seems like it's so repetitive that there needs to be a more efficient way to do this.
Attempt 2:
A "ResourceManager" class that anything to do with the 4 resources can go through this one script. I can update that one script instead of numerous scripts that does something with the resources.
Where I am undecided is this one:
public static void DeductResources(Resource resource, int amount)
{
if(resource.GetType() == typeof(Food))
{
Player.currentFoodSupply -= amount;
resource.resourceText.text = Player.currentFoodSupply.ToString();
}
else if (resource.GetType() == typeof(Wood))
{
Player.currentWoodSupply -= amount;
resource.resourceText.text = Player.currentWoodSupply.ToString();
}
else if (resource.GetType() == typeof(Iron))
{
Player.currentIronSupply -= amount;
resource.resourceText.text = Player.currentIronSupply.ToString();
}
else if (resource.GetType() == typeof(Stone))
{
Player.currentStoneSupply -= amount;
resource.resourceText.text = Player.currentStoneSupply.ToString();
}
}
It looks like it works and I can't see a way around calling the above method 4 times as I need to know how much food or wood to add or deduct.
Does anyone have any suggestions please on how to improve this code/call the method once?
I'm always willing to learn! Thank you