summary refs log tree commit diff stats
path: root/Classes/GameMode.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/GameMode.m')
-rw-r--r--Classes/GameMode.m112
1 files changed, 107 insertions, 5 deletions
diff --git a/Classes/GameMode.m b/Classes/GameMode.m index 6fa31e5..7afbf20 100644 --- a/Classes/GameMode.m +++ b/Classes/GameMode.m
@@ -8,6 +8,7 @@
8 8
9#import "GameMode.h" 9#import "GameMode.h"
10#import "FallingObject.h" 10#import "FallingObject.h"
11#import "MainMenuLayer.h"
11 12
12@implementation GameMode 13@implementation GameMode
13 14
@@ -16,7 +17,7 @@
16+ (CCScene*)scene 17+ (CCScene*)scene
17{ 18{
18 CCScene* scene = [CCScene node]; 19 CCScene* scene = [CCScene node];
19 20
20 GameMode* layer = [self node]; 21 GameMode* layer = [self node];
21 layer.tag = GAME_LAYER; 22 layer.tag = GAME_LAYER;
22 [scene addChild:layer]; 23 [scene addChild:layer];
@@ -35,6 +36,30 @@
35 isAccelerometerEnabled_ = YES; 36 isAccelerometerEnabled_ = YES;
36 37
37 objects = [[NSMutableSet alloc] init]; 38 objects = [[NSMutableSet alloc] init];
39
40 cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]];
41 cart.sprite.position = ccp(240, 22);
42 cart.sprite.scale = 2;
43 [self addChild:cart.sprite];
44
45 scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"];
46 scoreLabel.position = ccp(50, 300);
47 [self addChild:scoreLabel];
48
49 livesLabel = [CCLabelBMFont labelWithString:@"Lives: 3" fntFile:@"helvetica2.fnt"];
50 livesLabel.position = ccp(50, 280);
51 [self addChild:livesLabel];
52
53 score = 0;
54 lives = 3;
55
56 if ([self canPause])
57 {
58 CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)];
59 CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
60 [pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
61 [self addChild:pauseMenu];
62 }
38 } 63 }
39 64
40 return self; 65 return self;
@@ -45,9 +70,9 @@
45 [cart accelerometer:accelerometer didAccelerate:acceleration]; 70 [cart accelerometer:accelerometer didAccelerate:acceleration];
46} 71}
47 72
48- (void)onEnter 73- (void)onEnterTransitionDidFinish
49{ 74{
50 [super onEnter]; 75 [super onEnterTransitionDidFinish];
51 76
52 [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; 77 [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
53 [self schedule:@selector(tick:) interval:1.0f/60.0f]; 78 [self schedule:@selector(tick:) interval:1.0f/60.0f];
@@ -61,13 +86,90 @@
61 { 86 {
62 if ([object tick]) 87 if ([object tick])
63 { 88 {
64 [object retain];
65 [self removeChild:object.sprite cleanup:YES];
66 [objects removeObject:object]; 89 [objects removeObject:object];
67 } 90 }
68 } 91 }
69} 92}
70 93
94- (BOOL)canPause
95{
96 return YES;
97}
98
99- (void)pause
100{
101 if ([self canPause])
102 {
103 [self pauseSchedulerAndActions];
104
105 shadedLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)];
106 [[[CCDirector sharedDirector] runningScene] addChild:shadedLayer];
107
108 pauseLayer = [CCLayer node];
109 CCLabelBMFont* scoreLabel2 = [CCLabelBMFont labelWithString:@"PAUSE" fntFile:@"helvetica.fnt"];
110 scoreLabel2.position = ccp(240,90);
111 [pauseLayer addChild:scoreLabel2];
112
113 CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(unpause)];
114 CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
115 [pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
116 [pauseLayer addChild:pauseMenu];
117
118 CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)];
119 CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil];
120 myMenu.position = ccp(240, 60);
121 [pauseLayer addChild:myMenu];
122
123 [[[CCDirector sharedDirector] runningScene] addChild:pauseLayer];
124 }
125}
126
127- (void)unpause
128{
129 [[[CCDirector sharedDirector] runningScene] removeChild:shadedLayer cleanup:YES];
130 [[[CCDirector sharedDirector] runningScene] removeChild:pauseLayer cleanup:YES];
131
132 shadedLayer = nil;
133 pauseLayer = nil;
134
135 [self resumeSchedulerAndActions];
136}
137
138- (void)mainmenu
139{
140 [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
141}
142
143- (void)setScore:(int)m_score
144{
145 score = m_score;
146
147 [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
148}
149
150- (void)setLives:(int)m_lives
151{
152 lives = m_lives;
153
154 [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]];
155}
156
157- (void)scheduleDelayedAction:(void(^)(void))m_delayedAction delay:(float)delay
158{
159 delayedAction = Block_copy([m_delayedAction retain]);
160
161 [self schedule:@selector(runDelayedAction) interval:delay];
162}
163
164- (void)runDelayedAction
165{
166 [self unschedule:@selector(runDelayedAction)];
167
168 delayedAction();
169 [delayedAction release];
170 delayedAction = nil;
171}
172
71- (void)dealloc 173- (void)dealloc
72{ 174{
73 [objects release]; 175 [objects release];