I am trying to figure out how I can most easily make it easier to make new types of units in my game. I have buildings and ships, and would like to know how I could make it easy to add new units. I have been recently told about interfaces, and have worked with inheritance a little bit.
What I would like to able to do is have it so that all of the variables and methods common to all ships could be stored in a superclass or interface, and same with the buildings. I would also like to be able to assign behaviours to the buildings and ships, maybe as interfaces, which could contain all of the methods and variables required for the functions of that ship or building.
For example, creating a new type of building that can shoot, build ships, and can regenerate nearby ships. So it would possible inherit all of the variables and methods common to all buildings, such as health, image, x, y, getX(), getY() etc. But it would then also gain the variables and methods essential for its functionality, such as shootRange, shoot(), regenRate, etc.
You could probably structure your code around a base class for the units providing the common attributes and functionality to all units and provide specialized functionality through interfaces that aren't common to all units.
We know everything in our game is a unit so we make a base as follows:
Now we can create a family of classes tying a relationship with the unit class as follows:
Now say we have created the game and for the next release we now decide to include ships with shooting functionality then we create an interface to provide this function as follows:
By declaring this interface any object that needs shooting functionality can implement this interface to provide the shooting functions. And since we know for now that ships can shoot we create the following class:
By following this design we can expand our game based on an abstraction that is loosely coupled making it easy to extend as requirements change.
If we have an aircraft that can shoot we would create it like this:
First create the flyable interface
Now we create an abstraction for all aircrafts
Then create the specialized shooting aircraft
Now to put these units in action we create a ship and an aircraft in our Game class and shoot everything in sight
Ok, that seems as though it could simplify it a fair bit. But the FighterJet would still need to have those methods and variables in its class pertaining to shooting and flying, wouldn't it? But it wouldn't need to include the methods and variables that were in the Aircraft class?
Also, for methods that have been implemented, are you required to have @Override?
Alfie Noakes wrote:Ok, that seems as though it could simplify it a fair bit. But the FighterJet would still need to have those methods and variables in its class pertaining to shooting and flying, wouldn't it? But it wouldn't need to include the methods and variables that were in the Aircraft class?
This illustration was not meant to be taken literally as a solid design to copy. It was mainly trying to demonstrate how to go about supplying abstractions and the way in which you can model a system to promote extensibility. To answer your question, when you go about designing a system you must first do a thorough analysis to determine the requirements which will supply a solid foundation which you can build on. It is after this phase you can determine how you would structure your system to know where variables and methods will go. Although the first design may not be complete, you must initially design it in a way to promote extensibility making the code easy to modify, this was my purpose of illustration.
Alfie Noakes wrote:Also, for methods that have been implemented, are you required to have @Override?
This annotation (@Override) is not required but is recommended to catch errors at compile time with regards to method overriding.
I have an idea of what I want, but I need to figure out how I can make it easy to add to.
I would like it so that in a new class of structure, say a Teleporter, all I would have to do is implement an interface or extend a class. Then all I would have to do is add extra methods and variables required for the teleporting feature.
I'm just wondering how that could be achieved, if it can be.
Alfie Noakes wrote:I would like it so that in a new class of structure, say a Teleporter, all I would have to do is implement an interface or extend a class. Then all I would have to do is add extra methods and variables required for the teleporting feature.
Alfie Noakes wrote:I should be able to access a book such as that. Thanks for your your help.
The main thing whenever you're doing something like this is to thinkbefore you code.
With all these class hierarchies and interfaces, you potentially have a very complex structure, so you need to write it down (possibly in diagram form) so that you can keep track of it in your head. UML seems to be the most prevalent standard these days, and it has a number of diagrams that you might find useful. In you case, I suspect a Class diagram may be what you want.
However, if they look too complicated right now, just draw something that has a meaning to you.
Another tip: Do one thing at at time. Write a Building class and test it thoroughly; then write a Ship class, ditto; then (and ONLY then) maybe try a Shootable interface and a MissileCruiser.
If you try to do everything at once you'll overload your brain.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop