Source/Bit.m
author Jens Alfke <jens@mooseyard.com>
Thu Jul 03 17:44:30 2008 -0700 (2008-07-03)
changeset 10 6c78cc6bd7a6
parent 9 a59acc683080
child 11 436cbdf56810
permissions -rw-r--r--
Lots of reworking. Completed support for game history, including Turn class. Changed Game API around quite a bit.
     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 "Player.h"
    26 #import "QuartzUtils.h"
    27 
    28 
    29 #ifdef TARGET_OS_IPHONE
    30 #define kPickedUpScale   2.0      // more magnification, so piece shows up underneath fingertip
    31 #define kPickedUpOpacity 0.6
    32 #else
    33 #define kPickedUpScale   1.2
    34 #define kPickedUpOpacity 0.9
    35 #endif
    36 
    37 
    38 @implementation Bit
    39 
    40 
    41 - (id) copyWithZone: (NSZone*)zone
    42 {
    43     Bit *clone = [super copyWithZone: zone];
    44     clone->_owner = _owner;
    45     return clone;
    46 }
    47 
    48 - (void) dealloc
    49 {
    50     [super dealloc];
    51 }
    52 
    53 
    54 @synthesize owner=_owner;
    55 
    56 - (BOOL) isFriendly         {return _owner.friendly;}
    57 - (BOOL) isUnfriendly       {return _owner.unfriendly;}
    58 
    59 /*
    60 - (NSString*) identifier
    61 {
    62     if( _identifier )
    63         return _identifier;
    64     // Defaults to just identifying the owner:
    65     return [NSString stringWithFormat: @"p%i", _owner.index+1];
    66 }
    67 */
    68 
    69 - (CGFloat) scale
    70 {
    71     NSNumber *scale = [self valueForKeyPath: @"transform.scale"];
    72     return scale.floatValue;
    73 }
    74 
    75 - (void) setScale: (CGFloat)scale
    76 {
    77     [self setValue: [NSNumber numberWithFloat: scale]
    78         forKeyPath: @"transform.scale"];
    79 }
    80 
    81 
    82 - (int) rotation
    83 {
    84     NSNumber *rot = [self valueForKeyPath: @"transform.rotation"];
    85     return round( rot.doubleValue * 180.0 / M_PI );
    86 }
    87 
    88 - (void) setRotation: (int)rotation
    89 {
    90     [self setValue: [NSNumber numberWithDouble: rotation*M_PI/180.0]
    91         forKeyPath: @"transform.rotation"];
    92 }
    93 
    94 
    95 - (BOOL) pickedUp
    96 {
    97     return _pickedUp;
    98 }
    99 
   100 - (void) setPickedUp: (BOOL)up
   101 {
   102     if( up != _pickedUp ) {
   103         CGFloat shadow, offset, radius, opacity, z, scale;
   104         if( up ) {
   105             shadow = 0.8;
   106             offset = 2;
   107             radius = 8;
   108             opacity = kPickedUpOpacity;
   109             scale = kPickedUpScale;
   110             z = kPickedUpZ;
   111             _restingZ = self.zPosition;
   112         } else {
   113             shadow = offset = radius = 0.0;
   114             opacity = 1.0;
   115             scale = 1.0/kPickedUpScale;
   116             z = _restingZ;
   117         }
   118 
   119         //self.zPosition = z;
   120 #if !TARGET_OS_IPHONE
   121         self.shadowOpacity = shadow;
   122         self.shadowOffset = CGSizeMake(offset,-offset);
   123         self.shadowRadius = radius;
   124 #endif
   125         self.opacity = opacity;
   126         self.scale *= scale;
   127         _pickedUp = up;
   128     }
   129 }
   130 
   131 
   132 - (BOOL)containsPoint:(CGPoint)p
   133 {
   134     // Make picked-up pieces invisible to hit-testing.
   135     // Otherwise, while dragging a Bit, hit-testing the cursor position would always return
   136     // that Bit, since it's directly under the cursor...
   137     if( self.pickedUp )
   138         return NO;
   139     else
   140         return [super containsPoint: p];
   141 }
   142 
   143 
   144 -(id<BitHolder>) holder
   145 {
   146     // Look for my nearest ancestor that's a BitHolder:
   147     for( CALayer *layer=self.superlayer; layer; layer=layer.superlayer ) {
   148         if( [layer conformsToProtocol: @protocol(BitHolder)] )
   149             return (id<BitHolder>)layer;
   150         else if( [layer isKindOfClass: [Bit class]] )
   151             return nil;
   152     }
   153     return nil;
   154 }
   155 
   156 
   157 - (void) destroy
   158 {
   159     // "Pop" the Bit by expanding it 5x as it fades away:
   160     self.scale = 5;
   161     self.opacity = 0.0;
   162     // Removing the view from its superlayer right now would cancel the animations.
   163     // Instead, defer the removal until sometime shortly after the animations finish:
   164     [self performSelector: @selector(removeFromSuperlayer) withObject: nil afterDelay: 1.0];
   165 }
   166 
   167 
   168 @end