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