Esempio n. 1
0
void AStarLayer::onEnter(){
    Layer::onEnter();
    
    _addCheckNodeListener = EventListenerCustom::create("AddCheckNode", [&](EventCustom *eventCustom){
        
        AStarDataNode *userData = static_cast<AStarDataNode *>(eventCustom->getUserData());
        Sprite *checkNode = Sprite::create("res/astar_check.png");
        checkNode->setAnchorPoint(Vec2(0, 0));
        checkNode->setPosition(userData->column * 20, userData->row * 20);
        this->addChild(checkNode, 5);
        
    });
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_addCheckNodeListener, this);
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
    AStarDataNode beginNode = AStarDataNode(5, 5, true);
    AStarDataNode targetNode = AStarDataNode(90, 50, true);
    
    Sprite *beginNodeSprite = Sprite::create("res/astar_pathfinder.png");
    beginNodeSprite->setAnchorPoint(Vec2(0, 0));
    beginNodeSprite->setPosition(beginNode.column * 20, beginNode.row * 20);
    this->addChild(beginNodeSprite, 5);
    Sprite *targetNodeSprite = Sprite::create("res/astar_pathfinder.png");
    targetNodeSprite->setAnchorPoint(Vec2(0, 0));
    targetNodeSprite->setPosition(targetNode.column * 20, targetNode.row * 20);
    this->addChild(targetNodeSprite, 10);
    
    Label *starFindPathMenuItemLabel = Label::createWithSystemFont("开始", "Arial", 50);
    starFindPathMenuItemLabel->setColor(Color3B(0, 0, 0));
    MenuItemLabel *starFindPathMenuItem = MenuItemLabel::create(starFindPathMenuItemLabel, [&, beginNode, targetNode](Ref *sender){
        CCLOG("开始寻路");
        
        startTime = clock();
        
        std::vector<AStarDataNode> path = aStar(astarData, beginNode, targetNode);
        
        _calculateAStarTime();
        
        
        
        CCLOG("Path: %d", static_cast<int>(path.size()));
        for (std::vector<AStarDataNode>::iterator iter = path.begin(); iter != path.end(); ++iter){
            //        CCLOG("(%d, %d)", static_cast<int>(iter->column), static_cast<int>(iter->row));
            Sprite *path = Sprite::create("res/astar_pathfinder.png");
            path->setAnchorPoint(Vec2(0, 0));
            path->setPosition(iter->column * 20, iter->row * 20);
            this->addChild(path, 5);
        }
        
    });
    Menu *starFindPathMenu = Menu::create(starFindPathMenuItem, NULL);
    starFindPathMenu->setPosition(visibleSize.width - 100, 50);
    this->addChild(starFindPathMenu, 100);
    
}
Esempio n. 2
0
void AStarLayer::_makeWableableData(){
    
    AStarDataNode astarDataArray[100][60];
    
    for (int column = 0; column < mapSize.width; ++column){
        for (int row = 0; row < mapSize.height; ++row){
            Vec2 tileCoord = Vec2(static_cast<int>(column), static_cast<int>(mapSize.height - row - 1));
            int tileGID = _walkableLayer->getTileGIDAt(tileCoord);
//            CCLOG("TileGID:%d, Coord:(%f, %f)", tileGID, tileCoord.x, tileCoord.y);
            if (tileGID > 0){
                
                Value value = _tiledMap->getPropertiesForGID(tileGID);
                ValueMap valueMap = value.asValueMap();
                int isWalkable = valueMap["walkable"].asInt();
                
//                CCLOG("TileGID:%d, Coord:(%f, %f), IsWalkable:%d", tileGID, tileCoord.x, tileCoord.y, isWalkable);
                int x = static_cast<int>(tileCoord.x);
                int y = 59 - static_cast<int>(tileCoord.y);
                
                astarDataArray[x][y].column = x;
                astarDataArray[x][y].row = y;
                astarDataArray[x][y].walkable = isWalkable;
                
            }
            
        }
    }
    
//    CCLOG("AStarDataNodeArray:");
    for (int i = 0; i < 100; ++i){
        std::vector<AStarDataNode> oneList;
        for (int j = 0; j < 60; ++j){
//            printf("%d", astarDataArray[i][j].walkable);
//            printf("(%d, %d, %d)", astarDataArray[i][j].column, astarDataArray[i][j].row, astarDataArray[i][j].walkable);
            
            AStarDataNode astarDataNode = AStarDataNode(i, j, astarDataArray[i][j].walkable);
            oneList.push_back(astarDataNode);
        }
        astarData.push_back(oneList);
//        printf("\n");
    }
    
//    CCLOG("AstarDataNodeVector:");
//    for (int i = 0; i < 100; ++i){
//        for (int j = 0; j < 60; ++j){
//            printf("%d", astarData[i][j].walkable);
////            printf("i:%d, Col:%d j:%d, Row:%d\n", i, astarData[i][j].column, j, astarData[i][j].row);
//        }
//        printf("\n");
//    }
    
}
Esempio n. 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");
//    }
}