diff -r 000000000000 -r e9f7ba4718e1 Source/HexGrid.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Source/HexGrid.m Fri Mar 07 11:43:02 2008 -0800 @@ -0,0 +1,200 @@ +/* This code is based on Apple's "GeekGameBoard" sample code, version 1.0. + http://developer.apple.com/samplecode/GeekGameBoard/ + Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved. + + Redistribution and use in source and binary forms, with or without modification, are permitted + provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of + conditions and the following disclaimer in the documentation and/or other materials provided + with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#import "HexGrid.h" +#import "Game.h" + + +@implementation HexGrid + + +- (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns + spacing: (CGSize)spacing + position: (CGPoint)pos +{ + // Ignore given spacing.height; set it to make the hexagons regular. + CGFloat capHeight = spacing.height / 2 * tan(M_PI/6); + CGFloat side = spacing.height / 2 / cos(M_PI/6); + spacing.height = side + capHeight; + + self = [super initWithRows: nRows columns: nColumns + spacing: spacing + position: pos]; + if( self ) { + _capHeight = capHeight; + _side = side; + self.bounds = CGRectMake(-1, -1, + (nColumns+0.5)*spacing.width + 2, + nRows*spacing.height+capHeight + 2); + _cellClass = [Hex class]; + } + return self; +} + + +- (id) initWithRows: (unsigned)nRows columns: (unsigned)nColumns + frame: (CGRect)frame; +{ + // Compute the horizontal spacing: + CGFloat s = floor(MIN( (frame.size.width -2.0)/nColumns, + (frame.size.height-2.0)/(nRows+0.5*tan(M_PI/6)) / (0.5*(tan(M_PI/6)+1/cos(M_PI/6))) )); + return [self initWithRows: nRows columns: nColumns + spacing: CGSizeMake(s,s) + position: frame.origin]; +} + + +- (void) dealloc +{ + CGPathRelease(_cellPath); + [super dealloc]; +} + + +- (void) addCellsInHexagon +{ + int size = _nRows - !(_nRows & 1); // make it odd + for( int row=0; row<_nRows; row++ ) { + int n; // # of hexes remaining in this row + if( row < size ) + n = size - abs(row-size/2); + else + n = 0; + int c0 = floor(((int)_nRows+1-n -(row&1))/2.0); // col of 1st remaining hex + + for( int col=0; col<_nColumns; col++ ) + if( col>=c0 && col