Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Objective C - connection with Game Center and highscores board
//
// aGameCenter_Codea.h
//
// Created by Juan Belón on 28/05/12
// Games -> http://www.xixgames.com
// LGPL - @juaxix - Codea connection!
//
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface aGameCenter_Codea : NSObject
<GKLeaderboardViewControllerDelegate, AVAudioPlayerDelegate>
{
bool hasGameCenter;
bool playing;
AVAudioPlayer *player;
}
- (void) start;
- (void) authenticateLocalPlayer;
- (void) registerForAuthenticationNotification;
- (void) authenticationChanged;
- (BOOL) isGameCenterAvailable;
- (void) leaderboardViewControllerDidFinish:
(GKLeaderboardViewController *)viewController;
- (void) showLeaderboard;
- (void) reportScore:(int) score;
- (void) playMusic:(int) songNumber;
- (void) stopMusic;
@end
//
// aGameCenter_Codea.m
//
// Created by Juan Belón on 28/05/12
// Games -> http://www.xixgames.com
// LGPL - @juaxix - Codea connection!
//
#import "aGameCenter_Codea.h"
#import "SharedRenderer.h"
@implementation aGameCenter_Codea
- (id)init
{
self = [super init];
player = [[AVAudioPlayer alloc] init];
playing= false;
return self;
}
- (void)dealloc
{
[player release];
[super dealloc];
}
- (void) start {
//Game Center
hasGameCenter = false;
[self authenticateLocalPlayer];
}
- (BOOL) isGameCenterAvailable
{
// Check for presence of GKLocalPlayer API.
Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
// The device must be running running iOS 4.1 or later.
NSString *reqSysVer = @"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
- (void)authenticateLocalPlayer {
if(![self isGameCenterAvailable]){
hasGameCenter = false;
return;
}
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil){
[self registerForAuthenticationNotification];
hasGameCenter = true;
}else{
hasGameCenter = false;
}
}];
}
- (void)registerForAuthenticationNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver: self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
}
- (void)authenticationChanged
{
if([self isGameCenterAvailable ]){
return;
}
if ([GKLocalPlayer localPlayer].isAuthenticated){
hasGameCenter = true;
}else{
hasGameCenter = false;
}
}
- (void)showLeaderboard
{
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
if (leaderBoardCont) {
leaderBoardCont.category=@"YOUR_SCORE_BOARD_UNIQUE_NAME_FROM_ITUNES_CONNECT";
leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
leaderBoardCont.leaderboardDelegate=self;
[[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[[SharedRenderer renderer] dismissModalViewControllerAnimated:YES];
}
- (void) reportScore:(int) score
{
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:
@"YOUR_SCORE_BOARD_UNIQUE_NAME_FROM_ITUNES_CONNECT"] autorelease];
if(scoreReporter){
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil){
// handle the reporting error
}
}];
}
}
- (void) playMusic:(int) songNumber {
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
NSString* song = [[NSString alloc] initWithString:@""];
NSError* err;
switch (songNumber) {
case 1: //menu song
song = @"/menu.MP3";
break;
case 2:
song = @"/game.MP3";
break;
case 3:
song = @"/winner.MP3";
default:
break;
}
if ([song isEqualToString:@""]){
return ;
}
//NSLog(@"Playing song %d: %@",songNumber,song);
resourcePath = [resourcePath stringByAppendingString:song];
//NSLog(@"Path to play: %@", resourcePath);
//Initialize our player pointing to the path to our resource
if (playing && player) {
[player stop];
playing = false;
}
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
player.delegate = self;
[player prepareToPlay];
player.numberOfLoops = -1; //One Infinite Loop (music!) ;)
[player play];
playing = true;
}
}
- (void) stopMusic {
if (playing){
[player stop];
playing = false;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment