54
Multiplayer Games Magnus Jahnen sgd-ws13 Multiplayer Games with iOS Seminar Games Development (WS13/14) 04.10.2013 Session 14 “Tell me and I will forget. Show me and I will remember. Involve me and I will understand. Step back and I will act.” 1

Multiplayer games on iOS

Embed Size (px)

Citation preview

Page 1: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Multiplayer Games with iOS

Seminar Games Development (WS13/14) 04.10.2013

Session 14

“Tell me and I will forget. Show me and I will remember.

Involve me and I will understand. Step back and I will act.”

1

Page 2: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Multiplayer Games with iOS• Introduction

• Game Center

• MultipeerConnectivity

• Motivation

• Overview

• Realtime multiplayer (SpaceInvaders)

• With Game Center

• With MultipeerConnectivity

• Summary and tips

• Helpful links

• References2

Page 3: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Introduction - Game Center

• Social Gaming Network developed by Apple

• Makes third party frameworks like OpenFeint obsolete

• Available in iOS 4.1 (2010) and OS X 10.8 (2012)

• Features:

• Leaderboards

• Achievements

• Challenges

3

Page 4: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Introduction - Game Center

4

Real Time Multiplayer

Doodle Jump Real Racing 3

Page 5: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Introduction - Game Center

5

Turn Based Multiplayer

CarcassonneWord Rally

Page 6: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Introduction - MultipeerConnectivity

• Framework to communicate between nearby devices

• Introduced in iOS 7 (not available on OS X)

• Replaces facilities to communicate with nearby devices in GameKit Framework

• Features:

• Discovering and exchanging data between nearby devices

• Message, stream based data and resources such as files6

Page 7: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Motivation

• Multiplayer games obviously are fun

• Can fascinate players even if the storyline is over

• Real interaction with other players and social interaction are more appreciated than just with a computer

• Both Frameworks have a really easy to use API and a high level user interface

7

Page 8: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Network layer

• Layer 4 or Transport layer (OSI model)

8

TCP• Connection oriented

• Stream based data

• Reliable

• Higher overhead than UDP

• Slower transmission speed than UDP

UDP• Connection less

• Package based data

• Unreliable

• Very low overhead

• Very high transmission speed

Page 9: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Network Guidelines• Handle network disruptions

• Send packages at lowest frequency

• Network updates are not needed 30 times per second!

• Use smallest package size possible

• Send data only to the players who need it

9

Page 10: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

What we will do now

• Adapt the SpaceInvaders Tutorial

• To a real time multiplayer game

• Supporting two players

10

Player One

Player Two

Page 11: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Overview

11

1 1

MultiPeerCommunicationGameCenterCommunication

CommunicationStrategy«protocol»

+findMatch()+sendData(NSData *)+disconnect()

MultiPlayerHelper+sharedInstance()+findMatch()

Page 12: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Before we start

• Debug builds use the Game Center Sandbox environment

• Use eduroam network

• Use 3.5 inch iPhone Simulator (32bit)

• TODOs are marked with warnings

12

Page 13: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Game Center

13

Page 14: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Important classes

14

GKMatchRequest GKMatchmakerViewController

GKMatch

Page 15: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Overview

15

1

matchmakerDelegate

1 1

1

GKMatch+sendData(NSData)+disconnect()

GameCenterCommunication+findMatch()

GKMatchRequest+maxPlayers+minPlayers

GKMatchMakerViewController+initWithMatchRequest(GKMatchRequest)

Page 16: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Finding players

16

Page 17: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

GKMatchmakerViewController

17

Page 18: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Exercise: TODO 1

• Init a GKMatchRequest and set the properties minPlayers and maxPlayers

• Init a GKMatchmakerViewController using initWithMatchRequest

• Set the matchmakerDelegate property to self

• Present the ViewController via [Helper presentViewController:]

• Time: 7 minutes18

GKMatchRequest

GKMatchmakerViewController

Page 19: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Solution: TODO 1

19

