Permalink
Browse files

TMX Rotation support for TMX Generator

We may eventually want to support rotation on top of flipping, for now they are separate.  If a tile is rotated the rotation will override the horizontal or vertical flip.  There is also not an accessor for the diagonal flipping as it's primary purpose is to support rotation.
  • Loading branch information...
1 parent 3410455 commit 4db171fa59875a5a6d0de911bbb728f6701dc818 @slycrel slycrel committed Mar 11, 2012
@@ -74,6 +74,15 @@
#define kTMXGeneratorGroupObjectProperties @"groupObjectProperties"
+typedef enum
+{
+ kRotationNone = 0, // no rotation
+ kRotationOnce = 90, // 90 degrees
+ kRotationTwice = 180, // 180 degrees
+ kRotationThrice = 270 // 270 degrees
+} TMXGen_RotationValues;
+
+
@protocol TMXGeneratorDelegate <NSObject>
/** Returns the map's filePath to be saved to. */
@@ -153,11 +162,13 @@
*/
- (BOOL) tileflippedVerticallyAtPos:(CGPoint)inPoint layer:(NSString*)layerName;
-/** Returns a rotation value (no rotation if this method doesn't exist)
- * for the specified tile name and tile. */
-- (int) tileRotationForLayer:(NSString*)layerName
- X:(int)x
- Y:(int)y;
+/** Returns a rotation value (no rotation assumed if this
+ * method doesn't exist) for the specified layer name and tile.
+ * See the TMXGen_RotationValues enumeration for valid rotations
+ * to return. */
+- (TMXGen_RotationValues) tileRotationForLayer:(NSString*)layerName
+ X:(int)x
+ Y:(int)y;
@end
@@ -30,7 +30,8 @@
#define kFlippedHorizontallyFlag 0x80000000
#define kFlippedVerticallyFlag 0x40000000
-#define kFlippedMask ~(kFlippedHorizontallyFlag|kFlippedVerticallyFlag)
+#define kFlippedAntiDiagonallyFlag 0x20000000
+#define kFlippedMask ~(kFlippedHorizontallyFlag|kFlippedVerticallyFlag|kFlippedAntiDiagonallyFlag)
@interface TMXGenerator ()
@@ -625,15 +626,44 @@ - (NSString*) generateMapXML:(NSError**)error
if (GID)
{
- if ([delegate_ respondsToSelector:@selector(tileflippedHorizontallyAtPos:layer:)] &&
- [delegate_ tileflippedHorizontallyAtPos:CGPointMake(x, y) layer:key])
+ bool rotated = false;
+ if ([delegate_ respondsToSelector:@selector(tileRotationForLayer:X:Y:)])
{
- GID |= kFlippedHorizontallyFlag;
+ int rotationInDegrees = [delegate_ tileRotationForLayer:key X:x Y:y];
+ switch (rotationInDegrees)
+ {
+ case kRotationOnce: // 90 degrees
+ GID |= kFlippedHorizontallyFlag | kFlippedAntiDiagonallyFlag;
+ rotated = true;
+ break;
+
+ case kRotationTwice: // 180 degrees
+ GID |= kFlippedHorizontallyFlag | kFlippedVerticallyFlag;
+ rotated = true;
+ break;
+
+ case kRotationThrice: // 270 degrees
+ GID |= kFlippedAntiDiagonallyFlag | kFlippedVerticallyFlag;
+ rotated = true;
+ break;
+
+ default: // 0, 360, any other value, do no rotation.
+ break;
+ }
}
- if ([delegate_ respondsToSelector:@selector(tileflippedVerticallyAtPos:layer:)] &&
- [delegate_ tileflippedVerticallyAtPos:CGPointMake(x, y) layer:key])
+
+ if (!rotated)
{
- GID |= kFlippedVerticallyFlag;
+ if ([delegate_ respondsToSelector:@selector(tileflippedHorizontallyAtPos:layer:)] &&
+ [delegate_ tileflippedHorizontallyAtPos:CGPointMake(x, y) layer:key])
+ {
+ GID |= kFlippedHorizontallyFlag;
+ }
+ if ([delegate_ respondsToSelector:@selector(tileflippedVerticallyAtPos:layer:)] &&
+ [delegate_ tileflippedVerticallyAtPos:CGPointMake(x, y) layer:key])
+ {
+ GID |= kFlippedVerticallyFlag;
+ }
}
hasData = YES;
@@ -647,23 +677,24 @@ - (NSString*) generateMapXML:(NSError**)error
NSData* data = [NSData dataWithBytes:mapData length:sizeof(unsigned int) * mapWidth * mapHeight];
NSMutableDictionary* dictToAdd = [NSMutableDictionary dictionaryWithDictionary:dict];
[dictToAdd setObject:data forKey:kTMXGeneratorLayerData];
-
- // rotation data in addition to map data.
- if ([delegate_ respondsToSelector:@selector(tileRotationForLayer:X:Y:)])
- {
- unsigned int rotationData[mapHeight][mapWidth];
-
- for (int y = 0; y < mapHeight; y++)
- {
- for (int x = 0; x < mapWidth; x++)
- {
- rotationData[y][x] = [delegate_ tileRotationForLayer:key X:x Y:y];
- }
- }
-
- NSData* data = [NSData dataWithBytes:rotationData length:sizeof(unsigned int) * mapWidth * mapHeight];
- [dictToAdd setObject:data forKey:kTMXGeneratorLayerRotationData];
- }
+
+ // old method deprecated in favor of the new flipping method
+// // rotation data in addition to map data.
+// if ([delegate_ respondsToSelector:@selector(tileRotationForLayer:X:Y:)])
+// {
+// unsigned int rotationData[mapHeight][mapWidth];
+//
+// for (int y = 0; y < mapHeight; y++)
+// {
+// for (int x = 0; x < mapWidth; x++)
+// {
+// rotationData[y][x] = [delegate_ tileRotationForLayer:key X:x Y:y];
+// }
+// }
+//
+// NSData* data = [NSData dataWithBytes:rotationData length:sizeof(unsigned int) * mapWidth * mapHeight];
+// [dictToAdd setObject:data forKey:kTMXGeneratorLayerRotationData];
+// }
if (!layers)
layers = [[NSMutableArray alloc] initWithCapacity:10];
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -76,6 +76,7 @@ typedef enum {
kOutdoorTileRock,
kOutdoorTileWater,
kOutdoorTileFlipTest,
+ kOutdoorTileRotationTest,
kOutdoorTileEnd
} outdoorTileNames;
@@ -485,6 +485,11 @@ - (NSDictionary*) propertiesForTileSetNamed:(NSString*)name
[dict setObject:[NSString stringWithFormat:@"%i", kOutdoorTileFlipTest] forKey:kTileSetTypeNameKey];
[retVal setObject:dict forKey:@"4"];
+ // tile 5
+ dict = [NSMutableDictionary dictionaryWithCapacity:10];
+ [dict setObject:[NSString stringWithFormat:@"%i", kOutdoorTileRotationTest] forKey:kTileSetTypeNameKey];
+ [retVal setObject:dict forKey:@"5"];
+
}
else if ([name isEqualToString:kMetaLayerTileSetName])
{
@@ -556,10 +561,10 @@ - (NSString*) tileSetNameForLayer:(NSString*)layerName
{2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2},
+ {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 2, 5, 2, 5, 2, 5, 2},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2},
- {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2},
+ {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 5, 2, 5, 2, 5, 2, 5, 2},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
};
@@ -588,7 +593,7 @@ - (BOOL) tileflippedHorizontallyAtPos:(CGPoint)inPoint layer:(NSString*)layerNam
int x = inPoint.x;
int y = inPoint.y;
BOOL isEven = x % 2 == 0;
- if (tileGenerationData[y][x] == 4 && isEven)
+ if (tileGenerationData[y][x] == kOutdoorTileFlipTest && isEven)
return YES;
return NO;
@@ -602,7 +607,7 @@ - (BOOL) tileflippedVerticallyAtPos:(CGPoint)inPoint layer:(NSString*)layerName
int x = inPoint.x;
int y = inPoint.y;
BOOL isEven = y % 2 == 0;
- if (tileGenerationData[y][x] == 4 && isEven)
+ if (tileGenerationData[y][x] == kOutdoorTileFlipTest && isEven)
return YES;
return NO;
@@ -637,11 +642,25 @@ - (NSString*) tilePropertyForLayer:(NSString*)layerName tileSetName:(NSString*)t
}
-// If you had implemented rotated tiles then you would return degree rotation for the passed in tile coords on the passed layer. We are not using (meaningful) rotation in this example.
-//- (int) tileRotationForLayer:(NSString*)layerName X:(int)x Y:(int)y
-//{
-// return 0; // 0-360 degree rotation value for tile at x,y
-//}
+// If you had implemented rotated tiles then you would return degree rotation for the passed in tile coords on the passed layer.
+// you should be much smarter about rotation than we are here!
+- (TMXGen_RotationValues) tileRotationForLayer:(NSString*)layerName X:(int)x Y:(int)y
+{
+ static TMXGen_RotationValues val = kRotationNone;
+
+ // only rotate tiles of type 5
+ if (tileGenerationData[y][x] == kOutdoorTileRotationTest)
+ {
+ TMXGen_RotationValues retVal = val;
+ val += 90;
+ if (val > kRotationThrice)
+ val = 0;
+
+ return retVal;
+ }
+
+ return kRotationNone;
+}
#pragma mark -

0 comments on commit 4db171f

Please sign in to comment.