-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOver.m
113 lines (99 loc) · 3.82 KB
/
GameOver.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// GameOver.m
//
// Created by : Michael Heirendt
// Project : Crush
// Date : 1/10/16
//
// Copyright (c) 2016 Apportable.
// All rights reserved.
//
// -----------------------------------------------------------------
#import "GameOver.h"
#import "GameData.h"
#import "SignInScene.h"
// -----------------------------------------------------------------
@implementation GameOver{
CCLabelTTF *userName;
CCLabelTTF *rankLabel;
CCSprite *rankIcon;
CCSprite *progress;
}
// -----------------------------------------------------------------
+ (instancetype)node
{
return [[self alloc] init];
}
- (instancetype)init
{
self = [super init];
NSAssert(self, @"Unable to create class %@", [self class]);
return self;
}
+ (instancetype)sharedGameData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
-(void) onEnter
{
[super onEnter];
NSString *doubles = [NSString stringWithFormat:@"%li", [GameData sharedGameData].score];
CCLabelTTF* count = [CCLabelTTF labelWithString:doubles fontName:@"HelveticaNeue" fontSize:30];
count.positionType = CCPositionTypeNormalized;
count.position = ccp(.5f,.4f);
[self addChild:count];
[[GameData sharedGameData]summarizeRank:rankLabel andUser:userName andRankIcon:rankIcon];
[GameData sharedGameData].highScore = MAX([GameData sharedGameData].score,
[GameData sharedGameData].highScore);
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[GameData sharedGameData].managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSString *currentUser = [[NSUserDefaults standardUserDefaults]
stringForKey:@"defaultUser"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", currentUser];
[request setPredicate:predicate];
NSError *error2 = nil;
Person *person = [[[GameData sharedGameData].managedObjectContext executeFetchRequest:request error:&error2] objectAtIndex:0];
int score = [GameData sharedGameData].roundScore;
score +=person.experience.intValue;
[person setValue:[NSNumber numberWithInt:score] forKey:@"experience"];
[GameData sharedGameData].roundScore = 0;
NSNumber *highScore = [NSNumber numberWithLong:[GameData sharedGameData].score];
NSNumber *highRound = [NSNumber numberWithInt:[GameData sharedGameData].round];
if (![person.managedObjectContext save:&error2]) {
NSLog(@"Unable to save managed object context.");
NSLog(@"%@, %@", error2, error2.localizedDescription);
}
if(highScore > person.highscore){
[person setValue:highScore forKey:@"highscore"];
if (![person.managedObjectContext save:&error2]) {
NSLog(@"Unable to save managed object context.");
NSLog(@"%@, %@", error2, error2.localizedDescription);
}
}
if(highRound > person.highround){
[person setValue:highRound forKey:@"highround"];
if (![person.managedObjectContext save:&error2]) {
NSLog(@"Unable to save managed object context.");
NSLog(@"%@, %@", error2, error2.localizedDescription);
}
}
}
-(void)retryPressed{
[[GameData sharedGameData] reset];
[[CCDirector sharedDirector] popScene];
CCScene *game = [CCBReader loadAsScene:@"Gameplay"];
[[CCDirector sharedDirector] pushScene:game];
[GameData sharedGameData].bladeCount = 0;
[GameData sharedGameData].bombCount = 0;
}
-(void)backPressed{
[[GameData sharedGameData] reset];
[[CCDirector sharedDirector] popScene];
}
// -----------------------------------------------------------------
@end