Skip to content

Instantly share code, notes, and snippets.

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 shinriyo/3859151 to your computer and use it in GitHub Desktop.
Save shinriyo/3859151 to your computer and use it in GitHub Desktop.
//
// HelloWorldScene.cpp
// TileBasedGame
//
// Created by Clawoo on 8/17/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::node();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::node();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
HelloWorld::~HelloWorld()
{
CC_SAFE_RELEASE_NULL(_tileMap);
CC_SAFE_RELEASE_NULL(_background);
CC_SAFE_RELEASE_NULL(_player);
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
CCLOG("init");
if ( !CCLayer::init() )
{
return false;
}
// Inside init method
// cpp with cocos2d-x
this->setIsTouchEnabled(true);
_tileMap = CCTMXTiledMap::tiledMapWithTMXFile("TileMap.tmx");
_tileMap->retain();
_background = _tileMap->layerNamed("Background");
_background->retain();
CCTMXObjectGroup *objects = _tileMap->objectGroupNamed("Objects");
CCAssert(objects != NULL, "'Objects' object group not found");
CCStringToStringDictionary *spawnPoint = objects->objectNamed("SpawnPoint");
CCAssert(spawnPoint != NULL, "SpawnPoint object not found");
int x = spawnPoint->objectForKey("x")->toInt();
int y = spawnPoint->objectForKey("y")->toInt();
_player = CCSprite::spriteWithFile("Player.png");
_player->retain();
_player->setPosition(ccp (x, y));
this->addChild(_tileMap);
this->addChild(_player);
this->setViewpointCenter(_player->getPosition());
// これをつけて後ろ側にする
// _tileMap->setVertexZ(-1);
return true;
}
void HelloWorld::setViewpointCenter(CCPoint position)
{
CCLOG("setViewpointCenter");
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, (_tileMap->getMapSize().width * _tileMap->getTileSize().width) - winSize.width/2);
x = MIN(x, (_tileMap->getMapSize().height * _tileMap->getTileSize().height) - winSize.height/2);
CCPoint actualPosition = ccp(x, y);
CCPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
CCPoint viewPoint = ccpSub(centerOfView, actualPosition);
this->setPosition(viewPoint);
}
// 追加
void HelloWorld::registerWithTouchDispatcher()
{
// 2.0で非推奨
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
//CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
return true;
}
void HelloWorld::setPlayerPosition(cocos2d::CCPoint position)
{
_player->setPosition(position);
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *touch, CCEvent *event)
{
CCPoint touchLocation = touch->locationInView(touch->view());
touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
touchLocation = this->convertToNodeSpace(touchLocation);
CCPoint playerPos = _player->getPosition();
CCPoint diff = ccpSub(touchLocation, playerPos);
if (::abs(diff.x) > ::abs(diff.y)) {
if (diff.x > 0) {
playerPos.x += _tileMap->getTileSize().width;
} else {
playerPos.x -= _tileMap->getTileSize().width;
}
} else {
if (diff.y > 0) {
playerPos.y += _tileMap->getTileSize().height;
} else {
playerPos.y -= _tileMap->getTileSize().height;
}
}
if (playerPos.x <= (_tileMap->getMapSize().width * _tileMap->getTileSize().width) &&
playerPos.y <= (_tileMap->getMapSize().height * _tileMap->getTileSize().height) &&
playerPos.y >= 0 &&
playerPos.x >= 0 )
{
this->setPlayerPosition(playerPos);
}
this->setViewpointCenter(_player->getPosition());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment