author | Jens Alfke <jens@mooseyard.com> |
Tue Jul 08 13:12:01 2008 -0700 (2008-07-08) | |
changeset 13 | db7bb080c3d5 |
parent 10 | 6c78cc6bd7a6 |
child 14 | 4585c74d809c |
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 |
_local= [decoder decodeBoolForKey: @"local"]; |
jens@13 | 44 |
_extraValues = [[decoder decodeObjectForKey: @"extraValues"] mutableCopy]; |
jens@10 | 45 |
} |
jens@10 | 46 |
return self; |
jens@10 | 47 |
} |
jens@10 | 48 |
|
jens@10 | 49 |
- (void) encodeWithCoder: (NSCoder*)coder |
jens@10 | 50 |
{ |
jens@10 | 51 |
[coder encodeObject: _game forKey: @"game"]; |
jens@10 | 52 |
[coder encodeObject: _name forKey: @"name"]; |
jens@10 | 53 |
[coder encodeBool: _local forKey: @"local"]; |
jens@13 | 54 |
[coder encodeObject: _extraValues forKey: @"extraValues"]; |
jens@10 | 55 |
} |
jens@10 | 56 |
|
jens@10 | 57 |
- (void) dealloc |
jens@10 | 58 |
{ |
jens@10 | 59 |
[_name release]; |
jens@13 | 60 |
[_extraValues release]; |
jens@10 | 61 |
[super dealloc]; |
jens@10 | 62 |
} |
jens@10 | 63 |
|
jens@10 | 64 |
|
jens@13 | 65 |
- (id)valueForUndefinedKey:(NSString *)key |
jens@13 | 66 |
{ |
jens@13 | 67 |
return [_extraValues objectForKey: key]; |
jens@13 | 68 |
} |
jens@13 | 69 |
|
jens@13 | 70 |
- (void)setValue:(id)value forUndefinedKey:(NSString *)key |
jens@13 | 71 |
{ |
jens@13 | 72 |
if( ! _extraValues ) |
jens@13 | 73 |
_extraValues = [[NSMutableDictionary alloc] init]; |
jens@13 | 74 |
if( value ) |
jens@13 | 75 |
[_extraValues setObject: value forKey: key]; |
jens@13 | 76 |
else |
jens@13 | 77 |
[_extraValues removeObjectForKey: key]; |
jens@13 | 78 |
} |
jens@13 | 79 |
|
jens@13 | 80 |
|
jens@13 | 81 |
@synthesize game=_game, name=_name, local=_local; |
jens@10 | 82 |
|
jens@10 | 83 |
- (BOOL) isCurrent {return self == _game.currentPlayer;} |
jens@10 | 84 |
- (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners |
jens@10 | 85 |
- (BOOL) isUnfriendly {return ! self.friendly;} |
jens@10 | 86 |
|
jens@10 | 87 |
+ (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];} |
jens@10 | 88 |
|
jens@10 | 89 |
|
jens@10 | 90 |
- (int) index |
jens@10 | 91 |
{ |
jens@10 | 92 |
return [_game.players indexOfObjectIdenticalTo: self]; |
jens@10 | 93 |
} |
jens@10 | 94 |
|
jens@10 | 95 |
- (Player*) nextPlayer |
jens@10 | 96 |
{ |
jens@10 | 97 |
return [_game.players objectAtIndex: (self.index+1) % _game.players.count]; |
jens@10 | 98 |
} |
jens@10 | 99 |
|
jens@10 | 100 |
- (Player*) previousPlayer |
jens@10 | 101 |
{ |
jens@10 | 102 |
return [_game.players objectAtIndex: (self.index-1) % _game.players.count]; |
jens@10 | 103 |
} |
jens@10 | 104 |
|
jens@10 | 105 |
- (CGImageRef) icon |
jens@10 | 106 |
{ |
jens@10 | 107 |
return [_game iconForPlayer: self.index]; |
jens@10 | 108 |
} |
jens@10 | 109 |
|
jens@10 | 110 |
- (NSString*) description |
jens@10 | 111 |
{ |
jens@10 | 112 |
return [NSString stringWithFormat: @"%@[%@]", self.class,self.name]; |
jens@10 | 113 |
} |
jens@10 | 114 |
|
jens@10 | 115 |
@end |