the gamedesigninitiative at cornell university
Lecture 12
Component Design Recall: Problem with Subclassing
Games have lots of classes
Each game entity is different NPC Needs its own functionality (e.g. object methods) Human Orc Want to avoid redundancies Makes code hard to change Common source of bugs Human Human Orc Orc Warrior Archer Warrior Archer Might be tempted to subclass Common behavior in parents Redundant Behavior Specific behavior in children
the gamedesigninitiative 2 Architecture Patterns at cornell university Recall: Problem with Subclassing
Games have lots of classes
Each game entity is different NPC Needs its own functionality (e.g. object methods) Warrior Archer Want to avoid redundancies Makes code hard to change Common source of bugs Human Orc Human Orc Warrior Warrior Archer Archer Might be tempted to subclass Common behavior in parents Redundant Behavior Specific behavior in children
the gamedesigninitiative 3 Architecture Patterns at cornell university Model-Controller Separation (Standard)
Model NPC Store/retrieve object data Limit access (getter/setter) Preserve any invariants Human Orc Only affects this object
Implements object logic Human Human Orc Orc Warrior Archer Warrior Archer Complex actions on model May affect multiple models Example: attack, collide Redundant Behavior
the gamedesigninitiative 4 Architecture Patterns at cornell university Model-Controller Separation (Alternate)
Model Controller
Store/retrieve object data Process game actions Limit access (getter/setter) Determine from input or AI Preserve any invariants Find all objects effected Only affects this object Apply action to objects Process interactions In this case, models Look at current game state are lightweight Look for “triggering” event Apply interaction outcome
the gamedesigninitiative 5 Architecture Patterns at cornell university Programming and Parts of Speech
Classes/Types are Nouns Actions are Verbs
Methods have verb names Capability of a game object Method calls are sentences Often just a simple function subject.verb(object) damage(object) subject.verb() collide(object1,object1) Classes related by is-a Relates to objects via can-it Indicates class a subclass of Example: Orc can-it flee Example: String is-a Object Not necessarily tied to class Example: swapping items Objects are class instances
the gamedesigninitiative 6 Architecture Patterns at cornell university Possible Solution: Decorator Pattern
New Functionality
OriginalReference to Original Request Decorator Object Functionalitybase object Object
the gamedesigninitiative 7 Architecture Patterns at cornell university Java I/O Example
InputStream input = System.in; Built-in console input Reader reader = new InputStreamReader(input);
Make characters easy to read BufferedReader buffer = new BufferedReader(reader);
Read whole line at a time Most of java.io works this way
the gamedesigninitiative 8 Architecture Patterns at cornell university Alternate Solution: Delegation Pattern
Original Reference to Request Delegate Object delegate Object 1
Forward Request
Inversion of the Decorator Pattern
the gamedesigninitiative 9 Architecture Patterns at cornell university Alternate Solution: Delegation Pattern
Original Reference to Request Delegate Object delegate Object 12
Forward Request
Inversion of the Decorator Pattern
the gamedesigninitiative 10 Architecture Patterns at cornell university Example: Sort Algorithms public class SortableArray extends ArrayList{ private Sorter sorter = new MergeSorter(); new QuickSorter(); public void setSorter(Sorter s) { sorter = s; }
public void sort() { Object[] list = toArray(); public interface Sorter { sorter.sort(list); public void sort(Object[] list); clear(); } for (o:list) { add(o); } } }
the gamedesigninitiative 11 Architecture Patterns at cornell university Comparison of Approaches
Decoration Delegation
Pattern applies to decorator Applies to original object Given the original object You designed object class Requests through decorator All requests through object Monolithic solution Modular solution Decorator has all methods Each method can have own “Layer” for more methods delegate implementation (e.g. Java I/O classes) Like higher-order functions Works on any object/class Limited to classes you make
the gamedesigninitiative 12 Architecture Patterns at cornell university The Subclass Problem Revisited
Delegates?
NPC Orc
Slot Human Human Orc NPC Slot
Slot Warrior Human Human Orc Orc Warrior Archer Warrior Archer Archer
Redundant Behavior
the gamedesigninitiative 13 Architecture Patterns at cornell university Component-Based Programming
Delegates Role: Set of capabilities Role 1 Class with very little data A collection of methods Slot Role 2 Add it to object as delegate Entity Slot Object gains those methods Slot Role 3 Acts as a “function pointer”
Role 4 Can-it: search object roles Check class of each role Field storing a Better than duck typing single delegate or Possible at compile time? a set of delegates
the gamedesigninitiative 14 Architecture Patterns at cornell university Model-Controller Separation Revisited
Model Controller
Store/retrieve object data Process interactions Preserve any invariants Look at current game state Data may include delegates Look for “triggering” event Determines is-a properties Apply interaction outcome Components
? Process game actions ? Attached to a entity (model) Uses the model as context
the Determines can-it properties gamedesigninitiative 15 Architecture Patterns at cornell university What about the View?
Way too much to draw Backgrounds UI elements Update Individual NPCs Other moveable objects Cannot cram all in Draw Draw Put it in game object? But objects are models Violates MVC again
the gamedesigninitiative 16 Architecture Patterns at cornell university Solution: A Drawing Canvas
Treat display as a container Often called a canvas Cleared at start of frame Objects added to container Draw contents at frame end Canvas abstracts rendering Passed as reference Hides animation details Like working with widget void draw(Canvas c) { // Specify perspective Implement draw(c) in model // Add to canvas Classic heavyweight model } No problems with extension
the gamedesigninitiative 17 Architecture Patterns at cornell university Solution: A Drawing Canvas
Treat display as a container Often called a canvas Cleared at start of frame Objects added to container Draw contents at frame end Canvas abstracts rendering Passed as reference Hides animation details Like working with widget void draw(Canvas c) { // Specify perspective Implement draw(c) in model // Add to canvas Classic heavyweight model } No problems with extension
the gamedesigninitiative 18 Architecture Patterns at cornell university Drawing and Components
Canvas approach is not perfect Needs “one size fits all” code What if models are different? Slot Draw Entity Slot Remember first two labs Slot Needed multiple passes Different drawing each pass How can we avoid this? Pull drawing out of the canvas But do not put it in the model Unity 3D does this Component programming!
the gamedesigninitiative 19 Architecture Patterns at cornell university Problem: Canvas Methods
Ideal Model1 draw(canvas)
Canvas drawShape(…)
Model2 draw(canvas)
the gamedesigninitiative 20 Architecture Patterns at cornell university Problem: Canvas Methods
In Practice Model1 draw(canvas) Canvas
drawShape1(…)
Model2 drawShape2(…) draw(canvas)
the gamedesigninitiative 21 Architecture Patterns at cornell university Views and Components
Slot Component1 Model1 Slot drawShape1(…) Slot Canvas drawPrimitive(…)
Slot Component1 Model2 Slot drawShape1(…) Slot
the gamedesigninitiative 22 Architecture Patterns at cornell university Views and Components
Primitive Slot effects Component1 Model1 Slot drawShape1(…) Slot Complex, Shape-specific Canvas rendering drawPrimitive(…) Slot Component1 Model2 Slot drawShape1(…) Slot
the gamedesigninitiative 23 Architecture Patterns at cornell university Summary
Games naturally fit a specialized MVC pattern Want lightweight models (mainly for serialization) Want heavyweight controllers for the game loop View is specialized rendering with few widgets Proper design leads to unusual OO patterns Subclass hierarchies are unmanageable Want component-based design to model actions Will revisit this again when we talk about AI
the gamedesigninitiative 24 Architecture Patterns at cornell university