当前位置: 首页>>代码示例>>C++>>正文


C++ PathNode::getCostG方法代码示例

本文整理汇总了C++中PathNode::getCostG方法的典型用法代码示例。如果您正苦于以下问题:C++ PathNode::getCostG方法的具体用法?C++ PathNode::getCostG怎么用?C++ PathNode::getCostG使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PathNode的用法示例。


在下文中一共展示了PathNode::getCostG方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: search

void PathCmp::search (Eigen::Vector2f targetPos_) {
    _level = LogicSystem::getInstance()->getLevel();
    for (auto it = _openList.begin(); it != _openList.end(); ++ it) {
        if (*it != nullptr)
            delete *it;
    }
    _openList.clear();

    for (auto it = _closedList.begin(); it != _closedList.end(); ++ it) {
        if (*it != nullptr)
            delete *it;
    }
    _closedList.clear();
    _shortestPath.clear();

    auto posCmp = getEntity()->GET_CMP(PositionComponent);
    _srcIdx = _level->posToTileIndex(posCmp->getPosition());
    _tarIdx = _level->posToTileIndex(targetPos_);
    if (_srcIdx == _tarIdx)
        return;

    if (_level->isTileObstacle(_tarIdx.x(), _tarIdx.y()))
        return;

    PathNode* srcNode = new PathNode(_srcIdx);
    calcCostH(srcNode);
    _openList.push_back(srcNode);
    do {
        PathNode* curNode = _openList[0];
        _closedList.push_back(curNode);
        _openList.erase(_openList.begin());

        if (curNode->getIndex() == _tarIdx) {
            PathNode* walkNode = curNode;
            _shortestPath.clear();
            do {
                Eigen::Vector2f pos;
                pos.x() = _level->tileIndexXToPosX(walkNode->getIndex().x());
                pos.y() = _level->tileIndexYToPosY(walkNode->getIndex().y());
                _shortestPath.push_front(pos);
                walkNode = walkNode->getParent();
            } while (walkNode);
            for (auto it = _openList.begin(); it != _openList.end(); ++ it) {
                if (*it != nullptr)
                    delete *it;
            }
            _openList.clear();
            for (auto it = _closedList.begin(); it != _closedList.end(); ++ it) {
                if (*it != nullptr)
                    delete *it;
            }
            _closedList.clear();
            break;
        }

        putAdjacentTiles(curNode);
        for (auto it = _adjacentTiles.begin(); it != _adjacentTiles.end(); ++ it) {
            bool isInClosedList = false;
            for (int i = 0; i < _closedList.size(); ++ i)
                if ((*it) == _closedList[i]->getIndex())
                    isInClosedList = true;
            if (isInClosedList) continue;

            auto node = new PathNode(*it);
            int costGToAdd = calcCostG(node, curNode);
            std::vector<PathNode *>::iterator opit = _openList.begin();
            while (opit != _openList.end()) {
                if ((*opit)->getIndex() == node->getIndex())
                    break;
                ++ opit;
            }

            // already on openlist
            if (opit!= _openList.end()) {
                delete node;
                node = (*opit);
                if (curNode->getCostG() + costGToAdd < node->getCostG())
                    node->setCostG(curNode->getCostG() + costGToAdd);
            } else {
                node->setParent(curNode);
                node->setCostG(curNode->getCostG() + costGToAdd);
                calcCostH(node);
                addOpenList(node);
            }
        }
    } while (_openList.size() > 0);
}
开发者ID:sangdaekim,项目名称:greetings,代码行数:87,代码来源:pathCmp.cpp


注:本文中的PathNode::getCostG方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。