Exemplo n.º 1
0
    private void OccupyNewLocation(Vector3Int p_positionToOccupy)
    {
        Script_Tile currentlyOccupiedTile = _grid.AccessGridTile(_occupiedLocation.x, _occupiedLocation.z);

        currentlyOccupiedTile.SetOccupiedBySheep(false);

        Vector3Int  pos          = p_positionToOccupy;
        Script_Tile tileToOccupy = _grid.AccessGridTile(pos.x, pos.z);

        tileToOccupy.SetOccupiedBySheep(true);
        _occupiedLocation = pos;
    }
Exemplo n.º 2
0
    void InstantiateEntityAtRandomLocation <T> () where T : Script_IEntity
    {
        //While this does not have a 100% chance of success, seeing as there is a very small chance of not having any walkable tiles,
        //and we could also be randoming at positions that aren't walkable until we've tried 64 times, i deem it enough.
        //if this fails, we will have to place them on our own.
        int randomWalkablePositionX = Random.Range(0, width);
        int randomWalkablePositionZ = Random.Range(0, height);
        int attemptsToSpawn         = 64;
        int currentAttempts         = 0;

        while (_grid.AccessGridTile(randomWalkablePositionX, randomWalkablePositionZ).GetWalkable() == false || currentAttempts < attemptsToSpawn)
        {
            randomWalkablePositionX = Random.Range(0, width);
            randomWalkablePositionZ = Random.Range(0, height);
            currentAttempts++;
        }

        if (_grid.AccessGridTile(randomWalkablePositionX, randomWalkablePositionZ).GetWalkable() == true)
        {
            Vector3Int spawnLocation = new Vector3Int(randomWalkablePositionX, 0, randomWalkablePositionZ);

            if (typeof(T) == typeof(Script_StarChaser))
            {
                _chaser = new Script_StarChaser(this, _grid, spawnLocation);
                _entityList.Add(_chaser as Script_IEntity);
            }
            if (typeof(T) == typeof(Script_TradingPost))
            {
                _tradingPost = new Script_TradingPost(this, _grid, spawnLocation);
                _entityList.Add(_tradingPost as Script_IEntity);
            }
            if (typeof(T) == typeof(Script_FallenStar))
            {
                _fallenStar = new Script_FallenStar(this, _grid, spawnLocation);
                _entityList.Add(_fallenStar as Script_IEntity);
            }
            if (typeof(T) == typeof(Script_SpaceShip))
            {
                _spaceShip = new Script_SpaceShip(this, _grid, spawnLocation);
                _entityList.Add(_spaceShip as Script_IEntity);
            }
        }
    }
Exemplo n.º 3
0
    public override void DetermineIfDead()
    {
        if (_health <= 0)
        {
            _manager.DestroyMaterial(_objectMaterial);
            _manager.DestroyGameObject(_gameObject);
            _manager.RemoveEnemy(this);

            Vector3Int pos = _behaviourTree.GetBlackBoardElement <Vector3Int> (_locationFlag);
            _grid.AccessGridTile(pos.x, pos.z).SetOccupied(false);

            _behaviourTree.RemoveBlackboardElement(_locationFlag);
            _behaviourTree.RemoveBlackboardElement(_targetEntityFlag);
            _behaviourTree.RemoveBlackboardElement(_entityListFlag);
            _behaviourTree.RemoveBlackboardElement(_forwardSwingFlag);
            _behaviourTree.RemoveBlackboardElement(_getSpaceEntityListFlag);

            _behaviourTree = null;
        }
    }
Exemplo n.º 4
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);
    }
Exemplo n.º 5
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);
    }
Exemplo n.º 6
0
 public override NodeState RunNode(float p_delta)
 {
     _tile = _grid.AccessGridTile(_tree.GetBlackBoardElement <Vector3Int>(_flag).x, _tree.GetBlackBoardElement <Vector3Int>(_flag).z);
     _tile.SetOccupied(true);
     return(NodeState.Success);
 }
Exemplo n.º 7
0
    private List <Script_Tile> GetAvailableTilesSurroundingEntity(int p_range, Script_IEntity p_entity)
    {
        List <Script_Tile> surroundingTiles = new List <Script_Tile>();

        int xCurrent = p_entity.GetGridLocation().x;
        int zCurrent = p_entity.GetGridLocation().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++)
            {
                surroundingTiles.Add(_grid.AccessGridTile(x, z));
            }
        }
        return(surroundingTiles);
    }
Exemplo n.º 8
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);
                    }
                }
            }
        }
    }