summary refs log tree commit diff stats
path: root/Classes/GameModeInfo.m
blob: 91543faf9c8e7060a358b6d4dc2b6169db6e128c (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
//  GameModeInfo.m
//  Cartographic
//
//  Created by Starla Insigna on 11/28/11.
//  Copyright (c) 2011 Four Island. All rights reserved.
//

#import "GameModeInfo.h"
#import "CCNotifications.h"
#import "GameMode.h"
#import "GameModeManager.h"

@implementation GameModeInfo

@synthesize name, location, numOfStars, image, unlocked, globalHighscoreKey;

- (id)initWithName:(NSString*)m_name location:(NSString*)m_location numOfStars:(int)m_numOfStars imageFilename:(NSString*)m_imageFilename unlocked:(BOOL)m_unlocked gameClass:(Class)m_gameClass globalHighscoreKey:(NSString*)m_globalHighscoreKey
{
    self = [super init];
    
    if (nil != self)
    {
        name = m_name;
        location = m_location;
        numOfStars = m_numOfStars;
        image = [[UIImage alloc] initWithContentsOfFile:m_imageFilename];
        globalHighscoreKey = m_globalHighscoreKey;
        
        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        unlocked = [defaults boolForKey:[NSString stringWithFormat:@"gameModeUnlocked-%@", name]];
        if (!unlocked)
        {
            unlocked = m_unlocked;
        }
        
        if (numOfStars > 3)
        {
            @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"There does not exist support for more than three stars in a game mode." userInfo:nil];
        }
        
        stars = (BOOL*) calloc(numOfStars, sizeof(BOOL));
        for (int i=0; i<numOfStars; i++)
        {
            stars[i] = [defaults boolForKey:[NSString stringWithFormat:@"gameModeStar-%@-%d", name, i]];
        }
        
        if ([m_gameClass isSubclassOfClass:[GameMode class]])
        {
            gameClass = m_gameClass;
        } else {
            [NSException raise:@"Invalid gameClass value" format:@"gameClass must be a subclass of GameMode"];
        }
    }

    return self;
}

- (void)setStar:(int)star_id withMessage:(NSString*)message
{
    if (!stars[star_id])
    {
        stars[star_id] = YES;
        
        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:YES forKey:[NSString stringWithFormat:@"gameModeStar-%@-%d", name, star_id]];

        [[CCNotifications sharedManager] addWithTitle:@"Star Achieved!" message:message image:[NSString stringWithFormat:@"star_%d.png", star_id+1]];
        
        [GameModeManager sharedInstance].stars++;
    }
}

- (BOOL)star:(int)star_id
{
    return stars[star_id];
}

- (int)starsCollected
{
    int result = 0;
    
    for (int i=0; i<numOfStars; i++)
    {
        if (stars[i])
        {
            result++;
        }
    }
    
    return result;
}

- (CCScene*)scene
{
    CCScene* scene = [CCScene node];
    
	GameMode* layer = [[((GameMode*) [gameClass alloc]) init] autorelease];
    layer.tag = GAME_LAYER;
	[scene addChild:layer];
	
	scene.tag = GAME_SCENE;
	
	return scene;
}

- (void)unlock
{
    if (!unlocked)
    {
        unlocked = YES;
        
        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:YES forKey:[NSString stringWithFormat:@"gameModeUnlocked-%@", name]];
        
        CCTexture2D* texture = [[CCTexture2D alloc] initWithImage:image];
        [[CCNotifications sharedManager] addWithTitle:name message:@"You've unlocked a new game mode!" texture:texture];
        [texture release];
        
        [TestFlight passCheckpoint:[NSString stringWithFormat:@"Unlocked %@ Mode", name]];
    }
}

- (void)dealloc
{
    free(stars);
    [super dealloc];
}

@end