Source/Player.m
author Jens Alfke <jens@mooseyard.com>
Wed Jul 09 17:07:45 2008 -0700 (2008-07-09)
changeset 15 73f8c889f053
parent 13 db7bb080c3d5
child 16 28392c9a969f
permissions -rw-r--r--
More tweaks, including a "reversed" property for Grids to show the second player's perspective without turning the pieces upside-down.
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@14
    81
- (void) copyFrom: (Player*)player
jens@14
    82
{
jens@14
    83
    self.local = player.local;
jens@14
    84
    self.name = player.name;
jens@14
    85
    setObj(&_extraValues, [[player->_extraValues mutableCopy] autorelease]);
jens@14
    86
}
jens@14
    87
jens@14
    88
jens@13
    89
@synthesize game=_game, name=_name, local=_local;
jens@10
    90
jens@10
    91
- (BOOL) isCurrent      {return self == _game.currentPlayer;}
jens@10
    92
- (BOOL) isFriendly     {return self == _game.currentPlayer;}   // could be overridden for games with partners
jens@10
    93
- (BOOL) isUnfriendly   {return ! self.friendly;}
jens@10
    94
jens@10
    95
+ (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];}
jens@10
    96
jens@10
    97
jens@10
    98
- (int) index
jens@10
    99
{
jens@10
   100
    return [_game.players indexOfObjectIdenticalTo: self];
jens@10
   101
}
jens@10
   102
jens@10
   103
- (Player*) nextPlayer
jens@10
   104
{
jens@10
   105
    return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
jens@10
   106
}
jens@10
   107
jens@10
   108
- (Player*) previousPlayer
jens@10
   109
{
jens@10
   110
    return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
jens@10
   111
}
jens@10
   112
jens@10
   113
- (CGImageRef) icon
jens@10
   114
{
jens@10
   115
    return [_game iconForPlayer: self.index];
jens@10
   116
}
jens@10
   117
jens@10
   118
- (NSString*) description
jens@10
   119
{
jens@10
   120
    return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
jens@10
   121
}
jens@10
   122
jens@10
   123
@end