Created
January 31, 2013 13:30
-
-
Save anonymous/4682875 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PGViewController.h" | |
@interface PGViewController () | |
@end | |
@implementation PGViewController | |
@synthesize connect,disconnect,currentSession; | |
int check = 0; | |
float x=0,y=0; | |
int markerx=0,markery=0; | |
float gamedata[10]; | |
GKPeerPickerController *picker; | |
int uni=0; | |
float paddleonex; | |
float paddletwox; | |
int deviceresx,deviceresy; | |
int timef=0; | |
//gamedata[3] = scor client | |
//gamedata[4] = scor server | |
//gamedata[5] = paddlex | |
//gamedata[6] = ballx | |
//gamedata[7] = bally | |
//gamedata[8] = server id | |
//gamedata[9] = dev id | |
-(void)start{ | |
player.center=CGPointMake(180, 240); | |
randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; | |
ballPos= CGPointMake(4,3); | |
} | |
-(void)onTimer{ | |
if(gamedata[8]==1) | |
{ | |
player.center = CGPointMake(player.center.x+ballPos.x, player.center.y+ballPos.y); | |
if(player.center.x > 320 || player.center.x < 0) | |
{ | |
ballPos.x = -ballPos.x; | |
} | |
if(player.center.y > 405 ) | |
{ | |
if(player.center.x > one.center.x-40 && player.center.x < one.center.x+40) | |
ballPos.y = -ballPos.y; | |
else { | |
gamedata[3]++; | |
[self stop]; | |
[self start]; | |
} | |
} | |
if(player.center.y < 60) | |
{ | |
if(player.center.x > two.center.x-40 && player.center.x < two.center.x+40) | |
ballPos.y = -ballPos.y; | |
else{ | |
gamedata[4]++; | |
[self stop]; | |
[self start]; | |
} | |
} | |
NSString *resultString = [[NSString alloc] initWithFormat:@"%.2f ",gamedata[4]]; | |
ones.text = resultString; | |
resultString = [[NSString alloc] initWithFormat:@"%.2f ",gamedata[3]]; | |
twos.text = resultString; | |
gamedata[6] = player.center.x; | |
gamedata[7] = player.center.y; | |
gamedata[5] = one.center.x; | |
gamedata[9] = gameUniqueID; | |
[self sendarray]; | |
} | |
else{ | |
NSString *resultString = [[NSString alloc] initWithFormat:@"%.2f ",gamedata[3]]; | |
ones.text = resultString; | |
resultString = [[NSString alloc] initWithFormat:@"%.2f ",gamedata[4]]; | |
twos.text = resultString; | |
player.center = CGPointMake(gamedata[6], 480-gamedata[7]); | |
gamedata[5] = one.center.x; | |
gamedata[9] = gameUniqueID; | |
[self sendarray]; | |
} | |
} | |
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ | |
NSLog(@"s-a mutat"); | |
UITouch *myTouch = [[event allTouches] anyObject]; | |
CGPoint temp; | |
temp = [myTouch locationInView:self.view]; | |
one.center = CGPointMake(temp.x, 440); | |
} | |
-(void)draw{ | |
if(gamedata[8]==1) | |
{ | |
server.text=@"Server"; | |
} | |
else{ | |
server.text=@"Client"; | |
} | |
two.center=CGPointMake(paddletwox, 30); | |
} | |
-(void)stop{ | |
[randomMain invalidate]; | |
randomMain = nil; | |
} | |
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context{ | |
if(gamedata[2]==1) | |
{ | |
gamedata[2]=0; | |
} | |
else{ | |
[data getBytes:gamedata]; | |
if(gameUniqueID >= gamedata[9]) | |
{ | |
gamedata[8]=1; | |
} | |
else{ | |
gamedata[8]=0; | |
} | |
paddletwox = gamedata[5]; | |
[self draw]; | |
} | |
} | |
-(void)makeserver{ | |
gamedata[9] = gameUniqueID; | |
[self sendarray]; | |
} | |
-(void)sendarray{ | |
NSData *data3 = [NSData dataWithBytes:gamedata length:(10+sizeof(float))]; | |
[self mySendDataToPeers:data3]; | |
} | |
-(void)mySendDataToPeers:(NSData *)data{ | |
if(currentSession) | |
[self.currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil]; | |
} | |
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{ | |
switch (state) { | |
case GKPeerStateConnected: | |
NSLog(@"Connected"); | |
[self makeserver]; | |
[self start]; | |
break; | |
case GKPeerStateDisconnected: | |
[self stop]; | |
NSLog(@"disconnected"); | |
currentSession = nil; | |
[connect setHidden:NO]; | |
[disconnect setHidden:YES]; | |
break; | |
case GKPeerStateAvailable: | |
break; | |
case GKPeerStateConnecting: | |
break; | |
case GKPeerStateUnavailable: | |
break; | |
default: | |
break; | |
} | |
} | |
-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker{ | |
picker.delegate=nil; | |
[connect setHidden:NO]; | |
[disconnect setHidden:YES]; | |
if (self.currentSession != nil) { | |
[self invalidateSession:self.currentSession]; | |
self.currentSession=nil; | |
} | |
} | |
-(void)invalidateSession:(GKSession *)session{ | |
if(session != nil){ | |
[session disconnectFromAllPeers]; | |
session.available=NO; | |
[session setDataReceiveHandler:nil withContext:NULL]; | |
session.delegate=nil; | |
} | |
} | |
-(void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{ | |
self.currentSession = session; | |
session.delegate=self; | |
[session setDataReceiveHandler:self withContext:nil]; | |
picker.delegate=nil; | |
[picker dismiss]; | |
} | |
- (void)viewDidLoad | |
{ | |
currentSession=nil; | |
NSString *uid = [[UIDevice currentDevice] identifierForVendor]; | |
gameUniqueID = [uid hash]; | |
[connect setHidden:NO]; | |
[disconnect setHidden:YES]; | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (IBAction)btnConnect:(id)sender { | |
picker = [[GKPeerPickerController alloc]init]; | |
picker.delegate = self; | |
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby; | |
[connect setHidden:YES]; | |
[disconnect setHidden:NO]; | |
[picker show]; | |
} | |
- (IBAction)btnDisconnect:(id)sender { | |
[self.currentSession disconnectFromAllPeers]; | |
currentSession=nil; | |
[connect setHidden:NO]; | |
[disconnect setHidden:YES]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment