Saturday, 2 November 2013

Iron Heart Development Blog II: State Managers & Polymorphism

When creating a game, whether or not you choose to use tried and true design patterns is up to the developers. Some developers prefer not using design patterns because they feel as if they have to tackle problems in a certain way, while the other side may find them easy to use and structure your data and code. Polymorphism on the other hand is much more widely used as it is a core concept in C++ programming. For Iron Heart, we use design patterns and polymorphism extensively. While I spoke about these topics in a previous development blog, this post will discuss how we implemented certain features, for what purpose, and why.

State Managers are great because they allow you to organize data in an efficient and easy to read manner. Also they are very easy to explain to others who may not be programming guru's in your group. The way Iron Heart organizes its data is through, as I like to call it, a "box in box" method that uses "has a" logic. Essentially, we have a state manager called Game State, and Game State Manager inherits from it. We treat the Game State Manager as the highest level of boxes in our box in box method.

Well, if we have a game, what does a game have? Levels, enemies, AI, etc. So what we do is create a state manager for all of our level so that we can push and pop levels into the game without needing confusing if statements, and all we need in our Game State Manager is a pointer to the current state and then call that update every frame. When the player completes a level, it pushes it out and pops in a new state. This helps reduce the amount code, organize the data, reduce debugging time.

Example State Manager where a level
 inherits from a base level
This technique is incredibly useful for AI. We have a base tank class and several inheriting tanks. We also have a base AI class and several inheriting AI types. What we do is that we simply define each AI in their own individual CPP files, them simply create an AI object in the tank class. After that, we call the AI update for every individual tank object created. So all the programmer has to do is create a for loop that cycles through a vertex buffer calling the update for each tank, or better yet use an iterator that called each object in the vertex buffer and update their AI. If they AI for some reason ever needs to change, it will push and pop the necessary data and the programmer did not have to lift a finger.

So when looking at levels, levels are simply just states handled by a manager. Tanks are included in levels because levels "have" tanks. Tank "have" AI. So it sort of looks like this: Game->Level->Tank->AI, if you wanted to break down the current base state managers so far.

Example state manager for AI
All of this magic is capable simply because of inheritance and some effective c++ designs.

No comments:

Post a Comment