Exemple #1
0
void MapScene::playermapvcollision(){
    //主角与地图竖直(下)图素的碰撞检测
	CCPoint playerpoint = gameplayer->getPosition();
	CCSize palyersize = gameplayer->getContentSize();
	CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
	int indexx,indexy;
	CCTMXLayer* layer = map->layerNamed("logic");
    //人物下边界
	indexx = (playerpoint.x) / map->getTileSize().width;
	indexy = map->getMapSize().height - (playerpoint.y) / map->getTileSize().height;
	CCPoint playerindex = ccp(indexx,indexy);
	int tilegid = layer->tileGIDAt(playerindex);
	if(tilegid > 0){
       CCDictionary *tiledic = map->propertiesForGID(tilegid);
       CCString *mvalue = (CCString *)tiledic->objectForKey("collion");
       int mv = mvalue->intValue();
	   if(mv == 1){
		  if(vmove < 0){
		     vmove = 0;
			 hmove = 0;
		     playerpoint.y = (map->getMapSize().height - indexy) * map->getTileSize().height;
		     gameplayer->setPosition(playerpoint);
		  }
		  return;
	   }
	}
	vmove -= 0.2;
}
Exemple #2
0
// on "init" you need to initialize your instance
bool MapScene::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    //初始化地图
	CCTMXTiledMap *map = CCTMXTiledMap::create("iso-test-zorder.tmx");
	addChild(map, 0, kTagTileMap);
	CCSize s = map->getContentSize();
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	////----UXLOG("ContentSize: %f, %f", s.width,s.height);
	map->setPosition(ccp(-s.width/2 + winSize.width/2,0));
	//初始化人物
	m_tamara = CCSprite::create("grossinis_sister1.png");
	map->addChild(m_tamara, map->getChildren()->count() );
	m_tamara->retain();
	int mapWidth = map->getMapSize().width * map->getTileSize().width;
	int mapHeight = map->getMapSize().height * map->getTileSize().height;
	m_tamara->setPosition(ccp( mapWidth/2,112));
	m_tamara->setAnchorPoint(ccp(0.5f,0));
	setTouchEnabled(true);
	scheduleUpdate();
	vmove = -1;
	hmove = -1;
	stepindex = -1;
	myastar = new Astar();
    return true;
}
Exemple #3
0
void MapScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator it = pTouches->begin();
    CCTouch* touch = (CCTouch*)(*it);

    CCPoint m_tBeginPos = touch->getLocationInView();    
    m_tBeginPos = CCDirector::sharedDirector()->convertToGL( m_tBeginPos );
    char mch[256];
	CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
	CCPoint mapp = map->getPosition();
    //获得触摸点位置在地图上的索引(行列)
	CCPoint aimmapindex = convertto2d(m_tBeginPos.x - mapp.x,m_tBeginPos.y - mapp.y);
	if(aimmapindex.x < 0 || aimmapindex.y < 0 || aimmapindex.x >= map->getMapSize().width || aimmapindex.y >= map->getMapSize().height)
	{
		return;
	}
	CCPoint herop = m_tamara->getPosition();
	CCPoint mapindex = convertto2d(herop.x,herop.y);
	CCTMXLayer* layer = map->layerNamed("grass");
	int tilegid = layer->tileGIDAt(ccp(aimmapindex.x,aimmapindex.y));
	CCDictionary *tiledic = map->propertiesForGID(tilegid);
	CCString *mvalue = (CCString *)tiledic->objectForKey("conflict");
	int mv = mvalue->intValue();
	if(mv == 1){
	   return;
	}
    //A星搜索
	path = myastar->findPath(mapindex.x,mapindex.y,aimmapindex.x,aimmapindex.y,map);
	stepindex = 1;
	smallstepindex = 0;
}
Exemple #4
0
CCPoint TileMap::toXY(cocos2d::CCPoint pt){
    CCTMXTiledMap *tileMap = this->getTileMap();
    CCPoint rt = ccp(pt.x*tileMap->getTileSize().width,
                     (tileMap->getMapSize().height*tileMap->getTileSize().height)-
                     (pt.y*tileMap->getTileSize().height));
    CCLOG("[INFO TileMap:32] ROWCOL TO XY (%f,%f)",rt.x,rt.y);
    return rt;
}
Exemple #5
0
CCPoint TileMap::toRowCol(cocos2d::CCPoint pt){
    CCTMXTiledMap *tileMap = this->getTileMap();
    int x = pt.x/tileMap->getTileSize().width;
    int y = ((tileMap->getMapSize().height*tileMap->getTileSize().height)-pt.y)/
    tileMap->getTileSize().height;
    CCLOG("[INFO TileMap:32] XY TO ROWCOL (%d,%d)",x,y);
    return ccp(x,y);
}
Exemple #6
0
void MapScene::update(float dt)
{
    //主角与地图的水平和数值碰撞检测
	playermaphcollision();
	playermapvcollision();
    //设置主角位置
	CCPoint playerpoint = gameplayer->getPosition();
	playerpoint.y += vmove;
    playerpoint.x += 1 * hmove;
	enemytick();
	gameplayer->setPosition(playerpoint);
    //如果主角不是受伤状态,检测是否与敌人碰撞
	if(! isreduce && iscollision(gameplayer,enemy)){
       //设置为受伤状态
	   CCActionInterval*  action = CCBlink::actionWithDuration(5, 10);
	   gameplayer->runAction(action);
	   schedule(schedule_selector(MapScene::resetreduce), 5.0f);
	   isreduce = true;
	   hmove = 0;
	}
    //地图随主角移动逻辑
	CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	int mapWidth = map->getMapSize().width * map->getTileSize().width;
	int mapHeight = map->getMapSize().height * map->getTileSize().height;
    //获得地图相对主角位置,并设置地图位置
	float deltax = playerpoint.x - winSize.width/2;
	float deltay = playerpoint.y - 32;
	if(-deltax > 0){
	   deltax = 0;
	}
	if(-deltax < -mapWidth + winSize.width){
	   deltax = mapWidth - winSize.width;
	}
	if(-deltay > 0){
	   deltay = 0;
	}
	if(-deltay < -mapHeight + winSize.height){
	   deltay = mapHeight - winSize.height;
	}
    map->setPosition(ccp(- deltax,-deltay));
}
Exemple #7
0
//坐标与地图位置的转换
CCPoint MapScene::convertto2d(float x,float y){
	CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
	int mapWidth = map->getMapSize().width * map->getTileSize().width;
	int mapHeight = map->getMapSize().height * map->getTileSize().height;
	double distanse;
	double sin1;
	double sin11;
	double sin22;
	double cos11;
	double cos1;
	int d2x;
	int d2y;
	double mystatic5 = sqrt(5.0);//«Û∏˘∫≈5
	double mystatic = 16 * mystatic5;//–°ÕºøÈ¿‚≥§
	//char mch[256];
	if(x > mapWidth/2){
	   distanse = sqrt((x - mapWidth/2) * (x - mapWidth/2) + (mapHeight - y) * (mapHeight - y));
	   sin1 = (mapHeight - y)/distanse;
	   cos1 = (x - mapWidth/2)/distanse;
	   sin11 = (sin1 * 2 - cos1) / mystatic5;
	   cos11 = (sin1 + cos1 * 2) / mystatic5;
	   d2y = distanse * 5 / 4 * sin11 / mystatic;
	   sin22 = (2 * sin1 + cos1) / mystatic5;
	   d2x = distanse * 5 / 4 * sin22 / mystatic;
	   return ccp(d2x,d2y);
	}else{
	   distanse = sqrt((mapWidth/2 - x) * (mapWidth/2 - x) + (mapHeight - y) * (mapHeight - y));
	   sin1 = (mapHeight - y)/distanse;
	   cos1 = (mapWidth/2 - x)/distanse;
	   sin11 = (sin1 * 2 - cos1) / mystatic5;
	   cos11 = (sin1 + cos1 * 2) / mystatic5;
	   d2x = distanse * 5 / 4 * sin11 / mystatic;
	   //sin22 = 4.0 * cos11 / 5 + 3.0 * sin11 / 5;
	   sin22 = (2 * sin1 + cos1) / mystatic5;
	   d2y = distanse * 5 / 4 * sin22 / mystatic;
	   return ccp(d2x,d2y);
	}
}
Exemple #8
0
void TileMap::initWithTileMap(int index, cocos2d::CCLayer *layer){
    CCString *mapName = CCString::createWithFormat("gameMap%d.tmx",index);
    if (mapName) {
        CCLOG("[Info TileMap:13] you success to laoad Map gameMap%d.tmx",index);
    }
    CCTMXTiledMap *tileMap = CCTMXTiledMap::create(mapName->getCString());
    layer->addChild(tileMap,-1,tilemap_tag);
    
    //获取行列数
    ROWS = tileMap->getMapSize().height;
    COLS = tileMap->getMapSize().width;
    
    this->setLayer(layer);
}
Exemple #9
0
// on "init" you need to initialize your instance
bool MapScene::init()
{
    //地图背景层
	CCSprite *bg = CCSprite::create("background.png");
	bg->setScale(1.5f);
	addChild(bg, 0);
    //地图初始化
	CCTMXTiledMap *map = CCTMXTiledMap::create("ortho-objects.tmx");
	addChild(map, 0, kTagTileMap);
	CCSize s = map->getContentSize();
	int mapWidth = map->getMapSize().width * map->getTileSize().width;
	int mapHeight = map->getMapSize().height * map->getTileSize().height;
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	map->setPosition(ccp(0,0));
    //初始化主角
	gameplayer = CCSprite::create("grossini.png");
	enemy = CCSprite::create("SpinningPeas.png");
	map->addChild(gameplayer, map->getChildren()->count() );
	map->addChild(enemy, map->getChildren()->count() );
    //初始化敌人
	enemy->setPosition(ccp(winSize.width/2 + 80,32));
	enemy->setAnchorPoint(ccp(0.5f,0));
	gameplayer->setPosition(ccp(winSize.width/2,32));
	gameplayer->setAnchorPoint(ccp(0.5f,0));
	setTouchEnabled(true);
	scheduleUpdate();
	hmove = 0;
	vmove = 0;
	enemymovetick = 0;
    //主角矩形区域大小
	ownsize = CCSizeMake(40,100);
    //敌人矩形区域大小
	othersize = CCSizeMake(32,32);
    //是否是受伤状态
	isreduce = false;
    return true;
}
Exemple #10
0
void MapScene::update(float dt)
{
	CCPoint herop = m_tamara->getPosition();
	CCPoint mapindex = convertto2d(herop.x,herop.y);
    //根据路径移动
	if(stepindex >= 1){
        int count = path->count();
	   if(smallstepindex == 0){
		   int ncol = ((AstarItem *)path->objectAtIndex(stepindex))->getcol();
		   int nrow = ((AstarItem *)path->objectAtIndex(stepindex))->getrow();
		   int pcol = ((AstarItem *)path->objectAtIndex(stepindex - 1))->getcol();
		   int prow = ((AstarItem *)path->objectAtIndex(stepindex - 1))->getrow();
		   if(pcol == ncol){
			   if(prow > nrow){
			      vmove = 2;
			   }else if(prow < nrow){
                  vmove = 3;
			   }else{
			      vmove = -1;
			   }
		   }else if(prow == nrow){
			   if(pcol > ncol){
			      vmove = 1;
			   }else if(pcol < ncol){
                  vmove = 0;
			   }else{
			      vmove = -1;
			   }
		   }else{
               if(prow < nrow){
                   if(pcol < ncol){
                       vmove = 6;
                   }else if(pcol > ncol){
                       vmove = 5;
                   }else{
                       vmove = -1;
                   }
			   }else if(prow > nrow){
                   if(pcol < ncol){
                       vmove = 4;
                   }else if(pcol > ncol){
                       vmove = 7;
                   }else{
                       vmove = -1;
                   }
			   }else{
                   vmove = -1;
			   }
		   }
	   }
	   if(vmove == 0){
	      herop.x += 1;
	      herop.y -= 0.5;
	   }else if(vmove == 1){
	      herop.x -= 1;
	      herop.y += 0.5;
	   }else if(vmove == 2){
	      herop.x += 1;
	      herop.y += 0.5;
	   }else if(vmove == 3){
	      herop.x -= 1;
	      herop.y -= 0.5;
	   }else if(vmove == 4){
	      herop.x += 2;
	   }else if(vmove == 5){
	      herop.x -= 2;
	   }else if(vmove == 6){
	      herop.y -= 1;
	   }else if(vmove == 7){
	      herop.y += 1;
	   }
	   smallstepindex ++;
	   if(smallstepindex >= 32){
	      smallstepindex = 0;
		  if(stepindex >= path->count() - 1){
		     stepindex = -1;
			 vmove = -1;
		  }else{
		     stepindex ++;
			 vmove = -1;
		  }
	   }
	}
	m_tamara->setPosition(herop);
    //地图随主角移动
	CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
	CCSize s = map->getContentSize();
	int mapWidth = map->getMapSize().width * map->getTileSize().width;
	int mapHeight = map->getMapSize().height * map->getTileSize().height;
	float deltax = herop.x - mapWidth/2;
	float deltay = herop.y - 112;
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    map->setPosition(ccp(-s.width/2 + winSize.width/2 - deltax,-deltay));
}