an introduction to game programming with flash: design patterns
TRANSCRIPT
![Page 1: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/1.jpg)
An Introducton to Game Programming with Flash
3: Design Patterns
![Page 2: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/5.jpg)
Singleton Pattern
![Page 6: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/7.jpg)
import foo.Singleton; var singleton:Singleton = Singleton.getInstance();1
Singleton Pattern
![Page 8: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/9.jpg)
Object Pool Pattern
![Page 10: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/13.jpg)
Strategy Pattern
![Page 14: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/15.jpg)
var enemy:Enemy = new Enemy(new FastMoveStrategy()); enemy.move(); 1
Strategy Pattern
![Page 16: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/17.jpg)
Command Pattern
![Page 18: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/19.jpg)
import foo.CreateNewEnemyCommand; var command:ICommand = new CreateEnemyCommand(); command.execute();1
Command Pattern
![Page 20: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/20.jpg)
Jet&Giant
![Page 21: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/21.jpg)
git clone git://github.com/krzysztof-o/epi-wdpg.git
Jet&Giant
![Page 22: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/23.jpg)
Task 2Bullet Pool:● Create pool of 50 bullets using Object Pool Pattern
BulletPool
![Page 24: An Introduction to Game Programming with Flash: Design Patterns](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022052622/55905b5b1a28ab4e2e8b45b5/html5/thumbnails/25.jpg)
Task 4Explosion animaton:● Create command PlayBoomCommand● Invoke command with dispatched Event