I am currently gaining experience in Unity by trying to make a basic Citysim. Since its based on a really small town all my citizen are actual Agents rather than statistical calculations.
I am currently thinking about an Need System but I can't decide on a datatype.
Requirements for the Need System
- All needs are regularly iterated over to decrease their Satisfaction level
- Certain events trigger a function specific to each need:
OnSeekNeed()
,OnSatisfyNeed()
- Different Actors have different needs: Drug addict has a need "Drugs"
- It should be possible to add new generic needs or remove needs from each actor at runtime: Discovery of Internet
AddGenericNeed(Citizen c, Need n, float satisfaction)
- It should be able to add new needs to the game without changing other classes
- The need needs to be comparable: A pizza shop satisfies NeedFood but not NeedSleep`
OLD QUESTION INCLUDING SOLUTION IDEAS - feel free to ignore
I am currently gaining experience in Unity by trying to make a basic Citysim. Since its based on a really small town all my citizen are actual Agents rather than statistical calculations.
I am currently thinking about an Need System but I can't decide on a datatype. Each Agent should have his own needs and an satisfaction value for each. Not all Agents have the same Needs, a child for example does not need romantic interactions. An addict will have special drug needs.
My first Idea was to make an own class for each need, allowing me to make more Needs for DLCs or
Parent Need
public abstract class Need
{
public abstract void OnSeek();
public abstract void OnFurfill();
public abstract void OnNotFurfill();
public abstract bool IsHidden();
public abstract string GetName();
//ect...
}
Each Character should then get needs assigned and on each update they will be looped through and the value (0-100) would be decreased by 0.25f.
But how should I arrange the Needs in the Character Class?
Option1 - Harcoded Array
Each Need has an assigned spot in an float Array.
- con: Enabling or disabling Needs requires further overhead instead of just editing the set.
- con: all Citizen need to have the same need-set
- con: no Object orientation allowing to call special events 'OnSeek()'
Option2 - Dictinary<Need,satisfaction>:
I tough this would be the solution unfortunately it's impossible to iterate over it and change the value. If there was this with TypeOf Need as Key and an over writable value it would be perfect.
- con: It's impossible to change the satisfaction without replacing the entire node
con: Apparently its not possible to use Types as Keys
private Dictionary<Type, float> n2 = new Dictionary<Type, float>(); private Dictionary<Need, float> n3 = new Dictionary<Need, float>(); //both don't seem to work
Option3 - LinkedList<Need>:
- con: slow to find values when events change a need for all citizen 'needs.findByType(NeedRest)'
- con: needs can be added twice. Checking for duplicates is expensive
- con: Each need is a new Object for every Citizen containing the statisfaction float
So far I believe the Linked List will be my best bet, most of the time I will be looping through it anyway. I'll need to check for duplicates on insert but maybe somebody can help me get the Dictionary to work or knows a better solution.
Please keep in mind, I am new to C# and maybe I am missing something completely obvious... While trying to write this question I somehow managed to answer a lot of my problems and came up with something that will sort of work. I'll be posting this anyway just in case somebody is stuck on the same problem or has a solution that is way better.