31
Video Game Development: Design Patterns A. Babadi 1 of 31 In The Name Of God Video Game Development Amin Babadi Department of Electrical and Computer Engineering Isfahan University of Technology Spring 2015 Design Patterns

08. Design Patterns

Embed Size (px)

Citation preview

Page 1: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 1 of 31

In The Name Of God

Video Game Development

Amin Babadi

Department of Electrical and Computer Engineering

Isfahan University of Technology

Spring 2015

Design Patterns

Page 2: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 2 of 31

Outline

Design patterns

Some useful programming patterns

Some useful usability patterns

Page 3: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 3 of 31

Design Patterns

Using OOP, bigger programs could be handled.

A series of new techniques have been proposed to increase project complexity once again and keep programs organized. o STL is a good example.

In recent years, a new technique has surfaced, which is somewhat similar to STL in the sense that it allows you to use predefined components, but its scope and ambition is much broader.

Page 4: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 4 of 31

Design Patterns (DP)

Design patterns are proven solutions to well-established software engineering problems.

What’s the different between design patterns and data structures?

Black Box

A well-known DP can be used as a black box that we can trust.

Input Output

Page 5: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 5 of 31

Types of Design Patterns

There are many types of DPs.

The two that are most useful to game developers are 1. Programming patterns, and

2. Usability patterns.

We are going to review some of the most popular patterns in both classes

Page 6: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 6 of 31

1. Programming Patterns

Programming patterns describe specific coding problems and their standard solutions.

Some of the most popular programming patterns: 1. Singleton

2. Strategy

3. Factory

4. Spatial index

5. Composite

6. Flyweight

Page 7: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 7 of 31

1.1. Singleton

A singleton is a global object for which only one instance exists in the whole application.

What singletons are used in a game?

Problem: We need to have all singletons visible at all times, and we only want to store one of these in memory.

What is the solution?

Page 8: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 8 of 31

1.1. Singleton

This has been solved in two ways, neither of which is especially elegant! 1. Passing the singleton as a parameter to all calls requiring access to it.

2. Define the singletons in a source file and reference them using the extern mechanism.

Cohesion

Binding

Cohesion-binding relationship in an OOP component.

Page 9: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 9 of 31

1.2. Strategy

Sometimes you will need to create objects whose behavior can be changed dynamically.

Which objects need strategies in a game?

Problem: We need to handle different strategies.

How can we deal with strategies?

Page 10: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 10 of 31

1.2. Strategy

Take a soldier, for example!

He might have an update routine that recalculates all his AI.

Idle

Fight Escape

A simple state chart for an AI soldier.

Page 11: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 11 of 31

1.2.1. A Quick Solution

A quick and dirty solution would be to store a state variable and use it to drive a switch construct.

void recalc_AI()

{

switch (state)

{

case FIGHT: recalc_fight(); break;

case ESCAPE: recalc_escape(); break;

case IDLE: recalc_idle(); break;

}

}

Page 12: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 12 of 31

1.2.2. An Elegant Solution

class soldier

{

public:

soldier(strategy *);

void recalc_AI();

void change_strategy(strategy *);

private:

point pos;

float yaw;

strategy* _thestrategy;

};

Page 13: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 13 of 31

1.2.2. An Elegant Solution

soldier::soldier(strategy *stra)

{

_thestrategy = stra;

}

void soldier::recalc_AI()

{

_thestrategy->recalcstrategy(pos, yaw);

}

void soldier::changestrategy(strategy *stra)

{

_thestrategy = stra;

}

Page 14: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 14 of 31

1.2.2. An Elegant Solution

class strategy

{

public:

virtual int recalc_strategy(point,float) = 0;

protected:

strategy();

};

class fightstrategy : public strategy

{

public:

strategy();

virtual int recalcstrategy(point, float);

}

Page 15: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 15 of 31

1.3. Factory

Modern applications need to create and dispose of objects continually.

