|
1 // |
|
2 // GameConfigViewController.m |
|
3 // HedgewarsMobile |
|
4 // |
|
5 // Created by Vittorio on 18/04/10. |
|
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 // |
|
8 |
|
9 #import "GameConfigViewController.h" |
|
10 #import "SDL_uikitappdelegate.h" |
|
11 #import "CommodityFunctions.h" |
|
12 #import "MapConfigViewController.h" |
|
13 #import "TeamConfigViewController.h" |
|
14 #import "SchemeWeaponConfigViewController.h" |
|
15 |
|
16 @implementation GameConfigViewController |
|
17 |
|
18 |
|
19 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
20 return rotationManager(interfaceOrientation); |
|
21 } |
|
22 |
|
23 -(IBAction) buttonPressed:(id) sender { |
|
24 // works even if it's not actually a button |
|
25 UIButton *theButton = (UIButton *)sender; |
|
26 switch (theButton.tag) { |
|
27 case 0: |
|
28 [[self parentViewController] dismissModalViewControllerAnimated:YES]; |
|
29 break; |
|
30 case 1: |
|
31 [self performSelector:@selector(startGame) |
|
32 withObject:nil |
|
33 afterDelay:0.25]; |
|
34 break; |
|
35 default: |
|
36 break; |
|
37 } |
|
38 } |
|
39 |
|
40 -(IBAction) segmentPressed:(id) sender { |
|
41 UISegmentedControl *theSegment = (UISegmentedControl *)sender; |
|
42 |
|
43 switch (theSegment.selectedSegmentIndex) { |
|
44 case 0: |
|
45 // this init here is just aestetic as this controller was already set up in viewDidLoad |
|
46 if (mapConfigViewController == nil) { |
|
47 mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil]; |
|
48 } |
|
49 activeController = mapConfigViewController; |
|
50 break; |
|
51 case 1: |
|
52 if (teamConfigViewController == nil) { |
|
53 teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
54 // this message is compulsory otherwise the table won't be loaded at all |
|
55 } |
|
56 activeController = teamConfigViewController; |
|
57 break; |
|
58 case 2: |
|
59 if (schemeWeaponConfigViewController == nil) { |
|
60 schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
61 } |
|
62 activeController = schemeWeaponConfigViewController; |
|
63 break; |
|
64 } |
|
65 |
|
66 // this message is compulsory otherwise the table won't be loaded at all |
|
67 [activeController viewWillAppear:NO]; |
|
68 [self.view addSubview:activeController.view]; |
|
69 } |
|
70 |
|
71 -(void) startGame { |
|
72 // don't start playing if the preview is in progress |
|
73 if ([mapConfigViewController busy]) { |
|
74 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Wait for the Preview",@"") |
|
75 message:NSLocalizedString(@"Before playing the preview needs to be generated",@"") |
|
76 delegate:nil |
|
77 cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
78 otherButtonTitles:nil]; |
|
79 [alert show]; |
|
80 [alert release]; |
|
81 return; |
|
82 } |
|
83 |
|
84 // play only if there is more than one team |
|
85 if ([teamConfigViewController.listOfSelectedTeams count] < 2) { |
|
86 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too few teams playing",@"") |
|
87 message:NSLocalizedString(@"You need to select at least two teams to play a game",@"") |
|
88 delegate:nil |
|
89 cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
90 otherButtonTitles:nil]; |
|
91 [alert show]; |
|
92 [alert release]; |
|
93 return; |
|
94 } |
|
95 |
|
96 // play if there's room for enough hogs in the selected map |
|
97 int hogs = 0; |
|
98 for (NSDictionary *teamData in teamConfigViewController.listOfSelectedTeams) |
|
99 hogs += [[teamData objectForKey:@"number"] intValue]; |
|
100 |
|
101 if (hogs > mapConfigViewController.maxHogs) { |
|
102 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too many hogs",@"") |
|
103 message:NSLocalizedString(@"The map you selected is too small for that many hogs",@"") |
|
104 delegate:nil |
|
105 cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"") |
|
106 otherButtonTitles:nil]; |
|
107 [alert show]; |
|
108 [alert release]; |
|
109 return; |
|
110 } |
|
111 |
|
112 // create the configuration file that is going to be sent to engine |
|
113 NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:mapConfigViewController.seedCommand,@"seed_command", |
|
114 mapConfigViewController.templateFilterCommand,@"templatefilter_command", |
|
115 mapConfigViewController.mapGenCommand,@"mapgen_command", |
|
116 mapConfigViewController.mazeSizeCommand,@"mazesize_command", |
|
117 mapConfigViewController.themeCommand,@"theme_command", |
|
118 teamConfigViewController.listOfSelectedTeams,@"teams_list", |
|
119 schemeWeaponConfigViewController.selectedScheme,@"scheme", |
|
120 schemeWeaponConfigViewController.selectedWeapon,@"weapon", |
|
121 nil]; |
|
122 [dict writeToFile:GAMECONFIG_FILE() atomically:YES]; |
|
123 [dict release]; |
|
124 |
|
125 // finally launch game and remove this controller |
|
126 [[self parentViewController] dismissModalViewControllerAnimated:YES]; |
|
127 [[SDLUIKitDelegate sharedAppDelegate] startSDLgame]; |
|
128 } |
|
129 |
|
130 -(void) viewDidLoad { |
|
131 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
|
132 if (mapConfigViewController == nil) |
|
133 mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPad" bundle:nil]; |
|
134 if (teamConfigViewController == nil) |
|
135 teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStylePlain]; |
|
136 teamConfigViewController.view.frame = CGRectMake(0, 224, 300, 500); |
|
137 teamConfigViewController.view.backgroundColor = [UIColor clearColor]; |
|
138 [mapConfigViewController.view addSubview:teamConfigViewController.view]; |
|
139 if (schemeWeaponConfigViewController == nil) |
|
140 schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
141 schemeWeaponConfigViewController.view.frame = CGRectMake(362, 224, 300, 500); |
|
142 schemeWeaponConfigViewController.view.backgroundColor = [UIColor clearColor]; |
|
143 [mapConfigViewController.view addSubview:schemeWeaponConfigViewController.view]; |
|
144 } else |
|
145 mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil]; |
|
146 activeController = mapConfigViewController; |
|
147 |
|
148 [self.view addSubview:mapConfigViewController.view]; |
|
149 |
|
150 [super viewDidLoad]; |
|
151 } |
|
152 |
|
153 -(void) viewWillAppear:(BOOL)animated { |
|
154 [mapConfigViewController viewWillAppear:animated]; |
|
155 [teamConfigViewController viewWillAppear:animated]; |
|
156 [schemeWeaponConfigViewController viewWillAppear:animated]; |
|
157 // ADD other controllers here |
|
158 |
|
159 [super viewWillAppear:animated]; |
|
160 } |
|
161 |
|
162 -(void) viewDidAppear:(BOOL)animated { |
|
163 [mapConfigViewController viewDidAppear:animated]; |
|
164 [teamConfigViewController viewDidAppear:animated]; |
|
165 [schemeWeaponConfigViewController viewDidAppear:animated]; |
|
166 [super viewDidAppear:animated]; |
|
167 } |
|
168 |
|
169 -(void) didReceiveMemoryWarning { |
|
170 // Releases the view if it doesn't have a superview. |
|
171 [super didReceiveMemoryWarning]; |
|
172 // Release any cached data, images, etc that aren't in use. |
|
173 if (mapConfigViewController.view.superview == nil) |
|
174 mapConfigViewController = nil; |
|
175 if (teamConfigViewController.view.superview == nil) |
|
176 teamConfigViewController = nil; |
|
177 if (schemeWeaponConfigViewController.view.superview == nil) |
|
178 schemeWeaponConfigViewController = nil; |
|
179 activeController = nil; |
|
180 MSG_MEMCLEAN(); |
|
181 } |
|
182 |
|
183 -(void) viewDidUnload { |
|
184 activeController = nil; |
|
185 mapConfigViewController = nil; |
|
186 teamConfigViewController = nil; |
|
187 schemeWeaponConfigViewController = nil; |
|
188 [super viewDidUnload]; |
|
189 MSG_DIDUNLOAD(); |
|
190 } |
|
191 |
|
192 -(void) dealloc { |
|
193 [activeController release]; |
|
194 [mapConfigViewController release]; |
|
195 [teamConfigViewController release]; |
|
196 [schemeWeaponConfigViewController release]; |
|
197 [super dealloc]; |
|
198 } |
|
199 |
|
200 @end |