I was wondering what would be the best way to handle complex structures with relationships?
I am implementing a galaxy generation algorithm for my project, basically i have simple enums of
GalaxySize { Small, Medium, Large };
GalaxyAge { Young, Mature, Ancient};
StarTypes { Black = 1, White = 3, Yellow = 1, Red = 3 };
I am generating stars limited by the value of GalaxySize so far so good.
Then i get randomly a type from StarTypes and create a star from this type.
What i want to do is have a relationship between the StarTypes and the GalaxyAge.
Meaning that in a young galaxy (for example) there will be a higher chance for Yellow and White stars, and in a ancient galaxy there will be a higher chance for black and red stars.
What i am thinking of is having a base chance for a StarType to get “rolled” and then add the modifier depending on the GalaxyAge, which will result in more of the more common stars in the specific galaxy age.
Example: weight (chance) of a white star in young galaxy is 3 base + 3
from the "young" galaxy age modifier
against the weight of a red star which has 3 base + 1 from the "young" modifier.
Resulting in:
White star type weight = (3 + 3 ) * rand.nextDouble()
Red star type weight = (3 + 1) * rand.nextDouble()
Any suggestions on how to realize/represent this functionality since obviously enums alone will not be enough? 🙂
I didn’t really understand the algorithm but you can start from this:
Now let’s define a class for
Galaxy:Now you have to define a class to define the star:
Now let’s define a new class for each star (don’t forget to change algorithm, formula and weights according to what it is in reality for each type):
Of course this can’t be the final code but you can use it as hint to organize your code.
Best benefit is that the
Galaxyisn’t aware of the rules to get a particular type of star, each star type knows its own rules (it means if you add a new star type you won’t need to search for rules spanned all around in many classes). Of course this is valid for other classes too (for example if you add aMiddleAgegalaxy’s age you won’t need to update any other class).Finally don’t forget to change the name of the
Weightproperty to something more meaningful for your domain.