An Introduction to Game Programming with Flash: Design Patterns
-
Upload
krzysztof-opalka -
Category
Technology
-
view
617 -
download
2
Transcript of An Introduction to Game Programming with Flash: Design Patterns
An Introducton to Game Programming with Flash
3: 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
Design Patterns
● Creatonal● Builder● Abstract Factory● Factory Method● Singleton● Object Pool
● Structural● Decorator● Adapter● Bridge
● Behavioural● Command● Mediator● Strategy
● Architectural● Model-View-Controller
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
Singleton Pattern
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
import foo.Singleton; var singleton:Singleton = Singleton.getInstance();1
Singleton Pattern
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
Object Pool Pattern
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
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
Strategy Pattern
● Defines a group of algorithms ready to use
● Allows to change algorithms on runtme
● Helps in proper encapsulaton
Strategy Pattern
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
var enemy:Enemy = new Enemy(new FastMoveStrategy()); enemy.move(); 1
Strategy Pattern
Command Pattern
● Encapsulates all data needed to method invocaton later on
● Single Responsibility Principle
● Fire and Forget
● Commonly used for mult-level undo
Command Pattern
package foo { public class CreateEnemyCommand implements ICommand { public function execute():void { //add enemy & play sound here } } }
1
Command Pattern
import foo.CreateNewEnemyCommand; var command:ICommand = new CreateEnemyCommand(); command.execute();1
Command Pattern
Jet&Giant
git clone git://github.com/krzysztof-o/epi-wdpg.git
Jet&Giant
Task 1Game model:● Create GameModel singleton class responsible for
storing game data:● Enemies and bullets● Game object reference● Giant object reference
enemies bullets gamelivesgiant
Task 2Bullet Pool:● Create pool of 50 bullets using Object Pool Pattern
BulletPool
Task 3Enemies' movement:● Create strategies FastMoveStrategy and SineWaveMoveStrategy
responsible for moving enemies
enemy_1 enemy_2
Task 4Explosion animaton:● Create command PlayBoomCommand● Invoke command with dispatched Event