An Introduction to Game Programming with Flash: Design Patterns

25
An Introducton to Game Programming with Flash 3: Design Paerns

Transcript of An Introduction to Game Programming with Flash: Design Patterns

Page 1: An Introduction to Game Programming with Flash: Design Patterns

An Introducton to Game Programming with Flash

3: Design Patterns

Page 2: An Introduction to Game Programming with Flash: Design Patterns

Design Patterns

Characteristc● Universal solutons of common, repeatng problems● Descripton of a soluton, not an implementaton● Mainly depends on OOP paradigm

Pros● Standardises nomenclature● Standardises structure● Independent from programming language

Page 3: An Introduction to Game Programming with Flash: Design Patterns

Design Patterns

● Creatonal● Builder● Abstract Factory● Factory Method● Singleton● Object Pool

● Structural● Decorator● Adapter● Bridge

● Behavioural● Command● Mediator● Strategy

● Architectural● Model-View-Controller

Page 4: An Introduction to Game Programming with Flash: Design Patterns

Singleton Pattern

● Guarantees creatng only single instance of a class

● Doesn't pollute global namespace

● Overused and treated as new global

● Often used to store configuraton

● Alternatve: Dependency Injecton

Page 5: An Introduction to Game Programming with Flash: Design Patterns

Singleton Pattern

Page 6: An Introduction to Game Programming with Flash: Design Patterns

package foo { public class Singleton { private static var instance:Singleton; public function Singleton() { } public static function getInstance():Singleton { if (!instance) { instance = new Singleton(); } return instance; } } }

1

2

3

Singleton Pattern

Page 7: An Introduction to Game Programming with Flash: Design Patterns

import foo.Singleton; var singleton:Singleton = Singleton.getInstance();1

Singleton Pattern

Page 8: An Introduction to Game Programming with Flash: Design Patterns

Object Pool Pattern

● Creates set of objects ready to use rather then creatng/removing objects

● Used when creatng new objects is expensive

● You must remember about pool's limit

Page 9: An Introduction to Game Programming with Flash: Design Patterns

Object Pool Pattern

Page 10: An Introduction to Game Programming with Flash: Design Patterns

package foo { public class EnemyPool { private var enemies:Vector.<Enemy>; public function EnemyPool(count:int) { enemies = new Vector.<Enemy>(); while(--count >= 0) { enemies.push(new Enemy()); } } public function getEnemy():Enemy { return enemies.pop(); } public function returnEnemy(enemy:Enemy):void { enemies.push(enemy) } } }

2

3

Object Pool Pattern

1

Page 11: An Introduction to Game Programming with Flash: Design Patterns

import foo.EnemyPool; var pool:EnemyPool = new EnemyPool(20); var enemy:Enemy = pool.getEnemy();

//when we don't need this enemy anymore pool.returnEnemy(enemy);

2

3

Object Pool Pattern

1

Page 12: An Introduction to Game Programming with Flash: Design Patterns

Strategy Pattern

● Defines a group of algorithms ready to use

● Allows to change algorithms on runtme

● Helps in proper encapsulaton

Page 13: An Introduction to Game Programming with Flash: Design Patterns

Strategy Pattern

Page 14: An Introduction to Game Programming with Flash: Design Patterns

package foo { public class Enemy { private var moveStrategy:IMoveStrategy; public function Enemy(movingStrategy:IMoveStrategy) { this.moveStrategy = moveStrategy } public function update():void { moveStrategy.execute(this); } } }

1

Strategy Pattern

3

2

Page 15: An Introduction to Game Programming with Flash: Design Patterns

var enemy:Enemy = new Enemy(new FastMoveStrategy()); enemy.move(); 1

Strategy Pattern

Page 16: An Introduction to Game Programming with Flash: Design Patterns

Command Pattern

● Encapsulates all data needed to method invocaton later on

● Single Responsibility Principle

● Fire and Forget

● Commonly used for mult-level undo

Page 17: An Introduction to Game Programming with Flash: Design Patterns

Command Pattern

Page 18: An Introduction to Game Programming with Flash: Design Patterns

package foo { public class CreateEnemyCommand implements ICommand { public function execute():void { //add enemy & play sound here } } }

1

Command Pattern

Page 19: An Introduction to Game Programming with Flash: Design Patterns

import foo.CreateNewEnemyCommand; var command:ICommand = new CreateEnemyCommand(); command.execute();1

Command Pattern

Page 20: An Introduction to Game Programming with Flash: Design Patterns

Jet&Giant

Page 21: An Introduction to Game Programming with Flash: Design Patterns

git clone git://github.com/krzysztof-o/epi-wdpg.git

Jet&Giant

Page 22: An Introduction to Game Programming with Flash: Design Patterns

Task 1Game model:● Create GameModel singleton class responsible for

storing game data:● Enemies and bullets● Game object reference● Giant object reference

enemies bullets gamelivesgiant

Page 23: An Introduction to Game Programming with Flash: Design Patterns

Task 2Bullet Pool:● Create pool of 50 bullets using Object Pool Pattern

BulletPool

Page 24: An Introduction to Game Programming with Flash: Design Patterns

Task 3Enemies' movement:● Create strategies FastMoveStrategy and SineWaveMoveStrategy

responsible for moving enemies

enemy_1 enemy_2

Page 25: An Introduction to Game Programming with Flash: Design Patterns

Task 4Explosion animaton:● Create command PlayBoomCommand● Invoke command with dispatched Event