author | Jens Alfke <jens@mooseyard.com> |
Thu Jul 31 20:01:26 2008 -0700 (2008-07-31) | |
changeset 24 | db8640a38faf |
parent 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@16 | 11 |
#import "GGBUtils.h" |
jens@10 | 12 |
|
jens@10 | 13 |
|
jens@10 | 14 |
#pragma mark - |
jens@10 | 15 |
@implementation Player |
jens@10 | 16 |
|
jens@10 | 17 |
|
jens@10 | 18 |
- (id) initWithGame: (Game*)game |
jens@10 | 19 |
{ |
jens@10 | 20 |
self = [super init]; |
jens@10 | 21 |
if (self != nil) { |
jens@10 | 22 |
_game = game; |
jens@10 | 23 |
_local = YES; |
jens@10 | 24 |
} |
jens@10 | 25 |
return self; |
jens@10 | 26 |
} |
jens@10 | 27 |
|
jens@10 | 28 |
- (id) initWithName: (NSString*)name |
jens@10 | 29 |
{ |
jens@10 | 30 |
self = [super init]; |
jens@10 | 31 |
if (self != nil) { |
jens@10 | 32 |
self.name = name; |
jens@10 | 33 |
} |
jens@10 | 34 |
return self; |
jens@10 | 35 |
} |
jens@10 | 36 |
|
jens@10 | 37 |
|
jens@10 | 38 |
- (id) initWithCoder: (NSCoder*)decoder |
jens@10 | 39 |
{ |
jens@10 | 40 |
self = [self init]; |
jens@10 | 41 |
if( self ) { |
jens@10 | 42 |
_game = [decoder decodeObjectForKey: @"game"]; |
jens@10 | 43 |
_name = [[decoder decodeObjectForKey: @"name"] copy]; |
jens@10 | 44 |
_local= [decoder decodeBoolForKey: @"local"]; |
jens@13 | 45 |
_extraValues = [[decoder decodeObjectForKey: @"extraValues"] mutableCopy]; |
jens@10 | 46 |
} |
jens@10 | 47 |
return self; |
jens@10 | 48 |
} |
jens@10 | 49 |
|
jens@10 | 50 |
- (void) encodeWithCoder: (NSCoder*)coder |
jens@10 | 51 |
{ |
jens@10 | 52 |
[coder encodeObject: _game forKey: @"game"]; |
jens@10 | 53 |
[coder encodeObject: _name forKey: @"name"]; |
jens@10 | 54 |
[coder encodeBool: _local forKey: @"local"]; |
jens@13 | 55 |
[coder encodeObject: _extraValues forKey: @"extraValues"]; |
jens@10 | 56 |
} |
jens@10 | 57 |
|
jens@10 | 58 |
- (void) dealloc |
jens@10 | 59 |
{ |
jens@10 | 60 |
[_name release]; |
jens@13 | 61 |
[_extraValues release]; |
jens@10 | 62 |
[super dealloc]; |
jens@10 | 63 |
} |
jens@10 | 64 |
|
jens@10 | 65 |
|
jens@13 | 66 |
- (id)valueForUndefinedKey:(NSString *)key |
jens@13 | 67 |
{ |
jens@13 | 68 |
return [_extraValues objectForKey: key]; |
jens@13 | 69 |
} |
jens@13 | 70 |
|
jens@13 | 71 |
- (void)setValue:(id)value forUndefinedKey:(NSString *)key |
jens@13 | 72 |
{ |
jens@13 | 73 |
if( ! _extraValues ) |
jens@13 | 74 |
_extraValues = [[NSMutableDictionary alloc] init]; |
jens@13 | 75 |
if( value ) |
jens@13 | 76 |
[_extraValues setObject: value forKey: key]; |
jens@13 | 77 |
else |
jens@13 | 78 |
[_extraValues removeObjectForKey: key]; |
jens@13 | 79 |
} |
jens@13 | 80 |
|
jens@13 | 81 |
|
jens@14 | 82 |
- (void) copyFrom: (Player*)player |
jens@14 | 83 |
{ |
jens@14 | 84 |
self.local = player.local; |
jens@14 | 85 |
self.name = player.name; |
jens@14 | 86 |
setObj(&_extraValues, [[player->_extraValues mutableCopy] autorelease]); |
jens@14 | 87 |
} |
jens@14 | 88 |
|
jens@14 | 89 |
|
jens@13 | 90 |
@synthesize game=_game, name=_name, local=_local; |
jens@10 | 91 |
|
jens@10 | 92 |
- (BOOL) isCurrent {return self == _game.currentPlayer;} |
jens@10 | 93 |
- (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners |
jens@10 | 94 |
- (BOOL) isUnfriendly {return ! self.friendly;} |
jens@10 | 95 |
|
jens@10 | 96 |
+ (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];} |
jens@10 | 97 |
|
jens@10 | 98 |
|
jens@10 | 99 |
- (int) index |
jens@10 | 100 |
{ |
jens@10 | 101 |
return [_game.players indexOfObjectIdenticalTo: self]; |
jens@10 | 102 |
} |
jens@10 | 103 |
|
jens@10 | 104 |
- (Player*) nextPlayer |
jens@10 | 105 |
{ |
jens@10 | 106 |
return [_game.players objectAtIndex: (self.index+1) % _game.players.count]; |
jens@10 | 107 |
} |
jens@10 | 108 |
|
jens@10 | 109 |
- (Player*) previousPlayer |
jens@10 | 110 |
{ |
jens@10 | 111 |
return [_game.players objectAtIndex: (self.index-1) % _game.players.count]; |
jens@10 | 112 |
} |
jens@10 | 113 |
|
jens@10 | 114 |
- (CGImageRef) icon |
jens@10 | 115 |
{ |
jens@10 | 116 |
return [_game iconForPlayer: self.index]; |
jens@10 | 117 |
} |
jens@10 | 118 |
|
jens@10 | 119 |
- (NSString*) description |
jens@10 | 120 |
{ |
jens@10 | 121 |
return [NSString stringWithFormat: @"%@[%@]", self.class,self.name]; |
jens@10 | 122 |
} |
jens@10 | 123 |
|
jens@10 | 124 |
@end |