summary refs log tree commit diff stats
path: root/Classes/TutorialMode.m
blob: 17d94127f75e5552b853f9cbb0c7efa2976c4bb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//
//  TutorialMode.m
//  Cart Collect
//
//  Created by Starla Insigna on 8/10/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "TutorialMode.h"
#import "FallingObject.h"
#import "GameModeSelectionLayer.h"
#import "SimpleAudioEngine.h"
#import "ClassicGameMode.h"
#import "JumpGameMode.h"
#import "ScoreBarLayer.h"

// Item tags:
//    2000 - first dropped item
//    2001 - item that is dropped after you catch first dropped item to demonstrate what happens when you miss
//    2002 - items that are dropped after you miss first dropped item to demonstrate what happens when you catch
//    2003 - 1-Up
//    2009 - rock
//    2010 - random item dropped after death from first rock

typedef enum {
    kCherryObject = 0,
    kEnergyDrinkObject,
    kOneUpObject,
    kRockObject
} FallingObjects;

@implementation TutorialMode

@synthesize currentTutorial, lives, score;

static GameModeInfo* info;

+ (GameModeInfo*)info
{
    if (info == nil)
    {
        info = [[GameModeInfo alloc] initWithName:@"Tutorial"
                                         location:@"Florence"
                                       numOfStars:0
                                    imageFilename:[[NSBundle mainBundle] pathForResource:@"florence" ofType:@"png"] 
                                         unlocked:YES
                                        gameClass:[TutorialMode class]
                               globalHighscoreKey:nil];
    }
    
    return info;
}

- (id)init
{
    self = [super init];
    
    if (nil != self)
    {
        CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"];
		backgroundImage.position = ccp(240, 160);
		[self addChild:backgroundImage z:-1];
        
        score = 0;
        lives = 3;
        
        showedDeathBubble = NO;
        randomItemsDropped = 0;
        
        ScoreBarLayer* scoreBar = [ScoreBarLayer scoreBar];
        [self addChild:scoreBar];
        
        scoreLabel = [CCLabelBMFont labelWithString:@"0" fntFile:@"helvetica2.fnt"];
        scoreLabel.position = ccp(30,20);
        [scoreBar addChild:scoreLabel];
        
        CCSprite* livesImage = [CCSprite spriteWithFile:@"oneup.png"];
        livesImage.position = ccp(110, 20);
        [scoreBar addChild:livesImage];
        
        livesLabel = [CCLabelBMFont labelWithString:@"x3" fntFile:@"helvetica2.fnt"];
        livesLabel.position = ccp(142,20);
        [scoreBar addChild:livesLabel];
        
        [objectFactory createRecipeWithIdentifier:kCherryObject spriteFilename:@"cherry.png" weight:5];
        [objectFactory createRecipeWithIdentifier:kEnergyDrinkObject spriteFilename:@"bottle.png" weight:6];
        [objectFactory createRecipeWithIdentifier:kOneUpObject spriteFilename:@"oneup.png" weight:10];
        [objectFactory createRecipeWithIdentifier:kRockObject spriteFilename:@"rock.png" weight:7];
    }
    
    return self;
}

- (void)onEnterTransitionDidFinish
{
    [super onEnterTransitionDidFinish];
    
    [self scheduleDelayedAction:^{
        TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"Welcome to Cartographic. 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];
        self.currentTutorial = bubble;
        [bubble release];
    } delay:2.0f];
}

- (void)tick:(ccTime)dt
{
    [super tick:dt];
    
    FallingObject* object = [objects anyObject];
    if ((object.sprite.tag == 2000) && (object.sprite.position.y == 360-object.weight*14))
    {
        TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"This is an item. Try to catch it with your cart." name:@"item" spriteReference:object.sprite];
        self.currentTutorial = bubble;
        [bubble release];
    } else if ((object.sprite.tag == 2003) && (object.sprite.position.y == 360-object.weight*8))
    {
        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];
        self.currentTutorial = bubble;
        [bubble release];
    } else if ((object.sprite.tag == 2009) && (object.sprite.position.y == 360-object.weight*14))
    {
        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];
        self.currentTutorial = bubble;
        [bubble release];
    }
}

