summary refs log tree commit diff stats
path: root/Classes/GameLayer.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/GameLayer.m')
-rwxr-xr-xClasses/GameLayer.m301
1 files changed, 301 insertions, 0 deletions
diff --git a/Classes/GameLayer.m b/Classes/GameLayer.m new file mode 100755 index 0000000..32ca483 --- /dev/null +++ b/Classes/GameLayer.m
@@ -0,0 +1,301 @@
1//
2// GameLayer.m
3// Cart Collect
4//
5// Created by iD Student Account on 7/18/11.
6// Copyright 2011 __MyCompanyName__. All rights reserved.
7//
8
9#import "GameLayer.h"
10
11
12@implementation GameLayer
13
14+ (CCScene*)scene
15{
16 CCScene* scene = [CCScene node];
17
18 CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
19 [scene addChild:backgroundLayer];
20
21 GameLayer* layer = [GameLayer node];
22 [scene addChild:layer];
23
24 scene.tag = 436;
25
26 return scene;
27}
28
29- (void)tick:(ccTime)dt
30{
31 // Move the cart based on acceleration gathered from accelerometer
32 cartSprite.position = ccp(MIN(MAX(cartSprite.position.x+accelX, 16),464), cartSprite.position.y);
33
34 int lastScore = score;
35
36 for (FallingObject* object in objects)
37 {
38 // Move objects down
39 object.sprite.position = ccp(object.sprite.position.x, object.sprite.position.y-object.weight);
40
41 // Cart collision detection
42 CGSize first = [cartSprite boundingBox].size;
43 CGSize second = [object.sprite boundingBox].size;
44
45 if (cartSprite.position.x > (object.sprite.position.x - second.width/2 - first.width/2))
46 {
47 if (cartSprite.position.x < (object.sprite.position.x + second.width/2 + first.width/2))
48 {
49 if (cartSprite.position.y > (object.sprite.position.y - second.height/2 - first.height/2))
50 {
51 if (cartSprite.position.y < (object.sprite.position.y + second.height/2 + first.height/2))
52 {
53 [object retain];
54 [objects removeObject:object];
55
56 // If a cart collides with an object, it's going to vanish no matter what
57 [object.sprite.parent removeChild:object.sprite cleanup:YES];
58
59 NSString* audioFile = nil;
60 if ([object isKindOfClass:[OneUp class]])
61 {
62 audioFile = [[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"];
63 lives++;
64 } else if ([object isKindOfClass:[Rock class]])
65 {
66 audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
67 lives--;
68 } else if ([object conformsToProtocol:@protocol(ValuableObject)]) {
69 audioFile = [[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"];
70 score += [((FallingObject<ValuableObject>*)object) pointValue];
71 }
72
73 if (audioFile != nil)
74 {
75 [[SimpleAudioEngine sharedEngine] playEffect:audioFile];
76 }
77
78 [self updateLabels];
79
80 continue; // Don't check for collision with floor
81 }
82 }
83 }
84 }
85
86 // Collision detection with floor
87 if (object.sprite.position.y - (object.sprite.contentSize.height/2) < 0)
88 {
89 [object retain];
90 [objects removeObject:object];
91
92 [object.sprite.parent removeChild:object.sprite cleanup:YES];
93
94 if ([object conformsToProtocol:@protocol(ValuableObject)])
95 {
96 NSString* audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
97 [[SimpleAudioEngine sharedEngine] playEffect:audioFile];
98
99 lives--;
100
101 [self updateLabels];
102 }
103 }
104 }
105
106 if (lives == 0)
107 {
108 [self unschedule:@selector(randomlyAddObject:)];
109 [self unschedule:@selector(tick:)];
110 [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]];
111 } else if (score > lastScore)
112 {
113 if ((lastScore < 6500) && (score >= 6500))
114 {
115 [self unschedule:@selector(randomlyAddObject:)];
116 [self schedule:@selector(randomlyAddObject:) interval:0.6f];
117 addSpeed = 0.6f;
118 } else if ((lastScore < 4500) && (score >= 4500))
119 {
120 [self unschedule:@selector(randomlyAddObject:)];
121 [self schedule:@selector(randomlyAddObject:) interval:0.7f];
122 addSpeed = 0.7f;
123 } else if ((lastScore < 2500) && (score >= 2500))
124 {
125 [self unschedule:@selector(randomlyAddObject:)];
126 [self schedule:@selector(randomlyAddObject:) interval:0.8f];
127 addSpeed = 0.8f;
128 } else if ((lastScore < 1500) && (score >= 1500))
129 {
130 [self unschedule:@selector(randomlyAddObject:)];
131 [self schedule:@selector(randomlyAddObject:) interval:0.9f];
132 addSpeed = 0.9f;
133 } else if ((lastScore < 500) && (score >= 500))
134 {
135 [self unschedule:@selector(randomlyAddObject:)];
136 [self schedule:@selector(randomlyAddObject:) interval:1.0f];
137 addSpeed = 1.0f;
138 } else if ((lastScore < 150) && (score >= 150))
139 {
140 [self unschedule:@selector(randomlyAddObject:)];
141 [self schedule:@selector(randomlyAddObject:) interval:2.0f];
142 addSpeed = 2.0f;
143 }
144 }
145}
146
147- (void)randomlyAddObject:(ccTime)dt
148{
149 FallingObject* object;
150 int oneuppercent = 98 - (lives == 1 ? 1 : 0);
151
152 if (score < 1000)
153 {
154 int randomval = arc4random()%100;
155
156 if (randomval < 65)
157 {
158 object = [[Cherry alloc] init];
159 } else if (randomval < oneuppercent)
160 {
161 object = [[Bottle alloc] init];
162 } else {
163 object = [[OneUp alloc] init];
164 }
165 } else {
166 int randomval = arc4random()%100;
167
168 if (randomval < 40)
169 {
170 object = [[Cherry alloc] init];
171 } else if (randomval < 70)
172 {
173 object = [[Rock alloc] init];
174 } else if (randomval < oneuppercent)
175 {
176 object = [[Bottle alloc] init];
177 } else {
178 object = [[OneUp alloc] init];
179 }
180 }
181
182 int objectX = arc4random()%448+16;
183 object.sprite.position = ccp(objectX, 360);
184 object.sprite.scale = 1;
185 [self addChild:object.sprite];
186
187 [objects addObject:object];
188 [object release];
189
190 if (score >= 2000)
191 {
192 if (arc4random() % 100 > 80)
193 {
194 object = [[Rock alloc] init];
195
196 objectX = arc4random()%448+16;
197 object.sprite.position = ccp(objectX, 360);
198 object.sprite.scale = 1;
199 [self addChild:object.sprite];
200
201 [objects addObject:object];
202 [object release];
203 }
204 }
205
206 if (score >= 4000)
207 {
208 if (arc4random() % 100 > 80)
209 {
210 object = [[Rock alloc] init];
211
212 objectX = arc4random()%448+16;
213 object.sprite.position = ccp(objectX, 360);
214 object.sprite.scale = 1;
215 [self addChild:object.sprite];
216
217 [objects addObject:object];
218 [object release];
219 }
220 }
221}
222
223- (id)init
224{
225 self = [super init];
226
227 int winWidth = [CCDirector sharedDirector].winSize.width;
228 //int winHeight = [CCDirector sharedDirector].winSize.height;
229 int cartScale = 2;
230
231 if (self != nil)
232 {
233 CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"];
234 backgroundImage.position = ccp(240, 160);
235 [self addChild:backgroundImage z:0];
236
237 isAccelerometerEnabled_ = YES;
238
239 //cart = [[Cart alloc] init];
240 cartSprite = [CCSprite spriteWithFile:@"cart.png"];
241 cartSprite.position = ccp(winWidth/2, 22);
242 cartSprite.scale = cartScale;
243 [self addChild:cartSprite];
244
245 objects = [[NSMutableSet alloc] init];
246
247 score = 0;
248 lives = 3;
249
250 scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"];
251 scoreLabel.position = ccp(50, 300);
252 [self addChild:scoreLabel];
253
254 livesLabel = [CCLabelBMFont labelWithString:@"Lives: 3" fntFile:@"helvetica2.fnt"];
255 livesLabel.position = ccp(50, 280);
256 [self addChild:livesLabel];
257
258 CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)];
259 CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
260 [pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
261 [self addChild:pauseMenu];
262
263 addSpeed = 2.5f;
264 }
265
266 return self;
267}
268
269-(void) onEnter
270{
271 [super onEnter];
272
273 [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
274 [self schedule:@selector(tick:) interval:1.0f/60.0f];
275 [self schedule:@selector(randomlyAddObject:) interval:addSpeed];
276}
277
278- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
279{
280 static float prevY=0;
281
282#define kFilterFactor 0.05f
283
284 float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY);
285
286 prevY = accelY;
287 accelX = accelY * 750;
288}
289
290- (void)updateLabels
291{
292 [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
293 [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]];
294}
295
296- (void)pause
297{
298 [[CCDirector sharedDirector] replaceScene:[PauseLayer sceneWithScene:[[CCDirector sharedDirector] runningScene]]];
299}
300
301@end