PathNode* PathFinder::getOpenPathNodeForTile(Tile* aTile)
{
    //Get the tile index from the level for the tile pointer
    int tileIndex = m_Level->getTileIndexForTile(aTile);

    //Cycle through the open list and compare the tile indexes
    for(int i = 0; i < m_PathNodeOpen.size(); i++)
    {
        PathNode* pathNode = m_PathNodeOpen.at(i);
        if(m_Level->getTileIndexForTile(pathNode->getTile()) == tileIndex)
        {
            return pathNode;
        }
    }

    //The tile doesn't exist in the open list, return NULL
    return NULL;
}
bool PathFinder::doesTileExistInClosedList(Tile* aTile)
{
    //Get the tile index from the level for the tile pointer
    int tileIndex = m_Level->getTileIndexForTile(aTile);

    //Cycle through the closed list and compare the tile indexes
    for(int i = 0; i < m_PathNodeClosed.size(); i++)
    {
        PathNode* pathNode = m_PathNodeClosed.at(i);
        if(m_Level->getTileIndexForTile(pathNode->getTile()) == tileIndex)
        {
            return true;
        }
    }

    //The tile doesn't exist in the closed list
    return false;
}
PathNode* PathFinder::getPathNodeFromOpenList(Tile* aTile)
{
    //Get the tile's tile index.
    int tileIndex = m_Level->getTileIndexForTile(aTile);
    
    //Cycle through the open list and compare the tile indexes.
    for(int i = 0; i < m_OpenList.size(); i++)
    {
        PathNode* pathNode = m_OpenList.at(i);
        if(m_Level->getTileIndexForTile(pathNode->getTile()) == tileIndex)
        {
            return pathNode;
        }
    }
    
    //If we got here then the tile is not in the open list.
    return false;
}
bool PathFinder::isTileInClosedList(Tile* aTile)
{
    //Get the tile's tile index.
    int tileIndex = m_Level->getTileIndexForTile(aTile);
    
    //Cycle through the closed list and compare the tile indexes.
    for(int i = 0; i < m_ClosedList.size(); i++)
    {
        PathNode* pathNode = m_ClosedList.at(i);
        if(m_Level->getTileIndexForTile(pathNode->getTile()) == tileIndex)
        {
            return true;
        }
    }
    
    //If we got here then the tile is not in the closed list.
    return false;
}
Beispiel #5
0
void Player::update(double aDelta)
{
    std::string ammoCount = "";
    std::stringstream ammoAmount;
    ammoAmount << m_Ammo;
    ammoAmount >> ammoCount;
    m_Font->setText(ammoCount.c_str());


    //update the projectile
    for(int i = 0; i < m_Projectiles.size(); i++)
    {
        if(m_Projectiles.at(i)->getIsActive() == true)
        {
            m_Projectiles.at(i)->update(aDelta);

        }
    }
    //Tower1
    for(int i = 0; i < m_Level->getNumberOfTiles(); i++)
    {
        if(m_Level->getTileTypeForIndex(i) == TileTypeTower)
        {
            TowerTile* temp = (TowerTile*) m_Level->getTileForIndex(i);
            for(int location = 0; location < temp->getProjecticle().size(); location++)
            {
                if (temp->getProjecticle().at(location)->getIsActive())
                    temp->getProjecticle().at(location)->update(aDelta);
            }
        }
    }
    //remove aby inactive projectiles from the projectiles vectors
    int index = 0;
    while(index != m_Projectiles.size())
    {
        if(m_Projectiles.at(index)->getIsActive() == false)
        {

            //delete the projectile and remove it from the vector
            delete m_Projectiles.at(index);
            m_Projectiles.erase(m_Projectiles.begin() + index);
        }
        else
        {
            index++;
        }
    }

    if (m_PathFinder->isSearchingPath() == true)
    {
        m_PathFinder->update(aDelta);
    }

    if (isAnimating() && m_AnimationPathNodeIndex > -1)
    {
        PathNode* pathNode = m_PathFinder->getPathNodeAtIndex(m_AnimationPathNodeIndex);
        Tile* tile = pathNode != NULL ? pathNode->getTile() : NULL;

        if(tile)
        {
            float centerX = tile->getX() + (tile->getWidth() - getWidth()) / 2.0f;
            float centerY = tile->getY() + (tile->getHeight() - getHeight()) / 2.0f;
            Tile * playerTile = m_Level->getTileForPosition(getX(), getY());
            float speed = playerTile->getTileSpeed();

            float playerX = animate(getX(), centerX, aDelta, speed);
            float playerY = animate(getY(), centerY, aDelta, speed);
            setPosition(playerX, playerY);

            //change G as float for slower and faster tiles

            if (playerX == centerX && playerY == centerY)
            {
                m_AnimationPathNodeIndex++;
                m_CurrentTile->setIsPath(false);
                setCurrentTile(tile);
                if (m_AnimationPathNodeIndex >= m_PathFinder->getPathSize())
                {
                    stopAnimating();
                    m_CurrentTile->setIsPath(false);
                }

                if(m_AbortAnimation)
                {
                    m_AbortAnimation = false;

                    findPath();
                }
            }
            else
            {
                if(m_AbortAnimation == true)
                {
                    m_AbortAnimation =false;
                    findPath();
                }
            }
        }

    }
}
void PathFinder::update(double aDelta)
{
    if(m_SearchDelay > 0.0)
    {
        m_SearchDelay -= aDelta;
        if(m_SearchDelay <= 0.0)
        {
            m_SearchDelay = 0.0;
        }
        return;
    }


    while(isSearchingPath() == true && m_DestinationTileIndex != -1)
    {
        //Safety check that there is actually something in the openlist
        if(m_PathNodeOpen.size() == 0)
        {
            //set the state to error
            m_State = StateError;

            //notify the listener
            if(m_Listener != NULL)
            {
                m_Listener->pathFinderFinishedSearching(this, false);
            }
            return;
        }

        //Get the first node  with the lowest f score
        //it should be the first element in the open list
        PathNode* currentNode = m_PathNodeOpen.front();

        //add the node tot he closed list and remove it foem the openlist
        m_PathNodeClosed.push_back(currentNode);
        m_PathNodeOpen.erase(m_PathNodeOpen.begin());

        //check to see if the node is at the destination tile
        int currentNodeTileIndex = m_Level->getTileIndexForTile(currentNode->getTile());
        if(currentNodeTileIndex == m_DestinationTileIndex)
        {
            // build the final node path, this will use the current nodes parent node
            //to track back through the path nodes all the way back to the start
            buildFinalNodePath(currentNode);

            //set the state ti path found
            m_State = StateFoundPath;

            //notify the listener
            if(m_Listener != NULL)
            {
                m_Listener->pathFinderFinishedSearching(this,true);
            }
            return;
        }

        //if it is we stop searching and build our final path

        //if we get here that eamn we haven't reached out destination tile we need
        //to get the adjecent tiles from the node and add them to the open list
        std::vector<Tile*> adjacentTiles;

        //check the tile above, is it walkable
        addAdjacentTile(adjacentTiles, currentNode->getTile(), 0, -1);

        // check the tile below, is it walkable
        addAdjacentTile(adjacentTiles, currentNode->getTile(),0,1);

        //check the tile tot he left, is it walkable
        addAdjacentTile(adjacentTiles, currentNode->getTile(),-1,0);

        //check the tile to the right, is it walkable
        addAdjacentTile(adjacentTiles, currentNode->getTile(),1,0);

        //cycle throug the adjacent tiles that are walkable
        for(int i = 0; i < adjacentTiles.size(); i++)
        {
            Tile* adjacentTile = adjacentTiles.at(i);


            //does the tile exist in the closed list
            if(doesTileExistInClosedList(adjacentTile) == true)
            {

                //if it does disregard
                continue;
            }

            //does the tile exist in the open list
            if(doesTileExistInOpenList(adjacentTile) == false)
            {

                //if it doesn't creat a new path node for the tile
                PathNode* adjacentNode = new PathNode(adjacentTile);

                //set the parent node
                adjacentNode->setParentNode(currentNode);

                // calculate the g and h scores
                adjacentNode->setScoreG(currentNode->getScoreG() + adjacentTile->getMovemnetCost());
                Tile* destinationTile = m_Level->getTileForIndex(m_DestinationTileIndex);
                int scoreH = getManhattanDistanceCost(adjacentTile, destinationTile);
                adjacentNode->setScoreH(scoreH);

                // add the tile to the open list and sort it
                addPathNodeToOpenList(adjacentNode);
            }
            else
            {

                //if it does exist in the open list compare the score and
                //keep the one with the lower f score, whoch really means
                //keep the one with the lower g score, since the H score will be identical
                PathNode* existingNode = getOpenPathNodeForTile(adjacentTile);

                //if the tile has a lower f score, update the g score
                //and update the parent node
                if(currentNode->getScoreG() + adjacentTile->getMovemnetCost() < existingNode->getScoreG())
                {
                    existingNode->setScoreG(currentNode->getScoreG() + adjacentTile->getMovemnetCost());
                    existingNode->setParentNode(currentNode);

                    //re-sort the open list
                    sortOpenList();
                }


            }
        }
    }


    //If the search delay is enabled, set the delay timer
    if(m_EnableSearchDelay == true)
    {
        m_SearchDelay = PATH_FINDING_DELAY;
    }
}
void PathFinder::update(double aDelta)
{
    //Make sure we are in the seaching path state.
    while(isSearchingPath() == true)
    {
        //Next make sure there is at least 1 path node in the open list.
        //If there isn't it's possible that the destination can not be reached.
        if(m_OpenList.size() == 0)
        {
            m_State = StateError;
            return;
        }
        
        //Get the first path node from the Open List, because Open List is sorted,
        //we know this is the Path Node with the lowest F score.
        PathNode* currentNode = m_OpenList.front();
        
        //Next add it to the closed list.
        m_ClosedList.push_back(currentNode);
        
        //Lastly remove it from the Open List
        m_OpenList.erase(m_OpenList.begin());
        
        //Next get the current node's tile's index from the level.
        int currentNodeTileIndex = m_Level->getTileIndexForTile(currentNode->getTile());
        if(currentNodeTileIndex == m_DestinationTileIndex)
        {
            //If the current node's tile index is the same as the destination tile, the we
            //have reached out destination tile and now know the shortest path.
            
            do
            {
                //If the parent node doesn't equal NULL, and the current node to the final path list.
              if(currentNode->getParentNode() != NULL)
              {
                  m_FinalPath.insert(m_FinalPath.begin(), currentNode);
              }
                //Set the node's tile isPath flag to true
                currentNode->getTile()->setIsPath(true);
                
                //Set the current node to the parent node.
                currentNode = currentNode->getParentNode();
            }
            while(currentNode != NULL);
            
            //Set the state to the PathFound
            m_State = StatePathFound;
            
            return;
        }
        
        //Now, if we got here, it means we haven't reached the destination, we need to
        // get the current node's adjacent tiles and calculate their path scores.
        std::vector<Tile*> adjacentTiles;
        addAdjacentTile(adjacentTiles, currentNode->getTile(), -1, 0); //Left
        addAdjacentTile(adjacentTiles, currentNode->getTile(), 1, 0); //Right
        addAdjacentTile(adjacentTiles, currentNode->getTile(), 0, -1); //Up
        addAdjacentTile(adjacentTiles, currentNode->getTile(), 0, 1); //Down
        
        //Cycle through the adjacent tiles
        for(int i = 0; i < adjacentTiles.size(); i++)
        {
            Tile* adjacentTile = adjacentTiles.at(i);
            
            //Next we need to check if the adjacent tile is already in the close list,
            //if it is, then we can ignore this tile.
            if(isTileInClosedList(adjacentTile) == true)
            {
                continue;
            }
            
            //Then we need to check if the adjacent tile is already in the Open List
            if(isTileInOpenList(adjacentTile) == true)
            {
                //If we got here, then the tile is in the Open list alreadu and we have to determine
                //if the existing tile's score is lower or the adjacent tile.
                PathNode* existingNode = getPathNodeFromOpenList(adjacentTile);
                
                //Check to see if the adjacent node has a higher G Score
                //(since the h scores will be identical)
                //than the existing node's G score.
                
                if(existingNode->getScoreG() > currentNode->getScoreG() +1)
                {
                    //Update the existing node's parent.
                    existingNode->setParentNode(currentNode);
                    
                    //Set the lower G score
                    existingNode->setScoreG(currentNode->getScoreG() +1);
                    
                    //Sort the OpenList
                    sortOpenList();
                }
            }
            else
            {
               //If we got here then the tile doesn't exist in the Open or Closed list.
                //Let's make a new PathNode object and calculate it's tile score.
                PathNode* adjacentNode = new PathNode(adjacentTile);
                
                //Set the pathNode's parent.
                adjacentNode->setParentNode(currentNode);
                
                //Calculate the G score, it's a Parent node's G score plus 1.
                adjacentNode->setScoreG(currentNode->getScoreG() +1);
                
                //Calculate the H score, remember we use the manhattan distance method.
                adjacentNode->setScoreH(getManhattanDistanceCost(adjacentTile, m_Level->getTileForIndex(m_DestinationTileIndex)));
                
                //Lastly add is to the Open list, calling this method will sort the open list.
                addNodeToOpenList(adjacentNode);
                                    
            }
        }
    }
}