- (void)didCatchItem:(FallingObject *)item
{
    if (item.objectType == kCherryObject)
    {
        [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"]];
        self.score += 10;
    } else if (item.objectType == kEnergyDrinkObject)
    {
        [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"]];
        self.score += 25;
    } else if (item.objectType == kOneUpObject)
    {
        [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"]];
        self.lives++;
    } else if (item.objectType == kRockObject)
    {
        [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]];
        self.lives--;
    }
    
    if (item.sprite.tag == 2000)
    {
        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"];
        self.currentTutorial = bubble;
        [bubble release];
    } else if (item.sprite.tag == 2002)
    {
        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"];
        self.currentTutorial = bubble;
        [bubble release];
    }
}

- (void)didMissItem:(FallingObject *)item
{
    if ((item.objectType == kCherryObject) || (item.objectType == kEnergyDrinkObject))
    {
        [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]];
        self.lives--;
    }
    
    if (item.sprite.tag == 2000)
    {
        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"];
        self.currentTutorial = bubble;
        [bubble release];
    } else if (item.sprite.tag == 2001)
    {
        [cart setImmobile:NO];
        
        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"];
        self.currentTutorial = bubble;
        [bubble release];
    } else if (item.sprite.tag == 2002)
    {
        [self scheduleDelayedAction:^{
            FallingObject* object = [self dropRandomItem];
            object.sprite.tag = 2002;
        } delay:1.0f];
    }
}

- (void)didDestroyItem:(FallingObject *)item
{
    if (item.sprite.tag == 2003)
    {
        [self schedule:@selector(randomlyAddObject:) interval:2.5f];
    } else if (item.sprite.tag == 2009)
    {
        if ((lives < 1) && (!showedDeathBubble))
        {
            showedDeathBubble = YES;
            
            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-rock"];
            self.currentTutorial = bubble;
            [bubble release];
        } else {
            TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"As you play, Cartographic gets progressively more intense. Watch what happens when rocks are added to the mix and the speed is turned up." name:@"intense"];
            self.currentTutorial = bubble;
            [bubble release];
        }
    } else if (item.sprite.tag == 2010)
    {
        TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"As you play, Cartographic gets progressively more intense. Watch what happens when rocks are added to the mix and the speed is turned up." name:@"intense"];
        self.currentTutorial = bubble;
        [bubble release];
    }
    
    if ((lives < 1) && (!showedDeathBubble))
    {
        showedDeathBubble = YES;
        
        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"];
        self.currentTutorial = bubble;
        [bubble release];
    }
}

- (void)setCurrentTutorial:(TutorialBubble *)m_currentTutorial
{
    @synchronized(self)
    {
        if (currentTutorial != m_currentTutorial)
        {
            [currentTutorial removeFromSuperview];
            [currentTutorial release];
            currentTutorial = [m_currentTutorial retain];
        }
    }
    
    if (currentTutorial != nil)
    {
        [currentTutorial setTarget:self action:@selector(endTutorial)];
        [[[CCDirector sharedDirector] openGLView] addSubview:currentTutorial];
        [self pauseSchedulerAndActions];
    }
}

- (void)endTutorial
{
    [self resumeSchedulerAndActions];
    
    if ([currentTutorial.name isEqual:@"cart"])
    {
        [self scheduleDelayedAction:^{
            FallingObject* object = [self dropRandomItem];
            object.sprite.tag = 2000;
        } delay:3.0f];
    } else if ([currentTutorial.name isEqual:@"caught-first"])
    {
        [cart setImmobile:YES];
        
        [self scheduleDelayedAction:^{
            FallingObject* object = [self dropRandomItem];
            
            if (cart.sprite.position.x > 240)
            {
                object.sprite.position = ccp(20, 360);
            } else {
                object.sprite.position = ccp(460, 360);
            }
            
            object.sprite.tag = 2001;
        } delay:1.0f];
    } else if ([currentTutorial.name isEqual:@"missed-first"])
    {
        [self scheduleDelayedAction:^{
            FallingObject* object = [self dropRandomItem];
            object.sprite.tag = 2002;
        } delay:1.0f];
    } else if (([currentTutorial.name isEqual:@"caught-second"]) || ([currentTutorial.name isEqual:@"missed-second"]))
    {
        [self scheduleDelayedAction:^{
            FallingObject* object = [self dropSpecificItem:kOneUpObject];
            object.sprite.tag = 2003;
        } delay:2.0f];
    } else if ([currentTutorial.name isEqual:@"gameover-rock"])
    {
        [self scheduleDelayedAction:^{
            FallingObject* object = [self dropRandomItem];
            object.sprite.tag = 2010;
        } delay:1.0f];
    } else if ([currentTutorial.name isEqual:@"intense"])
    {
        [self schedule:@selector(randomlyAddObject:) interval:1.0f];
    } else if ([currentTutorial.name isEqual:@"end"])
    {
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:3.0f scene:[GameModeSelectionLayer scene] withColor:ccc3(0,0,0)]];
    }
    
    self.currentTutorial = nil;
}

