Example #1
0
//触摸事件
bool GameScene::onTouchBegan(Touch * touch, Event * unused_event)
{
	//移除建塔面板
	if (this->getChildByTag(100) != nullptr)
	{
		this->removeChildByTag(100);
	}

	Vec2 now = touch->getLocation();
	log("touch %f %f", now.x, now.y);
	TMXTiledMap * ourMap = (TMXTiledMap*)this->getChildByTag(0);
	//row为横坐标,col为纵坐标
	this->nowRow = (int)(now.x / 56.9);
	this->nowCol = 10-(int)(now.y / 56.9);
	log("%d %d", nowRow, nowCol);
	//获取点击块id
	int touchID = ourMap->getLayer("Layer1")->getTileGIDAt(Vec2(nowRow, nowCol));
	log("touch ID %d", touchID);
	//初始化可放塔标记
	bool canTouch = false;
	//检查该块是否可以建塔
	if (!ourMap->getPropertiesForGID(touchID).isNull())
	{
		auto tileTemp = ourMap->getPropertiesForGID(touchID).asValueMap();
		if (!tileTemp.empty())
		{
			canTouch = true;
			log("canTouch");
		}
	}

	if (canTouch)
	{
		//可以建塔,弹出选择面板
		if(towerInfo[nowCol][nowRow])
		{
			//如果已经有塔,变卖或升级
		}
		else
		{
			addTDSelect(nowRow, 10 - nowCol);
		}
	}
	else
	{
		//不可建塔,弹出错误提示
		auto tips = Sprite::create("notips.png");
		tips->setPosition(Vec2(nowRow * 56.9+28.45, (10 - nowCol) * 56.9+28.45));
		this->addChild(tips);
		tips->runAction(
			Sequence::create(
				DelayTime::create(0.8),
				CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, tips)), NULL));
	}
	return true;
}
Example #2
0
bool MapLayer::onTouchBegan(Touch *touch, Event *unused_event)
{
    Point beginPos = touch->getLocationInView();
    beginPos = Director::getInstance()->convertToGL(beginPos);

    TMXTiledMap* map = (TMXTiledMap*)this->getChildByTag(kTagTileMap);

    // 获取触摸点在地图位置上的索引
    Point mapPos = map->getPosition();
    Point aimMapIndex = convertTo2d(Point(beginPos.x - mapPos.x, beginPos.y - mapPos.y));
    if(aimMapIndex.x < 0 || aimMapIndex.y < 0 || aimMapIndex.x >= map->getMapSize().width || aimMapIndex.y >= map->getMapSize().height) {
        // 超出地图边界
        return false;
    }

    Point herop = _tamara->getPosition();
    Point mapIndex = convertTo2d(herop);
    TMXLayer* layer = map->getLayer("grass");
    int tilegid = layer->getTileGIDAt(aimMapIndex);
    Value tileValue = map->getPropertiesForGID(tilegid);
    int touchValue = tileValue.asValueMap().at("conflict").asInt();
    if(touchValue == 1) {
        return false;
    }

    _path = _myAstar->findPath(mapIndex.x, mapIndex.y, aimMapIndex.x, aimMapIndex.y, map);
    if(nullptr != _path && _path->count() >= 2) {
        _stepIndex = 1;
    } else {
        _stepIndex = -1;
    }
    _smallStepIndex = 0;

    return true;
}
Example #3
0
void BattleLayer::_makeAStarData(){
    TMXTiledMap *tiledMap = TMXTiledMap::create("res/map/battle_map.tmx");
    TMXLayer *walkableLayer = tiledMap->getLayer("walkable");
    Size mapSize = walkableLayer->getLayerSize();
//    CCLOG("MapSize: (%f, %f)", mapSize.width, mapSize.height);
    
    AStarDataNode astarDataVec[20][30];
    
    for (int column = 0; column < _battleMapTileSize.width; ++column){
        for (int row = 0; row < _battleMapTileSize.height; ++row){
            Vec2 tileCoord = Vec2(column + 5, mapSize.height - (row + 5) - 1);
            int tileGID = walkableLayer->getTileGIDAt(tileCoord);
            
            if (tileGID > 0){
                Value value = tiledMap->getPropertiesForGID(tileGID);
                ValueMap valueMap = value.asValueMap();
                int walkable = valueMap["walkable"].asInt();
//                CCLOG("Column: %d, Row: %d, Walkable: %d", column, row, walkable);
                
                astarDataVec[column][row].column = column;
                astarDataVec[column][row].row = row;
                astarDataVec[column][row].walkable = (walkable == 0) ? false : true;
                
            }
        }
    }
    
    for (int column = 0; column < _battleMapTileSize.width; ++column){
        std::vector<AStarDataNode> oneList;
        std::vector<BattleElement *> oneBattleElementList;
        for (int row = 0; row < _battleMapTileSize.height; ++row){
            AStarDataNode astarDataNode = AStarDataNode(column, row, astarDataVec[column][row].walkable);
            oneList.push_back(astarDataNode);
            oneBattleElementList.push_back(nullptr);
        }
        astarData.push_back(oneList);
        battleElements.push_back(oneBattleElementList);
    }
    
//    for (int row = 0; row < _battleMapTileSize.height; ++row){
//        for (int column = 0; column < _battleMapTileSize.width; ++column){
//            printf("%d", astarData[column][row].walkable);
//        }
//        printf("\n");
//    }
}