- (void)findMatch { NSLog(@"Searching a match ..."); #warning TODO 1a initialize a GKMatchRequest and set the maxPlayers and minPlayers properties GKMatchRequest *matchRequest = [[GKMatchRequest alloc] init]; matchRequest.maxPlayers = 2; matchRequest.minPlayers = 2; #warning TODO 1b present the ViewController GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:matchRequest]; mmvc.matchmakerDelegate = self; [Helper presentViewController:mmvc]; }

GameCenterCommunication.m

Page 20: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

GKMatchMakerViewControllerDelegate

20

#pragma mark GKMatchmakerViewControllerDelegate

- (void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController { NSLog(@"matchmaking cancelled"); [Helper dismissViewController]; [self.delegate communicationStrategyDidCancelMatch]; }

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFailWithError:(NSError *)error { NSLog(@"Error finding match: %@", error); [Helper dismissViewController]; [self.delegate communicationStrategyDidEndMatch]; }

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match { NSLog(@"found match!"); [Helper dismissViewController]; self.currentMatch = match; match.delegate = self; [self lookupPlayers]; }

GameCenterCommunication.m

Page 21: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Running the game

• You should see the ViewController

• When you click cancel or play, you should see the appropriate Messages in the console output

• You will not be able to play though!

21

2013-09-23 16:06:13.240 SpaceInvadersTutorial[1101:a0b] Searching a match ... 2013-09-23 16:06:15.768 SpaceInvadersTutorial[1101:a0b] matchmaking cancelled 2013-09-23 16:06:15.991 SpaceInvadersTutorial[1101:a0b] User clicked cancel!

2013-09-23 17:42:41.353 SpaceInvadersTutorial[4601:a0b] Searching a match ... 2013-09-23 17:43:29.832 SpaceInvadersTutorial[4601:a0b] found match! 2013-09-23 17:43:29.860 SpaceInvadersTutorial[4601:a0b] Looking up 1 players

Page 22: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws1322

Network Communication

Page 23: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Sending data

• Send data by calling sendData from class GKMatch

• Takes NSData which is just an array of bytes

• Data can be sent reliable or unreliable

• Remember the networking guidelines mentioned before

• Small package size

• Lowest frequency

• Network topologies

• etc.

23

Page 24: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Sending Reliable (TCP)

• Guaranteed delivery

• No out of order packets

➡ Easy to use

• But may be slower!

➡ For infrequent messages which need to reach their destination!

24

Page 25: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Sending Unreliable (UDP)

• Faster than reliable

• No guaranteed delivery

• Maybe out of order packets

➡ For real time updates (e.g. position updates)

25

Page 26: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Exercise: TODO 2

26

• Implement the method sendEnemySpawned in MultiPlayerHelper.m

• Use [self getRelativePosition:] to get a relative position to screen

• Init a EnemySpawnedMessage and send it via [self sendMessage:]

• Time: 5 minutes

@interface EnemySpawnedMessage : Message ... - (id)initWithEnemyIndex:(NSUInteger)index atPosition:(CGPoint)position; ... @end

Hint:

Page 27: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Solution: TODO 2

27

MultiPlayerHelper.m

- (void)sendEnemySpawned:(int)enemyIndex atPosition:(CGPoint)position { #warning TODO 2 send when an enemy/alien spawned CGPoint relativePosition = [self getRelativePosition:position];

EnemySpawnedMessage *message = [[EnemySpawnedMessage alloc] initWithEnemyIndex:enemyIndex atPosition:relativePosition];

[self sendMessage:message]; }

Page 28: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Exercise: TODO 3

28

• Implement the method parseEnemySpawned in MultiPlayerHelper.m

• Use [self getAbsolutePosition:] to get an absolute position to screen

• Use the property self.networkDelegate to notify the Game Scene

• Time: 5 minutes

@interface EnemySpawnedMessage : Message

@property NSUInteger enemyIndex; @property CGPoint position; ... @end

@protocol NetworkDelegate ... - (void)enemySpawned:(int)enemyIndex atPosition:(CGPoint)position; ... @end

Hints:

Page 29: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Solution: TODO 3

29

MultiPlayerHelper.m

- (void)parseEnemySpawnedMessage:(EnemySpawnedMessage *)message { #warning TODO 3 parse the enemy message CGPoint absolutePosition = [self getAbsolutePosition:message.position]; [self.networkDelegate enemySpawned:message.enemyIndex atPosition:absolutePosition]; }

Page 30: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Whats missing?

• Player Position updates ➡ Good example where unreliable packages are useful

30

Page 31: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

How to deal with the Problems

• Lost packages ➡ Just ignore them

• Out of order packages ➡ Just use the most recent received information

• These solutions apply for most scenarios!

31

Page 32: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Problem: Out of Order Packages

• Include a message counter in the position update message

• Compare it with the message count we expect next

➡ Bigger or equal → Valid

➡ Otherwise we just drop the package

32

Page 33: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Exercise: TODO 4

33

MultiPlayerHelper.m

- (void)sendPlayerPosition:(CGPoint)position { #warning TODO 4 send the player position unreliable self.outgoingCounter++; CGPoint relativePosition = [self getRelativePosition:position];

PlayerPositionUpdateMessage *message = [[PlayerPositionUpdateMessage alloc] initWithPosition:relativePosition andMessageCount:self.outgoingCounter];

[self sendMessageUnreliable:message]; }

Page 34: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Exercise: TODO 5

34

MultiPlayerHelper.m

- (void)parsePlayerPositionUpdateMessage:(PlayerPositionUpdateMessage *)message { #warning TODO 5 parse the position update message // sorry, but out of order -> just ignore if(self.incomingCounter >= message.messageCount) { NSLog(@"received out of order position update!"); return; } CGPoint absolutePosition = [self getAbsolutePosition:message.playerPosition]; self.incomingCounter = message.messageCount; [self.networkDelegate playerPositionChanged:absolutePosition]; }

Page 35: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws1335

Play!

Page 36: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws1336

MultipeerConnectivity

Page 37: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Overview

37

Session Session

Session

Page 38: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Important classes

38

MCAdvertiserAssistant

MCBrowserViewController

MCSessionMCPeerID

Page 39: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Overview

39

1

1

1

delegate

1 1delegate

1

1

MultiPeerCommunication+findMatch()

MCBrowserViewController+serviceType: NSString*+maximumNumberOfPeers+minimumNumberOfPeers

MCAdvertiserAssistant+serviceType: NSString*+start()+stop()

MCSession+sendData(NSData *)

MCPeerID+playerName

Page 40: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Bonjour

• Apples implementation of Zeroconf networking

• Used to find available services in a network using multicast

‣ Identified via the service type

• Used for example in iTunes or to easily find network printers

40

Page 41: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Presenting the UI

41

Passivevia Advertiser

Activevia ViewController

Page 42: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Change the Strategy!

42

#warning TODO change the strategy [MultiPlayerHelper sharedInstance].communicationStrategy = [[GameCenterCommunication alloc] init];

ViewController.m

Change

To

[MultiPlayerHelper sharedInstance].communicationStrategy = [[MultiPeerCommunication alloc] initWithPlayerName:@"Your Name"];

Page 43: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Exercise: TODO 6

• Init a MCPeerID with self.playerName

• Init a MCSession (property) with the peer ID • Do not forget to set the delegate!

• Init a MCAdvertiserAssistant (property) with service type „space“, „nil“ as discovery Info and the session

• Then start the Advertiser

• Time: 7 minutes43

MCPeerID

MCSession

MCAdvertiserAssistant

Page 44: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Solution: TODO 6

44

- (void)setupSession { #warning TODO 6 setup up the MultipeerConnectivity Session MCPeerID *peer = [[MCPeerID alloc] initWithDisplayName:self.playerName]; self.session = [[MCSession alloc] initWithPeer:peer]; self.session.delegate = self; self.advertiserAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:@"space" discoveryInfo:nil session:self.session]; [self.advertiserAssistant start]; }

MultiPeerCommunication.m

Page 45: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Exercise: TODO 7

• First stop the Advertiser!

• Init a MCBrowserViewController with Service Type „space“ and the property session

• Do not forget to set the delegate and maximum and minimum number of peers

• Present the ViewController via [Helper presentViewController:]

• Time: 7 minutes

45

MCPeerID

MCSession

MCBrowserViewController

Page 46: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Solution: TODO 7

46

- (void)findMatch { #warning TODO 7 find a match/other peers using MultipeerConnectivity [self.advertiserAssistant stop];

MCBrowserViewController *viewController = [[MCBrowserViewController alloc] initWithServiceType:@"space" session:self.session]; viewController.delegate = self; viewController.maximumNumberOfPeers = 2; viewController.minimumNumberOfPeers = 2;

[Helper presentViewController:viewController]; }

MultiPeerCommunication.m

Page 47: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws1347

Play!

Page 48: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Summary

• You are now able to build a multiplayer game! Congratulations!

• You know why Game Center and MultipeerConnectivity are useful

• You know how you can find matches (other players) with the standard View Controllers

• You know how to send and receive data over a GKMatch or MCSession

• You know where you should pay attention at when designing a multiplayer real time game

48

Page 49: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

‣ Let the user decide

49

Game Center• Server infrastructure supplied

by Apple

• Wide community

• Available on iOS and OS X

• More features available like leaderboards or voice chat

• Dependent on Apple Servers

• No nearby players

MultipeerConnectivity• No server needed

• Different communication Methods (Bluetooth and Wifi)

• Seems to work better and faster

• Only nearby devices

• Available only on iOS

• Not optimized for gaming

Page 50: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Some tips

• Use my strategy pattern

• Enable Game Center/MultipeerConnectivity in the Xcode project

• Register your app Bundle ID in iTunes Connect

• Check if the sandbox is online

• https://sandbox.itunes.apple.com/verifyReceipt

50

Page 51: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Helpful links• Official Apple Game Center Guide

• https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008304-CH1-SW1

• How to Make a simple multiplayer game by Ray Wenderlich • http://www.raywenderlich.com/3276/how-to-make-a-simple-multiplayer-game-with-

game-center-tutorial-part-12

• Nearby Networking with MultipeerConnectivity (Video) • https://developer.apple.com/wwdc/videos/index.php?id=708

• About Multipeer Connectivity by Apple

• https://developer.apple.com/library/ios/documentation/MultipeerConnectivity/Reference/MultipeerConnectivityFramework/Introduction/Introduction.html

• Snap (card game by Ray Wenderlich)

• GameKit API deprecated, but good example how to exchange packages!

• http://www.raywenderlich.com/12735

51

Page 52: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Thank you!

Questions? Do not hesitate to ask!

52

Page 53: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

References• https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Introduction/Introduction.html#//

apple_ref/doc/uid/TP40008304-CH1-SW1

• https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Matchmaking/Matchmaking.html#//apple_ref/doc/uid/TP40008304-CH9-SW1

• https://developer.apple.com/library/ios/documentation/MultipeerConnectivity/Reference/MultipeerConnectivityFramework

• http://www.raywenderlich.com/12735

• http://iphonedevsdk.com/forum/iphone-sdk-development/96012-is-the-sandbox-down-i-keep-getting-http-unavailable.html

• http://www.raywenderlich.com/3276/how-to-make-a-simple-multiplayer-game-with-game-center-tutorial-part-12

• http://devstreaming.apple.com/videos/wwdc/2013/708xbx3x7xusbzidl0j3acxest/708/708.pdf?dl=1

• https://developer.apple.com/library/ios/documentation/MultipeerConnectivity/Reference/MultipeerConnectivityFramework/Introduction/Introduction.html

• https://developer.apple.com/library/ios/documentation/MultipeerConnectivity/Reference/MultipeerConnectivityFramework/_index.html#//apple_ref/doc/uid/TP40013328

• http://en.wikipedia.org/wiki/Bonjour_(software)

• http://en.wikipedia.org/wiki/OSI_model

• http://en.wikipedia.org/wiki/User_Datagram_Protocol

• http://en.wikipedia.org/wiki/Transmission_Control_Protocol

• http://en.wikipedia.org/wiki/Game_Center

53

Page 54: Multiplayer games on iOS

Multiplayer Games Magnus Jahnensgd-ws13

Images• Slide 3: Game Center Icon

• http://www.icreatemagazine.com/wp-content/uploads/2013/08/Game-Center2.png

• Slide 4 + 5: Example Applications

• http://1.bp.blogspot.com/-VN8uai_77pg/TZTNonsIzmI/AAAAAAAAAxY/qdVvIjwUNGA/s1600/Doodle%2BJump%2BMultiplayer.jpg

• https://itunes.apple.com/de/app/real-racing-3/id556164350?mt=8

• https://itunes.apple.com/us/app/word-rally/id447075138?mt=8

• https://itunes.apple.com/de/app/carcassonne/id375295479?mt=8

• Slide 6 + 37: MultipeerConnectivity

• http://devstreaming.apple.com/videos/wwdc/2013/708xbx3x7xusbzidl0j3acxest/708/708.pdf?dl=1

• Slide 10: Network topologies:

• https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Art/network_topologies_2x.png

• Slide 40: Bonjour Icon

• https://devimages.apple.com.edgekey.net/bonjour/images/bonjour-hero.png

• Slide 52: MultipeerConnectivity • http://www.theonliner.de/wp-content/uploads/2013/07/multipeer_004_220-e1373416225609.jpg

• Plus and minus signs:

• http://mountainss.files.wordpress.com/2012/12/plus-sign.png?w=256

• http://www.omgomfg.com/file/pic/photo/2012/09/rebelius-minus-sign.png

• All other images were made by myself

54