void BattleLayer::_dealMap(){ Size visibleSize = Director::getInstance()->getVisibleSize(); _battleMapBackground = LayerColor::create(Color4B(0, 0, 0, 255), visibleSize.width, visibleSize.height); _battleMapBackground->setPosition(0, 0); this->addChild(_battleMapBackground, 0); TMXTiledMap *tiledMap = TMXTiledMap::create("res/map/battle_map.tmx"); TMXLayer *backgroundLayer = tiledMap->getLayer("background"); TMXLayer *walkableLayer = tiledMap->getLayer("walkable"); walkableLayer->setAnchorPoint(Vec2(0.5, 0.5)); walkableLayer->setPosition(visibleSize.width / 2, visibleSize.height / 2 + 90); _battleMapBackground->addChild(walkableLayer, 1); backgroundLayer->setAnchorPoint(Vec2(0.5, 0.5)); backgroundLayer->setPosition(visibleSize.width / 2, visibleSize.height / 2 + 90); _battleMapBackground->addChild(backgroundLayer, 1); Size mapSize = walkableLayer->getLayerSize(); _battleMapTileSize = Size(20, 30); _battleTileSize = walkableLayer->getMapTileSize(); _battleMapSize = Size(20 * _battleTileSize.width, 30 * _battleTileSize.height); _battleMap = LayerColor::create(Color4B(255, 255, 255, 150), _battleMapSize.width, _battleMapSize.height); _battleMap->ignoreAnchorPointForPosition(false); _battleMap->setAnchorPoint(Vec2(0.5, 0.5)); _battleMap->setPosition(visibleSize.width / 2, visibleSize.height / 2 + 90); _battleMapBackground->addChild(_battleMap, 5); }
bool BaseLayer::initTileMap(int ID)//初始化地图 { bool bRet = false; do { m_tileParallaxNode = TileParallaxNode::createWithID(ID); CCAssert(m_tileParallaxNode != NULL, "TileParallaxNode cannot be NULL"); CC_BREAK_IF(!m_tileParallaxNode); m_tileParallaxNode->retain(); addChild(m_tileParallaxNode, 0, -6); TMXTiledMap* pTileMap = m_tileParallaxNode->getTileMap(); TMXLayer *tmxLayer; tmxLayer = pTileMap->getLayer("ObjectsFar"); CCAssert(tmxLayer != NULL, "ObjectsFar layer not found!"); tmxLayer->retain(); tmxLayer->removeFromParentAndCleanup(false); m_objectsFar->addChild(tmxLayer, 0, OBJECT_FAR_TAG); addChild(m_objectsFar); tmxLayer->release(); tmxLayer = pTileMap->getLayer("ObjectsMiddle"); CCAssert(tmxLayer != NULL, "ObjectsMiddle layer not found!"); tmxLayer->retain(); tmxLayer->removeFromParentAndCleanup(false); m_objectsMiddle->addChild(tmxLayer); tmxLayer->setTag(OBJECT_MIDDLE_TAG); addChild(m_objectsMiddle, CaculateObjectMiddleOrder(),OBJECT_MIDDLE_TAG); //reorderTMXLayer(tmxLayer);//这样渲染批次有点高 tmxLayer->release(); tmxLayer = pTileMap->getLayer("ObjectsNear"); CCAssert(tmxLayer != NULL, "ObjectsNear layer not found!"); tmxLayer->retain(); tmxLayer->removeFromParentAndCleanup(false); m_objectsNear->addChild(tmxLayer,0,OBJECT_NEAR_TAG); addChild(m_objectsNear); tmxLayer->release(); reorderChild(m_objectsFar, 0);//reorder the layer reorderChild(m_objectsNear, DataManager::getInstance()->getMaxZorder() + 1); bRet = true; } while (0); return bRet; }
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; }
//触摸事件 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; }
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"); // } }