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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
//
// GameMode.m
// Cart Collect
//
// Created by Starla Insigna on 8/9/11.
// Copyright 2011 Four Island. All rights reserved.
//
#import "GameMode.h"
#import "FallingObject.h"
#import "MainMenuLayer.h"
@implementation GameMode
@synthesize cart, score, lives, isPaused, pointMultiplier;
+ (CCScene*)scene
{
CCScene* scene = [CCScene node];
GameMode* layer = [self node];
layer.tag = GAME_LAYER;
[scene addChild:layer];
scene.tag = GAME_SCENE;
return scene;
}
- (id)init
{
self = [super init];
if (nil != self)
{
objects = [[NSMutableSet alloc] init];
cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]];
cart.sprite.position = ccp(240, 22);
cart.sprite.scale = 2;
cart.delegate = self;
[self addChild:cart.sprite];
scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"];
scoreLabel.position = ccp(50, 300);
[self addChild:scoreLabel];
livesLabel = [CCLabelBMFont labelWithString:@"Lives: 3" fntFile:@"helvetica2.fnt"];
livesLabel.position = ccp(50, 280);
[self addChild:livesLabel];
score = 0;
lives = 3;
pointMultiplier = 1;
if ([self canPause])
{
CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)];
CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
[pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
[self addChild:pauseMenu];
}
isPaused = NO;
Class cmClass = (NSClassFromString(@"CMMotionManager"));
if (cmClass)
{
motionManager = [[CMMotionManager alloc] init];
if (motionManager.gyroAvailable)
{
[motionManager setDeviceMotionUpdateInterval:1.0f/60.0f];
isAccelerometerEnabled_ = NO;
hasGyroscope = YES;
} else {
isAccelerometerEnabled_ = YES;
motionManager = nil;
hasGyroscope = NO;
}
} else {
isAccelerometerEnabled_ = YES;
hasGyroscope = NO;
}
}
return self;
}
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
self.pitch = acceleration.y;
}
- (void)onEnterTransitionDidFinish
{
[super onEnterTransitionDidFinish];
if (hasGyroscope)
{
[motionManager startDeviceMotionUpdates];
} else {
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
}
[self schedule:@selector(tick:) interval:1.0f/60.0f];
}
- (void)onExit
{
if (hasGyroscope)
{
[motionManager stopDeviceMotionUpdates];
}
}
- (void)tick:(ccTime)dt
{
if (hasGyroscope)
{
CMDeviceMotion* motion = [motionManager deviceMotion];
self.pitch = -motion.attitude.pitch;
}
[cart tick];
NSMutableSet* discardedObjects = [NSMutableSet set];
for (FallingObject* object in objects)
{
if ([object tick])
{
[discardedObjects addObject:object];
}
}
[objects minusSet:discardedObjects];
}
- (BOOL)canPause
{
return YES;
}
- (void)pause
{
if (([self canPause]) && (!isPaused))
{
isPaused = YES;
[self pauseSchedulerAndActions];
if (hasGyroscope)
{
[motionManager stopDeviceMotionUpdates];
}
shadedLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)];
[[[CCDirector sharedDirector] runningScene] addChild:shadedLayer];
pauseLayer = [CCLayer node];
CCLabelBMFont* scoreLabel2 = [CCLabelBMFont labelWithString:@"PAUSE" fntFile:@"helvetica.fnt"];
scoreLabel2.position = ccp(240,90);
[pauseLayer addChild:scoreLabel2];
CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(unpause)];
CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
[pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
[pauseLayer addChild:pauseMenu];
CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)];
CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil];
myMenu.position = ccp(240, 60);
[pauseLayer addChild:myMenu];
[[[CCDirector sharedDirector] runningScene] addChild:pauseLayer];
}
}
- (void)unpause
{
if (isPaused)
{
[[[CCDirector sharedDirector] runningScene] removeChild:shadedLayer cleanup:YES];
[[[CCDirector sharedDirector] runningScene] removeChild:pauseLayer cleanup:YES];
shadedLayer = nil;
pauseLayer = nil;
if (hasGyroscope)
{
[motionManager startDeviceMotionUpdates];
}
[self resumeSchedulerAndActions];
isPaused = NO;
}
}
- (void)mainmenu
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Cartographic" message:@"Are you sure you want to quit the currently playing game?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[alert show];
[alert release];
}
- (void)setScore:(int)m_score
{
score = m_score;
if (pointMultiplier > 1)
{
[scoreLabel setString:[NSString stringWithFormat:@"Score: %d x%d", score, pointMultiplier]];
} else {
[scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
}
}
- (void)setLives:(int)m_lives
{
lives = m_lives;
[livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]];
}
- (void)setPointMultiplier:(int)m_pointMultiplier
{
pointMultiplier = m_pointMultiplier;
if (pointMultiplier > 1)
{
[scoreLabel setString:[NSString stringWithFormat:@"Score: %d x%d", score, pointMultiplier]];
} else {
[scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
}
}
- (void)scheduleDelayedAction:(void(^)(void))m_delayedAction delay:(float)delay
{
delayedAction = Block_copy([m_delayedAction retain]);
[self schedule:@selector(runDelayedAction) interval:delay];
}
- (void)runDelayedAction
{
[self unschedule:@selector(runDelayedAction)];
delayedAction();
[delayedAction release];
delayedAction = nil;
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
}
}
- (void)setPitch:(double)m_pitch
{
pitch = m_pitch;
[cart deviceDidRotate:pitch];
}
- (void)dealloc
{
[objects release];
[cart release];
[super dealloc];
}
@end
|