TLDR: I want to have a bunch of classes that can generate resources at different times.
Whenever they generate a resource I would like the PlayerData class to be notified of the amount and type of resource generated.
This is so I can add that or subtract it from the totals which are saved inside the PlayerData class.
I was trying to use an Message subscriber pattern but I can’t figure out how to send a messe
End TLDR
I’m making a moderately complex Incremental Game. The player ranks up generators which produce passive income. The income generated is added to the total income in the player data file.
I want to see how you guys would change the layout. I will try and not over complicate the situation
- There is a [PlayerData class] This class contains your current money and all your unlocks and a list of all the [Generators] you own.
- There is a [Generator class] This class Contains information about the Generator such as name,level,generation amount. Everything related to generation is calculated in this class;
The goal is to make it so whenever one of the Generators produces money it is added to the playerdata class
Here is some simplified Generator Code
//A class that generates income based on the level (rank) of the generator
//Generators can be things like a lemonade stand or a car wash or paper route
//Think Adventure Capitalist
public class Generator
{
public string name; //Name of Generator
public double generationRate; //Amount generated each second
public int level; //Increases amount generated
public Generator(string genName,double GenRate)
{
name = genName;
generationRate = GenRate;
level = 1;
}
//Generator is run each frame
//If enough time has passed we should generate income based on some calculation we hold
//In the GenerateIncome() Method
public void RunGenerator()
{
//If we should generate income then
GenerateIncome();
}
//Calculate income to be generated
public void GenerateIncome()
{
double incomeGenerated;
incomeGenerated = generationRate * level;
}
}
And here is simplified PlayerData code
This class is serialized and saved which is why it does not extend monobehavior
public class PlayerData
{
public double totalMoney; //Account Wide money available for spending
public List<Generator> generatorList; //A list of generators that I have implimented into the game
public PlayerData()
{
//Creating a few basic Generators
generatorList = new List<Generator> ();
generatorList.Add (new Generator ("Lemonade Stand", 10));
generatorList.Add(new Generator("Car Wash",100));
}
//**HERE IS WHERE I NEED HELP
//How would you find out if any generators created income?
//Called each frame by gamemanager in scene
public UpdateGenerators()
{
//For each Generator in our list Run the generator
foreach(Generator gen in generatorList)
{
gen.RunGenerator();
}
//**QUESTIONS**
//Is it possible to Have each generator put out a message that contains the amount generated and the type of income generated
//Then classes who listen for that IncomeGenerated message can do what they want with the information?
}
}