3. The ‘Game’
One becomes the ‘server’
and creates new disks
adds them locally to the view
and packages them up and sends
them to the client
One becomes the ‘client’
listens for new disks, when found
creates a new one and adds it locally
Tuesday, October 13, 2009
4. The ‘Game’
User taps on the disks and they
spin in 3D and disappear
the game sends what was
hit to the other peer so both
know what has been hit
Tuesday, October 13, 2009
5. How to get there
• Connect Peers
• Run Game Loop
• Process Events
Tuesday, October 13, 2009
6. Application Startup
Connect Peers
• Start looking for peers
Tuesday, October 13, 2009
7. Application Startup
Connect Peers
• Peer found and selected
Tuesday, October 13, 2009
8. Application Startup
Connect Peers
• Shall we play a game?
Tuesday, October 13, 2009
9. Peer Picker Process
Anyone There? (A, B)
Connect Peers
I’m Here (A, B)
Display A (B)
Display B (A)
Client A Client B
Choose B (A)
Confirm A (B)
Conn passed to App (A, B)
Send/Receive (A,B)
Tuesday, October 13, 2009
10. Connect
Connect Peers
Server Mode
wait for connection
requests
Client Mode
look for servers
Tuesday, October 13, 2009
11. Connect
Connect Peers
Peer Mode
wait for conn reqs
look for servers
Tuesday, October 13, 2009
23. Session
Connect Peers/Game Loop
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
// index zero is the 'End Game' button, the only one
!if(buttonIndex == 0) {
!! _status = kServerNotStartedStatus;
!}
}
Tuesday, October 13, 2009
24. Game Loop
- (id) init {
Run Game Loop
self = [super init];
if (self != nil) {
...
self.link = [CADisplayLink displayLinkWithTarget:self
selector:@selector(gameLoop)];
self.link.frameInterval = 2;
...
}
return self;
}
- (void)start {
[self.link addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
}
Tuesday, October 13, 2009
25. Game Loop
Run Game Loop
• Start the peer picker
• Send new disk messages
Tuesday, October 13, 2009
26. Start the Picker
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
27. Send New Disks
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
28. Send New Disks
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
29. Send New Disks
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
30. Send New Disks
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
31. Send New Disks
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
32. Send New Disks
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
33. Send New Disks
- (void)gameLoop {
_loopId++;
Run Game Loop
if(kServerNotStartedStatus == _status) {
_status = kServerPickerShownStatus;
[self startPeerPicker];
} else if(kServerGameStartedServerStatus == _status) {
if(0 == (_loopId % _diskFrequency)) {
srandom(_loopId);
long x = random() % 300 + 10;
long y = random() % 460 + 10;
CGPoint location = CGPointMake((CGFloat)x, (CGFloat)y);
long colorIndex = random() % _colors.count;
NSMutableData *data = [NSMutableData dataWithBytes:&location
length:sizeof(location)];
[data appendBytes:&colorIndex length:sizeof(colorIndex)];
[self sendMessageTypeID:kServerNewDiskMessageType data:data];
UIColor *color = [_colors objectAtIndex:colorIndex];
[self.delegate diskAtPoint:location withColor:color];
}
}
}
Tuesday, October 13, 2009
34. Send Data
Run Game Loop
- (void)sendMessageTypeID:(ServerPacketTypeId)packetTypeId data:(NSData *)data {
NSMutableData *sentData = [NSMutableData data];
[sentData appendBytes:&packetTypeId length:sizeof(packetTypeId)];
[sentData appendData:data];
[self.gameSession sendData:sentData
toPeers:[NSArray arrayWithObject:self.peerId]
withDataMode:GKSendDataUnreliable error:nil];
}
Tuesday, October 13, 2009