18
Object Oriented Game Framework Design Riz Verghese Joj

Object Oriented Game Framework Design Riz Verghese Joj

Embed Size (px)

Citation preview

Page 1: Object Oriented Game Framework Design Riz Verghese Joj

Object Oriented Game Framework Design

Riz Verghese Joj

Page 2: Object Oriented Game Framework Design Riz Verghese Joj

Frameworks: What are they?

• Semi complete applications• Complete applications can be developed by

inheriting from and instantiating parameterized framework components.

• Provide domain-specific functionality• Business, telecom, databases, games.

• Exhibit inversion of control at run-time• Determines which objects and methods to invoke

in response to events.

Page 3: Object Oriented Game Framework Design Riz Verghese Joj

Benefits

“The most profoundly elegant framework will never be reused unless the cost of understanding it and then using its abstractions is lower than the programmer’s perceived cost of writing then from scratch.”

- Booch, Dr. Dobbs Journal, 1994.

Page 4: Object Oriented Game Framework Design Riz Verghese Joj

Design Patterns

“Each pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to that problem, in such a way that you can use the solution a million times over, without ever doing it the same way twice.”

- Christopher Alexander, A Pattern Language, 1977.

Page 5: Object Oriented Game Framework Design Riz Verghese Joj

Design Patterns … what are they?

• A Solution to a problem in context

• Provide experience reuse instead of code reuse.

• Hundreds of patterns exists!

• Vocabulary: Facilitates communication among developers

• Lets you think at an architecture level, not implementation level.

• Eg: Factory, Observer, Composite, Singleton

Page 6: Object Oriented Game Framework Design Riz Verghese Joj

Frameworks and Design Patterns

Page 7: Object Oriented Game Framework Design Riz Verghese Joj

An example game framework

• Classic arcade style games

• 2D tile based games

• Basic elementary collision

• Supports dynamic class loading

Page 8: Object Oriented Game Framework Design Riz Verghese Joj

Another Game

Page 9: Object Oriented Game Framework Design Riz Verghese Joj

The Design

• Resource Manager• Resource Image• Resource Sound• Resource Sprite

• Game board• Tile Indexer• Tile manager

• Tiles• Immovable Tile• Movable Tile

Page 10: Object Oriented Game Framework Design Riz Verghese Joj

Patterns

• Resource Manager• Factory• Reads from an xml file• Dynamic creation of classes• Associate images and sounds with a tile

• Tile Manager• Observer• Tiles created subscribes to the Tile Manager• Tile Manager tells the board where the tiles are• Tiles have to request movement

from the Tile Manager.

Page 11: Object Oriented Game Framework Design Riz Verghese Joj

Game specific patterns

• Patterns aren’t limited to the framework

• Game specific code can have lots of patterns• The Snake game:• Especially uses the composite pattern

• Framework objects mostly follow the singleton pattern where as game specific objects do not.

Page 12: Object Oriented Game Framework Design Riz Verghese Joj

How does the framework work?

• Resource manager loads the tiles in memory from information in the XML file.

• Level loader prepares an instance of the board, tiles and placement of the tiles on the board.

• Game specific class maps input to tile action (usually movement)

• Tile asks board if it can move to the desired place• Board lets tile move or not• Property of the tile determine what happens when two

tiles occupy the same space• Collide, Consume, Cancel.

Page 13: Object Oriented Game Framework Design Riz Verghese Joj

Implementation specifics

• Two threads• The first allows the board to redraw/refresh• The second allows every tile to ‘think’

• The think method allows the tiles to• Keep moving• Keep falling• Know what to do on collision• Tells other tiles what to do

Page 14: Object Oriented Game Framework Design Riz Verghese Joj

More specifics

• Tiles can have relations• The observer/observable relationship• Eg: Acno game: Door disappears when key is

eaten• Key tells door to disappear

• Eg: Snake gets longer when apple is eaten• Apple tells snake body to spawn

Page 15: Object Oriented Game Framework Design Riz Verghese Joj

Advantages

• Allows you to step back and generalize• More time spent on the framework• Isn’t worth it for just one game

• Allows you to spend time on the game (content) not the implementation• Saves a LOT of time for one particular game

• Clean implementation• The framework code is generally hidden• Game specific code is small (from reusing

framework components)

Page 16: Object Oriented Game Framework Design Riz Verghese Joj

Disadvantages

• Games are limited to the framework

• Can’t build all games

• Features Limited to framework provision and functionality• Sometimes overcome through ugly

workarounds/tricks/hacks

Page 17: Object Oriented Game Framework Design Riz Verghese Joj

References• The development of a Game Playing Framework using

Interface-based programming, Mark Cohen, Issue 12.1, ACM Student Magazine, Fall 2005.

• Object-oriented Game Development, Julian Gold, ISBN: 032117660X, Addison Wesley, 2004.

• Introducing Object Oriented Frameworks, Taligent white paper.

• Designing Reusable Classes, R. Johnson and B. Foote, Journal of Object-Oriented Programming, 1(2):22--35, June/July 1988

• Design patterns: Abstraction and reuse of object-oriented design, E. Gamma, R. Helm, R. Johnson, and J. Vlissides, In European Conference on Object-Oriented Programming Proceedings (ECOOP'93), volume 707 of Lecture Notes in Computer Science. Springer-Verlag, July 1993.

Page 18: Object Oriented Game Framework Design Riz Verghese Joj

The end …

Discussion