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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
//
// GameLayer.m
// Cart Collect
//
// Created by iD Student Account on 7/18/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "GameLayer.h"
@implementation GameLayer
+ (CCScene*)scene
{
CCScene* scene = [CCScene node];
CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
[scene addChild:backgroundLayer];
GameLayer* layer = [GameLayer node];
[scene addChild:layer];
scene.tag = 436;
return scene;
}
- (void)tick:(ccTime)dt
{
// Move the cart based on acceleration gathered from accelerometer
cartSprite.position = ccp(MIN(MAX(cartSprite.position.x+accelX, 16),464), cartSprite.position.y);
int lastScore = score;
for (FallingObject* object in objects)
{
// Move objects down
object.sprite.position = ccp(object.sprite.position.x, object.sprite.position.y-object.weight);
// Cart collision detection
CGSize first = [cartSprite boundingBox].size;
CGSize second = [object.sprite boundingBox].size;
if (cartSprite.position.x > (object.sprite.position.x - second.width/2 - first.width/2))
{
if (cartSprite.position.x < (object.sprite.position.x + second.width/2 + first.width/2))
{
if (cartSprite.position.y > (object.sprite.position.y - second.height/2 - first.height/2))
{
if (cartSprite.position.y < (object.sprite.position.y + second.height/2 + first.height/2))
{
[object retain];
[objects removeObject:object];
// If a cart collides with an object, it's going to vanish no matter what
[object.sprite.parent removeChild:object.sprite cleanup:YES];
NSString* audioFile = nil;
if ([object isKindOfClass:[OneUp class]])
{
audioFile = [[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"];
lives++;
} else if ([object isKindOfClass:[Rock class]])
{
audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
lives--;
} else if ([object conformsToProtocol:@protocol(ValuableObject)]) {
audioFile = [[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"];
score += [((FallingObject<ValuableObject>*)object) pointValue];
}
if (audioFile != nil)
{
[[SimpleAudioEngine sharedEngine] playEffect:audioFile];
}
[self updateLabels];
continue; // Don't check for collision with floor
}
}
}
}
// Collision detection with floor
if (object.sprite.position.y - (object.sprite.contentSize.height/2) < 0)
{
[object retain];
[objects removeObject:object];
[object.sprite.parent removeChild:object.sprite cleanup:YES];
if ([object conformsToProtocol:@protocol(ValuableObject)])
{
NSString* audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
[[SimpleAudioEngine sharedEngine] playEffect:audioFile];
lives--;
[self updateLabels];
}
}
}
if (lives == 0)
{
[self unschedule:@selector(randomlyAddObject:)];
[self unschedule:@selector(tick:)];
[[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]];
} else if (score > lastScore)
{
if ((lastScore < 6500) && (score >= 6500))
{
[self unschedule:@selector(randomlyAddObject:)];
[self schedule:@selector(randomlyAddObject:) interval:0.6f];
addSpeed = 0.6f;
} else if ((lastScore < 4500) && (score >= 4500))
{
[self unschedule:@selector(randomlyAddObject:)];
[self schedule:@selector(randomlyAddObject:) interval:0.7f];
addSpeed = 0.7f;
} else if ((lastScore < 2500) && (score >= 2500))
{
[self unschedule:@selector(randomlyAddObject:)];
[self schedule:@selector(randomlyAddObject:) interval:0.8f];
addSpeed = 0.8f;
} else if ((lastScore < 1500) && (score >= 1500))
{
[self unschedule:@selector(randomlyAddObject:)];
[self schedule:@selector(randomlyAddObject:) interval:0.9f];
addSpeed = 0.9f;
} else if ((lastScore < 500) && (score >= 500))
{
[self unschedule:@selector(randomlyAddObject:)];
[self schedule:@selector(randomlyAddObject:) interval:1.0f];
addSpeed = 1.0f;
} else if ((lastScore < 150) && (score >= 150))
{
[self unschedule:@selector(randomlyAddObject:)];
[self schedule:@selector(randomlyAddObject:) interval:2.0f];
addSpeed = 2.0f;
}
}
}
- (void)randomlyAddObject:(ccTime)dt
{
FallingObject* object;
int oneuppercent = 98 - (lives == 1 ? 1 : 0);
if (score < 1000)
{
int randomval = arc4random()%100;
if (randomval < 65)
{
object = [[Cherry alloc] init];
} else if (randomval < oneuppercent)
{
object = [[Bottle alloc] init];
} else {
object = [[OneUp alloc] init];
}
} else {
int randomval = arc4random()%100;
if (randomval < 40)
{
object = [[Cherry alloc] init];
} else if (randomval < 70)
{
object = [[Rock alloc] init];
} else if (randomval < oneuppercent)
{
object = [[Bottle alloc] init];
} else {
object = [[OneUp alloc] init];
}
}
int objectX = arc4random()%448+16;
object.sprite.position = ccp(objectX, 360);
object.sprite.scale = 1;
[self addChild:object.sprite];
[objects addObject:object];
[object release];
if (score >= 2000)
{
if (arc4random() % 100 > 80)
{
object = [[Rock alloc] init];
objectX = arc4random()%448+16;
object.sprite.position = ccp(objectX, 360);
object.sprite.scale = 1;
[self addChild:object.sprite];
[objects addObject:object];
[object release];
}
}
if (score >= 4000)
{
if (arc4random() % 100 > 80)
{
object = [[Rock alloc] init];
objectX = arc4random()%448+16;
object.sprite.position = ccp(objectX, 360);
object.sprite.scale = 1;
[self addChild:object.sprite];
[objects addObject:object];
[object release];
}
}
}
- (id)init
{
self = [super init];
int winWidth = [CCDirector sharedDirector].winSize.width;
//int winHeight = [CCDirector sharedDirector].winSize.height;
int cartScale = 2;
if (self != nil)
{
CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"];
backgroundImage.position = ccp(240, 160);
[self addChild:backgroundImage z:0];
isAccelerometerEnabled_ = YES;
//cart = [[Cart alloc] init];
cartSprite = [CCSprite spriteWithFile:@"cart.png"];
cartSprite.position = ccp(winWidth/2, 22);
cartSprite.scale = cartScale;
[self addChild:cartSprite];
objects = [[NSMutableSet alloc] init];
score = 0;
lives = 3;
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];
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];
addSpeed = 2.5f;
}
return self;
}
-(void) onEnter
{
[super onEnter];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
[self schedule:@selector(tick:) interval:1.0f/60.0f];
[self schedule:@selector(randomlyAddObject:) interval:addSpeed];
}
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
static float prevY=0;
#define kFilterFactor 0.05f
float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY);
prevY = accelY;
accelX = accelY * 750;
}
- (void)updateLabels
{
[scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
[livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]];
}
- (void)pause
{
[[CCDirector sharedDirector] replaceScene:[PauseLayer sceneWithScene:[[CCDirector sharedDirector] runningScene]]];
}
@end
|