Example #1
0
    public override NodeState RunNode(float p_delta)
    {
        if (_entityToMove == null)
        {
            return(NodeState.Failed);
        }

        _targetEntity = _tree.GetBlackBoardElement <Script_IEntity>(_entityFlag);

        if (_targetEntity == null || _targetEntity.GetGameObject() == null)
        {
            return(NodeState.Failed);
        }


        int maxRange     = _grid.GetWidth() > _grid.GetHeight() ? _grid.GetWidth() : _grid.GetHeight();
        int currentRange = 1;

        List <Script_Tile> availableTiles = new List <Script_Tile> ();

        while (currentRange <= maxRange && availableTiles.Count == 0)
        {
            availableTiles = GetAvailableTilesSurroundingEntity(currentRange, _targetEntity);
        }

        if (availableTiles.Count == 0)
        {
            return(NodeState.Failed);
        }

        foreach (Script_Tile tile in availableTiles)
        {
            if (_entityToMove.GetGridLocation() == tile.GetGridPosition() || _entityToMove.GetGridLocation() == _targetEntity.GetGridLocation())
            {
                return(NodeState.Success);
            }
        }


        GameObject entityObject       = _entityToMove.GetGameObject();
        GameObject targetEntityObject = _targetEntity.GetGameObject();

        entityObject.transform.position += (targetEntityObject.transform.position - entityObject.transform.position).normalized * p_delta;
        return(NodeState.Running);
    }
    public override NodeState RunNode(float p_delta)
    {
        _targetEntity = _tree.GetBlackBoardElement <Script_IEntity>(_entityToGetLocationAround);

        if (_targetEntity == null)
        {
            return(NodeState.Failed);
        }

        if (_fromEntity == null)
        {
            return(NodeState.Failed);
        }


        int maxRange     = _grid.GetWidth() > _grid.GetHeight() ? _grid.GetWidth() : _grid.GetHeight();
        int currentRange = 1;

        List <Script_Tile> availableTiles = new List <Script_Tile> ();

        while (currentRange <= maxRange && availableTiles.Count == 0)
        {
            availableTiles = GetAvailableTilesSurroundingEntity(currentRange, _targetEntity);
            currentRange++;
        }

        if (availableTiles.Count == 0)
        {
            return(NodeState.Failed);
        }

        Script_Tile tileToMoveTowards = GetNearestAvailableTile(availableTiles);
        Vector3Int  location          = tileToMoveTowards.GetGridPosition();

        _tree.SetBlackboardElement(_locationToMoveTowardsFlag, location);

        return(NodeState.Success);
    }
Example #3
0
    public override NodeState RunNode(float p_delta)
    {
        Vector3Int gridPos = _entity.GetGridLocation();

        int x = Random.Range(gridPos.x - _range, gridPos.x + _range + 1);
        int z = Random.Range(gridPos.z - _range, gridPos.z + _range + 1);

        x = Mathf.Min(x, _grid.GetWidth() - 1);
        x = Mathf.Max(x, 0);

        z = Mathf.Min(z, _grid.GetHeight() - 1);
        z = Mathf.Max(z, 0);

        if (_grid.AccessGridTile(x, z).GetOccupied())
        {
            return(NodeState.Running);
        }

        _location = new Vector3Int(x, 0, z);

        _tree.SetBlackboardElement(_locationFlag, _location);
        return(NodeState.Success);
    }
Example #4
0
    public List <Script_Tile> GetTilesWithinRange(int p_range)
    {
        List <Script_Tile> tilesWithinRange = new List <Script_Tile>();
        int xCurrent = _gridXCoordinate;
        int zCurrent = _gridZCoordinate;


        int xMin = xCurrent;
        int xMax = xCurrent;
        int zMin = zCurrent;
        int zMax = zCurrent;

        for (int x = xCurrent - p_range; x <= xCurrent; x++)
        {
            if (x < 0)
            {
                continue;
            }

            xMin = x;
            break;
        }
        for (int x = xCurrent + p_range; x >= xCurrent; x--)
        {
            if (x >= _grid.GetWidth())
            {
                continue;
            }

            xMax = x;
            break;
        }
        for (int z = zCurrent - p_range; z <= zCurrent; z++)
        {
            if (z < 0)
            {
                continue;
            }

            zMin = z;
            break;
        }
        for (int z = zCurrent + p_range; z >= zCurrent; z--)
        {
            if (z >= _grid.GetHeight())
            {
                continue;
            }

            zMax = z;
            break;
        }

        for (int z = zMin; z <= zMax; z++)
        {
            for (int x = xMin; x <= xMax; x++)
            {
                Script_Tile tile = _grid.AccessGridTile(x, z);
                tilesWithinRange.Add(tile);
            }
        }

        return(tilesWithinRange);
    }
    private List <Script_IEntity> GetEntitiesWithinRange(int p_range, List <Script_IEntity> p_listOfEntities)
    {
        List <Script_IEntity> entitiesWithinRange = new List <Script_IEntity>();
        int xCurrent = (int)_location.x;
        int zCurrent = (int)_location.z;

        int xMin = xCurrent;
        int xMax = xCurrent;
        int zMin = zCurrent;
        int zMax = zCurrent;

        for (int x = xCurrent - p_range; x <= xCurrent; x++)
        {
            if (x < 0)
            {
                continue;
            }

            xMin = x;
            break;
        }
        for (int x = xCurrent + p_range; x >= xCurrent; x--)
        {
            if (x >= _grid.GetWidth())
            {
                continue;
            }

            xMax = x;
            break;
        }
        for (int z = zCurrent - p_range; z <= zCurrent; z++)
        {
            if (z < 0)
            {
                continue;
            }

            zMin = z;
            break;
        }
        for (int z = zCurrent + p_range; z >= zCurrent; z--)
        {
            if (z >= _grid.GetHeight())
            {
                continue;
            }

            zMax = z;
            break;
        }

        for (int z = zMin; z <= zMax; z++)
        {
            for (int x = xMin; x <= xMax; x++)
            {
                foreach (Script_IEntity entity in p_listOfEntities.ToList())
                {
                    if (entity.GetGridLocation().x == x && entity.GetGridLocation().z == z)
                    {
                        entitiesWithinRange.Add(entity);
                    }
                }
            }
        }

        return(entitiesWithinRange);
    }
