Sunday, 29 September 2013

Polymorphism, design patterns, and Iron Heart

This is less of a development blog and more of a discussion about design patterns for Last Life's game: Iron Heart. As discussed last week, Iron Heart will be a 3D tank game that will utilize the Havok physics engine, Ogre rendering engine, as well as a wrapper developed by my TA called TwoLoc. The topic today is how can we use polymorphism to its greatest potential as well as other design patterns. So, let us get to the meat of this.

How is polymorphism useful? 

"Polymorphism is a language feature that allows a collection of objects of different types to be manipulated through a common interface." (Gregory, 2009, p. 94). What this means is that we can have a template class with different functions and variables that other classes can inherit. So when you want to have a conversation with C++ about making your code cleaner, easier to use, and easier to debug, this is how you want to structure your classes.

As mentioned in the previous post, Iron Heart is a tank arena game. Now, there are two important things to take into consideration when designing your polymorphic code: inheritance and aggregation. When you have a base class, let's say class Tank, the types of relationships sub classes can have are, "IS A" and "HAS A".

Using Inheritance

Inheritance can be described as a "IS A" relationship. A sub class, or child class, that inheritances from a parent class will inherit all of its functions and variables. Essentially, the child class is a copy of the parent class. For example: Iron Heart will have a Tank class. This tank class will have different functions and attributes that make up the class.


As you can see, the tank class, the parent class, has an attack function, as well damage and health. The heavyTank "IS A" tank, and the teslaTank "IS A" tank. The heavyTank and teslaTank inherit the health and damage variables so it is easy to set their own variables. The reason different attack functions exist is because of "virtual" functions. Whenever you make a function virtual it means that your going to be using it polymorphically and that way whenever we're using the heavyTank attack or the teslaTank attack, it knows it is supposed to look in the specific class as opposed to the parent class.

While this isn't the only use of "IS A" relationships in a game, this is merely an example of how you can save on time and code when developing your game.

Using Aggregation/Composition

I had very little idea of what aggregation was until I started reading other student blogs and the courses textbook. While I'm still learning of how to use aggregation, here is a summary of what I've learned so far. "Composition is the practice of using a group of interacting objects to accomplish a high-level task" (Gregory 2009, p. 96). Aggregation is a way of describing a "HAS A" relationship. This is when you a class inside another class. For example, a teslaTank object "IS A" tank, but a teslaTank object "HAS A" weapon, where weapon would be its own class. What composition/aggregation does is help make individual classes more simple and focused.

Different Design Patterns and they can be useful

  • Singleton
    • A singleton is a class that has a single instance and has a global point of access to it. Singletons are often called managers. They are useful for eliminating the need for externs. It has a single member variable that is static and a function to get the instance of that class. This way ensures we always have one instance of our class. Ogre3D makes heavy use of singleton classes for its construction/deconstruction order. 
    • For a game like Iron Heart, it would be useful to use singletons for manager classes for construction/deconstruction order so we avoid memory errors. It is also easier to organize your code, so a little forward planning can help later down the road.
  •  Facade
    • The facade pattern is a manager class that provides a single interface to a large collection of related classes, often a set of singletons. One has to be careful when developing a facade in that is encapsulates related classes, and does not because a massive all-encompassing class.
    • This is excellent for making communication between subsystems easier. It can use a single interface for communication and reduces the interdependancies. 
  • State
    • I have to admit, this is the design pattern I understand the least. States in games can be a pain to keep track of, from experience. While enums can be useful for organizing your states, it becomes tricky later on if you have a lot of states, more so if some funcionality must be shared between different states. 
Example of a State Manager
    • What the example image above demonstrates is a state management design pattern. What we have is a manager, the context class. Then a base state class with a function to switch states. (Notice how every child state class inherits that function from the base state class). Those child state classes then have a goNext() function which allows them to take a state as an argument. This allows them to switch to a different state. 
    • This is very useful for enemy AI for example. An example base state for Iron Heart AI may be "Search". When the AI finds a player it would switch to state "Attack". If the tank defeats the player, it would switch back to "Search", if it was destroyed, it would switch to a "Death" state. 
  • Factory
    • Factory pattern is a design pattern that deals with the creation of objects. It is exactly what it sounds like: an assembly line that can pump out new instances of all types. All you have to do is pass in an ID and have the factory create the objects for you. This relies heavily on inheritance. The factory can create new meshes, models, or other entities. 
    • While I would have a lot of fun programming a factory class for Iron Heart, it would have limited uses because Iron Heart would be a prototype. However, if the game were to feature a "Horde Mode" or a campaign, then it would  be useful for pumping out dozens of different instances of tanks. While I could still make some use out of it for creating the different tanks in the arena, it wouldn't have as much of an effect as I would like it to have. 
Conclusion

Polymorphism is a powerful way of laying out your classes. Using inheritance and composition can help reduce code, ease debugging, and make your code easier to read and understand. Couple the concepts of polymorphism with the different design patterns of using singletons, facades, state managers, and factories, it can make your game's code so much more efficient and awesome. I look forward to using these learned concepts in Iron Heart. 

Works Cited:

Gregory, Jason. Game engine architecture. Wellesley, Mass.: A K Peters, 2009. Print.



No comments:

Post a Comment