summary refs log tree commit diff stats
path: root/Classes/GameLayer.m
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-08-09 21:36:09 -0400
committerStarla Insigna <starla4444@gmail.com>2011-08-09 21:36:09 -0400
commit002dc99a071a4a8f7d2afc15d1fdf2ae0312faed (patch)
tree82808d85a131ed0f970b374828a3e80ee1fb8d35 /Classes/GameLayer.m
parent1ed37fda409479769c8d110aa22d5dadc9d2cb85 (diff)
downloadcartcollect-002dc99a071a4a8f7d2afc15d1fdf2ae0312faed.tar.gz
cartcollect-002dc99a071a4a8f7d2afc15d1fdf2ae0312faed.tar.bz2
cartcollect-002dc99a071a4a8f7d2afc15d1fdf2ae0312faed.zip
Abstracted a lot of GameLayer's functionality out
In order to prepare for tutorial mode (and eventually, the other game modes), a lot of GameLayer's functionality (reading the accelerometer, moving the cart, moving falling objects, reacting to collisions…) has been abstracted out to FallingObject and subclasses of FallingObject, as well as a new Cart class, and GameMode, a new superclass of GameLayer. The only things that were not abstracted out that probably will be in the future are the pause functionality and the tutorial bubble functionality (which will in fact be moved to the tutorial mode class).

A lot of work was also done to cut down on compiler warnings and a lot of #imports were moved from header files to implementation files because I realized THAT'S HOW YOU'RE SUPPOSED TO DO IT.

