From 002dc99a071a4a8f7d2afc15d1fdf2ae0312faed Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 9 Aug 2011 21:36:09 -0400 Subject: Abstracted a lot of GameLayer's functionality out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Classes/GameLayer.m | 149 ++++++++++------------------------------------------ 1 file changed, 29 insertions(+), 120 deletions(-) (limited to 'Classes/GameLayer.m') 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 @@ // #import "GameLayer.h" - +#import "FallingObject.h" +#import "Cherry.h" +#import "Bottle.h" +#import "OneUp.h" +#import "Rock.h" +#import "GameOverLayer.h" +#import "SimpleAudioEngine.h" +#import "MainMenuLayer.h" @implementation GameLayer @synthesize currentTutorial; -+ (CCScene*)scene -{ - CCScene* scene = [CCScene node]; - - CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; - [scene addChild:backgroundLayer]; - - GameLayer* layer = [GameLayer node]; - layer.tag = GAME_LAYER; - [scene addChild:layer]; - - scene.tag = GAME_SCENE; - - 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*)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]; - } - } - } + [super tick:dt]; if (lives == 0) { - [self unschedule:@selector(randomlyAddObject:)]; - [self unschedule:@selector(tick:)]; + [self unscheduleAllSelectors]; + [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]]; } else if (score > lastScore) { @@ -237,20 +157,12 @@ 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"]; + cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]]; + cart.sprite.position = ccp(winWidth/2, 22); + cart.sprite.scale = cartScale; + [self addChild:cart.sprite]; + + scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"]; scoreLabel.position = ccp(50, 300); [self addChild:scoreLabel]; @@ -258,6 +170,9 @@ livesLabel.position = ccp(50, 280); [self addChild:livesLabel]; + score = 0; + lives = 3; + 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)]; @@ -273,27 +188,21 @@ { [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)setScore:(int)m_score +{ + score = m_score; + + [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; } -- (void)updateLabels +- (void)setLives:(int)m_lives { - [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; - [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]]; + lives = m_lives; + + [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]]; } - (void)pause -- cgit 1.4.1