-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewController.m
364 lines (296 loc) · 10.7 KB
/
MainViewController.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
357
358
359
360
361
362
363
364
#import "MainViewController.h"
#import "UIButton+SnapAdditions.h"
#import "Game.h"
#import "ViewController.h"
@interface MainViewController ()
@property (nonatomic, weak) IBOutlet UIImageView *sImageView;
@property (nonatomic, weak) IBOutlet UIImageView *nImageView;
@property (nonatomic, weak) IBOutlet UIImageView *aImageView;
@property (nonatomic, weak) IBOutlet UIImageView *pImageView;
@property (nonatomic, weak) IBOutlet UIImageView *jokerImageView;
@property (nonatomic, weak) IBOutlet UIButton *hostGameButton;
@property (nonatomic, weak) IBOutlet UIButton *joinGameButton;
@property (nonatomic, weak) IBOutlet UIButton *singlePlayerGameButton;
@end
@implementation MainViewController
{
BOOL _buttonsEnabled;
BOOL _performAnimations;
}
/*@synthesize sImageView = _sImageView;
@synthesize nImageView = _nImageView;
@synthesize aImageView = _aImageView;
@synthesize pImageView = _pImageView;
@synthesize jokerImageView = _jokerImageView;*/
@synthesize hostGameButton = _hostGameButton;
@synthesize joinGameButton = _joinGameButton;
@synthesize singlePlayerGameButton = _singlePlayerGameButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
{
_performAnimations = YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.hostGameButton rw_applySnapStyle];
[self.joinGameButton rw_applySnapStyle];
[self.singlePlayerGameButton rw_applySnapStyle];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (_performAnimations)
[self prepareForIntroAnimation];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (_performAnimations)
[self performIntroAnimation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
- (IBAction)hostGameAction:(id)sender
{
if (_buttonsEnabled)
{
[self performExitAnimationWithCompletionBlock:^(BOOL finished)
{
HostViewController *controller = [[HostViewController alloc] initWithNibName:@"HostViewController" bundle:nil];
controller.delegate = self;
[self presentViewController:controller animated:NO completion:nil];
}];
}
}
- (IBAction)joinGameAction:(id)sender
{
if (_buttonsEnabled)
{
[self performExitAnimationWithCompletionBlock:^(BOOL finished)
{
JoinViewController *controller = [[JoinViewController alloc] initWithNibName:@"JoinViewController" bundle:nil];
controller.delegate = self;
[self presentViewController:controller animated:NO completion:nil];
}];
}
}
- (IBAction)singlePlayerGameAction:(id)sender
{
}
- (void)prepareForIntroAnimation
{
self.sImageView.hidden = YES;
self.nImageView.hidden = YES;
self.aImageView.hidden = YES;
self.pImageView.hidden = YES;
self.jokerImageView.hidden = YES;
self.hostGameButton.alpha = 0.0f;
self.joinGameButton.alpha = 0.0f;
self.singlePlayerGameButton.alpha = 0.0f;
_buttonsEnabled = NO;
}
- (void)performIntroAnimation
{
self.sImageView.hidden = NO;
self.nImageView.hidden = NO;
self.aImageView.hidden = NO;
self.pImageView.hidden = NO;
self.jokerImageView.hidden = NO;
CGPoint point = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height * 2.0f);
self.sImageView.center = point;
self.nImageView.center = point;
self.aImageView.center = point;
self.pImageView.center = point;
self.jokerImageView.center = point;
[UIView animateWithDuration:0.65f
delay:0.5f
options:UIViewAnimationOptionCurveEaseOut
animations:^
{
self.sImageView.center = CGPointMake(80.0f, 108.0f);
self.sImageView.transform = CGAffineTransformMakeRotation(-0.22f);
self.nImageView.center = CGPointMake(160.0f, 93.0f);
self.nImageView.transform = CGAffineTransformMakeRotation(-0.1f);
self.aImageView.center = CGPointMake(240.0f, 88.0f);
self.pImageView.center = CGPointMake(320.0f, 93.0f);
self.pImageView.transform = CGAffineTransformMakeRotation(0.1f);
self.jokerImageView.center = CGPointMake(400.0f, 108.0f);
self.jokerImageView.transform = CGAffineTransformMakeRotation(0.22f);
}
completion:nil];
[UIView animateWithDuration:0.5f
delay:1.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^
{
self.hostGameButton.alpha = 1.0f;
self.joinGameButton.alpha = 1.0f;
self.singlePlayerGameButton.alpha = 1.0f;
}
completion:^(BOOL finished)
{
_buttonsEnabled = YES;
}];
}
- (void)performExitAnimationWithCompletionBlock:(void (^)(BOOL))block
{
_buttonsEnabled = NO;
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^
{
self.sImageView.center = self.aImageView.center;
self.sImageView.transform = self.aImageView.transform;
self.nImageView.center = self.aImageView.center;
self.nImageView.transform = self.aImageView.transform;
self.pImageView.center = self.aImageView.center;
self.pImageView.transform = self.aImageView.transform;
self.jokerImageView.center = self.aImageView.center;
self.jokerImageView.transform = self.aImageView.transform;
}
completion:^(BOOL finished)
{
CGPoint point = CGPointMake(self.aImageView.center.x, self.view.frame.size.height * -2.0f);
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^
{
self.sImageView.center = point;
self.nImageView.center = point;
self.aImageView.center = point;
self.pImageView.center = point;
self.jokerImageView.center = point;
}
completion:block];
[UIView animateWithDuration:0.3f
delay:0.3f
options:UIViewAnimationOptionCurveEaseOut
animations:^
{
self.hostGameButton.alpha = 0.0f;
self.joinGameButton.alpha = 0.0f;
self.singlePlayerGameButton.alpha = 0.0f;
}
completion:nil];
}];
}
#pragma mark - HostViewControllerDelegate
- (void)hostViewControllerDidCancel:(HostViewController *)controller
{
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)hostViewController:(HostViewController *)controller didEndSessionWithReason:(QuitReason)reason
{
if (reason == QuitReasonNoNetwork)
{
[self showNoNetworkAlert];
}
}
- (void)hostViewController:(HostViewController *)controller startGameWithSession:(GKSession *)session playerName:(NSString *)name clients:(NSArray *)clients isHost:(BOOL)isHost
{
_performAnimations = NO;
__isHost = isHost;
[self dismissViewControllerAnimated:NO completion:^
{
_performAnimations = YES;
[self startGameWithBlock:^(Game *game)
{
[game startServerGameWithSession:session playerName:name clients:clients isHost:isHost];
}];
}];
}
#pragma mark - JoinViewControllerDelegate
- (void)joinViewControllerDidCancel:(JoinViewController *)controller
{
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)joinViewController:(JoinViewController *)controller didDisconnectWithReason:(QuitReason)reason
{
if (reason == QuitReasonNoNetwork)
{
[self showNoNetworkAlert];
}
else if (reason == QuitReasonConnectionDropped)
{
[self dismissViewControllerAnimated:NO completion:^
{
[self showDisconnectedAlert];
}];
}
}
- (void)joinViewController:(JoinViewController *)controller startGameWithSession:(GKSession *)session playerName:(NSString *)name server:(NSString *)peerID isHost:(bool) isHost
{
_performAnimations = NO;
__isHost = isHost;
[self dismissViewControllerAnimated:NO completion:^
{
_performAnimations = YES;
[self startGameWithBlock:^(Game *game)
{
[game startClientGameWithSession:session playerName:name server:peerID isHost:isHost];
}];
}];
}
#pragma mark - GameViewControllerDelegate
- (void)gameViewController:(GameViewController *)controller didQuitWithReason:(QuitReason)reason
{
[self dismissViewControllerAnimated:NO completion:^
{
if (reason == QuitReasonConnectionDropped)
{
[self showDisconnectedAlert];
}
}];
}
#pragma mark - Alerts
- (void)showNoNetworkAlert
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"No Network", @"No network alert title")
message:NSLocalizedString(@"To use multiplayer, please enable Bluetooth or Wi-Fi in your device's Settings.", @"No network alert message")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"Button: OK")
otherButtonTitles:nil];
[alertView show];
}
- (void)showDisconnectedAlert
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Disconnected", @"Client disconnected alert title")
message:NSLocalizedString(@"You were disconnected from the game.", @"Client disconnected alert message")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"Button: OK")
otherButtonTitles:nil];
[alertView show];
}
#pragma mark - Misc
- (void)startGameWithBlock:(void (^)(Game *))block
{
GameViewController *gameViewController = [[GameViewController alloc] initWithHost:__isHost];
gameViewController.delegate = self;
SKView * view = [[SKView alloc] init];
gameViewController.view = view;
[self presentViewController:gameViewController animated:NO completion:^
{
Game *game = [[Game alloc] init];
gameViewController.game = game;
game.delegate = gameViewController;
block(game);
}];
}
#pragma mark - Delloc
- (void)dealloc
{
#ifdef DEBUG
NSLog(@"dealloc %@", self);
#endif
}
@end