-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.m
58 lines (49 loc) · 1.37 KB
/
GameState.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// GameState.m
// JumboJump
//
// Created by David Pointeau on 24/04/14.
// Copyright (c) 2014 David Pointeau. All rights reserved.
//
#import "GameState.h"
@implementation GameState
+ (instancetype)sharedInstance
{
static dispatch_once_t pred = 0;
static GameState *_sharedInstance = nil;
dispatch_once( &pred, ^{
_sharedInstance = [[super alloc] init];
});
return _sharedInstance;
}
- (id) init
{
if (self = [super init]) {
// Init
_score = 0;
_highScore = 0;
_stars = 0;
// Load game state
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id highScore = [defaults objectForKey:@"highScore"];
if (highScore) {
_highScore = [highScore intValue];
}
id stars = [defaults objectForKey:@"stars"];
if (stars) {
_stars = [stars intValue];
}
}
return self;
}
- (void) saveState
{
// Update highScore if the current score is greater
_highScore = MAX(_score, _highScore);
// Store in user defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:_highScore] forKey:@"highScore"];
[defaults setObject:[NSNumber numberWithInt:_stars] forKey:@"stars"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end