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 implemen...
Design Patterns
● Creatonal
● Builder
● Abstract Factory
● Factory Method
● Singleton
● Object Pool
● Structural
● Decorat...
Singleton Pattern
● Guarantees creatng only single instance of a class
● Doesn't pollute global namespace
● Overused and t...
Singleton Pattern
package foo
{
public class Singleton
{
private static var instance:Singleton;
public function Singleton()
{
}
public stati...
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 obj...
Object Pool Pattern
package foo
{
public class EnemyPool
{
private var enemies:Vector.<Enemy>;
public function EnemyPool(count:int)
{
enemies ...
import foo.EnemyPool;
var pool:EnemyPool = new EnemyPool(20);
var enemy:Enemy = pool.getEnemy();
//when we don't need this...
Strategy Pattern
● Defines a group of algorithms ready to use
● Allows to change algorithms on runtme
● Helps in proper en...
Strategy Pattern
package foo
{
public class Enemy
{
private var moveStrategy:IMoveStrategy;
public function Enemy(movingStrategy:IMoveStrat...
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 F...
Command Pattern
package foo
{
public class CreateEnemyCommand implements ICommand
{
public function execute():void
{
//add enemy & play so...
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 1
Game model:
● Create GameModel singleton class responsible for
storing game data:
● Enemies and bullets
● Game obje...
Task 2
Bullet Pool:
● Create pool of 50 bullets using Object Pool Pattern
BulletPool
Task 3
Enemies' movement:
● Create strategies FastMoveStrategy and SineWaveMoveStrategy
responsible for moving enemies
ene...
Task 4
Explosion animaton:
● Create command PlayBoomCommand
● Invoke command with dispatched Event
Upcoming SlideShare
Loading in...5
×

An Introduction to Game Programming with Flash: Design Patterns

682

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
682
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

An Introduction to Game Programming with Flash: Design Patterns

  1. 1. An Introducton to Game Programming with Flash 3: Design Patterns
  2. 2. 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
  3. 3. Design Patterns ● Creatonal ● Builder ● Abstract Factory ● Factory Method ● Singleton ● Object Pool ● Structural ● Decorator ● Adapter ● Bridge ● Behavioural ● Command ● Mediator ● Strategy ● Architectural ● Model-View-Controller
  4. 4. 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
  5. 5. Singleton Pattern
  6. 6. 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
  7. 7. import foo.Singleton; var singleton:Singleton = Singleton.getInstance();1 Singleton Pattern
  8. 8. 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
  9. 9. Object Pool Pattern
  10. 10. 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
  11. 11. 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
  12. 12. Strategy Pattern ● Defines a group of algorithms ready to use ● Allows to change algorithms on runtme ● Helps in proper encapsulaton
  13. 13. Strategy Pattern
  14. 14. 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
  15. 15. var enemy:Enemy = new Enemy(new FastMoveStrategy()); enemy.move(); 1 Strategy Pattern
  16. 16. Command Pattern ● Encapsulates all data needed to method invocaton later on ● Single Responsibility Principle ● Fire and Forget ● Commonly used for mult-level undo
  17. 17. Command Pattern
  18. 18. package foo { public class CreateEnemyCommand implements ICommand { public function execute():void { //add enemy & play sound here } } } 1 Command Pattern
  19. 19. import foo.CreateNewEnemyCommand; var command:ICommand = new CreateEnemyCommand(); command.execute();1 Command Pattern
  20. 20. Jet&Giant
  21. 21. git clone git://github.com/krzysztof-o/epi- wdpg.git Jet&Giant
  22. 22. Task 1 Game model: ● Create GameModel singleton class responsible for storing game data: ● Enemies and bullets ● Game object reference ● Giant object reference enemies bullets game lives giant
  23. 23. Task 2 Bullet Pool: ● Create pool of 50 bullets using Object Pool Pattern BulletPool
  24. 24. Task 3 Enemies' movement: ● Create strategies FastMoveStrategy and SineWaveMoveStrategy responsible for moving enemies enemy_1 enemy_2
  25. 25. Task 4 Explosion animaton: ● Create command PlayBoomCommand ● Invoke command with dispatched Event
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×