-
Notifications
You must be signed in to change notification settings - Fork 0
/
JazzBallViewController.m
356 lines (293 loc) · 10.9 KB
/
JazzBallViewController.m
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
//
// JazzBallViewController.m
// JazzBall
//
// Created by Julian on 4/1/09.
// Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import "JazzBallViewController.h"
#import "JazzBallView.h"
#import "GameManager.h"
#import "JBAppController.h"
#import "LevelSummaryView.h"
#import "GADRequest.h"
#import "GADBannerView.h"
#define ARCHIVE_KEY_GAME_MANAGER @"gameman"
#define SAVE_GAME_FILE @"savedgame"
#define SCREEN_UNLOCK_DELAY 1.0
#define ADMOB_VIEW_FRAME (CGRectMake(0, 430, 320, 50))
#define LITE_GAMEVIEW_WIDTH (16*16)
@implementation JazzBallViewController
@synthesize percentCompleteLabel = lblPercentComplete;
@synthesize levelSummaryView;
@synthesize isShowingGameSubmenu = showingGameSubmenu;
static NSString* _saveFilePath = nil;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[lblLeveLintro.layer setCornerRadius:8.0];
// do this before creating gameMan
#ifdef JBALL_LITE
CGRect gvframe = gameView.bounds;
CGPoint center = gameView.center;
gvframe.size.height -= GAD_SIZE_320x50.height;
gvframe.size.width = LITE_GAMEVIEW_WIDTH;
NSLog(@"gv frame: %@", NSStringFromCGRect(gameView.frame));
center.y -= .5*GAD_SIZE_320x50.height;
[gameView setBounds:gvframe];
[gameView setCenter:center];
#endif
// iPad: scale the whole gameView so it works on the big screen
#ifdef TARGET_IPAD
[gameView setTransform:CGAffineTransformMakeScale(IPAD_SCALE, IPAD_SCALE)];
#endif
gameMan = [[GameManager alloc] initWithController:self];
[self initViewsAndShit];
oneLifeColor = [[UIColor alloc] initWithRed:.9 green:0 blue:0 alpha:1];
[Atom setContainerLayer:self.gameView.layer];
showingGameSubmenu = false;
}
-(void) initViewsAndShit {
[self.gameView setGameManager:gameMan];
[gameMan setLevelSummaryView:levelSummaryView];
[gameMan setLevelIntroLabel:lblLeveLintro];
}
-(void) viewDidUnload {
[adView release];
adView = nil;
[super viewDidUnload];
}
// ad stuff goes here.
-(void) viewWillAppear:(BOOL)animated {
#ifdef JBALL_LITE
adView = [[GADBannerView alloc] initWithFrame:ADMOB_VIEW_FRAME];
[adView setAdUnitID: @"a14b6cdc09b2a67"];
[adView setRootViewController:self];
[adView setDelegate:self];
[self.view addSubview:adView];
GADRequest* request = [GADRequest request];
dlog(@"GAD SDK: %@", [GADRequest sdkVersion]);
NSArray* testDeviceArray = [[NSArray alloc] initWithObjects: GAD_SIMULATOR_ID, @"6c2dc822dd43ed5a87c36f8e02d4e6d5c6a474e5", @"74c84bb43fd0e54cce6b80213c9570e1ff7b19b2", nil];
[request setTestDevices:testDeviceArray];
[adView loadRequest:request];
[testDeviceArray release];
#endif
}
-(void) viewWillDisappear:(BOOL)animated {
// get rid of the banner, it auto refreshes!
[adView removeFromSuperview];
[adView release];
adView = nil;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(JazzBallView*) gameView {
return gameView;
}
-(void) startGame {
[self startGameWithContext:nil];
}
-(void) startGameWithContext:(NSDictionary*)context {
[gameMan prepareNewGame];
[gameView prepareForNewGame];
if (context) {
[gameMan setGameContext:context];
}
NSUserDefaults* sud = [NSUserDefaults standardUserDefaults];
didStartByViewingInstructions = false;
if ([sud boolForKey:UDHasReadInstructionsKey] == NO) {
UIViewController* instructionsViewController = ((JBAppController*)[[UIApplication sharedApplication] delegate]).instructionsViewController;
[self presentModalViewController:instructionsViewController animated:YES];
didStartByViewingInstructions = true;
}
else {
[gameMan startGame];
}
}
-(void) pauseGame {
if ([gameMan levelPlaying] == 0)
return;
[gameMan pause];
}
-(BOOL) resumeGame {
if ([gameMan levelPlaying] == 0)
return true;
// note that because we can't tell if the screen was unlocked, or if it was something else (call, text msg)
// we always delay.
// bugfix: if showing the submenu, don't resume.
if (showingGameSubmenu)
return false;
[gameMan performSelector:@selector(resume) withObject:nil afterDelay:SCREEN_UNLOCK_DELAY];
return true;
}
-(void) saveGame {
dlog(@"SAVING GAME");
NSString* savefile = [JazzBallViewController saveFilePath];
NSMutableData* mdata = [NSMutableData new];
NSKeyedArchiver* keyarch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mdata];
[keyarch encodeObject:gameMan forKey:ARCHIVE_KEY_GAME_MANAGER];
[keyarch finishEncoding];
if (![mdata writeToFile:savefile atomically:NO]) {
NSLog(@"failed to write save file");
}
[keyarch release];
[mdata release];
}
-(void) loadGame {
dlog(@"LOADING GAME");
NSString* savefile = [JazzBallViewController saveFilePath];
NSMutableData* mdata = [[NSMutableData alloc] initWithContentsOfFile:savefile];
NSKeyedUnarchiver* keyunarch = [[NSKeyedUnarchiver alloc] initForReadingWithData:mdata];
[gameMan release];
gameMan = [keyunarch decodeObjectForKey:ARCHIVE_KEY_GAME_MANAGER];
[gameMan retain];
[keyunarch finishDecoding];
[keyunarch release];
[mdata release];
[gameMan setController:self];
[self initViewsAndShit];
[gameMan resumeSavedGame];
}
-(void) endLevel {
[levelSummaryView setHidden:NO];
}
-(void) beginLevel {
[levelSummaryView setHidden:YES];
}
-(void) setScore:(unsigned long)score {
NSString* scoreStr = [[NSString alloc] initWithFormat:@"%u", score];
[lblScore setText:scoreStr];
[scoreStr release];
}
-(void) setLives:(unsigned short)lives {
static unsigned short lastLives = 0;
NSString* livesStr = [[NSString alloc] initWithFormat:@"%hu", lives];
[lblLives setText:livesStr];
if (lastLives == 1) {
[lblLives setTextColor:[UIColor blackColor]];
}
if (lives == 1) {
[lblLives setTextColor:[UIColor redColor]];
}
lastLives = lives;
[livesStr release];
}
-(void) setPercentComplete:(short)pct {
NSString* completedAreaPercent = [NSString stringWithFormat:@"%i%%", pct];
[lblPercentComplete setText:completedAreaPercent];
}
-(void) updateBonusTimer:(short)bonus {
NSString* bonusString = [NSString stringWithFormat:@"%i", bonus];
[lblBonus setText:bonusString];
}
-(void) gameTileChanged:(id)sender {
[gameMan checkLoadTileImage];
}
-(void) showSubmenuActionSheet {
if (showingGameSubmenu) {
return;
}
UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Resume" destructiveButtonTitle:@"Quit to Menu" otherButtonTitles:nil];
[sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[self pauseGame];
[sheet showInView:self.view];
[sheet release];
showingGameSubmenu = true;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
+(NSString*) saveFilePath {
if (_saveFilePath == nil) {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] == 0) {
NSLog(@"CRITICAL ERROR: documents directory not found");
return @"";
}
NSString* docsDir = [paths objectAtIndex:0];
_saveFilePath = [[docsDir stringByAppendingPathComponent:SAVE_GAME_FILE] retain];
}
return _saveFilePath;
}
- (void)dealloc {
// [window release];
[oneLifeColor release];
[super dealloc];
}
#pragma mark -
#pragma mark UIViewController Overrides
-(void)dismissModalViewControllerAnimated:(BOOL)animated {
if (didStartByViewingInstructions) {
[gameMan startGame];
}
[super dismissModalViewControllerAnimated:animated];
}
#pragma mark -
#pragma mark UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) {
[gameMan resume];
}
else {
UIAlertView* confirmAlert = [[UIAlertView alloc] initWithTitle:@"End Game?" message:@"Your score will not be considered for the high scores list." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"End Game", nil];
[confirmAlert show];
[confirmAlert release];
}
showingGameSubmenu = false;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == alertView.cancelButtonIndex) {
[gameMan resume];
}
else {
[gameMan endGame];
JBAppController* appC = (JBAppController*)[[UIApplication sharedApplication] delegate];
[appC dismissModalViewControllerAnimated:NO];
}
}
#pragma mark -
#pragma mark Touch Events
-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[gameMan touchEvent:event];
}
#pragma mark -
#pragma mark AdMob delegate
// Sent when an ad request loaded an ad. This is a good opportunity to add this
// view to the hierarchy if it has not yet been added. If the ad was received
// as a part of the server-side auto refreshing, you can examine the
// hasAutoRefreshed property of the view.
- (void)adViewDidReceiveAd:(GADBannerView *)view {
}
// Sent when an ad request failed. Normally this is because no network
// connection was available or no ads were available (i.e. no fill). If the
// error was received as a part of the server-side auto refreshing, you can
// examine the hasAutoRefreshed property of the view.
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
}
// Sent just before presenting the user a full screen view, such as a browser,
// in response to clicking on an ad. Use this opportunity to stop animations,
// time sensitive interactions, etc.
//
// Normally the user looks at the ad, dismisses it, and control returns to your
// application by calling adViewDidDismissScreen:. However if the user hits the
// Home button or clicks on an App Store link your application will end. On iOS
// 4.0+ the next method called will be applicationWillResignActive: of your
// UIViewController (UIApplicationWillResignActiveNotification). Immediately
// after that adViewWillLeaveApplication: is called.
- (void)adViewWillPresentScreen:(GADBannerView *)bannerView {
[self pauseGame];
}
// Sent just before dismissing a full screen view.
//- (void)adViewWillDismissScreen:(GADBannerView *)adView;
// Sent just after dismissing a full screen view. Use this opportunity to
// restart anything you may have stopped as part of adViewWillPresentScreen:.
- (void)adViewDidDismissScreen:(GADBannerView *)adView {
[self resumeGame];
}
@end