Refs #193
Diffstat (limited to 'Classes/GameLayer.m')
-rwxr-xr-xClasses/GameLayer.m149
1 files changed, 29 insertions, 120 deletions
diff --git a/Classes/GameLayer.m b/Classes/GameLayer.m index 2ec8ef5..95bc83b 100755 --- a/Classes/GameLayer.m +++ b/Classes/GameLayer.m
@@ -7,109 +7,29 @@
7// 7//
8 8
9#import "GameLayer.h" 9#import "GameLayer.h"
10 10#import "FallingObject.h"
11#import "Cherry.h"
12#import "Bottle.h"
13#import "OneUp.h"
14#import "Rock.h"
15#import "GameOverLayer.h"
16#import "SimpleAudioEngine.h"
17#import "MainMenuLayer.h"
11 18
12@implementation GameLayer 19@implementation GameLayer
13 20
14@synthesize currentTutorial; 21@synthesize currentTutorial;
15 22
16+ (CCScene*)scene
17{
18 CCScene* scene = [CCScene node];
19
20 CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
21 [scene addChild:backgroundLayer];
22
23 GameLayer* layer = [GameLayer node];
24 layer.tag = GAME_LAYER;
25 [scene addChild:layer];
26
27 scene.tag = GAME_SCENE;
28
29 return scene;
30}
31
32- (void)tick:(ccTime)dt 23- (void)tick:(ccTime)dt
33{ 24{
34 // Move the cart based on acceleration gathered from accelerometer
35 cartSprite.position = ccp(MIN(MAX(cartSprite.position.x+accelX, 16),464), cartSprite.position.y);
36
37 int lastScore = score; 25 int lastScore = score;
38 26
39 for (FallingObject* object in objects) 27 [super tick:dt];
40 {
41 // Move objects down
42 object.sprite.position = ccp(object.sprite.position.x, object.sprite.position.y-object.weight);
43
44 // Cart collision detection
45 CGSize first = [cartSprite boundingBox].size;
46 CGSize second = [object.sprite boundingBox].size;
47
48 if (cartSprite.position.x > (object.sprite.position.x - second.width/2 - first.width/2))
49 {
50 if (cartSprite.position.x < (object.sprite.position.x + second.width/2 + first.width/2))
51 {
52 if (cartSprite.position.y > (object.sprite.position.y - second.height/2 - first.height/2))
53 {
54 if (cartSprite.position.y < (object.sprite.position.y + second.height/2 + first.height/2))
55 {
56 [object retain];
57 [objects removeObject:object];
58
59 // If a cart collides with an object, it's going to vanish no matter what
60 [object.sprite.parent removeChild:object.sprite cleanup:YES];
61
62 NSString* audioFile = nil;
63 if ([object isKindOfClass:[OneUp class]])
64 {
65 audioFile = [[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"];
66 lives++;
67 } else if ([object isKindOfClass:[Rock class]])
68 {
69 audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
70 lives--;
71 } else if ([object conformsToProtocol:@protocol(ValuableObject)]) {
72 audioFile = [[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"];
73 score += [((FallingObject<ValuableObject>*)object) pointValue];
74 }
75
76 if (audioFile != nil)
77 {
78 [[SimpleAudioEngine sharedEngine] playEffect:audioFile];
79 }
80
81 [self updateLabels];
82
83 continue; // Don't check for collision with floor
84 }
85 }
86 }
87 }
88
89 // Collision detection with floor
90 if (object.sprite.position.y - (object.sprite.contentSize.height/2) < 0)
91 {
92 [object retain];
93 [objects removeObject:object];
94
95 [object.sprite.parent removeChild:object.sprite cleanup:YES];
96
97 if ([object conformsToProtocol:@protocol(ValuableObject)])
98 {
99 NSString* audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
100 [[SimpleAudioEngine sharedEngine] playEffect:audioFile];
101
102 lives--;
103
104 [self updateLabels];
105 }
106 }
107 }
108 28
109 if (lives == 0) 29 if (lives == 0)
110 { 30 {
111 [self unschedule:@selector(randomlyAddObject:)]; 31 [self unscheduleAllSelectors];
112 [self unschedule:@selector(tick:)]; 32
113 [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]]; 33 [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]];
114 } else if (score > lastScore) 34 } else if (score > lastScore)
115 { 35 {
@@ -237,20 +157,12 @@
237 backgroundImage.position = ccp(240, 160); 157 backgroundImage.position = ccp(240, 160);
238 [self addChild:backgroundImage z:0]; 158 [self addChild:backgroundImage z:0];
239 159
240 isAccelerometerEnabled_ = YES; 160 cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]];
241 161 cart.sprite.position = ccp(winWidth/2, 22);
242 //cart = [[Cart alloc] init]; 162 cart.sprite.scale = cartScale;
243 cartSprite = [CCSprite spriteWithFile:@"cart.png"]; 163 [self addChild:cart.sprite];
244 cartSprite.position = ccp(winWidth/2, 22); 164
245 cartSprite.scale = cartScale; 165 scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"];
246 [self addChild:cartSprite];
247
248 objects = [[NSMutableSet alloc] init];
249
250 score = 0;
251 lives = 3;
252
253 scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"];
254 scoreLabel.position = ccp(50, 300); 166 scoreLabel.position = ccp(50, 300);
255 [self addChild:scoreLabel]; 167 [self addChild:scoreLabel];
256 168
@@ -258,6 +170,9 @@
258 livesLabel.position = ccp(50, 280); 170 livesLabel.position = ccp(50, 280);
259 [self addChild:livesLabel]; 171 [self addChild:livesLabel];
260 172
173 score = 0;
174 lives = 3;
175
261 CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)]; 176 CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)];
262 CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; 177 CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
263 [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; 178 [pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
@@ -273,27 +188,21 @@
273{ 188{
274 [super onEnter]; 189 [super onEnter];
275 190
276 [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
277 [self schedule:@selector(tick:) interval:1.0f/60.0f];
278 [self schedule:@selector(randomlyAddObject:) interval:addSpeed]; 191 [self schedule:@selector(randomlyAddObject:) interval:addSpeed];
279} 192}
280 193
281- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration 194- (void)setScore:(int)m_score
282{ 195{
283 static float prevY=0; 196 score = m_score;
284 197
285#define kFilterFactor 0.05f 198 [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
286
287 float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY);
288
289 prevY = accelY;
290 accelX = accelY * 750;
291} 199}
292 200
293- (void)updateLabels 201- (void)setLives:(int)m_lives
294{ 202{
295 [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; 203 lives = m_lives;
296 [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]]; 204
205 [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]];
297} 206}
298 207
299- (void)pause 208- (void)pause