-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameViewController.m
502 lines (401 loc) · 15.2 KB
/
GameViewController.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//
// GameViewController.m
// Snap
//
// Created by Ray Wenderlich on 5/25/12.
// Copyright (c) 2012 Hollance. All rights reserved.
//
#import "GameViewController.h"
#import "UIFont+SnapAdditions.h"
#import "Game.h"
#import <SpriteKit/SpriteKit.h>
#import "MyScene.h"
@interface GameViewController ()
@property (nonatomic, weak) IBOutlet UILabel *centerLabel;
@property (nonatomic, weak) IBOutlet UIImageView *backgroundImageView;
@property (nonatomic, weak) IBOutlet UIView *cardContainerView;
@property (nonatomic, weak) IBOutlet UIButton *turnOverButton;
@property (nonatomic, weak) IBOutlet UIButton *snapButton;
@property (nonatomic, weak) IBOutlet UIButton *nextRoundButton;
@property (nonatomic, weak) IBOutlet UIImageView *wrongSnapImageView;
@property (nonatomic, weak) IBOutlet UIImageView *correctSnapImageView;
@property (nonatomic, weak) IBOutlet UILabel *playerNameBottomLabel;
@property (nonatomic, weak) IBOutlet UILabel *playerNameLeftLabel;
@property (nonatomic, weak) IBOutlet UILabel *playerNameTopLabel;
@property (nonatomic, weak) IBOutlet UILabel *playerNameRightLabel;
@property (nonatomic, weak) IBOutlet UILabel *playerWinsBottomLabel;
@property (nonatomic, weak) IBOutlet UILabel *playerWinsLeftLabel;
@property (nonatomic, weak) IBOutlet UILabel *playerWinsTopLabel;
@property (nonatomic, weak) IBOutlet UILabel *playerWinsRightLabel;
@property (nonatomic, weak) IBOutlet UIImageView *playerActiveBottomImageView;
@property (nonatomic, weak) IBOutlet UIImageView *playerActiveLeftImageView;
@property (nonatomic, weak) IBOutlet UIImageView *playerActiveTopImageView;
@property (nonatomic, weak) IBOutlet UIImageView *playerActiveRightImageView;
@property (nonatomic, weak) IBOutlet UIImageView *snapIndicatorBottomImageView;
@property (nonatomic, weak) IBOutlet UIImageView *snapIndicatorLeftImageView;
@property (nonatomic, weak) IBOutlet UIImageView *snapIndicatorTopImageView;
@property (nonatomic, weak) IBOutlet UIImageView *snapIndicatorRightImageView;
@end
@implementation GameViewController {
UIAlertView *_alertView;
}
@synthesize delegate = _delegate;
@synthesize game = _game;
@synthesize centerLabel = _centerLabel;
@synthesize backgroundImageView = _backgroundImageView;
@synthesize cardContainerView = _cardContainerView;
@synthesize turnOverButton = _turnOverButton;
@synthesize snapButton = _snapButton;
@synthesize nextRoundButton = _nextRoundButton;
@synthesize wrongSnapImageView = _wrongSnapImageView;
@synthesize correctSnapImageView = _correctSnapImageView;
@synthesize playerNameBottomLabel = _playerNameBottomLabel;
@synthesize playerNameLeftLabel = _playerNameLeftLabel;
@synthesize playerNameTopLabel = _playerNameTopLabel;
@synthesize playerNameRightLabel = _playerNameRightLabel;
@synthesize playerWinsBottomLabel = _playerWinsBottomLabel;
@synthesize playerWinsLeftLabel = _playerWinsLeftLabel;
@synthesize playerWinsTopLabel = _playerWinsTopLabel;
@synthesize playerWinsRightLabel = _playerWinsRightLabel;
@synthesize playerActiveBottomImageView = _playerActiveBottomImageView;
@synthesize playerActiveLeftImageView = _playerActiveLeftImageView;
@synthesize playerActiveTopImageView = _playerActiveTopImageView;
@synthesize playerActiveRightImageView = _playerActiveRightImageView;
@synthesize snapIndicatorBottomImageView = _snapIndicatorBottomImageView;
@synthesize snapIndicatorLeftImageView = _snapIndicatorLeftImageView;
@synthesize snapIndicatorTopImageView = _snapIndicatorTopImageView;
@synthesize snapIndicatorRightImageView = _snapIndicatorRightImageView;
- (void)dealloc
{
#ifdef DEBUG
NSLog(@"dealloc %@", self);
#endif
}
- (void)viewDidLoad
{
[super viewDidLoad];
// self.centerLabel.font = [UIFont rw_snapFontWithSize:18.0f];
//
// self.snapButton.hidden = YES;
// self.nextRoundButton.hidden = YES;
// self.wrongSnapImageView.hidden = YES;
// self.correctSnapImageView.hidden = YES;
//
// [self hidePlayerLabels];
// [self hideActivePlayerIndicator];
// [self hideSnapIndicators];
}
- (id)initWithHost:(BOOL)isHost
{
self = [super init];
if (!self) { return nil; }
self.isHost = isHost;
return self;
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
// skView.showsFPS = YES;
// skView.showsNodeCount = YES;
// Create and configure the scene.
ITScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
scene.vc = self;
scene.isHost = self.isHost;
// Present the scene.
[skView presentScene:scene];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
}
#pragma mark - Game UI
- (void)hidePlayerLabels
{
self.playerNameBottomLabel.hidden = YES;
self.playerWinsBottomLabel.hidden = YES;
self.playerNameLeftLabel.hidden = YES;
self.playerWinsLeftLabel.hidden = YES;
self.playerNameTopLabel.hidden = YES;
self.playerWinsTopLabel.hidden = YES;
self.playerNameRightLabel.hidden = YES;
self.playerWinsRightLabel.hidden = YES;
}
- (void)hideActivePlayerIndicator
{
self.playerActiveBottomImageView.hidden = YES;
self.playerActiveLeftImageView.hidden = YES;
self.playerActiveTopImageView.hidden = YES;
self.playerActiveRightImageView.hidden = YES;
}
- (void)hideSnapIndicators
{
self.snapIndicatorBottomImageView.hidden = YES;
self.snapIndicatorLeftImageView.hidden = YES;
self.snapIndicatorTopImageView.hidden = YES;
self.snapIndicatorRightImageView.hidden = YES;
}
- (void)hidePlayerLabelsForPlayer:(Player *)player
{
switch (player.position)
{
case PlayerPositionBottom:
self.playerNameBottomLabel.hidden = YES;
self.playerWinsBottomLabel.hidden = YES;
break;
case PlayerPositionLeft:
self.playerNameLeftLabel.hidden = YES;
self.playerWinsLeftLabel.hidden = YES;
break;
case PlayerPositionTop:
self.playerNameTopLabel.hidden = YES;
self.playerWinsTopLabel.hidden = YES;
break;
case PlayerPositionRight:
self.playerNameRightLabel.hidden = YES;
self.playerWinsRightLabel.hidden = YES;
break;
}
}
- (void)hideActiveIndicatorForPlayer:(Player *)player
{
switch (player.position)
{
case PlayerPositionBottom: self.playerActiveBottomImageView.hidden = YES; break;
case PlayerPositionLeft: self.playerActiveLeftImageView.hidden = YES; break;
case PlayerPositionTop: self.playerActiveTopImageView.hidden = YES; break;
case PlayerPositionRight: self.playerActiveRightImageView.hidden = YES; break;
}
}
- (void)hideSnapIndicatorForPlayer:(Player *)player
{
switch (player.position)
{
case PlayerPositionBottom: self.snapIndicatorBottomImageView.hidden = YES; break;
case PlayerPositionLeft: self.snapIndicatorLeftImageView.hidden = YES; break;
case PlayerPositionTop: self.snapIndicatorTopImageView.hidden = YES; break;
case PlayerPositionRight: self.snapIndicatorRightImageView.hidden = YES; break;
}
}
#pragma mark - Actions
- (IBAction)exitAction:(id)sender
{
if (self.game.isServer)
{
_alertView = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"End Game?", @"Alert title (user is host)")
message:NSLocalizedString(@"This will terminate the game for all other players.", @"Alert message (user is host)")
delegate:self
cancelButtonTitle:NSLocalizedString(@"No", @"Button: No")
otherButtonTitles:NSLocalizedString(@"Yes", @"Button: Yes"),
nil];
[_alertView show];
}
else
{
_alertView = [[UIAlertView alloc]
initWithTitle: NSLocalizedString(@"Leave Game?", @"Alert title (user is not host)")
message:nil
delegate:self
cancelButtonTitle:NSLocalizedString(@"No", @"Button: No")
otherButtonTitles:NSLocalizedString(@"Yes", @"Button: Yes"),
nil];
[_alertView show];
}
}
#pragma mark - GameDelegate
- (void)game:(Game *)game didQuitWithReason:(QuitReason)reason
{
[self.delegate gameViewController:self didQuitWithReason:reason];
}
- (void)gameWaitingForServerReady:(Game *)game
{
self.centerLabel.text = NSLocalizedString(@"Waiting for game to start...", @"Status text: waiting for server");
}
- (void)gameWaitingForClientsReady:(Game *)game
{
self.centerLabel.text = NSLocalizedString(@"Waiting for other players...", @"Status text: waiting for clients");
}
- (void)game:(Game *)game playerDidDisconnect:(Player *)disconnectedPlayer
{
[self hidePlayerLabelsForPlayer:disconnectedPlayer];
[self hideActiveIndicatorForPlayer:disconnectedPlayer];
[self hideSnapIndicatorForPlayer:disconnectedPlayer];
}
#pragma mark - Action methods
- (IBAction)turnOverPressed:(id)sender
{
}
- (IBAction)turnOverEnter:(id)sender
{
}
- (IBAction)turnOverExit:(id)sender
{
}
- (IBAction)turnOverAction:(id)sender
{
}
- (IBAction)snapAction:(id)sender
{
}
- (IBAction)nextRoundAction:(id)sender
{
}
- (void)gameDidBegin:(Game *)game
{
// [self showPlayerLabels];
// [self calculateLabelFrames];
// [self updateWinsLabels];
}
- (void)showPlayerLabels
{
Player *player = [self.game playerAtPosition:PlayerPositionBottom];
if (player != nil)
{
self.playerNameBottomLabel.hidden = NO;
self.playerWinsBottomLabel.hidden = NO;
}
player = [self.game playerAtPosition:PlayerPositionLeft];
if (player != nil)
{
self.playerNameLeftLabel.hidden = NO;
self.playerWinsLeftLabel.hidden = NO;
}
player = [self.game playerAtPosition:PlayerPositionTop];
if (player != nil)
{
self.playerNameTopLabel.hidden = NO;
self.playerWinsTopLabel.hidden = NO;
}
player = [self.game playerAtPosition:PlayerPositionRight];
if (player != nil)
{
self.playerNameRightLabel.hidden = NO;
self.playerWinsRightLabel.hidden = NO;
}
}
- (void)updateWinsLabels
{
NSString *format = NSLocalizedString(@"%d Won", @"Number of games won");
Player *player = [self.game playerAtPosition:PlayerPositionBottom];
if (player != nil)
self.playerWinsBottomLabel.text = [NSString stringWithFormat:format, player.gamesWon];
player = [self.game playerAtPosition:PlayerPositionLeft];
if (player != nil)
self.playerWinsLeftLabel.text = [NSString stringWithFormat:format, player.gamesWon];
player = [self.game playerAtPosition:PlayerPositionTop];
if (player != nil)
self.playerWinsTopLabel.text = [NSString stringWithFormat:format, player.gamesWon];
player = [self.game playerAtPosition:PlayerPositionRight];
if (player != nil)
self.playerWinsRightLabel.text = [NSString stringWithFormat:format, player.gamesWon];
}
- (void)resizeLabelToFit:(UILabel *)label
{
[label sizeToFit];
CGRect rect = label.frame;
rect.size.width = ceilf(rect.size.width/2.0f) * 2.0f; // make even
rect.size.height = ceilf(rect.size.height/2.0f) * 2.0f; // make even
label.frame = rect;
}
- (void)calculateLabelFrames
{
UIFont *font = [UIFont rw_snapFontWithSize:14.0f];
self.playerNameBottomLabel.font = font;
self.playerNameLeftLabel.font = font;
self.playerNameTopLabel.font = font;
self.playerNameRightLabel.font = font;
font = [UIFont rw_snapFontWithSize:11.0f];
self.playerWinsBottomLabel.font = font;
self.playerWinsLeftLabel.font = font;
self.playerWinsTopLabel.font = font;
self.playerWinsRightLabel.font = font;
self.playerWinsBottomLabel.layer.cornerRadius = 4.0f;
self.playerWinsLeftLabel.layer.cornerRadius = 4.0f;
self.playerWinsTopLabel.layer.cornerRadius = 4.0f;
self.playerWinsRightLabel.layer.cornerRadius = 4.0f;
UIImage *image = [[UIImage imageNamed:@"ActivePlayer"] stretchableImageWithLeftCapWidth:20 topCapHeight:0];
self.playerActiveBottomImageView.image = image;
self.playerActiveLeftImageView.image = image;
self.playerActiveTopImageView.image = image;
self.playerActiveRightImageView.image = image;
CGFloat viewWidth = self.view.bounds.size.width;
CGFloat centerX = viewWidth / 2.0f;
Player *player = [self.game playerAtPosition:PlayerPositionBottom];
if (player != nil)
{
self.playerNameBottomLabel.text = player.name;
[self resizeLabelToFit:self.playerNameBottomLabel];
CGFloat labelWidth = self.playerNameBottomLabel.bounds.size.width;
CGPoint point = CGPointMake(centerX - 19.0f - 3.0f, 306.0f);
self.playerNameBottomLabel.center = point;
CGPoint winsPoint = point;
winsPoint.x += labelWidth/2.0f + 6.0f + 19.0f;
winsPoint.y -= 0.5f;
self.playerWinsBottomLabel.center = winsPoint;
self.playerActiveBottomImageView.frame = CGRectMake(0, 0, 20.0f + labelWidth + 6.0f + 38.0f + 2.0f, 20.0f);
point.x = centerX - 9.0f;
self.playerActiveBottomImageView.center = point;
}
player = [self.game playerAtPosition:PlayerPositionLeft];
if (player != nil)
{
self.playerNameLeftLabel.text = player.name;
[self resizeLabelToFit:self.playerNameLeftLabel];
CGFloat labelWidth = self.playerNameLeftLabel.bounds.size.width;
CGPoint point = CGPointMake(2.0 + 20.0f + labelWidth/2.0f, 48.0f);
self.playerNameLeftLabel.center = point;
CGPoint winsPoint = point;
winsPoint.x += labelWidth/2.0f + 6.0f + 19.0f;
winsPoint.y -= 0.5f;
self.playerWinsLeftLabel.center = winsPoint;
self.playerActiveLeftImageView.frame = CGRectMake(2.0f, 38.0f, 20.0f + labelWidth + 6.0f + 38.0f + 2.0f, 20.0f);
}
player = [self.game playerAtPosition:PlayerPositionTop];
if (player != nil)
{
self.playerNameTopLabel.text = player.name;
[self resizeLabelToFit:self.playerNameTopLabel];
CGFloat labelWidth = self.playerNameTopLabel.bounds.size.width;
CGPoint point = CGPointMake(centerX - 19.0f - 3.0f, 15.0f);
self.playerNameTopLabel.center = point;
CGPoint winsPoint = point;
winsPoint.x += labelWidth/2.0f + 6.0f + 19.0f;
winsPoint.y -= 0.5f;
self.playerWinsTopLabel.center = winsPoint;
self.playerActiveTopImageView.frame = CGRectMake(0, 0, 20.0f + labelWidth + 6.0f + 38.0f + 2.0f, 20.0f);
point.x = centerX - 9.0f;
self.playerActiveTopImageView.center = point;
}
player = [self.game playerAtPosition:PlayerPositionRight];
if (player != nil)
{
self.playerNameRightLabel.text = player.name;
[self resizeLabelToFit:self.playerNameRightLabel];
CGFloat labelWidth = self.playerNameRightLabel.bounds.size.width;
CGPoint point = CGPointMake(viewWidth - labelWidth/2.0f - 2.0f - 6.0f - 38.0f - 12.0f, 48.0f);
self.playerNameRightLabel.center = point;
CGPoint winsPoint = point;
winsPoint.x += labelWidth/2.0f + 6.0f + 19.0f;
winsPoint.y -= 0.5f;
self.playerWinsRightLabel.center = winsPoint;
self.playerActiveRightImageView.frame = CGRectMake(self.playerNameRightLabel.frame.origin.x - 20.0f, 38.0f, 20.0f + labelWidth + 6.0f + 38.0f + 2.0f, 20.0f);
}
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
[self.game quitGameWithReason:QuitReasonUserQuit];
}
}
@end