summary refs log tree commit diff stats
path: root/Classes/GameModeSelectionLayer.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/GameModeSelectionLayer.m')
-rw-r--r--Classes/GameModeSelectionLayer.m138
1 files changed, 138 insertions, 0 deletions
diff --git a/Classes/GameModeSelectionLayer.m b/Classes/GameModeSelectionLayer.m new file mode 100644 index 0000000..ad6a455 --- /dev/null +++ b/Classes/GameModeSelectionLayer.m
@@ -0,0 +1,138 @@
1//
2// GameModeSelectionLayer.m
3// Cartographic
4//
5// Created by Starla Insigna on 8/18/11.
6// Copyright 2011 Four Island. All rights reserved.
7//
8
9#import "GameModeSelectionLayer.h"
10#import "GameModeSelection.h"
11#import <sqlite3.h>
12#import "Cart_CollectAppDelegate.h"
13#import "MainMenuLayer.h"
14#import "TutorialMode.h"
15#import "ClassicGameMode.h"
16
17@implementation GameModeSelectionLayer
18
19+ (CCScene*)scene
20{
21 CCScene* scene = [CCScene node];
22
23 CCLayer* backgroundLayer = [[[CCLayer alloc] init] autorelease];
24 CCSprite* backgroundImage = [CCSprite spriteWithFile:@"paintdaubs.png"];
25 backgroundImage.position = ccp(240,160);
26 [backgroundLayer addChild:backgroundImage];
27 [scene addChild:backgroundLayer];
28
29 GameModeSelectionLayer* layer = [GameModeSelectionLayer node];
30 [scene addChild:layer];
31
32 return scene;
33}
34
35- (id)init
36{
37 self = [super init];
38
39 if (nil != self)
40 {
41 gameModes = [[NSMutableArray alloc] init];
42 CCMenu* menu = [CCMenu menuWithItems:nil];
43 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
44 float onePanelWide = 128;
45
46 GameModeSelection* tutorialSelection = [GameModeSelection selectionWithName:@"Tutorial" location:@"Florence" filename:@"florence" unlocked:YES];
47 [gameModes addObject:tutorialSelection];
48
49 GameModeSelection* collectSelection;
50
51 if ([defaults boolForKey:@"hasDoneTutorial"])
52 {
53 const char* sqlQuery = "SELECT * FROM highscores ORDER BY score DESC LIMIT 1";
54 sqlite3_stmt* compiled_statement;
55 int score = 0;
56
57 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
58 {
59 if (sqlite3_step(compiled_statement) == SQLITE_ROW)
60 {
61 score = sqlite3_column_int(compiled_statement, 2);
62 }
63 }
64
65 collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" highscore:score];
66 } else {
67 collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" unlockCondition:@"Beat the tutorial!"];
68 }
69
70 [gameModes addObject:collectSelection];
71
72 float padding = 15;
73 float totalPanelWidth = onePanelWide + padding*2;
74 float numberOfPanels = [gameModes count];
75 float totalWidth = numberOfPanels * totalPanelWidth;
76 int currentWorldOffset = [defaults integerForKey:@"lastSelectedMode"];
77 CCLayer* panels = [CCLayer node];
78
79 for (GameModeSelection* gameMode in gameModes)
80 {
81 [gameMode setDelegate:self];
82 [menu addChild:gameMode];
83 }
84
85 [menu alignItemsHorizontallyWithPadding:padding*2];
86 [panels addChild:menu];
87 [self addChild:panels];
88
89 pageControl = [[UIPageControl alloc] init];
90 pageControl.numberOfPages = numberOfPanels;
91 pageControl.currentPage = currentWorldOffset;
92 pageControl.frame = CGRectMake(0, 250, 480, 20);
93 [[[CCDirector sharedDirector] openGLView] addSubview:pageControl];
94
95 menu.position = ccpAdd(menu.position, ccp(totalWidth/2 - totalPanelWidth/2, 320));
96 touchDelegatingView = [[TouchDelegatingView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
97 scrollView = [[CocosOverlayScrollView alloc] initWithFrame:CGRectMake(0, 0, totalPanelWidth, 320) numPages:numberOfPanels width:totalPanelWidth layer:panels pageControl:pageControl];
98 touchDelegatingView.scrollView = scrollView;
99 [scrollView setContentOffset:CGPointMake(currentWorldOffset*totalPanelWidth+1,0) animated:NO];
100 [[[CCDirector sharedDirector] openGLView] addSubview:touchDelegatingView];
101 [[[CCDirector sharedDirector] openGLView] addSubview:scrollView];
102
103 CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)];
104 CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil];
105 myMenu.position = ccp(240, 30);
106 [self addChild:myMenu];
107 }
108
109 return self;
110}
111
112- (void)onExit
113{
114 [touchDelegatingView removeFromSuperview];
115 [scrollView removeFromSuperview];
116 [pageControl removeFromSuperview];
117}
118
119- (void)mainmenu
120{
121 [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
122}
123
124- (void)didSelectGameMode:(GameModeSelection *)gameMode
125{
126 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
127 [defaults setInteger:[gameModes indexOfObject:gameMode] forKey:@"lastSelectedMode"];
128
129 if ([gameMode.name isEqual:@"Tutorial"])
130 {
131 [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]];
132 } else if ([gameMode.name isEqual:@"Collect"])
133 {
134 [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]];
135 }
136}
137
138@end