So, we are at the sixth chapter of the book, and you may be wondering why everything was so simple. You may be wondering where the really complicated parts are hiding out. I will be talking about all the complicated aspects of the flag defense game in this chapter. The source code for the completed web game can be found in the FlagDefenseComplete
folder and the source code for the completed cross-platform game (Android and iOS) can be found in the FlagDefenseDevice
folder.
In this chapter, we will cover the following topics:
If you have not yet played the game, please go ahead, compile the FlagDefenseComplete
project, and give it a go. The code base has the usual single player game as well as a same device multiplayer game, where you can play against a friend by sharing the mouse on the same PC. It is not possible to explain the whole code in this chapter; I am focusing on explaining the complicated aspects only.
Let me briefly explain the organization of the code base. We have a few dependencies such as the Starling framework, the Starling Particles extension, AS3 signals, TweenLite, Flox, and sounds.swc
, which resides in the assets
folder and contains all the sounds used in the game. The default application is Web_Preloader.as
, which shows the loading progress and eventually loads our main entry class. This compiler argument sets up the StartupFrame FlagDefenseComplete
frame.
The libs
package has the Pathfinder
class and com.csharks
has all the game-specific classes. The ResourceManager
class handles playing sounds, accessing support files for particle effects, and populating the AssetManager
instance with all the textures. We are using a single 1024 x 768-based texture atlas embedded in the EmbeddedAssets
class. The GameStates
class has two new states to facilitate the automatic movement of bombs per turn that are mentioned as follows:
BLUE_BOMBS_MOVE
: This moves blue bombs automaticallyGREEN_BOMBS_MOVE
: This moves green bombs automaticallyThe com.csharks.flagdefense.entities
package has the Bomb
class and the Explosion
class, which were introduced earlier. The com.csharks.flagdefense.helpers
package contains the following classes:
The com.csharks.flagdefense.scenes
package has the MainLevel
class and the MultiSamePC
class, which respectively handle the single player and multiplayer modes of gameplay. Additional classes, AiStates
and AiDecisions
, help AiManager
to go through the process of AI decision-making.
Now that you have gone through the code base, I am sure that you can easily understand the logic flow and realize how the game works. Let me reconfirm what you have already found out. The MainLevel
class is the starting point, which initializes an instance of the WorldLayer
class, spawns one soldier each for each side, sets the initial game state, sets up the UI, adds the game loop listener, adds the game UI listener, and a touch listener. The following list explains what the game loop does:
The touch listener handles the following logic:
The touch listener is overridden by MultiSamePC
to make it work for both sides.
The most important part among all this is the following line of code in the game loop, which actually advances the game logic:
worldLayer.render(e.passedTime,groundImage);
3.135.247.181