Work in progress: A Generalized Game Engine with AS3 in Flex
So, I made the move to Flex about a month back, and have spent most of my time experimenting with how to create a generalized Game Engine. I started with the idea that I would combine the use of a Model View Controller pattern with an OOP State Machine in the view or the controller (depends on which theory you are implementing). I worked out a half-way decent basic object model, but after a few hours, I got annoyed by the constraints of other people’s patterns (as you do) and decided to create my own. I like the idea of these two patterns, but don’t like being hampered by both my lack of complete understanding and my lack of patience over all (blame it on all the Diet Coke). I had also talked to Squize about his implementation of a state machine, and liked his simple effective ideas the best.
Based on the games I have made in AS3 before this, I have chosen some of the main pain points to address with this engine. Usually I start out with the best intentions and then my game logic and code become a mess of some sort. I want to try and prevent that in future games with this model. Here are the other goals that I had in mind when I sat and and started the initial implementation:
1. I need my specialized timer to constantly loop through my game engine. This is so can I animate all game objects (even tweens) with code and control the the speed at which they animate.
2. I need a class that controls the entire game experience, timing, user input, etc
3. I need a class that handles the data for the entire game
4. I need a class that handles the view for the entire game
5. I need a class with game play specific logic
6. I need specialized packages of classes that can be reused for things like blit animation, sounds, etc
That sounds a lot like the MVC and the State Machine patterns, but to be more flexible, I decided to just use the ideas of those as a jumping off point and then try to create my own structure that uniquely addresses all of the problems I usually end up with in my game engines.
So far I have created these classes: (these are not fully fleshed out and are completely subject to changes, additions, and feedback).
1. Main – Contains the main state machine, and handles all user input, and timing. Updates View and GameControl classes. This is the document class (or Always Compile class in FlashDevelop), it extends Sprite.
2. Model – Contains data and does not have a reference to the View or the GameController, but can issue events that they listen for. For instance the model will load in the XML for game and level data, hold high score information.
3. View – Uses Model data to Draw itself. Knows about the Model, but never writes to it. It reads data from Model and listens for updates from the Model. The View, in my instance will also need to listen for updates from Main and the GameControl. For me, the view is very important as the overall render engine. All screens (title, high score, instructions, game play) will be a part of the View.
4. GameControl – The GameControl will handle all of the game play specific logic. While the Model will hold all of the XML loaded data, and provide a facility for obtaining the data, it will not have an methods for acting on it. The GameControl will use the game specific data to initialize itself. It might also update the data in the Model (not fleshed out yet).
Display Manager (View)
I started to fully flesh out the that View will utilize. I started with a basic set of objects for Bitmap rendering:
These are currently in the com/8bitrocket/display package:
TileSheet.as – This is the most basic set of data for a bitmap. The TileSheet holds a source BitmapData object and contains information on tile sizes for the tile sheet. It can be used for 1 tile or any number of tiles that all are related to the same object.
BlitObject.as – Takes a TileSheet as its one parameter, and contains Rectangle and Point information for blitting. This is to be used for objects that don’t animate for there is no need for the additional overhead of properties needed in the BlitAnimation (next).
BlitAnimation.as – Extends BlitObject, but for an animated object. Takes a TileSheet, FrameStart, FrameEnd, and FrameDelay parameters needed for animation the object.
BlitContainer.as – The first real game objects can be created with this class. It contains x,y,dx,dy information, as well as properties for collision offsets. It contains an array of BlitObject (and BlitAnimation) objects that can be layered on top of one another.
BlitCanvas.as – The actual canvas that will be displayed on the screen. It contains an array of BlitContainer layers.
Sound Manager (not started)
I have started my sound manager based on the MP3 tutorial I created last week. My ideas for the sound manager include the ability to dynamically add sound and music to the manager and define listeners for events that will trigger sounds.
GameControl (not started)
While the game control will contain game specific logic, it will also have a set of pre-defined states that all games need (level in, level out, game init). I would also like to aid game logic creation by setting up a set up a Trigger/Response system for adding in game logic.
That’s all I have so far. I have been procrastinating a lot lately, but need to buckle down and finish this so I can start making some new games.