330 likes | 392 Views
Object Oriented Analysis & Design. Game Patterns 3. Contents. Iterator Handle Model Database Appearance Map Magnetism Shield Visual state Automatic mode cancellation Focus Progress. Iterator. Description
E N D
Object Oriented Analysis & Design Game Patterns 3
Contents • Iterator • Handle • Model Database • Appearance Map • Magnetism • Shield • Visual state • Automatic mode cancellation • Focus • Progress
Iterator • Description • Allows access to members of a container in an organized fashion without requiring any knowledge of the structure of the container. • Motivation • Programmers want to store objects in containers and be able to access each of them once • If the programmer has to understand how the container is implemented • This takes extra effort • The programmer will structure his program using this knowledge • The developer of the container will not be able to change his implementation
Iterator • Solution • Provide an iterator which • Returns every member of the container once • Allows you to determine when you are at the end • Visits the members in a pre-defined order • Provides a way to move on to the next member of the container • The Standard Template Library has a nice implementation • Container::iterator is a type used to declare an iterator • Container::begin() returns an iterator to the first member • Container::end() returns a past-the-end iterator
Iterator • STL Iterators • operator++() – moves to next member • operator*() – retrieves value of current member • operator==() – compares iterators • operator!=() – compares iterators • Usage Container::iteratoriter; Container c; while(iter=c.begin(); iter != c.end(); iter++) { GameObject o = *iter; }
Handle • Description • Provide access to another object without revealing how the identity of the other object is represented. • Motivation • We have a container and want to allow the user access to the members of the container via a reference • However, the container might reorganize its storage or change its implementation • Therefore we need the user to have a reference to an object without knowing how the reference is implemented
Handle • Solution • Declare the class in the Handle header, obscuring the implementation class HandleImplementation; class Handle { private: HandleImplementation *impl; public: Handle(); operator=(); operator==(); }; • The details of HandleImplementation are in the .cpp file
Model Database • Description • Provides centralized storage for all models in the game so that there is one place where models can be found. • Motivation • Models are needed by various parts of the game • The level loader loads them • The logic attaches them to the scene graph and detaches them • The renderer draws them on the screen • It can become confusing • To know where a model is located • Whether a model has already been loaded or not • To ensure all parts of the game can access the models
Model Database • Solution • Maintain a central repository of all models • Ensure that each model is in the database only once • Allow models to be retrieved in several ways • Via an iterator • Via an opaque handle • By name • Being able to reference models by name is handy when the models are stored in files and loaded by a level loader rather than the code which will use them
Appearance Map • Description • Isolates the state of an object from the visual appearance of an object • Motivation • An object in a game can change its appearance when • It is in motion • There is some change to the object state which must be shown visually • Having the logic for this in the controller can make the control logic overly complex
Appearance Map • Solution • It is better to create a table which lists the appearance based on • Object state • The time • The state and time information can be passed to the map and the map will return the image which is needed at a particular time in a particular state
Mini Kernel • Description • The control logic for each game object is moved into the object to reduce the complexity of the control logic in the game loop. • Motivation • As the number of objects in a game increase, the game loop looks like: void update() { for(inti = 0; i < numTanks; i++) { updateTankPhysics(tank[i]); updateTankAI(tank[i]); } for(inti = 0; i < numSoldiersi++) { ... } • This gets very complicated
Mini Kernel • Solution • Move the control logic for each object into the object itself • Have the central controller • Maintain a list of active game object • Ask each object to update itself when needed • This has the advantages • Main controller is simpler • Objects can add themselves to controller or remove themselves at any time • Adding a new object type does not require a change to main controller • Logic in main controller is simpler
Usability Patterns • The concept of patterns has been extended beyond pure programming • Patterns are now used to describe solutions to • User interface design • Game mechanics • One area is the usability patterns that try to make games more usable for the player
Magnetism • Description • Provide magnetism on hit detection so that when the user selects near a target, the selection point will be drawn towards the target as if by magnetism. This will make it much easier to hit a target. • Motivation • Games often implement highly accurate hit detection on small targets with the result that players find it difficult to hit a target. This becomes very frustrating to the player and reduces the fun in playing the game.
Magnetism • Solution • The solution is to make the hit detection less accurate. • The simplest way to do this is to expand the bounding area for the object to make it easier to hit. • The result is that the game will be much easier and more fun to play. • This can also speed up the game since a simpler hit detection algorithm can be used.
Magnetism Accurate bounding boxwith sub boxes. This makes the target hard to hit. Accurate bounding boxis a little easier to hit. Larger bounding circle is easiest to hit.
Shield • Description • Game controls which can cause undesirable consequences, such as terminating the game, are protected from accidental use. • Motivation • Many times, there are controls or gestures which cause operations to take place • Sometimes, these can be accidentally triggered when not intended • For example, clicking the mouse to fire the weapon can be dangerous when using a trackpad • There needs to be a way to prevent accidental triggering of operations
Shield • Solution • The solution can take several forms • When the user is in a mode where a control is not needed, it can be disabled and greyed out • Some operations, like terminating the game, can require the user to confirm his or her actions before they actually take place • Other operations could only be activated by double clicking the mouse or a key instead of using a single click.
Visual State • Description • Allows information about the state of the game to be conveyed to the user via visual clues so that the user does not have to remember the state of the game. • Motivation • Characters can have power-ups, be carrying weapons, be under the influence of a spell or be in good or poor health • The player has to be aware of this state information and be able to see it at a glance • If the information is not readily visible, the player must remember and track it all, placing a significant load on the player’s memory and detracting from the game
Visual State • Solution • The solution is to make the information readily visible on the screen • There are numerous way to do this • The image of the player can be altered to show state • Part of the screen can be used to display state information • Billboards in urban settings could be used to indicate state information • Buttons can be used to temporarily show state information • The interface can be configurable to show and hide various state information depending on what the player needs to see • Head up displays can show a wealth of information and can be ignored when the information is not immediately needed
Visual State • Head-up displays are very useful for some types of games, like flight simulators
Automatic Mode Cancellation • Description • Mode based games should allow the mode to be cancelled and the user automatically switched to another mode when commands which are inappropriate to a mode are used. • Motivation • In a real-time strategy game, the player can move a character by • Clicking on the character to select it • Selecting the move operation • Clicking on the destination location for the character • If the player decides he or she does not want to do the move, they should be able to click on another command and automatically cancel the move.
Automatic Mode Cancellation • Solution • The solution is to design the controls such that • When the user is in a mode • The control class watches for commands outside the controls for that mode • Ejects the user from the mode when one of the other commands is given • Passes the command onto a more general-purpose control class
Focus • Description • The amount of information presented to the player is reduced to just what is relevant so the user can focus on the task at hand. • Motivation • The player is presented with a large number of options, many of which are not relevant to the task the user is performing • The user is distracted by information from other parts of the game • The user is overwhelmed by the sheer amount of information and spends valuable time determining what is relevant
Focus • Solution • The solution is to reduce the amount of information and choices presented to the player • Buttons which cannot be used can be grayed out • The character in a sports game that is being manipulated can stay in sharp focus and the other characters can be blurred • State information that is temporarily irrelevant can be hidden until it is needed again • The player can zoom into one area of the game world so the larger world can be ignored
Progress • Description • To visually depict the progress of an operation so that the player can readily see the state • Motivation • When the game is initializing or loading a new level the player is often left for a long period not knowing what is happening • Values like the strength of the character are not immediately visible • The progress in attaining a goal can be difficult to discern
Progress • Solution • Display a progress bar • Change the image or shape of a character to show health • Use an hour glass • Show bottles with various fractions full • Use a numeric counter • Display various number of things, like weapons