- (void)pause
{
    if (self.currentTutorial != nil)
    {
        [self.currentTutorial removeFromSuperview];
    }
    
    [super pause];
}

- (void)unpause
{
    [super unpause];
    
    if (self.currentTutorial != nil)
    {
        [self pauseSchedulerAndActions];
        [[[CCDirector sharedDirector] openGLView] addSubview:self.currentTutorial];
    }
}

- (FallingObject*)dropSpecificItem:(int)objectType
{
    FallingObject* object = [[objectFactory buildFallingObjectWithRecipeIdentifier:objectType] retain];
    int objectX = arc4random()%448+16;
	object.sprite.position = ccp(objectX, 360);
	object.sprite.scale = 1;
	[self addChild:object.sprite];
    
    object.delegate = self;
	
	[objects addObject:object];
    
    return [object autorelease];
}

- (FallingObject*)dropRandomItem
{
    int randomval = arc4random()%100;
    int recipeIdentifier;
    
    if (randomval < 65)
    {
        recipeIdentifier = kCherryObject;
    } else {
        recipeIdentifier = kEnergyDrinkObject;
    }
    
    return [self dropSpecificItem:recipeIdentifier];
}

- (void)randomlyAddObject:(ccTime)dt
{
	int recipeIdentifier;
    
	if (randomItemsDropped < 5)
	{
		int randomval = arc4random()%100;
		
		if (randomval < 65)
		{
            recipeIdentifier = kCherryObject;
		} else if (randomval < 98)
		{
			recipeIdentifier = kEnergyDrinkObject;
		} else {
			recipeIdentifier = kOneUpObject;
		}
    } else if (randomItemsDropped == 5)
    {
        recipeIdentifier = kRockObject;
        
        [self unschedule:@selector(randomlyAddObject:)];
	} else if (randomItemsDropped < 15) {
		int randomval = arc4random()%100;
		
		if (randomval < 40)
		{
			recipeIdentifier = kCherryObject;
		} else if (randomval < 70)
		{
			recipeIdentifier = kRockObject;
		} else if (randomval < 98)
		{
			recipeIdentifier = kEnergyDrinkObject;
		} else {
			recipeIdentifier = kOneUpObject;
		}
	} else if (randomItemsDropped == 15)
    {
        [self scheduleDelayedAction:^{
            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 Cartographic!" name:@"end"];
            self.currentTutorial = bubble;
            [bubble release];
        } delay:2.0f];
        
        [self unschedule:@selector(randomlyAddObject:)];
        
        [[ClassicGameMode info] unlock];
        [[JumpGameMode info] unlock];
        
        return;
    } else {
        NSLog(@"randomItemsDropped in TutorialMode is greater than 15--this should never happen.");
        
        return;
    }
    
    FallingObject* object = [self dropSpecificItem:recipeIdentifier];

    if (randomItemsDropped == 5)
    {
        object.sprite.tag = 2009;
    }
    
    randomItemsDropped++;
}


- (void)setScore:(int)m_score
{
    score = m_score;
    
    [scoreLabel setString:[NSString stringWithFormat:@"%d", score]];
}

- (void)setLives:(int)m_lives
{
    lives = m_lives;
    
    [livesLabel setString:[NSString stringWithFormat:@"x%d", lives]];
}

@end