#include "HelloWorld.h"
USING_NS_CC;
CCScene* MyHelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
MyHelloWorld *layer = MyHelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool MyHelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
initdefalut();
initmap();
return true;
}
void MyHelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
void MyHelloWorld::initmap()
{
this->scheduleUpdate();
map=CCTMXTiledMap::create("my.tmx");
CCTMXObjectGroup* obj1= map->objectGroupNamed("object1");
wall=map->layerNamed("wall");
floor=map->layerNamed("floor");
meta=map->layerNamed("meta");
CCDictionary* dict=obj1->objectNamed("sprite");
float x=dict->valueForKey("x")->floatValue();
float y=dict->valueForKey("y")->floatValue();
float width=dict->valueForKey("width")->floatValue();
float height=dict->valueForKey("height")->floatValue();
int i=dict->valueForKey("key")->intValue();
sprite=CCSprite::create("Player.png");
sprite->retain();
sprite->setAnchorPoint(ccp(0,0));
sprite->setPosition(ccp(x,y));
this->addChild(map,10,1);
map->addChild(sprite,0);
CCLOG("%d",map->getChildren()->count());
CCLOG("%d",floor->getZOrder());CCLOG("%d",wall->getZOrder());
CCLOG("%d",meta->getZOrder());
CCLOG("%d",sprite->getZOrder());
CCLOG("%f.%f,%d",x,y,i);
CCLOG("%f.%f,%d",width,height,i);
}
void MyHelloWorld::initdefalut()
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(MyHelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 20);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "", 24);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
}
void MyHelloWorld::onEnter()
{
CCLayer::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
}
bool MyHelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint pointtemp= pTouch->getLocation(); //返回的是相当屏的本地坐标
CCPoint touchPoint= this->convertTouchToNodeSpace(pTouch); //返回的是相对layer
CCLOG("pointtemp:%f,%f",pointtemp.x,pointtemp.y);
CCLOG("touchPoint:%f,%f",touchPoint.x,touchPoint.y);
//
////图块的惟一标识
//
//CCPoint mapTieldPoint=getTileCoordinate(point);
//CCLOG("mapTieldPoint:%f,%f",mapTieldPoint.x,mapTieldPoint.y);
//CCSize contentSize=map->getContentSize();
//CCLOG("contentSize:%f,%f",contentSize.width,contentSize.height);
//unsigned int tieldGid=floor->tileGIDAt(mapTieldPoint);
//if(tieldGid!=0){
// CCDictionary* dict=wall->propertiesForGID(tieldGid);
// if(dict!=NULL){
// /*int keyvalue=dict->valueForKey("key")->intValue();
// CCLOG("keyvalue%d",keyvalue);
// if(keyvalue==100){*/
// return true;
// //}
// }
//}
CCPoint playerPoint = sprite->getPosition();
CCLOG("playerPoint:%f,%f",playerPoint.x,playerPoint.y);
CCPoint diff = ccpSub(touchPoint, playerPoint);
if (abs(diff.x) > abs(diff.y))
{
if (diff.x > 0)
{
playerPoint.x += map->getTileSize().width;
}
else
{
playerPoint.x -= map->getTileSize().width;
}
}
else
{
if (diff.y > 0 )
{
playerPoint.y += map->getTileSize().height;
}
else
{
playerPoint.y -= map->getTileSize().height;
}
}
setPlayerPosition(playerPoint);
setViewpointCenter(playerPoint);
return true;
}
MyHelloWorld::~MyHelloWorld()
{
sprite->release();
}
void MyHelloWorld::update(float)
{
/*CCLOG("update");
float x,y;
sprite->getPosition(&x,&y);
CCLOG("%f,%f",x,y);*/
}
/************************************************************************/
/* 參数是精灵的位置 */
/************************************************************************/
void MyHelloWorld::setViewpointCenter(CCPoint& position) {
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, map->getMapSize().width * map->getTileSize().width - winSize.width / 2);
y = MIN(y, map->getMapSize().height *map->getTileSize().height- winSize.height/2);
CCPoint actualPosition = ccp(x, y);
CCPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
CCPoint viewPoint = ccpSub(centerOfView, actualPosition);
CCLOG("viewPoint:%f,%f",viewPoint.x,viewPoint.y);
this->setPosition(viewPoint);
/*CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, map->getTileSize().width * map->getMapSize().height - winSize.width / 2);
y = MIN(y, map->getTileSize().height * map->getMapSize().height - winSize.height / 2);
CCPoint acutalPoint = ccp(x, y);
CCPoint centerOfView = ccp(winSize.width / 2, winSize.height / 2);
CCPoint ViewOfPoint = ccpSub(centerOfView, acutalPoint);
this->setPosition(ViewOfPoint);*/
}
CCPoint MyHelloWorld::getTileCoordinate(CCPoint& point){
float width=map->getTileSize().width;
float height=map->getTileSize().height;
CCLOG("getContentSize:%f",map->getContentSize().height);
CCPoint re=ccp((int)(point.x/width),(int)((map->getContentSize().height-point.y)/height));
return re;
}
/************************************************************************/
/* position 目标点 相对于layer的 */
/************************************************************************/
void MyHelloWorld::setPlayerPosition(cocos2d::CCPoint position)
{
CCPoint tileCoord2 = this->tileCoordForGid(position);
CCPoint tileCoord = this->getTileCoordinate(position);
CCLOG("tileCoord:%f,%f",tileCoord.x,tileCoord.y);
CCLOG("tileCoord2:%f,%f",tileCoord2.x,tileCoord2.y);
int theGid = meta->tileGIDAt(tileCoord);
if (theGid)
{
CCLOG("gid:%d",theGid);
CCDictionary* properties = map->propertiesForGID(theGid);
if (properties)
{
CCSprite* tempSprite=meta->tileAt(tileCoord);
CCLOG("%d,",tempSprite->getZOrder());
CCLOG("%d,",sprite->getZOrder());
//sprite->setZOrder(0);
//const CCString* str = properties->valueForKey("key");
//CCLOG("STR:%d",str->intValue());
//if (str && str->compare("200") == 0)
////if (str !=NULL)
//{
// return;
//}
/*const CCString* tempstr = properties->valueForKey("Eat");
if (tempstr && tempstr->compare("true") == 0)
{
_numCollected++;
_hud->numCollectedChanged(_numCollected);
_meta ->removeTileAt(tileCoord);
_foodLayer->removeTileAt(tileCoord);
}*/
}
}
//else{sprite->setZOrder(11);}
sprite->setPosition(position);
}
/************************************************************************/
/返回的要转成int */
/************************************************************************/
CCPoint MyHelloWorld::tileCoordForGid(CCPoint position)
{
int x = position.x / map->getTileSize().width;
CCLOG("tiled:%f",map->getMapSize().height * map->getTileSize().height);
int y = ((map->getMapSize().height * map->getTileSize().height) - position.y) / map->getTileSize().height ;
return ccp(x, y);
}