Created
September 25, 2010 19:35
-
-
Save amnn/597188 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// mapEngine.m | |
// TestRun | |
// | |
// Created by Ashok Menon on 20/08/2010. | |
// Copyright 2010 __MyCompanyName__. All rights reserved. | |
// | |
#import "mapEngine.h" | |
#import "ImageCache.h" | |
void initializeMapEngine ( | |
CGPoint top, | |
CGPoint right, | |
CGPoint left, | |
CGPoint bottom, | |
CGFloat height, | |
MapEngine *map) | |
{ | |
map->vTop = top; | |
map->vRight = right; | |
map->vBottom = bottom; | |
map->vLeft = left; | |
map->pitHeight = height; | |
map->vGridX = CGPointMake( (map->vLeft.x - map->vBottom.x), (map->vLeft.y - map->vBottom.y) ); | |
map->vGridY = CGPointMake( (map->vTop.x - map->vLeft.x), (map->vTop.y - map->vLeft.y) ); | |
map->x = 0.; | |
map->y = -( map->pitHeight / 2 ); | |
map->sideNum = MAP_SIDES; | |
map->bottomWallAlpha = .5f; | |
map->rightWallAlpha = .5f; | |
map->bWallAlphaDir = 0; | |
map->rWallAlphaDir = 0; | |
map->scale = 1.; | |
map->canSwipe = NO; | |
//Init grid if necessary | |
CGColorSpaceRef grad_colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGFloat locations[2] = { 0., 1. }; | |
CGFloat bgGrad_colors[8] = { .8, .8, .8, 1., | |
1., 1., 1., 1.}; | |
CGFloat fWall_colors[8] = { 0., 0., 0., 1., | |
.8, .8, .8, 1.}; | |
CGFloat lWall_colors[8] = { .6, .6, .6, 1., | |
.93,.93,.93, 1.}; | |
map->bgGrad = CGGradientCreateWithColorComponents(grad_colorSpace, | |
bgGrad_colors, | |
locations, 2); | |
map->fWallGrad = CGGradientCreateWithColorComponents(grad_colorSpace, | |
fWall_colors, | |
locations, 2); | |
map->lWallGrad = CGGradientCreateWithColorComponents(grad_colorSpace, | |
lWall_colors, | |
locations, 2); | |
CGColorSpaceRelease(grad_colorSpace); | |
map->clippingMask = CGPathCreateMutable(); //Generate Clipping Mask For Later Use | |
CGPathMoveToPoint(map->clippingMask, NULL, map->vTop.x, map->vTop.y); | |
CGPathAddLineToPoint(map->clippingMask, NULL, map->vRight.x, map->vRight.y); | |
CGPathAddLineToPoint(map->clippingMask, NULL, map->vRight.x, map->vRight.y-map->pitHeight); | |
CGPathAddLineToPoint(map->clippingMask, NULL, map->vBottom.x, map->vBottom.y-map->pitHeight); | |
CGPathAddLineToPoint(map->clippingMask, NULL, map->vLeft.x, map->vLeft.y-map->pitHeight); | |
CGPathAddLineToPoint(map->clippingMask, NULL, map->vLeft.x, map->vLeft.y); | |
CGPathAddLineToPoint(map->clippingMask, NULL, map->vTop.x, map->vTop.y); | |
CGPathCloseSubpath(map->clippingMask); | |
map->fWall = CGPathCreateMutable(); //Generate Front Facing Wall For Later Use | |
CGPathMoveToPoint(map->fWall, NULL, map->vTop.x, map->vTop.y); | |
CGPathAddLineToPoint(map->fWall, NULL, map->vRight.x-1, map->vRight.y); | |
CGPathAddLineToPoint(map->fWall, NULL, map->vRight.x-1, map->vRight.y-map->pitHeight-1); | |
CGPathAddLineToPoint(map->fWall, NULL, map->vTop.x, map->vTop.y-map->pitHeight-1); | |
CGPathAddLineToPoint(map->fWall, NULL, map->vTop.x, map->vTop.y); | |
CGPathCloseSubpath(map->fWall); | |
map->lWall = CGPathCreateMutable(); //Generate Left Facing Wall For Later Use | |
CGPathMoveToPoint(map->lWall, NULL, map->vTop.x, map->vTop.y); | |
CGPathAddLineToPoint(map->lWall, NULL, map->vLeft.x+1, map->vLeft.y); | |
CGPathAddLineToPoint(map->lWall, NULL, map->vLeft.x+1, map->vLeft.y-map->pitHeight-1); | |
CGPathAddLineToPoint(map->lWall, NULL, map->vTop.x, map->vTop.y-map->pitHeight-1); | |
CGPathAddLineToPoint(map->lWall, NULL, map->vTop.x, map->vTop.y); | |
CGPathCloseSubpath(map->lWall); | |
map->mapFloor = CGPathCreateMutable(); //Generate Map Floor For Later Use | |
CGPathMoveToPoint(map->mapFloor, NULL, map->vTop.x, map->vTop.y-map->pitHeight); | |
CGPathAddLineToPoint(map->mapFloor, NULL, map->vRight.x, map->vRight.y-map->pitHeight); | |
CGPathAddLineToPoint(map->mapFloor, NULL, map->vBottom.x, map->vBottom.y-map->pitHeight+1); | |
CGPathAddLineToPoint(map->mapFloor, NULL, map->vLeft.x, map->vLeft.y-map->pitHeight+1); | |
CGPathAddLineToPoint(map->mapFloor, NULL, map->vTop.x, map->vTop.y-map->pitHeight); | |
CGPathCloseSubpath(map->mapFloor); | |
map->mapTile = CGPathCreateMutable(); //Generate Map Tile For Later Use | |
CGPathMoveToPoint(map->mapTile, NULL, map->vTop.x/MAP_SIDES, map->vTop.y/MAP_SIDES); | |
CGPathAddLineToPoint(map->mapTile, NULL, map->vRight.x/MAP_SIDES, map->vRight.y/MAP_SIDES); | |
CGPathAddLineToPoint(map->mapTile, NULL, map->vBottom.x/MAP_SIDES, map->vBottom.y/MAP_SIDES); | |
CGPathAddLineToPoint(map->mapTile, NULL, map->vLeft.x/MAP_SIDES, map->vLeft.y/MAP_SIDES); | |
CGPathAddLineToPoint(map->mapTile, NULL, map->vTop.x/MAP_SIDES, map->vTop.y/MAP_SIDES); | |
CGPathCloseSubpath(map->mapTile); | |
map->bWall = CGPathCreateMutable(); //Generate Rear Facing (Bottom) Wall For Later Use | |
CGPathMoveToPoint(map->bWall, NULL, map->vLeft.x, map->vLeft.y); | |
CGPathAddLineToPoint(map->bWall, NULL, map->vBottom.x+1, map->vBottom.y); | |
CGPathAddLineToPoint(map->bWall, NULL, map->vBottom.x+1, map->vBottom.y-map->pitHeight-1); | |
CGPathAddLineToPoint(map->bWall, NULL, map->vLeft.x, map->vLeft.y-map->pitHeight); | |
CGPathAddLineToPoint(map->bWall, NULL, map->vLeft.x, map->vLeft.y); | |
CGPathCloseSubpath(map->bWall); | |
map->rWall = CGPathCreateMutable(); //Generate Right Facing Wall For Later Use | |
CGPathMoveToPoint(map->rWall, NULL, map->vRight.x, map->vRight.y); | |
CGPathAddLineToPoint(map->rWall, NULL, map->vBottom.x-1, map->vBottom.y); | |
CGPathAddLineToPoint(map->rWall, NULL, map->vBottom.x-1, map->vBottom.y-map->pitHeight-2); | |
CGPathAddLineToPoint(map->rWall, NULL, map->vRight.x, map->vRight.y-map->pitHeight-2); | |
CGPathAddLineToPoint(map->rWall, NULL, map->vRight.x, map->vRight.y); | |
CGPathCloseSubpath(map->rWall); | |
} | |
CGPoint indexForGridSizeAtDiagonalIndex ( | |
CGFloat n, | |
CGFloat i) | |
{ | |
CGFloat total, x0, rownum, rowindex, | |
row_start_number, rowtotal, | |
rowx, rowy; | |
CGPoint gridIndex; | |
total = 0; | |
for (int i0 = 1; i0 <= (2*n+1); i0++) | |
{ | |
if ( i0 <= n+1 ) x0 = i0; | |
else x0 = 2*(n+1)-i0; | |
total += x0; | |
if (total >= i) | |
{ | |
rownum = i0; | |
rowindex = i - (total - x0); | |
break; | |
} | |
} | |
row_start_number = (n+1)-rownum >= 0 ? (n+1) - rownum : 0; | |
rowtotal = (2*n+1)-rownum; | |
rowx = (rowindex - 1) + row_start_number; | |
//rowx = rowx == -0. ? 0. : rowx; | |
rowy = rowtotal - rowx; | |
gridIndex = CGPointMake(rowx, rowy); | |
return gridIndex; | |
} | |
void correctXandY ( | |
MapEngine *map) | |
{ | |
CGFloat maxX, maxY, minX, minY; | |
maxX = (240. * map->scale) - map->vRight.x; | |
maxY = (160. * map->scale) - map->vTop.y; | |
minX = -maxX; | |
minY = -maxY; | |
if ( map->x > maxX ) map->x = maxX; | |
if ( map->x < minX ) map->x = minX; | |
if ( map->y > maxY ) map->y = maxY; | |
if ( map->y < minY ) map->y = minY; | |
} | |
void changeMapXandYBy ( | |
CGFloat x0, | |
CGFloat y0, | |
MapEngine *map) | |
{ | |
if ( map->canSwipe ) | |
{ | |
map->x += y0 / map->scale; | |
map->y += x0 / map->scale; | |
correctXandY(map); | |
} | |
} | |
void setScale ( | |
CGFloat s, | |
MapEngine *map) | |
{ | |
map->scale = s; | |
correctXandY(map); | |
} | |
void setScaleAsScaleFactorOfSelf ( | |
CGFloat sf, | |
MapEngine *map) | |
{ | |
map->scale *= sf; | |
if ( map->scale < 0.8 ) map->scale = 0.8; | |
if ( map->scale > 3.0 ) map->scale = 3.0; | |
correctXandY(map); | |
if ( map->scale > 1.0 ) map->canSwipe = YES; | |
else | |
{ | |
map->x = 0.; | |
map->y = -(map->pitHeight) / 2; | |
map->canSwipe = NO; | |
} | |
} | |
void draw ( | |
CGContextRef c, | |
MapEngine *map) | |
{ | |
CGFloat scale0 = map->scale >= 1 ? map->scale : 1 / (map->scale) ; | |
CGContextSaveGState(c); | |
CGAffineTransform t = CGAffineTransformIdentity; | |
t = CGAffineTransformTranslate(t, map->y+160, 240-map->x); | |
t = CGAffineTransformRotate(t, 0 - 3.141592*0.5); | |
t = CGAffineTransformScale(t, map->scale, map->scale); | |
CGContextConcatCTM(c, t); | |
CGContextDrawLinearGradient(c, map->bgGrad, | |
CGPointMake(-260.*scale0, 180.*scale0), | |
CGPointMake(250.*scale0, -170.*scale0), | |
kCGGradientDrawsAfterEndLocation); | |
CGContextBeginTransparencyLayer(c, NULL); | |
CGContextAddPath(c, map->clippingMask); //Draw Clipping Mask | |
CGContextClip(c); | |
CGContextSaveGState(c); | |
CGContextAddPath(c, map->fWall);; | |
CGContextClip(c); | |
CGContextDrawLinearGradient(c, map->fWallGrad, | |
CGPointMake(0.f, -170.*scale0), | |
CGPointMake(0.f, 170.*scale0), | |
kCGGradientDrawsAfterEndLocation); | |
CGContextRestoreGState(c); | |
CGContextSaveGState(c); | |
CGContextAddPath(c, map->lWall); | |
CGContextClip(c); | |
CGContextDrawLinearGradient(c, map->lWallGrad, | |
CGPointMake(0.f, -170.*scale0), | |
CGPointMake(0.f, 170.*scale0), | |
kCGGradientDrawsAfterEndLocation); | |
CGContextRestoreGState(c); | |
CGContextSetRGBFillColor(c, 1, 1, 1, 1); | |
CGContextAddPath(c, map->mapFloor); //Draw Map Floor | |
CGContextFillPath(c); | |
NSUInteger gridSize = MAP_SIDES - 1; | |
int i; | |
for (i = 1; i <=MAP_SIDES*MAP_SIDES; i++) | |
{ | |
CGPoint currIndex = indexForGridSizeAtDiagonalIndex(gridSize, i); | |
t = CGContextGetCTM(c); //Reset transforms to center of landscape display | |
t = CGAffineTransformInvert(t); | |
t = CGAffineTransformTranslate(t, map->y+160, 240-map->x); | |
t = CGAffineTransformRotate(t, 0 - 3.141592*0.5); | |
t = CGAffineTransformScale(t, map->scale, map->scale); | |
t = CGAffineTransformTranslate(t, 0.f, (map->vBottom.y-map->pitHeight-3)+(map->vTop.y-map->vBottom.y)/(2*MAP_SIDES)); //Translate to 0,0 reference | |
t = CGAffineTransformTranslate(t, (map->vGridX.x/MAP_SIDES)*currIndex.x, (map->vGridX.y/MAP_SIDES)*currIndex.x); //Further Translate to vector for grid reference in x | |
t = CGAffineTransformTranslate(t, (map->vGridY.x/MAP_SIDES)*currIndex.y, (map->vGridY.y/MAP_SIDES)*currIndex.y); // and in y | |
CGContextConcatCTM(c, t); | |
CGContextSetRGBFillColor(c, (arc4random()%100)/100.f, (arc4random()%100)/100.f, (arc4random()%100)/100.f, 1.f); | |
CGContextAddPath(c, map->mapTile); | |
CGContextFillPath(c); | |
//call draw to tile at currIndex (on grid) | |
//call draw on player if he exists in said grid | |
//CGContextRestoreGState(c); | |
} | |
t = CGContextGetCTM(c); //Reset transforms to center of landscape display | |
t = CGAffineTransformInvert(t); | |
t = CGAffineTransformTranslate(t, map->y+160, 240-map->x); | |
t = CGAffineTransformRotate(t, 0 - 3.141592*0.5); | |
t = CGAffineTransformScale(t, map->scale, map->scale); | |
CGContextConcatCTM(c, t); | |
CGContextSaveGState(c); //Save Main Clipping Mask | |
CGContextSetAlpha(c, map->bottomWallAlpha); | |
CGContextAddPath(c, map->bWall); //Draw Rear Facing (Bottom) Wall | |
CGContextClip(c); | |
CGContextDrawLinearGradient(c, map->bgGrad, | |
CGPointMake(-250.*scale0, 170.*scale0), | |
CGPointMake(250.*scale0, -170.*scale0), | |
kCGGradientDrawsAfterEndLocation); | |
CGContextRestoreGState(c); //Restore Clipping Mask | |
CGContextSaveGState(c); //Save Main Clipping Mask | |
CGContextSetAlpha(c, map->rightWallAlpha); | |
CGContextAddPath(c, map->rWall); //Draw Right Facing Wall | |
CGContextClip(c); | |
CGContextDrawLinearGradient(c, map->bgGrad, | |
CGPointMake(-250.*scale0, 170.*scale0), | |
CGPointMake(250.*scale0, -170.*scale0), | |
kCGGradientDrawsAfterEndLocation); | |
CGContextRestoreGState(c); //Restore Clipping Mask | |
CGContextEndTransparencyLayer(c); | |
CGContextRestoreGState(c); | |
} | |
void tic ( | |
MapEngine *map) | |
{ | |
map->bottomWallAlpha += map->bWallAlphaDir*0.05f; | |
map->rightWallAlpha += map->rWallAlphaDir*0.05f; | |
if ( map->bottomWallAlpha <= 0.5 ) map->bWallAlphaDir = 0; | |
if ( map->rightWallAlpha <= 0.5 ) map->rWallAlphaDir = 0; | |
//Calculate x, y of IsoMap based on gridX and gridY of player. | |
//Calculate transparency of walls | |
//Call tic for all members of IsoMap (player and tiles) | |
} | |
void clearMemory ( | |
MapEngine *map) | |
{ | |
CGPathRelease(map->clippingMask); | |
CGPathRelease(map->fWall); | |
CGPathRelease(map->rWall); | |
CGPathRelease(map->bWall); | |
CGPathRelease(map->lWall); | |
CGPathRelease(map->mapFloor); | |
CGPathRelease(map->mapTile); | |
CGGradientRelease(map->bgGrad); | |
CGGradientRelease(map->lWallGrad); | |
CGGradientRelease(map->fWallGrad); | |
clearPlayerMemory( map->player ); | |
free( map ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment