Computer Science – Game DesignUC Santa Cruz Announcements Website – Readings and sample code updated – Last year’s mid-term exam with answer key – Mid-term.

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Ch 12: Object-Oriented Analysis
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
Decorator & Chain of Responsibility Patterns Game Design Experience Professor Jim Whitehead February 2, 2009 Creative Commons Attribution 3.0 (Except for.
Plab – Tirgul 12 Design Patterns
Computer Science – Game DesignUC Santa Cruz Today Publish/Subscribe Design Pattern Delegates Sprite movement Particles.
Computer Science – Game DesignUC Santa Cruz Game Jam Two teams from CMPS 20 – Less Than Royal – Colon Trey.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Code Organization UML Patterns Code Smells Feb Arnav Jhala.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Delegates & Events Observer and Strategy Patterns Game Design Experience Professor Jim Whitehead January 30, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Particle Systems Final Exam Study Guide Game Design Experience Professor Jim Whitehead March 16, 2009 Creative Commons Attribution 3.0 (Except copyrighted.
Lecture 1 CS171: Game Design Studio 1I UC Santa Cruz School of Engineering 5 January 2010.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
Modelling classes Drawing a Class Diagram. Class diagram First pick the classes –Choose relevant nouns, which have attributes and operations. Find the.
Chapter 3.4 Programming Fundamentals. 2 Data Structures Arrays – Elements are adjacent in memory (great cache consistency) – They never grow or get reallocated.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ fundamentals.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Database Management System Prepared by Dr. Ahmed El-Ragal Reviewed & Presented By Mr. Mahmoud Rafeek Alfarra College Of Science & Technology Khan younis.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
© 2012 The McGraw-Hill Companies, Inc. All rights reserved. word 2010 Chapter 3 Formatting Documents.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Object Oriented Software Development
Appendix D UML at a Glance Summary prepared by Kirk Scott 1.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
ITEC0724 Modern Related Technology on Mobile Devices Lecture Notes #2 1.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
Design Patterns: MORE Examples
Where are we ? Setup Sprites Input Collision Drawing Sprites
MPCS – Advanced java Programming
Factory Patterns 1.
Behavioral Design Patterns
Object-Orientated Programming
Introduction to Events
Decorator Pattern Richard Gesick.
Presentation transcript:

Computer Science – Game DesignUC Santa Cruz Announcements Website – Readings and sample code updated – Last year’s mid-term exam with answer key – Mid-term exam date

Computer Science – Game DesignUC Santa Cruz Problem: Adding exhaust, shield to spaceship Vertically scrolling 2D space shooters have a spaceship that: – Banks left or right, depending on motion – Typically have three animations: Banking left, straight ahead, banking right Also want to – Add particle effect for exhaust if the ship is moving up the screen But turn this off if it’s moving down the screen – Add a shield in front of the ship if the player picks up a shield powerup Could accomplish all of these visuals using a series of combined animations – Bank left, bank left and shield, bank left and exhaust, bank left and shield and exhaust, etc.. – But, leads to combinatoric expansion Isn’t there a better way? Why, yes! Ikaruga, Treasure

Computer Science – Game DesignUC Santa Cruz Decorating the spaceship with shield & exhaust Instead of pre-defining all possible animations… Draw the shield, exhaust, and spaceship separately – Can think of “decorating” the spaceship with a shield, or an exhaust particle effect, as needed – No longer need large combinations of animations Some powerup options in Gradius Gratuitous picture of a Vic Viper model

Computer Science – Game DesignUC Santa Cruz Decorators using object-oriented code Basic idea is to create a linked list of objects – Each object draws one part of final scene… E.g., the exhaust, the shield, etc. – … then calls the next object on the list – The final object is the main object being decorated E.g., the spaceship spaceship shield exhaust Exhaust, shield, and spaceship are object instances

Computer Science – Game DesignUC Santa Cruz Decorator pattern IComponent interface gives just the decoration Operation() – Can mix in this Interface with a wide range of classes Component is terminal element (spaceship) Decorator provides decorating elements (shield, exhaust) – Diamond-headed arrow indicates that Decorator instances have a reference to an instance of IComponent (another Decorator, or a Component) Example of Decorator pattern

Computer Science – Game DesignUC Santa Cruz Essence of the Decorator pattern The essence of the Decorator pattern – Linked list of object instances, first one calls next one, etc. – Each object adds its particular computation (often, but not exclusively, visual) to the output before passing to next object – It is possible to have multiple chains, each ending at the same terminal (or decorator) object Can pre-arrange commonly used decorator chains

Computer Science – Game DesignUC Santa Cruz Implementation details Need to know next object in chain – Typically passed into the constructor Could have a separate method instead (would need to add this to IComponent May need to know details about terminal object – Need to know position of spaceship to put shield directly in front, and exhaust directly in back – Each object in list can be given reference to terminal object Use reference to spaceship object to retrieve its current location May need to remove a decorator – E.g., shields no longer active – Will need to write code that removes a decorator instance, and repairs the links among remaining ones – If the removed decorator is the first in the chain, removal is easy

Computer Science – Game DesignUC Santa Cruz Decorator Pattern: Pros and Cons Pro: Favors composition over inheritance – Decorator chains created at runtime, can be dynamically changed – Avoids combinatoric expansion of subclasses Pro: Avoids feature-rich parent classes – New decorations are “pay as you go” – Avoids need for adding all features into large, customizable parent class Con: Decorator and component aren’t identical – Requires more complex handling of situations where you really want direct access to terminal component Con: Many little objects – Can result in designs that have lots of little objects that are very similar – Can be hard to learn and debug, since so many classes affect output

Computer Science – Game DesignUC Santa Cruz Problem: Magic system with variable effects Consider a game that has castles, fighters, wizards, and magic cars – Fighters can be inside castles – Wizards have spells: Some only affect the castle, weakening its walls (lightening bolt) Other, more powerful spells, weaken the walls and do damage to the people inside (fireball explosion) If the people inside the castle are inside a magic car, the car absorbs some, but not all, of the damage Need some way to have the applied damage vary depending on who is inside what – E.g., damage depends on containment

Computer Science – Game DesignUC Santa Cruz An approach: Chain of Spell Handlers Create objects that – Represent a cast spell – Represent castles, fighters, cars Create a chain of object references that represent containment – Castle –references-> Car –references-> Fighter Pass each spell object along the containment chain – Castle handles spell first, absorbs some damage, reduces power of spell, and passes it along to Car – Car handles spell, reduces power of spell (no damage – it’s magic), and passes along to Fighter – Fighter handles spell last, applying damage

Computer Science – Game DesignUC Santa Cruz Chain of Spell Handlers Chain of object references looks like Decorator chain – Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 100 HP = 500 HP = 100 HP = 50 Fireball spell handed to castle for processing first

Computer Science – Game DesignUC Santa Cruz Chain of Spell Handlers Chain of object references looks like Decorator chain – Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 25 HP = 425 HP = 100 HP = 50 Fireball spell handed to castle for processing first Castle reduces its HP, and reduces power of fireball

Computer Science – Game DesignUC Santa Cruz Chain of Spell Handlers Chain of object references looks like Decorator chain – Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 10 HP = 425 HP = 100 HP = 50 Castle hands Fireball spell off to car (next in chain), which reduces spell power

Computer Science – Game DesignUC Santa Cruz Chain of Spell Handlers Chain of object references looks like Decorator chain – Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 0 HP = 425 HP = 100 HP = 40 Car hands Fireball spell off to fighter (next in chain), which applies remaining spell power as damage. Fighter is end of chain, so no further handoffs.

Computer Science – Game DesignUC Santa Cruz Chain of Responsibility Pattern IHandler interface – Successor: reference to next object in the chain – Request(): method that receives request object (e.g. spell), and takes action on it Handler1, Handler2 – Specific handler implementations (e.g., castle, car, fighter) – Each handles requests in a different way

Computer Science – Game DesignUC Santa Cruz Implementation details Request is often represented as an object instance – In this case, need to add IRequest interface and Request1…RequestN implementations to UML diagram +Request(IRequest r) Request1Request2 // Request state > IRequest

Computer Science – Game DesignUC Santa Cruz Chain of Responsibility: Pros and Cons Benefits – Reduces coupling between requests, and objects that handle requests Would be very easy to add a new character class (say, a thief) and have it handle spells in a class-specific way, without modifying the spell class – Dynamic modification of request handling Can change the chain of request handlers at runtime For example, as fighters enter/exit cars, castles, etc. Drawback – Handling isn’t guaranteed Since it is up to each object in the chain whether to take action, there is no global guarantee that anyone will handle a request

Computer Science – Game DesignUC Santa Cruz Problem: Representing Game State In most computer games, there is some state associated with the entire game – Current level number – Pointer to level data – Time elapsed while playing – High score during this play session Want one and only one instance of the object holding this state Also want to easily gain access to this state anywhere in the game – Avoid need for long data passing chains

Computer Science – Game DesignUC Santa Cruz Approach: Singleton Pattern Singleton pattern ensures only one instance is ever made of a class – Wait.. How is this possible? Can’t I have my code just call “new” as many times as I want? – Not if the constructor is private! Ensures that only code within the class can call the constructor – OK, so now I can’t call new, which means there is no way to ever create an instance, right? – Wrong – a private class variable (“instance”) in the Singleton is initialized by calling the Singleton’s constructor – Great, but how do I get access to “instance” if it’s private? – Use a public static property (“UniqueInstance”). You can always call this since it’s static.

Computer Science – Game DesignUC Santa Cruz Singleton Implementation Constructor is private by default In C#, the first call on any methods, properties or constructors of a class causes its class variables to be set up. – First access to the UniqueInstance property causes variable “instance” to be initialized by calling constructor – Hence, first access to UniqueInstance causes new instance to be created, then returns a reference to it – Clever! Sealed keyword means no subclasses are possible public sealed class Singleton { // Private Constructor Singleton() { } // Private object initialized by calling private // constructor static readonly Singleton instance = new Singleton(); // Public static property is used to retrieve // reference to the Singleton instance public static Singleton UniqueInstance { get { return instance; } } Code from C# 3.0 Design Patterns, p Originally from:

Computer Science – Game DesignUC Santa Cruz Singleton Advantages Pro: – Ensures only one instance of class is ever created – Can get access to data in this class from anywhere Con: – Acts like global variables – Can cause methods to have unintended side- effects, due to data sharing via state in the singleton

Computer Science – Game DesignUC Santa Cruz Homework Read in C# 3.0 Design Patterns (linked from website) – pages 8-22 (Decorator pattern) – pages (Chain of Responsibility pattern) – pages (Factory Method and Singleton patterns)

Computer Science – Game DesignUC Santa Cruz Potential Exam Topics As Univ. of California students, you are expected to be able to assess complex material and make judgments concerning its relative importance. That said, it can be helpful to have some input from the Professor to help focus studying activity. The following are questions/topics that are likely, but not guaranteed to appear on the exam. Anything covered in class or in the assigned readings may appear, even if not explicitly mentioned today.

Computer Science – Game DesignUC Santa Cruz Potential Exam Questions C# language – Basic syntax – Foreach – Properties – Delegates – Lists – Arrays – Visibility rules – Enumerations – Interfaces and Abstract classes Know how to use these, and difference between these Abstract methods – Be prepared to read and understand C# code examples, and to write short segments of C# code

Computer Science – Game DesignUC Santa Cruz Potential Exam Questions Object-oriented design – Know the difference between procedural and object- oriented design Know the indicators of a pure procedural design – Know the nouns/verbs rule of thumb for determining classes and methods from a problem description Refactoring – What is a bad code smell? How is it related to refactoring? – Know the bad code smells covered in class Duplicate code, long method, long class, switch, feature envy Be able to define these, and identify these in a code sample Know how to refactor these to remove them

Computer Science – Game DesignUC Santa Cruz Potential Exam Questions Be able to define classification – Know how classification relates to the is-a relationship – Know how abstraction relates to classification UML structure diagrams – Know how to represent a class in UML – Know how to represent inheritance (is-a) and aggregation (contains) relationships among classes – Given some C# code, be able to draw a UML diagram for it

Computer Science – Game DesignUC Santa Cruz Potential Exam Questions XNA – What is a spritebatch? – What is a clock tick? How long is the default clock tick in XNA? – What is the difference between Update() and Draw() methods? – Be able to understand code that receives input from an Xbox controller Design patterns – Know UML diagrams for each of the design patterns covered in class Strategy, Observer, Decorator Need to memorize these, and be able to reproduce them Need to understand how they work Given an example of one these patterns, be able to identify the pattern – Know what these mean: Favor composition over inheritance Write to an interface, not an implementation