Slide 1

Slide 1 text

Game Engines are Fun: Cocoa Design Patterns and a UIKit Game Engine Moshe Berman Sophomore, Brooklyn College CocoaHeads NYC Friday, October 5, 12

Slide 2

Slide 2 text

• Why make a game engine? • Let’s Create a Game • Building a Better Engine with Design Patterns What We’ll Talk About Friday, October 5, 12

Slide 3

Slide 3 text

Game Engines are Fun Friday, October 5, 12

Slide 4

Slide 4 text

Game Engines are Fun • Game engines make building games fun by hiding the complex parts. • Game engines are fun because games are fun. I grew up with Mario and Pikachu. I want those kinds of games on my iPhone. • Why? My iPhone has a better resolution than my GameBoy. Friday, October 5, 12

Slide 5

Slide 5 text

How Many GameBoys? Friday, October 5, 12

Slide 6

Slide 6 text

How Many GameBoys? • Retina resolution is 960 pixels by 640 pixels • GameBoy resolution is 160 pixels by 144 pixels • Fits 24 games at the same time, or, every Pokémon title ever with room for more! Retina iPhone GameBoy Friday, October 5, 12

Slide 7

Slide 7 text

How Many GameBoys? • Retina resolution is 1136 pixels by 640 pixels • GameBoy resolution is 160 pixels by 144 pixels • Fits 31 games at the same time. Retina iPhone GameBoy Friday, October 5, 12

Slide 8

Slide 8 text

Why cocos2d? • It's really popular. • It's free. • Other engines were expensive, or lacked community and resources. Friday, October 5, 12

Slide 9

Slide 9 text

Let’s Make a Game Friday, October 5, 12

Slide 10

Slide 10 text

Let’s Make a Game • Tiled, used to create tile apps from artwork. Friday, October 5, 12

Slide 11

Slide 11 text

Let’s Make a Game • Tiled, used to create tile apps from artwork. • SneakyJoystick library for user input. Friday, October 5, 12

Slide 12

Slide 12 text

Let’s Make a Game • Tiled, used to create tile apps from artwork. • SneakyJoystick library for user input. • TexturePacker, used to create sprite animation sequences. Friday, October 5, 12

Slide 13

Slide 13 text

How cocos2d Works • One shared CCDirector • CCScenes contain CCLayers which contain CCNodes • All of these are visual classes. They’re the V in MVC. Friday, October 5, 12

Slide 14

Slide 14 text

Why cocos2d? • It's really popular. • It’s free. • Other engines were expensive, or lacked community and resources. Friday, October 5, 12

Slide 15

Slide 15 text

Why Not cocos2d? • Memory Management • Anti patterns • Memory Management • Memory Management: Autorelease ALL the things. • Anti patterns: No MVC, everything is a visual node, or an effect. • Memory Management: No ARC support until 1.1 and 2.0. Friday, October 5, 12

Slide 16

Slide 16 text

Building a Better Game Engine Using Cocoa Design Patterns Friday, October 5, 12

Slide 17

Slide 17 text

A game engine written on top of UIKit MBTileParser Friday, October 5, 12

Slide 18

Slide 18 text

A few patterns... • Model View Controller • Delegation (2x) • Composition Friday, October 5, 12

Slide 19

Slide 19 text

Obligatory MVC Diagram Controller View Model Friday, October 5, 12

Slide 20

Slide 20 text

MBMapView Friday, October 5, 12

Slide 21

Slide 21 text

MBMapView MBMapView - (void) initWithName:(NSString*)name; - (void) loadMap; - (void) buildCache; - (void) renderLayers; - (void) addSpriteForKey:(NSString*)key; - (void) removeSpriteForKey:(NSString*)key; - (void) moveSpriteForKey(NSString*) toCoords:(CGPoint)point; - (void) beginFollowingSpriteForKey:(NSString*)key; - (void) stopFollowingSpriteForKey:(NSString*)key; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; - (BOOL) tileIsOpenAtCoordinates; Object Group Object Group Object Group Sprite Tile Layer View Tile Layer View Tile Layer View Sprite Tile Layer Data Tile Layer Data Tile Layer Data Friday, October 5, 12

Slide 22

Slide 22 text

MBMap MBMapView MBMap(View)(Controller) MBMapViewController - (void) initWithName:(NSString*)name; - (void) loadMap; - (CGSize)tileSizeInPoints; - (BOOL)tileIsOpenAtCoordinates: (CGPoint)coordinates forSprite: (MBSpriteView *)sprite - (void)interactWithTileAtCoordinates: (CGPoint)coordinates; Object Group Object Group Object Group Tile Layer Data Tile Layer Data Tile Layer Data Tile Layer View Tile Layer View Tile Layer View Sprite Sprite - (void) buildCache; - (void) layoutMap; - (void) moveSpriteForKey(NSString*) toCoords:(CGPoint)point; - (void) beginFollowingSpriteForKey: (NSString*)key; - (void) stopFollowingSpriteForKey: (NSString*)key; MBMap MBMapView Friday, October 5, 12

Slide 23

Slide 23 text

•One-to-One relationships. •Don’t have to subclass to get special behavior. •Defined by protocols. Delegation Friday, October 5, 12

Slide 24

Slide 24 text

Delegation Friday, October 5, 12

Slide 25

Slide 25 text

#import @class MBSpriteView; @protocol MBSpriteMovementDelegate - (CGSize)tileSizeInPoints; - (BOOL)tileIsOpenAtCoordinates:(CGPoint)coordinates forSprite:(MBSpriteView *)sprite; - (void)interactWithTileAtCoordinates:(CGPoint)coordinates; @end Delegation Friday, October 5, 12

Slide 26

Slide 26 text

#import @class MBSpriteView; @protocol MBSpriteMovementDelegate - (CGSize)tileSizeInPoints; - (BOOL)tileIsOpenAtCoordinates:(CGPoint)coordinates forSprite:(MBSpriteView *)sprite; - (void)interactWithTileAtCoordinates:(CGPoint)coordinates; @end Delegation Friday, October 5, 12

Slide 27

Slide 27 text

#import @class MBSpriteView; @protocol MBSpriteMovementDelegate - (CGSize)tileSizeInPoints; - (BOOL)tileIsOpenAtCoordinates:(CGPoint)coordinates forSprite:(MBSpriteView *)sprite; - (void)interactWithTileAtCoordinates:(CGPoint)coordinates; @end Delegation Friday, October 5, 12

Slide 28

Slide 28 text

#import @class MBSpriteView; @protocol MBSpriteMovementDelegate - (CGSize)tileSizeInPoints; - (BOOL)tileIsOpenAtCoordinates:(CGPoint)coordinates forSprite:(MBSpriteView *)sprite; - (void)interactWithTileAtCoordinates:(CGPoint)coordinates; @end Delegation Friday, October 5, 12

Slide 29

Slide 29 text

Composition • The idea that you can make one class from many. • Allows you to avoid subclassing. • You do it all the time. Friday, October 5, 12

Slide 30

Slide 30 text

Demo Friday, October 5, 12

Slide 31

Slide 31 text

It's UIKit • Yet incredibly fast. • Easy to use, no OpenGL. • Works with UIKit. Friday, October 5, 12

Slide 32

Slide 32 text

Thank You Friday, October 5, 12