Skip to content

Instantly share code, notes, and snippets.

@LearnCocos2D
Last active December 16, 2015 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LearnCocos2D/5513068 to your computer and use it in GitHub Desktop.
Save LearnCocos2D/5513068 to your computer and use it in GitHub Desktop.
KoboldTouch tilemap demo code http://www.koboldtouch.com
//
// TilemapWithBorderViewController.m
// _Feature-Demo-Template_
//
// Created by Steffen Itterheim on 19.03.13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import "TilemapWithBorderSceneViewController.h"
@implementation TilemapWithBorderSceneViewController
// this is called from the super class' initWithDefaults method
-(void) createTest
{
#if KK_PLATFORM_IOS
// for this tilemap only sd & hd tilesets are provided because the tilemap will show a larger portion of the map on iPads,
// ... therefore: regular iPads should use the SD tileset instead of the -hd tileset
[[CCFileUtils sharedFileUtils] setiPadSuffix:@""];
// ... and Retina iPads should load the -hd tilesets
[[CCFileUtils sharedFileUtils] setiPadRetinaDisplaySuffix:@"-hd"];
#endif /* if KK_PLATFORM_IOS */
// create a tilemap view controller with a TMX tilemap
_tilemapVC = [KTTilemapViewController tilemapControllerWithTMXFile:@"crawl-tilemap.tmx"];
// add the tilemap and its layers, this creates the subcontrollers of _tilemapVC:
[self addSubController:_tilemapVC];
// set the initial position
[_tilemapVC scrollToTileCoordinate:CGPointMake(19, 24)];
// get the object layer and turn off debug drawing (off by default, this is just an example)
KTObjectLayerViewController* objectLayerVC = [_tilemapVC tilemapLayerViewControllerByName:@"objects"];
objectLayerVC.drawObjects = NO;
objectLayerVC.drawPolyObjectBoundingBoxes = NO;
// add a label that follows the scrolling of the floor layer
NSString* text = @"THIS .. IS .. LAVAAAAAAAA!!!";
KTTrueTypeFontLabel* ttfLabel = [KTTrueTypeFontLabel trueTypeFontLabelWithText:text];
ttfLabel.fontName = @"Chalkduster";
ttfLabel.fontSize = 30;
KTLabelViewController* labelVC = [KTLabelViewController labelControllerWithFontLabel:ttfLabel];
labelVC.rootNode.position = [_tilemapVC pointCenterOfTileCoord:CGPointMake(20, 15)];
// now get the layer view controller to add the label to:
KTTileLayerViewController* floorLayerVC = [_tilemapVC tilemapLayerViewControllerByName:@"floor"];
[floorLayerVC addSubController:labelVC];
// rotate & move the label in place (it will continue to scroll along with the layer)
id rotate1 = [CCRotateTo actionWithDuration:2.0f angle:10.0f];
id rotate2 = [CCRotateTo actionWithDuration:2.0f angle:-10.0f];
id ease1 = [CCEaseBackInOut actionWithAction:rotate1];
id ease2 = [CCEaseBackInOut actionWithAction:rotate2];
id sequence = [CCSequence actions:ease1, ease2, nil];
[labelVC.rootNode runAction:[CCRepeatForever actionWithAction:sequence]];
id move1 = [CCMoveBy actionWithDuration:2.3f position:CGPointMake(0, 140)];
id move2 = [CCMoveBy actionWithDuration:2.3f position:CGPointMake(0, -140)];
id easem1 = [CCEaseElasticInOut actionWithAction:move1 period:0.99f];
id easem2 = [CCEaseElasticInOut actionWithAction:move2 period:0.99f];
id sequence2 = [CCSequence actions:easem1, easem2, nil];
[labelVC.rootNode runAction:[CCRepeatForever actionWithAction:sequence2]];
[self createDrawObjectsButton];
} /* createTest */
-(void) toggleDrawObjects
{
_drawObjectsStyle++;
if (_drawObjectsStyle >= DrawObjectsStyle_Count)
{
_drawObjectsStyle = DrawObjectsDisabled;
}
KTObjectLayerViewController* objectLayerVC = [_tilemapVC tilemapLayerViewControllerByName:@"objects"];
KTTileLayerViewController* floorLayerVC = [_tilemapVC tilemapLayerViewControllerByName:@"floor"];
switch (_drawObjectsStyle)
{
default:
case DrawObjectsDisabled:
objectLayerVC.drawObjects = NO;
objectLayerVC.drawPolyObjectBoundingBoxes = NO;
floorLayerVC.drawTileCoordinates = NO;
break;
case DrawObjectsEnabled:
objectLayerVC.drawObjects = YES;
objectLayerVC.drawPolyObjectBoundingBoxes = NO;
floorLayerVC.drawTileCoordinates = NO;
break;
case DrawObjectsEnabledWithBoundingBoxes:
objectLayerVC.drawObjects = YES;
objectLayerVC.drawPolyObjectBoundingBoxes = YES;
floorLayerVC.drawTileCoordinates = NO;
break;
case DrawTileCoordinates:
objectLayerVC.drawObjects = NO;
objectLayerVC.drawPolyObjectBoundingBoxes = NO;
floorLayerVC.drawTileCoordinates = YES;
floorLayerVC.drawTileCoordinatesColor = ccWHITE;
break;
} /* switch */
} /* toggleDrawObjects */
-(void) createDrawObjectsButton
{
KTTextMenuItem* item = [KTTextMenuItem itemWithText:@"Toggle Draw Objects" executionBlock:^(id sender){
[self toggleDrawObjects];
}];
KTTextMenu* textMenu = [KTTextMenu menuWithTextMenuItems:[NSArray arrayWithObjects:item, nil]];
textMenu.fontSize = 26;
item.color = ccYELLOW;
CGSize winSize = self.gameController.windowController.windowSize;
KTMenuViewController* buttonVC = [KTMenuViewController menuControllerWithTextMenu:textMenu];
buttonVC.rootNode.position = CGPointMake(winSize.width / 2, winSize.height - winSize.height / 6);
[self addSubController:buttonVC];
} /* createDrawObjectsButton */
-(void) touchBeganWithEvent:(KTTouchEvent*)touchEvent
{
CGPoint offset = ccpSub(touchEvent.locationInGLView, [CCDirector sharedDirector].windowCenter);
[_tilemapVC scrollByPointOffset:offset speed:4.0f];
CGPoint touchLocation = touchEvent.locationInGLView;
LOG_EXPR(touchLocation);
CGPoint touchedTileCoord = [_tilemapVC tileCoordFromPoint:touchLocation];
LOG_EXPR(touchedTileCoord);
CGPoint pointOriginOfTouchedTile = [_tilemapVC pointOriginOfTileCoord:touchedTileCoord];
LOG_EXPR(pointOriginOfTouchedTile);
pointOriginOfTouchedTile = CGPointZero; // shut up compiler
// get the tile's gid at the touched location (adjusted to the tilemap's width/height)
KTTilemapLayer* backgroundLayer = [_tilemapVC.tilemap layerByName:@"floor"];
gid_t gid = [backgroundLayer tileGidAt:touchedTileCoord];
LOG_EXPR(gid);
// increase gid and do bounds check, to avoid creating an invalid gid
gid++;
if (gid > _tilemapVC.tilemap.highestGid)
{
gid = 0;
}
// set the new gid at the touched location, always setting the flip flag
[backgroundLayer setTileGidWithFlags:gid | KTTilemapTileVerticalFlip tileCoord:touchedTileCoord];
} /* touchBeganWithEvent */
-(void) sceneDidChange
{
// register touch delegate here, to make sure navigation controller's delegate is set first (workaround until delegates can have priority)
self.multiTouchController.enabled = YES;
self.multiTouchController.emulateTouchesWithMouse = YES;
[self.multiTouchController addDelegate:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment