Saturday, May 9, 2015

Day Two: Color


Seems simple enough doesn't it? Well there's a lot of mind-numbing, classy stuff going on behind the scenes. Like Classes. In order to get the Morgs to belong to a "Species" with its own colors, I have to define each species and that means classes. Defining a class is like looking out at the universe and dividing it into things with names. What things in your game's universe are similar and numerous enough that they deserve their own class? How can you reduce the idea of that thing down into a set of attributes?

To get more technical, instead of having an array of active morg transforms, which I would need to dig around in to find the associated scripts anytime I wanted to know something interesting about that morg, I have an array of active morgs... of class "Morg", a custom class I defined to have all the interesting information easily available. No more game object > find > getComponents just to get at the relevant script. If a script is needed, it's part of the classmember I pull directly from the array.
I also started using statics as a way of having some of the variables from gameMaster available and synchronized across all scripts. It's useful for gameSpeed and overall population and such. Again, as with classes, I don't have to pull an instance of the script I need into the script I'm working with just to get at that variable. If it's marked as static, all my scripts seem to know about it. I was thinking the word for that was a "global" but the C# purists of the web warn me all over the place not to use globals. Actually, quite a few warnings against statics too, but it's just what I needed... they recommend something called a "Singleton" instead.... wtf? Now that's new.

To get even more technical, I needed three new classes to pull this off. "Morg" is the workhorse, it contains references to the MorgMotion script and the transform in addition to the usual stuff that defines an individual morg (like age and energy and name). The stuff I actually need in runtime. "MorgDef" is a class of morg definitions. Like a gallery of platonic forms. Where "Morg" is a group of Individuals, this is a group of Species. This is where I associate the type of morg with all the attributes that typify its Species, like its color (and eventually behaviour). And finally "cColor" ("Color" is already a built-in static class) where I define my own custom colors because Unity's defaults aren't pretty enough.

No comments:

Post a Comment