summary refs log tree commit diff stats
path: root/Classes/GameModeSelection.m
blob: 0816828b0d61858cf9028889b8393840516448e0 (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
//
//  GameModeSelection.m
//  Cartographic
//
//  Created by Starla Insigna on 8/18/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "GameModeSelection.h"
#import "TutorialMode.h"
#import "ClassicGameMode.h"

@implementation GameModeSelection

@synthesize name, location, unlocked, highscore, unlockCondition;

- (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked;
{
    self = [super init];
    
    if (nil != self)
    {
        self.anchorPoint = CGPointMake(0.5f, 0.5f);
        
        name = m_name;
        location = m_location;
        unlocked = m_unlocked;
        
        if (!unlocked)
        {
            name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0];
            location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0];
        }
        
        nameLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%@ (%@)", location, name] fntFile:@"levelnames.fnt"];
        
        [self addChild:nameLabel];
        nameLabel.position = ccp(0, -32-nameLabel.contentSize.height);
        
        UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]];
        UIGraphicsBeginImageContext(CGSizeMake(128, 128));
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIGraphicsPushContext(context);
        
        if (unlocked)
        {
            [innerPicture drawInRect:CGRectMake(0, 0, 128, 128)];
        } else {
            CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
            CGContextFillRect(context, CGRectMake(0, 0, 128, 128));
            [innerPicture drawInRect:CGRectMake(0, 0, 128, 128) blendMode:kCGBlendModeLuminosity alpha:1.0];
        }
        
        UIGraphicsPopContext();
        CGImageRef innerPictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
        
        if (unlocked)
        {
            CCSprite* picture = [CCSprite spriteWithCGImage:innerPictureRef key:[NSString stringWithFormat:@"gms-%@", filename]];
            UIGraphicsPushContext(context);
            CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
            CGContextFillRect(context, CGRectMake(0, 0, 128, 128));
            UIGraphicsPopContext();
            CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
            CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filename]];
            
            CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)];
            CCMenu* theMenu = [CCMenu menuWithItems:pictureMenuItem, nil];
            theMenu.position = ccp(0, 32);
            [self addChild:theMenu];
        } else {
            CCSprite* picture = [CCSprite spriteWithCGImage:innerPictureRef key:[NSString stringWithFormat:@"gms-%@-locked", filename]];
            picture.position = ccp(0, 32);
            [self addChild:picture];
        }
        
        UIGraphicsEndImageContext();
    }
    
    return self;
}

- (void)setHighscore:(int)m_highscore
{
    if (unlocked)
    {
        highscore = m_highscore;
        
        otherLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"Highscore: %d", highscore] fntFile:@"leveldescriptions.fnt"];
        otherLabel.position = ccp(0, -32-nameLabel.contentSize.height-otherLabel.contentSize.height);
        [self addChild:otherLabel];
    }
}

- (void)setUnlockCondition:(NSString *)m_unlockCondition
{
    if (!unlocked)
    {
        unlockCondition = m_unlockCondition;
    
        otherLabel = [CCLabelBMFont labelWithString:unlockCondition fntFile:@"leveldescriptions.fnt"];
        otherLabel.position = ccp(0, -32-nameLabel.contentSize.height-otherLabel.contentSize.height);
        [self addChild:otherLabel];
    }
}

- (void)buttonTapped
{
    if ([name isEqual:@"Tutorial"])
    {
        [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]];
    } else if ([name isEqual:@"Collect"])
    {
        [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]];
    }
}

@end