/
StoreLayer.m
185 lines (157 loc) · 6.46 KB
/
StoreLayer.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// StoreLayer.m
// Maestro
//
// Created by Matthew McGlincy on 4/1/12.
// Copyright (c) 2012 n/a. All rights reserved.
//
#import "Constants.h"
#import "Devil.h"
#import "GameManager.h"
#import "GameScene.h"
#import "Floor.h"
#import "ShopSign.h"
#import "Store.h"
#import "StoreItem.h"
#import "StoreLayer.h"
#import "GameSoundManager.h"
@interface StoreLayer()
@property (nonatomic, retain) CCMenu *storeMenu;
@property (nonatomic, retain) CCLabelTTF *tearsLabel;
@property (nonatomic, retain) Devil *devil;
@end
@implementation StoreLayer
@synthesize devil = _devil;
@synthesize storeMenu = _storeMenu;
@synthesize tearsLabel = _tearsLabel;
/*
NSInteger nextLevelNum = self.levelNum + 1;
if (nextLevelNum > MAX_LEVEL_IDX) {
[[CCDirector sharedDirector] replaceScene:[CCTransitionCrossFade transitionWithDuration:0.5f scene:[VictoryScene node]]];
} else {
[[CCDirector sharedDirector] replaceScene:[CCTransitionCrossFade transitionWithDuration:0.5f scene:[GameScene nodeWithLevelNum:nextLevelNum]]];
}
*/
- (void)dealloc
{
[[GameManager sharedInstance] removeObserver:self forKeyPath:@"tearsCollectedTotal"];
[_devil release];
[_storeMenu release];
[_tearsLabel release];
[super dealloc];
}
-(id)init {
self = [super init];
if (self != nil) {
self.isTouchEnabled = YES;
CCSprite *backgroundImage;
backgroundImage = [CCSprite spriteWithFile:@"shop-background.png"];
CGSize winSize = [[CCDirector sharedDirector] winSize];
[backgroundImage setPosition:
CGPointMake(winSize.width/2, winSize.height/2)];
[self addChild:backgroundImage z:-1 tag:0];
// tears remaining
self.tearsLabel = [CCLabelTTF labelWithString:[self tearsString] fontName:FONT_NAME fontSize:28.0];
self.tearsLabel.position = ccp(winSize.width - self.tearsLabel.contentSize.width / 2 - 20.0,
0 + self.tearsLabel.contentSize.height / 2 + 5);
[self addChild:self.tearsLabel z:2];
// store sign
ShopSign *shopSign = [ShopSign node];
shopSign.position = ccp(250, 620);
[self addChild:shopSign];
// store menu
[CCMenuItemFont setFontName:FONT_NAME];
[CCMenuItemFont setFontSize:24];
self.storeMenu = [CCMenu menuWithItems:nil];
[self updateMenu];
[self.storeMenu setPosition:ccp(250, 378)];
[self addChild:self.storeMenu];
// done/continue button
// CCMenuItem *doneItem = [CCMenuItemImage
// itemFromNormalImage:@"ButtonStar.png" selectedImage:@"ButtonStarSel.png"
// target:self selector:@selector(starButtonTapped:)];
CCMenuItemFont *doneItem = [CCMenuItemFont itemWithString:@"Continue" block:^(id sender) {
[self doneStore];
}];
doneItem.fontSize = 60;
[doneItem setColor:ccc3(255, 255, 255)];
CCMenu *doneMenu = [CCMenu menuWithItems:doneItem, nil];
doneMenu.position = ccp(250, 98);
[self addChild:doneMenu];
GameManager *gameManager = [GameManager sharedInstance];
[gameManager addObserver:self
forKeyPath:@"tearsCollectedTotal"
options:0
context:nil];
self.devil = [Devil node];
self.devil.position = ccp(900, 40 + self.devil.contentSize.height / 2);
[self addChild:self.devil z:1];
Floor *floor = [Floor node];
floor.position = ccp(winSize.width / 2, 0 + floor.contentSize.height / 2);
[self addChild:floor];
//Reset music
[[GameSoundManager sharedInstance] stopMaestro];
[[GameSoundManager sharedInstance].soundEngine playBackgroundMusic:@"Shop_Theme.wav"];
}
return self;
}
- (NSString *)tearsString
{
return [NSString stringWithFormat:@"%d", [GameManager sharedInstance].tearsCollectedTotal];
}
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
if ([keyPath isEqualToString:@"tearsCollectedTotal"]) {
self.tearsLabel.string = [self tearsString];
} else {
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
- (void)buyStoreItem:(StoreItem *)item
{
GameManager *gameManager = [GameManager sharedInstance];
if ([gameManager canAffordStoreItem:item] &&
![gameManager hasAlreadyPurchasedStoreItem:item]) {
// if we can afford it and haven't bought it already
gameManager.tearsCollectedTotal = gameManager.tearsCollectedTotal - item.price;
[gameManager.purchasedItems addObject:item];
[[GameSoundManager sharedInstance].soundEngine playEffect:SOUND_STORE_REGISTER];
[self updateMenu];
[self.devil animateForPurchase];
}
}
- (void)updateMenu
{
[self.storeMenu removeAllChildrenWithCleanup:YES];
GameManager *gameManager = [GameManager sharedInstance];
for (StoreItem *storeItem in gameManager.store.items) {
BOOL canAfford = [gameManager canAffordStoreItem:storeItem];
BOOL alreadyPurchased = [gameManager hasAlreadyPurchasedStoreItem:storeItem];
//NSString *checkmark = alreadyPurchased ? @"X ": @" ";
NSString *checkmark = @"";
NSString *itemString = [NSString stringWithFormat:@"%@ %d - %@", checkmark, storeItem.price, storeItem.name];
CCMenuItemFont *menuItem = [CCMenuItemFont itemWithString:itemString block:^(id sender) {
[self buyStoreItem:storeItem];
}];
[menuItem setColor:ccc3(255, 255, 255)];
if (!canAfford || alreadyPurchased) {
menuItem.isEnabled = NO;
}
[self.storeMenu addChild:menuItem];
}
[self.storeMenu alignItemsVerticallyWithPadding:30];
}
- (void)doneStore
{
[[GameSoundManager sharedInstance].soundEngine playEffect:SOUND_MENU_1];
GameManager *gameManager = [GameManager sharedInstance];
NSInteger nextLevelNum = gameManager.currentLevelNum + 1;
[[CCDirector sharedDirector] replaceScene:[CCTransitionCrossFade transitionWithDuration:0.5f scene:[GameScene nodeWithLevelNum:nextLevelNum]]];
}
@end