Source/Bit.m
author Jens Alfke <jens@mooseyard.com>
Sun Mar 16 15:06:47 2008 -0700 (2008-03-16)
changeset 7 428a194e3e59
parent 1 3eb7be1dd7b6
child 8 45c82a071aca
permissions -rw-r--r--
Game class now tracks board state and moves, as strings, and can step through its history.
Fixed another bug in Go (you could drag your captured stones back to the board!)
     1 /*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     2     http://developer.apple.com/samplecode/GeekGameBoard/
     3     Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     8     * Redistributions of source code must retain the above copyright notice, this list of conditions
     9       and the following disclaimer.
    10     * Redistributions in binary form must reproduce the above copyright notice, this list of
    11       conditions and the following disclaimer in the documentation and/or other materials provided
    12       with the distribution.
    13 
    14     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    15     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    16     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    17     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    18     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    19     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    20     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    21     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    22 */
    23 #import "Bit.h"
    24 #import "Game.h"
    25 #import "QuartzUtils.h"
    26 
    27 
    28 @implementation Bit
    29 
    30 
    31 - (id) copyWithZone: (NSZone*)zone
    32 {
    33     Bit *clone = [super copyWithZone: zone];
    34     clone->_owner = _owner;
    35     return clone;
    36 }
    37 
    38 - (void) dealloc
    39 {
    40     [super dealloc];
    41 }
    42 
    43 
    44 @synthesize owner=_owner;
    45 
    46 - (BOOL) isFriendly         {return _owner.friendly;}
    47 - (BOOL) isUnfriendly       {return _owner.unfriendly;}
    48 
    49 /*
    50 - (NSString*) identifier
    51 {
    52     if( _identifier )
    53         return _identifier;
    54     // Defaults to just identifying the owner:
    55     return [NSString stringWithFormat: @"p%i", _owner.index+1];
    56 }
    57 */
    58 
    59 - (CGFloat) scale
    60 {
    61     NSNumber *scale = [self valueForKeyPath: @"transform.scale"];
    62     return scale.floatValue;
    63 }
    64 
    65 - (void) setScale: (CGFloat)scale
    66 {
    67     [self setValue: [NSNumber numberWithFloat: scale]
    68         forKeyPath: @"transform.scale"];
    69 }
    70 
    71 
    72 - (int) rotation
    73 {
    74     NSNumber *rot = [self valueForKeyPath: @"transform.rotation"];
    75     return round( rot.doubleValue * 180.0 / M_PI );
    76 }
    77 
    78 - (void) setRotation: (int)rotation
    79 {
    80     [self setValue: [NSNumber numberWithDouble: rotation*M_PI/180.0]
    81         forKeyPath: @"transform.rotation"];
    82 }
    83 
    84 
    85 - (BOOL) pickedUp
    86 {
    87     return self.zPosition >= kPickedUpZ;
    88 }
    89 
    90 - (void) setPickedUp: (BOOL)up
    91 {
    92     if( up != self.pickedUp ) {
    93         CGFloat shadow, offset, radius, opacity, z, scale;
    94         if( up ) {
    95             shadow = 0.8;
    96             offset = 2;
    97             radius = 8;
    98             opacity = 0.9;
    99             scale = 1.2;
   100             z = kPickedUpZ;
   101             _restingZ = self.zPosition;
   102         } else {
   103             shadow = offset = radius = 0.0;
   104             opacity = 1.0;
   105             scale = 1.0/1.2;
   106             z = _restingZ;
   107         }
   108         
   109         self.zPosition = z;
   110 #if !TARGET_OS_ASPEN
   111         self.shadowOpacity = shadow;
   112         self.shadowOffset = CGSizeMake(offset,-offset);
   113         self.shadowRadius = radius;
   114 #endif
   115         self.opacity = opacity;
   116         self.scale *= scale;
   117     }
   118 }
   119 
   120 
   121 - (BOOL)containsPoint:(CGPoint)p
   122 {
   123     // Make picked-up pieces invisible to hit-testing.
   124     // Otherwise, while dragging a Bit, hit-testing the cursor position would always return
   125     // that Bit, since it's directly under the cursor...
   126     if( self.pickedUp )
   127         return NO;
   128     else
   129         return [super containsPoint: p];
   130 }
   131 
   132 
   133 -(id<BitHolder>) holder
   134 {
   135     // Look for my nearest ancestor that's a BitHolder:
   136     for( CALayer *layer=self.superlayer; layer; layer=layer.superlayer ) {
   137         if( [layer conformsToProtocol: @protocol(BitHolder)] )
   138             return (id<BitHolder>)layer;
   139         else if( [layer isKindOfClass: [Bit class]] )
   140             return nil;
   141     }
   142     return nil;
   143 }
   144 
   145 
   146 - (void) destroy
   147 {
   148     // "Pop" the Bit by expanding it 5x as it fades away:
   149     self.scale = 5;
   150     self.opacity = 0.0;
   151     // Removing the view from its superlayer right now would cancel the animations.
   152     // Instead, defer the removal until sometime shortly after the animations finish:
   153     [self performSelector: @selector(removeFromSuperlayer) withObject: nil afterDelay: 1.0];
   154 }
   155 
   156 
   157 @end