I am a bit stumped on how to achieve this, so I have simplified the problem as much as I can, and hope to get a suggestion or two.
I have a class called creature.
class Creature
{
private string m_species;//This needs to be updated whenever any of the other variables change. Based on a list of user-defined species.
private string m_name;
private int m_numberOfLegs;
private SkinColorEnum m_skinColor;
//etc...
}
I would like to somehow be able to define species, and then compare instances of Creature against this. (A human would have pink or black skin. An orc would have green skin.)
To make it more difficult, I would also like to serialize/deserialize both the instances of creature, and instances of species. That way I can make editors where I define my creatures and species, and after importing into my actual program (game, in this case) do a comparison and figure out which species each creature belongs to.
So the definitions of species would be something like:
class Species
{//Not actual c# syntax. Just trying to explain.
Creature.m_name;
Creature.m_numberOfLegs;
Creature.m_skinColor;
}
Human:
m_name == ?; //Don't care what the name is.
m_numberOfLegs == 2; //Must have 2 legs.
m_skinColor == (Black || Pink);//Need to be able to specify multiple possible values.
Orc:
m_name == ?; //Don't care what the name is.
m_numberOfLegs == 2; //Must have 2 legs.
m_skinColor == Green;//Need to be able to specify multiple possible values, even though we only need 1 for orc.
Is there any C# construct that can do this? Maybe there is some way to use Attributes? Inheritance? I can't think of anything that would work well.
Features like giving me compiler errors if I add variables to Creature but not Species would be a bonus.
Use case:
1. The user starts the "EditSpeciesAndCreatures" executable.
2. The user defines a list of species. Each species has for every variable in "Creature" defined either: A set of matching values, a single value or no value(indicating it does not matter what that variable is).
3. The user defines a list of creatures. Simply setting each variable to something. Strings as strings, ints as ints, enums as enums. Nothing unusual.
4. The user saves the two lists to file. (Serializing)
5. The user starts "The Game", and the game loads the file. (Deserializing)
6. All the values in each Creature are set as they were defined in the file.
7. All the values in each Species are set as they were defined in the file.
8. A comparison is run with every creature against every Species. The first match results in Creature's m_species variable being set to that Species.
9. All creatures are now marked as their respective species.
Additional use case:
10. One or more of the creatures change. Skin color, number of legs, anything.
11. A new comparison is run for these Creatures, and a new species is found, unless the changed variable did not matter to any of the defined Species.