Example #6
0
    private void CalculateNeighbours(Script_Node p_node)
    {
        int xCurrent = p_node.GetNodePosition().x;
        int zCurrent = p_node.GetNodePosition().z;

        int xMin = xCurrent;
        int xMax = xCurrent;
        int zMin = zCurrent;
        int zMax = zCurrent;

        for (int x = xCurrent - 1; x <= xCurrent; x++)
        {
            if (x < 0)
            {
                continue;
            }

            xMin = x;
            break;
        }
        for (int x = xCurrent + 1; x >= xCurrent; x--)
        {
            if (x >= _grid.GetWidth())
            {
                continue;
            }

            xMax = x;
            break;
        }
        for (int z = zCurrent - 1; z <= zCurrent; z++)
        {
            if (z < 0)
            {
                continue;
            }

            zMin = z;
            break;
        }
        for (int z = zCurrent + 1; z >= zCurrent; z--)
        {
            if (z >= _grid.GetHeight())
            {
                continue;
            }

            zMax = z;
            break;
        }

        for (int z = zMin; z <= zMax; z++)
        {
            for (int x = xMin; x <= xMax; x++)
            {
                if (_grid.AccessGridTile(x, z).GetWalkable() == true && !_closedDictionary.ContainsKey(z * _grid.GetWidth() + x))
                {
                    if ((x != xCurrent && z != zCurrent))
                    {
                        if (_grid.AccessGridTile(x, z + (zCurrent - z)) != null && _grid.AccessGridTile(x, z + (zCurrent - z)).GetWalkable() == false)
                        {
                            continue;
                        }

                        if (_grid.AccessGridTile(x + (xCurrent - x), z) != null && _grid.AccessGridTile(x + (xCurrent - x), z).GetWalkable() == false)
                        {
                            continue;
                        }
                    }

                    int tempGScore = 0;
                    if (x == xCurrent || z == zCurrent)
                    {
                        tempGScore = Constants.linearMovementCost;
                    }
                    else
                    {
                        tempGScore = Constants.diagonalMovementCost;
                    }

                    if (_openDictionary.ContainsKey(z * _grid.GetWidth() + x))
                    {
                        Script_Node node = GetOpenListElement(x, z);

                        if (node.GetParent() != null)
                        {
                            tempGScore += p_node.GetGScore();
                        }

                        if (tempGScore < node.GetGScore())
                        {
                            node.SetGScore(tempGScore);
                            node.CalculateFScore();
                            node.SetParent(p_node);
                        }
                    }


                    if (!_openDictionary.ContainsKey(z * _grid.GetWidth() + x))
                    {
                        Vector3Int location = new Vector3Int(x, 0, z);

                        Script_Node newNode = new Script_Node(location, p_node.GetNodeDestination(), p_node);
                        tempGScore += newNode.GetParent().GetGScore();

                        newNode.SetGScore(tempGScore);
                        newNode.CalculateHScore();
                        newNode.CalculateFScore();
                        AddOpenListElementAtPosition(x, z, newNode);
                    }
                }
            }
        }
    }
Example #7
0
    private bool IsEntityWithinRange(int p_range, Script_IEntity p_entity)
    {
        int xCurrent = (int)_location.x;
        int zCurrent = (int)_location.z;

        int xMin = xCurrent;
        int xMax = xCurrent;
        int zMin = zCurrent;
        int zMax = zCurrent;

        for (int x = xCurrent - p_range; x <= xCurrent; x++)
        {
            if (x < 0)
            {
                continue;
            }

            xMin = x;
            break;
        }
        for (int x = xCurrent + p_range; x >= xCurrent; x--)
        {
            if (x >= _grid.GetWidth())
            {
                continue;
            }

            xMax = x;
            break;
        }
        for (int z = zCurrent - p_range; z <= zCurrent; z++)
        {
            if (z < 0)
            {
                continue;
            }

            zMin = z;
            break;
        }
        for (int z = zCurrent + p_range; z >= zCurrent; z--)
        {
            if (z >= _grid.GetHeight())
            {
                continue;
            }

            zMax = z;
            break;
        }

        for (int z = zMin; z <= zMax; z++)
        {
            for (int x = xMin; x <= xMax; x++)
            {
                if (p_entity.GetGridLocation().x == x && p_entity.GetGridLocation().z == z)
                {
                    return(true);
                }
            }
        }

        return(false);
    }