diff options
Diffstat (limited to 'Classes/TutorialMode.m')
-rw-r--r-- | Classes/TutorialMode.m | 349 |
1 files changed, 349 insertions, 0 deletions
diff --git a/Classes/TutorialMode.m b/Classes/TutorialMode.m new file mode 100644 index 0000000..37e8bd0 --- /dev/null +++ b/Classes/TutorialMode.m | |||
@@ -0,0 +1,349 @@ | |||
1 | // | ||
2 | // TutorialMode.m | ||
3 | // Cart Collect | ||
4 | // | ||
5 | // Created by Starla Insigna on 8/10/11. | ||
6 | // Copyright 2011 Four Island. All rights reserved. | ||
7 | // | ||
8 | |||
9 | #import "TutorialMode.h" | ||
10 | #import "FallingObject.h" | ||
11 | #import "Cherry.h" | ||
12 | #import "Bottle.h" | ||
13 | #import "OneUp.h" | ||
14 | #import "Rock.h" | ||
15 | #import "ClassicGameMode.h" | ||
16 | |||
17 | // Item tags: | ||
18 | // 2000 - first dropped item | ||
19 | // 2001 - item that is dropped after you catch first dropped item to demonstrate what happens when you miss | ||
20 | // 2002 - items that are dropped after you miss first dropped item to demonstrate what happens when you catch | ||
21 | // 2003 - 1-Up | ||
22 | // 2009 - rock | ||
23 | |||
24 | @implementation TutorialMode | ||
25 | |||
26 | @synthesize currentTutorial; | ||
27 | |||
28 | - (id)init | ||
29 | { | ||
30 | self = [super init]; | ||
31 | |||
32 | if (nil != self) | ||
33 | { | ||
34 | CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"]; | ||
35 | backgroundImage.position = ccp(240, 160); | ||
36 | [self addChild:backgroundImage z:-1]; | ||
37 | |||
38 | CCMenuItemImage* menuItem1 = [CCMenuItemImage itemFromNormalImage:@"skiptutorial.png" selectedImage:@"skiptutorial2.png" target:self selector:@selector(skipTutorial)]; | ||
39 | CCMenu* theMenu = [CCMenu menuWithItems:menuItem1, nil]; | ||
40 | theMenu.position = ccp(480-16-16-62, 320-8-16); | ||
41 | [self addChild:theMenu]; | ||
42 | |||
43 | showedDeathBubble = NO; | ||
44 | randomItemsDropped = 0; | ||
45 | } | ||
46 | |||
47 | return self; | ||
48 | } | ||
49 | |||
50 | - (void)onEnterTransitionDidFinish | ||
51 | { | ||
52 | [super onEnterTransitionDidFinish]; | ||
53 | |||
54 | [self scheduleDelayedAction:^{ | ||
55 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"Welcome to Cart Collect. This is a tutorial designed to help you get started playing the game. Below this bubble is a cart. Tilt your device to move it." name:@"cart" spriteReference:cart.sprite]; | ||
56 | self.currentTutorial = bubble; | ||
57 | [bubble release]; | ||
58 | } delay:2.0f]; | ||
59 | } | ||
60 | |||
61 | - (void)tick:(ccTime)dt | ||
62 | { | ||
63 | [super tick:dt]; | ||
64 | |||
65 | FallingObject* object = [objects anyObject]; | ||
66 | if ((object.sprite.tag == 2000) && (object.sprite.position.y == 360-object.weight*14)) | ||
67 | { | ||
68 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"This is an item. Try to catch it with your cart." name:@"item" spriteReference:object.sprite]; | ||
69 | self.currentTutorial = bubble; | ||
70 | [bubble release]; | ||
71 | } else if ((object.sprite.tag == 2003) && (object.sprite.position.y == 360-object.weight*8)) | ||
72 | { | ||
73 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"This is a 1-Up. Catch it to gain an extra life. There's no penalty for not catching it, though." name:@"oneup" spriteReference:object.sprite]; | ||
74 | self.currentTutorial = bubble; | ||
75 | [bubble release]; | ||
76 | } else if ((object.sprite.tag == 2009) && (object.sprite.position.y == 360-object.weight*14)) | ||
77 | { | ||
78 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"This is a rock. It would be better for your health if you did not catch this item." name:@"rock" spriteReference:object.sprite]; | ||
79 | self.currentTutorial = bubble; | ||
80 | [bubble release]; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | - (void)didCatchItem:(FallingObject *)item | ||
85 | { | ||
86 | if (item.sprite.tag == 2000) | ||
87 | { | ||
88 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"Congratulations! If you look at your score, you'll see it increased. Catching items is good. Now, let's see what happens when you don't catch an item." name:@"caught-first"]; | ||
89 | self.currentTutorial = bubble; | ||
90 | [bubble release]; | ||
91 | } else if (item.sprite.tag == 2002) | ||
92 | { | ||
93 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"There you go! If you look at your score, you'll see it increased. Catching items is good." name:@"caught-second"]; | ||
94 | self.currentTutorial = bubble; | ||
95 | [bubble release]; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | - (void)didMissItem:(FallingObject *)item | ||
100 | { | ||
101 | if (item.sprite.tag == 2000) | ||
102 | { | ||
103 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"Whoops, you missed it! Look at your lives counter--you lost one! If you lose all of your lives, you lose the game. Try catching the item again." name:@"missed-first"]; | ||
104 | self.currentTutorial = bubble; | ||
105 | [bubble release]; | ||
106 | } else if (item.sprite.tag == 2001) | ||
107 | { | ||
108 | [cart setImmobile:NO]; | ||
109 | |||
110 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"You lost a life! You only have three lives, so try not to miss any items! However..." name:@"missed-second"]; | ||
111 | self.currentTutorial = bubble; | ||
112 | [bubble release]; | ||
113 | } else if (item.sprite.tag == 2002) | ||
114 | { | ||
115 | [self scheduleDelayedAction:^{ | ||
116 | FallingObject* object = [self dropRandomItem]; | ||
117 | object.sprite.tag = 2002; | ||
118 | [object release]; | ||
119 | } delay:1.0f]; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | - (void)didDestroyItem:(FallingObject *)item | ||
124 | { | ||
125 | if (item.sprite.tag == 2003) | ||
126 | { | ||
127 | [self schedule:@selector(randomlyAddObject:) interval:2.5f]; | ||
128 | } else if (item.sprite.tag == 2009) | ||
129 | { | ||
130 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"As you play, Cart Collect gets progressively more intense. Watch what happens when rocks are added to the mix and the speed is turned up." name:@"intense"]; | ||
131 | self.currentTutorial = bubble; | ||
132 | [bubble release]; | ||
133 | } | ||
134 | } | ||
135 | |||
136 | - (void)setCurrentTutorial:(TutorialBubble *)m_currentTutorial | ||
137 | { | ||
138 | @synchronized(self) | ||
139 | { | ||
140 | if (currentTutorial != m_currentTutorial) | ||
141 | { | ||
142 | [currentTutorial removeFromSuperview]; | ||
143 | [currentTutorial release]; | ||
144 | currentTutorial = [m_currentTutorial retain]; | ||
145 | } | ||
146 | } | ||
147 | |||
148 | if (currentTutorial != nil) | ||
149 | { | ||
150 | [currentTutorial setTarget:self action:@selector(endTutorial)]; | ||
151 | [[[CCDirector sharedDirector] openGLView] addSubview:currentTutorial]; | ||
152 | [self pauseSchedulerAndActions]; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | - (void)endTutorial | ||
157 | { | ||
158 | [self resumeSchedulerAndActions]; | ||
159 | |||
160 | if ([currentTutorial.name isEqual:@"cart"]) | ||
161 | { | ||
162 | [self scheduleDelayedAction:^{ | ||
163 | FallingObject* object = [self dropRandomItem]; | ||
164 | object.sprite.tag = 2000; | ||
165 | [object release]; | ||
166 | } delay:3.0f]; | ||
167 | } else if ([currentTutorial.name isEqual:@"caught-first"]) | ||
168 | { | ||
169 | [cart setImmobile:YES]; | ||
170 | |||
171 | [self scheduleDelayedAction:^{ | ||
172 | FallingObject* object = [self dropRandomItem]; | ||
173 | |||
174 | if (cart.sprite.position.x > 240) | ||
175 | { | ||
176 | object.sprite.position = ccp(20, 360); | ||
177 | } else { | ||
178 | object.sprite.position = ccp(460, 360); | ||
179 | } | ||
180 | |||
181 | object.sprite.tag = 2001; | ||
182 | |||
183 | [object release]; | ||
184 | } delay:1.0f]; | ||
185 | } else if ([currentTutorial.name isEqual:@"missed-first"]) | ||
186 | { | ||
187 | [self scheduleDelayedAction:^{ | ||
188 | FallingObject* object = [self dropRandomItem]; | ||
189 | object.sprite.tag = 2002; | ||
190 | [object release]; | ||
191 | } delay:1.0f]; | ||
192 | } else if (([currentTutorial.name isEqual:@"caught-second"]) || ([currentTutorial.name isEqual:@"missed-second"])) | ||
193 | { | ||
194 | [self scheduleDelayedAction:^{ | ||
195 | FallingObject* object = [self dropSpecificItem:[[OneUp alloc] init]]; | ||
196 | object.sprite.tag = 2003; | ||
197 | [object release]; | ||
198 | } delay:2.0f]; | ||
199 | } else if ([currentTutorial.name isEqual:@"intense"]) | ||
200 | { | ||
201 | [self schedule:@selector(randomlyAddObject:) interval:1.0f]; | ||
202 | } else if ([currentTutorial.name isEqual:@"end"]) | ||
203 | { | ||
204 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | ||
205 | [defaults setBool:YES forKey:@"hasDoneTutorial"]; | ||
206 | |||
207 | [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:3.0f scene:[ClassicGameMode scene] withColor:ccc3(0,0,0)]]; | ||
208 | } | ||
209 | |||
210 | self.currentTutorial = nil; | ||
211 | } | ||
212 | |||
213 | - (void)pause | ||
214 | { | ||
215 | if (self.currentTutorial != nil) | ||
216 | { | ||
217 | [self.currentTutorial removeFromSuperview]; | ||
218 | } | ||
219 | |||
220 | [super pause]; | ||
221 | } | ||
222 | |||
223 | - (void)unpause | ||
224 | { | ||
225 | [super unpause]; | ||
226 | |||
227 | if (self.currentTutorial != nil) | ||
228 | { | ||
229 | [self pauseSchedulerAndActions]; | ||
230 | [[[CCDirector sharedDirector] openGLView] addSubview:self.currentTutorial]; | ||
231 | } | ||
232 | } | ||
233 | |||
234 | - (FallingObject*)dropSpecificItem:(FallingObject*)object | ||
235 | { | ||
236 | int objectX = arc4random()%448+16; | ||
237 | object.sprite.position = ccp(objectX, 360); | ||
238 | object.sprite.scale = 1; | ||
239 | [self addChild:object.sprite]; | ||
240 | |||
241 | object.delegate = self; | ||
242 | |||
243 | [objects addObject:object]; | ||
244 | |||
245 | return object; | ||
246 | } | ||
247 | |||
248 | - (FallingObject*)dropRandomItem | ||
249 | { | ||
250 | FallingObject* object; | ||
251 | int randomval = arc4random()%100; | ||
252 | |||
253 | if (randomval < 65) | ||
254 | { | ||
255 | object = [[Cherry alloc] init]; | ||
256 | } else { | ||
257 | object = [[Bottle alloc] init]; | ||
258 | } | ||
259 | |||
260 | return [self dropSpecificItem:object]; | ||
261 | } | ||
262 | |||
263 | - (void)setLives:(int)m_lives | ||
264 | { | ||
265 | [super setLives:m_lives]; | ||
266 | |||
267 | if ((lives < 1) && (!showedDeathBubble)) | ||
268 | { | ||
269 | showedDeathBubble = YES; | ||
270 | |||
271 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"You lost all your lives! Normally, you'd be taken to a game over screen where you could submit your score to the highscore list, but we're a bit more forgiving in tutorial mode." name:@"gameover"]; | ||
272 | self.currentTutorial = bubble; | ||
273 | [bubble release]; | ||
274 | } | ||
275 | } | ||
276 | |||
277 | - (void)randomlyAddObject:(ccTime)dt | ||
278 | { | ||
279 | FallingObject* object; | ||
280 | |||
281 | if (randomItemsDropped < 5) | ||
282 | { | ||
283 | int randomval = arc4random()%100; | ||
284 | |||
285 | if (randomval < 65) | ||
286 | { | ||
287 | object = [[Cherry alloc] init]; | ||
288 | } else if (randomval < 98) | ||
289 | { | ||
290 | object = [[Bottle alloc] init]; | ||
291 | } else { | ||
292 | object = [[OneUp alloc] init]; | ||
293 | } | ||
294 | } else if (randomItemsDropped == 5) | ||
295 | { | ||
296 | object = [[Rock alloc] init]; | ||
297 | object.sprite.tag = 2009; | ||
298 | |||
299 | [self unschedule:@selector(randomlyAddObject:)]; | ||
300 | } else if (randomItemsDropped < 15) { | ||
301 | int randomval = arc4random()%100; | ||
302 | |||
303 | if (randomval < 40) | ||
304 | { | ||
305 | object = [[Cherry alloc] init]; | ||
306 | } else if (randomval < 70) | ||
307 | { | ||
308 | object = [[Rock alloc] init]; | ||
309 | } else if (randomval < 98) | ||
310 | { | ||
311 | object = [[Bottle alloc] init]; | ||
312 | } else { | ||
313 | object = [[OneUp alloc] init]; | ||
314 | } | ||
315 | } else if (randomItemsDropped == 15) | ||
316 | { | ||
317 | [self scheduleDelayedAction:^{ | ||
318 | TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"That's pretty much it! You've completed the tutorial, so now it's time to play an actual game of Cart Collect!" name:@"end"]; | ||
319 | self.currentTutorial = bubble; | ||
320 | [bubble release]; | ||
321 | } delay:2.0f]; | ||
322 | |||
323 | [self unschedule:@selector(randomlyAddObject:)]; | ||
324 | |||
325 | return; | ||
326 | } else { | ||
327 | NSLog(@"randomItemsDropped in TutorialMode is greater than 15--this should never happen."); | ||
328 | |||
329 | return; | ||
330 | } | ||
331 | |||
332 | [self dropSpecificItem:object]; | ||
333 | [object release]; | ||
334 | |||
335 | randomItemsDropped++; | ||
336 | } | ||
337 | |||
338 | - (void)skipTutorial | ||
339 | { | ||
340 | [self unscheduleAllSelectors]; | ||
341 | self.currentTutorial = nil; | ||
342 | |||
343 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | ||
344 | [defaults setBool:YES forKey:@"hasDoneTutorial"]; | ||
345 | |||
346 | [[CCDirector sharedDirector] replaceScene:[CCTransitionFlipY transitionWithDuration:1.0f scene:[ClassicGameMode scene]]]; | ||
347 | } | ||
348 | |||
349 | @end | ||