jens@10: // jens@10: // Player.m jens@10: // YourMove jens@10: // jens@10: // Created by Jens Alfke on 7/3/08. jens@10: // Copyright 2008 Jens Alfke. All rights reserved. jens@10: // jens@10: jens@10: #import "Player.h" jens@10: #import "Game.h" jens@16: #import "GGBUtils.h" jens@10: jens@10: jens@10: #pragma mark - jens@10: @implementation Player jens@10: jens@10: jens@10: - (id) initWithGame: (Game*)game jens@10: { jens@10: self = [super init]; jens@10: if (self != nil) { jens@10: _game = game; jens@10: _local = YES; jens@10: } jens@10: return self; jens@10: } jens@10: jens@10: - (id) initWithName: (NSString*)name jens@10: { jens@10: self = [super init]; jens@10: if (self != nil) { jens@10: self.name = name; jens@10: } jens@10: return self; jens@10: } jens@10: jens@10: jens@10: - (id) initWithCoder: (NSCoder*)decoder jens@10: { jens@10: self = [self init]; jens@10: if( self ) { jens@10: _game = [decoder decodeObjectForKey: @"game"]; jens@10: _name = [[decoder decodeObjectForKey: @"name"] copy]; jens@10: _local= [decoder decodeBoolForKey: @"local"]; jens@13: _extraValues = [[decoder decodeObjectForKey: @"extraValues"] mutableCopy]; jens@10: } jens@10: return self; jens@10: } jens@10: jens@10: - (void) encodeWithCoder: (NSCoder*)coder jens@10: { jens@10: [coder encodeObject: _game forKey: @"game"]; jens@10: [coder encodeObject: _name forKey: @"name"]; jens@10: [coder encodeBool: _local forKey: @"local"]; jens@13: [coder encodeObject: _extraValues forKey: @"extraValues"]; jens@10: } jens@10: jens@10: - (void) dealloc jens@10: { jens@10: [_name release]; jens@13: [_extraValues release]; jens@10: [super dealloc]; jens@10: } jens@10: jens@10: jens@13: - (id)valueForUndefinedKey:(NSString *)key jens@13: { jens@13: return [_extraValues objectForKey: key]; jens@13: } jens@13: jens@13: - (void)setValue:(id)value forUndefinedKey:(NSString *)key jens@13: { jens@13: if( ! _extraValues ) jens@13: _extraValues = [[NSMutableDictionary alloc] init]; jens@13: if( value ) jens@13: [_extraValues setObject: value forKey: key]; jens@13: else jens@13: [_extraValues removeObjectForKey: key]; jens@13: } jens@13: jens@13: jens@14: - (void) copyFrom: (Player*)player jens@14: { jens@14: self.local = player.local; jens@14: self.name = player.name; jens@14: setObj(&_extraValues, [[player->_extraValues mutableCopy] autorelease]); jens@14: } jens@14: jens@14: jens@13: @synthesize game=_game, name=_name, local=_local; jens@10: jens@10: - (BOOL) isCurrent {return self == _game.currentPlayer;} jens@10: - (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners jens@10: - (BOOL) isUnfriendly {return ! self.friendly;} jens@10: jens@10: + (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];} jens@10: jens@10: jens@10: - (int) index jens@10: { jens@10: return [_game.players indexOfObjectIdenticalTo: self]; jens@10: } jens@10: jens@10: - (Player*) nextPlayer jens@10: { jens@10: return [_game.players objectAtIndex: (self.index+1) % _game.players.count]; jens@10: } jens@10: jens@10: - (Player*) previousPlayer jens@10: { jens@10: return [_game.players objectAtIndex: (self.index-1) % _game.players.count]; jens@10: } jens@10: jens@10: - (CGImageRef) icon jens@10: { jens@10: return [_game iconForPlayer: self.index]; jens@10: } jens@10: jens@10: - (NSString*) description jens@10: { jens@10: return [NSString stringWithFormat: @"%@[%@]", self.class,self.name]; jens@10: } jens@10: jens@10: @end