Source/Player.m
author Jens Alfke <jens@mooseyard.com>
Mon Jul 07 15:47:42 2008 -0700 (2008-07-07)
changeset 12 4e567e11f45f
child 13 db7bb080c3d5
permissions -rw-r--r--
Added new convenience methods for Game implementations.
     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         _uuid = [[decoder decodeObjectForKey: @"UUID"] copy];
    44         _address = [[decoder decodeObjectForKey: @"address"] copy];
    45         _addressType = [[decoder decodeObjectForKey: @"addressType"] copy];
    46         _local=  [decoder decodeBoolForKey:   @"local"];
    47     }
    48     return self;
    49 }
    50 
    51 - (void) encodeWithCoder: (NSCoder*)coder
    52 {
    53     [coder encodeObject: _game  forKey: @"game"];
    54     [coder encodeObject: _name  forKey: @"name"];
    55     [coder encodeObject: _uuid  forKey: @"UUID"];
    56     [coder encodeObject: _address forKey: @"address"];
    57     [coder encodeObject: _addressType forKey: @"addressType"];
    58     [coder encodeBool:   _local forKey: @"local"];
    59 }
    60 
    61 - (void) dealloc
    62 {
    63     [_name release];
    64     [_uuid release];
    65     [_address release];
    66     [_addressType release];
    67     [super dealloc];
    68 }
    69 
    70 
    71 @synthesize game=_game, name=_name, UUID=_uuid, address=_address, addressType=_addressType, local=_local;
    72 
    73 - (BOOL) isCurrent      {return self == _game.currentPlayer;}
    74 - (BOOL) isFriendly     {return self == _game.currentPlayer;}   // could be overridden for games with partners
    75 - (BOOL) isUnfriendly   {return ! self.friendly;}
    76 
    77 + (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];}
    78 
    79 
    80 - (int) index
    81 {
    82     return [_game.players indexOfObjectIdenticalTo: self];
    83 }
    84 
    85 - (Player*) nextPlayer
    86 {
    87     return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
    88 }
    89 
    90 - (Player*) previousPlayer
    91 {
    92     return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
    93 }
    94 
    95 - (CGImageRef) icon
    96 {
    97     return [_game iconForPlayer: self.index];
    98 }
    99 
   100 - (NSString*) description
   101 {
   102     return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
   103 }
   104 
   105 @end