What do games need factories for?

Problem: Creating objects on demand and destroying them at the end of their life cycle.

How can we handle factories?

Page 16: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 16 of 31

1.3. Factory

class Product {};

class Texture : public Product {};

class Mesh : public Product {};

class Item : public Product {};

typedef int ProductId;

#define TEXTURE 0

#define MESH 1

#define ITEM 2

class AbstractFactory

{

public:

Product*Create(ProductId);

};

Page 17: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 17 of 31

1.4. Spatial Index

As games grow in complexity, the need for fast 3D tests increases as well.

What are examples of situations where spatial indexing is needed?

Problem: We need to perform queries without examining the whole set.

What is the solution?

Page 18: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 18 of 31

1.4.1. Spatial Index as a List

The simplest (and slowest) spatial index is a regular linked list.

Is using a linked list an efficient solution?

Items can be stored in a simple linked list.

Page 19: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 19 of 31

1.4.2. Spatial Index as a Regular Grid

A regular grid divides the space into buckets of the same size.

Bucket size can be determined at load time (Why?).

A diagram showing the grid spatial index with lists of items in each cell.

Page 20: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 20 of 31

1.5. Composite

Many types of games need to hold heterogeneous collections of data together for different reasons.

What heterogeneous objects can be seen in a game?

Problem: Most programming languages only support homogeneous arrays, so a higher-abstraction solution is needed.

How can we reach to a more abstraction level?

Page 21: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 21 of 31

1.5. Composite

We can create part-whole heterogeneous hierarchies where we can access primitives and composite objects using a standard interface.

?

Levels

Walls Trees

Enemies

Tanks Patrols

Objects

Having all data in a single structure makes traversal more intuitive.

Page 22: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 22 of 31

1.6. Flyweight

Flyweight pattern is extremely useful when we need to have large collections of objects that are fundamentally the same except for a few parameters.

What examples of such objects can be seen in games?

Problem: We want to use system resources efficiently while keeping a uniform access interface.

How can we avoid redundancy?

Page 23: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 23 of 31

1.6. Flyweight

The flyweight pattern suggests dividing the object into two separate classes.

First, we need to create the actual flyweight, which is the core object and is shared among all instances.

Second, we will need external objects that will use the flyweights, passing the extrinsic (thus, state dependent) information as a parameter.

Page 24: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 24 of 31

2. Usability Patterns

Usability patterns deal with interface design, human-computer interaction, and related concepts.

Some of the most popular usability patterns: 1. Shield

2. State

3. Automatic mode cancellation

4. Magnetism

5. Focus

6. Progress

Page 25: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 25 of 31

2.1. Shield

A shield is a protective layer that prevents the user from accidentally activating a feature or function that causes undesirable side effects.

A classic example of shields is the confirmation messages to leave the game.

Page 26: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 26 of 31

2.2. State

States are visual cues of the user's current configuration.

Speadbreaker mode in Need for Speed: Most Wanted (2005)

Page 27: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 27 of 31

2.3. Automatic Mode Cancellation

The automatic mode cancellation (AMC) pattern can be used in games that have modes/automata.

Mortal Kombat 3 (1995)

Page 28: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 28 of 31

2.4. Magnetism

Magnetism is a very powerful paradigm to use when we need the user to be precise.

FIFA 14 (2014)

Page 29: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 29 of 31

2.5. Focus

The focus pattern is useful when we need the user to concentrate on a specific piece of information so the rest becomes momentarily irrelevant.

FIFA 12 (2012)

Page 30: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 30 of 31

2.6. Progress

The main application of progress pattern is displaying quantitative information about a process with a beginning and an end, so the user knows how long the process is and exactly where he is currently.

Indigo Prophecy (2005)

Page 31: 08. Design Patterns

Video Game Development: Design Patterns A. Babadi 31 of 31

References

Sanchez-Crespo’s textbook,

Wikipedia, and

Some other sources on the Internet.