author | Jens Alfke <jens@mooseyard.com> |
Sat Jul 05 17:46:43 2008 -0700 (2008-07-05) | |
changeset 11 | 436cbdf56810 |
child 13 | db7bb080c3d5 |
permissions | -rw-r--r-- |
jens@10 | 1 |
// |
jens@10 | 2 |
// Player.m |
jens@10 | 3 |
// YourMove |
jens@10 | 4 |
// |
jens@10 | 5 |
// Created by Jens Alfke on 7/3/08. |
jens@10 | 6 |
// Copyright 2008 Jens Alfke. All rights reserved. |
jens@10 | 7 |
// |
jens@10 | 8 |
|
jens@10 | 9 |
#import "Player.h" |
jens@10 | 10 |
#import "Game.h" |
jens@10 | 11 |
|
jens@10 | 12 |
|
jens@10 | 13 |
#pragma mark - |
jens@10 | 14 |
@implementation Player |
jens@10 | 15 |
|
jens@10 | 16 |
|
jens@10 | 17 |
- (id) initWithGame: (Game*)game |
jens@10 | 18 |
{ |
jens@10 | 19 |
self = [super init]; |
jens@10 | 20 |
if (self != nil) { |
jens@10 | 21 |
_game = game; |
jens@10 | 22 |
_local = YES; |
jens@10 | 23 |
} |
jens@10 | 24 |
return self; |
jens@10 | 25 |
} |
jens@10 | 26 |
|
jens@10 | 27 |
- (id) initWithName: (NSString*)name |
jens@10 | 28 |
{ |
jens@10 | 29 |
self = [super init]; |
jens@10 | 30 |
if (self != nil) { |
jens@10 | 31 |
self.name = name; |
jens@10 | 32 |
} |
jens@10 | 33 |
return self; |
jens@10 | 34 |
} |
jens@10 | 35 |
|
jens@10 | 36 |
|
jens@10 | 37 |
- (id) initWithCoder: (NSCoder*)decoder |
jens@10 | 38 |
{ |
jens@10 | 39 |
self = [self init]; |
jens@10 | 40 |
if( self ) { |
jens@10 | 41 |
_game = [decoder decodeObjectForKey: @"game"]; |
jens@10 | 42 |
_name = [[decoder decodeObjectForKey: @"name"] copy]; |
jens@10 | 43 |
_uuid = [[decoder decodeObjectForKey: @"UUID"] copy]; |
jens@10 | 44 |
_address = [[decoder decodeObjectForKey: @"address"] copy]; |
jens@10 | 45 |
_addressType = [[decoder decodeObjectForKey: @"addressType"] copy]; |
jens@10 | 46 |
_local= [decoder decodeBoolForKey: @"local"]; |
jens@10 | 47 |
} |
jens@10 | 48 |
return self; |
jens@10 | 49 |
} |
jens@10 | 50 |
|
jens@10 | 51 |
- (void) encodeWithCoder: (NSCoder*)coder |
jens@10 | 52 |
{ |
jens@10 | 53 |
[coder encodeObject: _game forKey: @"game"]; |
jens@10 | 54 |
[coder encodeObject: _name forKey: @"name"]; |
jens@10 | 55 |
[coder encodeObject: _uuid forKey: @"UUID"]; |
jens@10 | 56 |
[coder encodeObject: _address forKey: @"address"]; |
jens@10 | 57 |
[coder encodeObject: _addressType forKey: @"addressType"]; |
jens@10 | 58 |
[coder encodeBool: _local forKey: @"local"]; |
jens@10 | 59 |
} |
jens@10 | 60 |
|
jens@10 | 61 |
- (void) dealloc |
jens@10 | 62 |
{ |
jens@10 | 63 |
[_name release]; |
jens@10 | 64 |
[_uuid release]; |
jens@10 | 65 |
[_address release]; |
jens@10 | 66 |
[_addressType release]; |
jens@10 | 67 |
[super dealloc]; |
jens@10 | 68 |
} |
jens@10 | 69 |
|
jens@10 | 70 |
|
jens@10 | 71 |
@synthesize game=_game, name=_name, UUID=_uuid, address=_address, addressType=_addressType, local=_local; |
jens@10 | 72 |
|
jens@10 | 73 |
- (BOOL) isCurrent {return self == _game.currentPlayer;} |
jens@10 | 74 |
- (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners |
jens@10 | 75 |
- (BOOL) isUnfriendly {return ! self.friendly;} |
jens@10 | 76 |
|
jens@10 | 77 |
+ (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];} |
jens@10 | 78 |
|
jens@10 | 79 |
|
jens@10 | 80 |
- (int) index |
jens@10 | 81 |
{ |
jens@10 | 82 |
return [_game.players indexOfObjectIdenticalTo: self]; |
jens@10 | 83 |
} |
jens@10 | 84 |
|
jens@10 | 85 |
- (Player*) nextPlayer |
jens@10 | 86 |
{ |
jens@10 | 87 |
return [_game.players objectAtIndex: (self.index+1) % _game.players.count]; |
jens@10 | 88 |
} |
jens@10 | 89 |
|
jens@10 | 90 |
- (Player*) previousPlayer |
jens@10 | 91 |
{ |
jens@10 | 92 |
return [_game.players objectAtIndex: (self.index-1) % _game.players.count]; |
jens@10 | 93 |
} |
jens@10 | 94 |
|
jens@10 | 95 |
- (CGImageRef) icon |
jens@10 | 96 |
{ |
jens@10 | 97 |
return [_game iconForPlayer: self.index]; |
jens@10 | 98 |
} |
jens@10 | 99 |
|
jens@10 | 100 |
- (NSString*) description |
jens@10 | 101 |
{ |
jens@10 | 102 |
return [NSString stringWithFormat: @"%@[%@]", self.class,self.name]; |
jens@10 | 103 |
} |
jens@10 | 104 |
|
jens@10 | 105 |
@end |