Source/Bit.m
author Jens Alfke <jens@mooseyard.com>
Mon Mar 10 17:30:57 2008 -0700 (2008-03-10)
changeset 1 3eb7be1dd7b6
parent 0 e9f7ba4718e1
child 7 428a194e3e59
permissions -rw-r--r--
Tic-tac-toe works on the iPhone simulator!
     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 @synthesize owner=_owner;
    39 
    40 - (BOOL) isFriendly         {return _owner.friendly;}
    41 - (BOOL) isUnfriendly       {return _owner.unfriendly;}
    42 
    43 
    44 - (CGFloat) scale
    45 {
    46     NSNumber *scale = [self valueForKeyPath: @"transform.scale"];
    47     return scale.floatValue;
    48 }
    49 
    50 - (void) setScale: (CGFloat)scale
    51 {
    52     [self setValue: [NSNumber numberWithFloat: scale]
    53         forKeyPath: @"transform.scale"];
    54 }
    55 
    56 
    57 - (int) rotation
    58 {
    59     NSNumber *rot = [self valueForKeyPath: @"transform.rotation"];
    60     return round( rot.doubleValue * 180.0 / M_PI );
    61 }
    62 
    63 - (void) setRotation: (int)rotation
    64 {
    65     [self setValue: [NSNumber numberWithDouble: rotation*M_PI/180.0]
    66         forKeyPath: @"transform.rotation"];
    67 }
    68 
    69 
    70 - (BOOL) pickedUp
    71 {
    72     return self.zPosition >= kPickedUpZ;
    73 }
    74 
    75 - (void) setPickedUp: (BOOL)up
    76 {
    77     if( up != self.pickedUp ) {
    78         CGFloat shadow, offset, radius, opacity, z, scale;
    79         if( up ) {
    80             shadow = 0.8;
    81             offset = 2;
    82             radius = 8;
    83             opacity = 0.9;
    84             scale = 1.2;
    85             z = kPickedUpZ;
    86             _restingZ = self.zPosition;
    87         } else {
    88             shadow = offset = radius = 0.0;
    89             opacity = 1.0;
    90             scale = 1.0/1.2;
    91             z = _restingZ;
    92         }
    93         
    94         self.zPosition = z;
    95 #if !TARGET_OS_ASPEN
    96         self.shadowOpacity = shadow;
    97         self.shadowOffset = CGSizeMake(offset,-offset);
    98         self.shadowRadius = radius;
    99 #endif
   100         self.opacity = opacity;
   101         self.scale *= scale;
   102     }
   103 }
   104 
   105 
   106 - (BOOL)containsPoint:(CGPoint)p
   107 {
   108     // Make picked-up pieces invisible to hit-testing.
   109     // Otherwise, while dragging a Bit, hit-testing the cursor position would always return
   110     // that Bit, since it's directly under the cursor...
   111     if( self.pickedUp )
   112         return NO;
   113     else
   114         return [super containsPoint: p];
   115 }
   116 
   117 
   118 -(id<BitHolder>) holder
   119 {
   120     // Look for my nearest ancestor that's a BitHolder:
   121     for( CALayer *layer=self.superlayer; layer; layer=layer.superlayer ) {
   122         if( [layer conformsToProtocol: @protocol(BitHolder)] )
   123             return (id<BitHolder>)layer;
   124         else if( [layer isKindOfClass: [Bit class]] )
   125             return nil;
   126     }
   127     return nil;
   128 }
   129 
   130 
   131 - (void) destroy
   132 {
   133     // "Pop" the Bit by expanding it 5x as it fades away:
   134     self.scale = 5;
   135     self.opacity = 0.0;
   136     // Removing the view from its superlayer right now would cancel the animations.
   137     // Instead, defer the removal until sometime shortly after the animations finish:
   138     [self performSelector: @selector(removeFromSuperlayer) withObject: nil afterDelay: 1.0];
   139 }
   140 
   141 
   142 @end