Пример #1
0
    private bool DecideToReproduce()
    {
        if (_surroundingTiles != null && _health >= _healthNeededToReproduce)
        {
            List <Script_Tile> surroundingTilesExcludingPosition = _surroundingTiles;
            Script_Tile        tileToRemove = null;

            foreach (Script_Tile tile in _surroundingTiles)
            {
                if (GetCurrentGridPosition() == tile.GetPositionAsInt())
                {
                    tileToRemove = tile;
                }
            }
            if (tileToRemove != null)
            {
                surroundingTilesExcludingPosition.Remove(tileToRemove);
            }

            List <Script_Tile> availableTiles = new List <Script_Tile> ();
            foreach (Script_Tile tile in surroundingTilesExcludingPosition)
            {
                if (tile.GetOccupiedByWolf() == false)
                {
                    availableTiles.Add(tile);
                }
            }
            if (availableTiles.Count > 0)
            {
                return(true);
            }
        }
        return(false);
    }
Пример #2
0
    public Script_Sheep(Vector3 p_position, Script_GameManager p_gameManager, Script_Grid p_grid, Quaternion p_rotation)
    {
        _sheepObject      = GameObject.CreatePrimitive(PrimitiveType.Cube);
        _sheepObject.name = "SheepObject";
        _sheepObject.transform.position = p_position;
        _sheepObject.transform.rotation = p_rotation;
        _material       = _sheepObject.GetComponent <Renderer>().material;
        _material.color = Color.white;

        _evasionSpeed    = 0.066f;
        _speed           = 0.033f;
        _movementDecay   = 0.01f;
        _actionDecay     = 0.1f;
        _size            = 0.5f;
        _grassBeingEaten = null;
        _maxHealth       = 10.0f;
        _health          = 5.0f;

        _decidedAction     = DecidedAction.Wandering;
        _actDelegate       = null;
        _tilesWithinRange  = new List <Script_Tile> ();
        _grassWithinRange  = new List <Script_Tile> ();
        _surroundingTiles  = new List <Script_Tile> ();
        _wolvesWithinRange = new List <Script_Wolf> ();
        _grid = p_grid;


        _occupiedLocation = GetCurrentGridPosition();
        _targetLocation   = GetCurrentGridPosition();
        OccupyNewLocation(GetCurrentGridPosition());
    }
Пример #3
0
    private Vector3Int DecideReproductionTile()
    {
        List <Script_Tile> tilesWithinRangeExcludingPosition = _surroundingTiles;
        Script_Tile        tileToRemove = null;

        foreach (Script_Tile tile in _surroundingTiles)
        {
            if (GetCurrentGridPosition() == tile.GetPositionAsInt())
            {
                tileToRemove = tile;
            }
        }
        if (tileToRemove != null)
        {
            tilesWithinRangeExcludingPosition.Remove(tileToRemove);
        }

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

        foreach (Script_Tile tile in tilesWithinRangeExcludingPosition)
        {
            if (tile.GetOccupiedByWolf() == false)
            {
                availableTiles.Add(tile);
            }
        }
        int randomNumber = Random.Range(0, availableTiles.Count - 1);

        if (availableTiles.Count > 0)
        {
            return(availableTiles [randomNumber].GetPositionAsInt());
        }
        return(Vector3Int.zero);
    }
Пример #4
0
    public void InstantiateGrid(GameObject p_tileObject, int p_width, int p_height, float p_yOffset, Quaternion p_rotation)
    {
        _width  = p_width;
        _height = p_height;

        System.Array.Resize(ref _grid, p_width * p_height);


        for (int z = 0; z < _width; z++)
        {
            for (int x = 0; x < _height; x++)
            {
                if (Random.Range(0.0f, 10.0f) > 6.0f)
                {
                    Script_Tile myTile = new Script_Tile(this, GrassStates.Grass, x, (int)p_yOffset, z, p_rotation);
                    SetGridTile(x, z, myTile);
                }
                else
                {
                    Script_Tile myTile = new Script_Tile(this, GrassStates.Dirt, x, (int)p_yOffset, z, p_rotation);
                    SetGridTile(x, z, myTile);
                }
            }
        }
    }
Пример #5
0
    public static GameObject[,] tiles = new GameObject[rows, cols]; // Array of all tiles


    void Start()
    {
        GenerateTerrain(); // Generate all the tiles
        first_Forest = tiles[Random.Range(0, rows), Random.Range(0, cols)];
        Script_Tile script_Generate = first_Forest.GetComponent <Script_Tile>();

        script_Generate.GenerateForest();// Generate the first forest
    }
Пример #6
0
    private void OccupyNewLocation(Vector3Int p_positionToOccupy)
    {
        Script_Tile currentlyOccupiedTile = _grid.AccessGridTile(_occupiedLocation.x, _occupiedLocation.z);

        currentlyOccupiedTile.SetOccupiedByWolf(false);

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

        tileToOccupy.SetOccupiedByWolf(true);
        _occupiedLocation = pos;
    }
Пример #7
0
    public void Decide()
    {
        _grassBeingEaten = null;
        if (DecideToDie())
        {
            _decidedAction = DecidedAction.dying;
            _actDelegate   = new ActDelegate(Die);
        }

        if (DecideToEvade() && !DecideToDie())
        {
            if (AtTargetLocation() || _decidedAction != DecidedAction.Evading)
            {
                DecideDirectionToEvade();
            }
            _decidedAction = DecidedAction.Evading;
            _actDelegate   = new ActDelegate(EvadeToLocation);
        }

        else if (DecideToReproduce() && !DecideToDie() && !DecideToEvade())
        {
            _reproductionLocation = DecideReproductionTile();

            Script_Tile currentlyOccupiedTile = _grid.AccessGridTile(_reproductionLocation.x, _reproductionLocation.z);
            currentlyOccupiedTile.SetOccupiedBySheep(false);
            _decidedAction = DecidedAction.Reproducing;
            _actDelegate   = new ActDelegate(Reproduce);
        }
        else if (DecideToEatGrass() && !DecideToDie() && !DecideToReproduce() && !DecideToEvade())
        {
            OccupyNewLocation(GetCurrentGridPosition());
            _decidedAction = DecidedAction.Eating;
            _actDelegate   = new ActDelegate(EatGrass);
        }
        else if (DecideToSeekGrass() && !DecideToEatGrass() && !DecideToReproduce() && !DecideToDie() && !DecideToEvade())
        {
            if (AtTargetLocation() || _decidedAction != DecidedAction.Seeking)
            {
                DecideGrassToSeek();
            }
            _decidedAction = DecidedAction.Seeking;
            _actDelegate   = new ActDelegate(MoveToLocation);
        }
        else if (DecideToWander() && !DecideToSeekGrass() && !DecideToEatGrass() && !DecideToReproduce() && !DecideToDie() && !DecideToEvade())
        {
            if (AtTargetLocation() || _decidedAction != DecidedAction.Wandering)
            {
                DecideWanderingLocation();
            }
            _decidedAction = DecidedAction.Wandering;
            _actDelegate   = new ActDelegate(MoveToLocation);
        }
    }
Пример #8
0
    private void Die()
    {
        Vector3Int position = GetCurrentGridPosition();

        _grid.AccessGridTile(position.x, position.z).NourishGrass();

        Script_Tile currentlyOccupiedTile = _grid.AccessGridTile(_occupiedLocation.x, _occupiedLocation.z);

        currentlyOccupiedTile.SetOccupiedByWolf(false);

        _grid.DestroyWolf(this, _material);
        _wolfObject = null;
    }
    private Script_Tile GetNearestAvailableTile(List <Script_Tile> p_tileList)
    {
        float       distance    = float.MaxValue;
        Script_Tile nearestTile = null;

        foreach (Script_Tile tile in p_tileList)
        {
            if (distance > Vector3Int.Distance(tile.GetGridPosition(), _fromEntity.GetGridLocation()))
            {
                distance    = Vector3Int.Distance(tile.GetGridPosition(), _fromEntity.GetGridLocation());
                nearestTile = tile;
            }
        }
        return(nearestTile);
    }
Пример #10
0
    void Update()
    {
        if (goTree != null) // Check if the tile has a tree
        {
            if (forest == true)
            {
                GenerateForest();
            }
            elapsedTime += Time.deltaTime;

            if (elapsedTime > timeLimit)     // Check the time
            {
                elapsedTime = 0;
                Script_Tree goTree_Script = goTree.GetComponent <Script_Tree>();

                Collider[] hitColliders = Physics.OverlapSphere(transform.position, 1.5f); //get every tile in range of the main tile

                for (int i = 0; i < hitColliders.Length; i++)                              // For every tile in range
                {
                    if (goTree_Script.state == "Alive")
                    {
                        if (hitColliders[i].tag == "Ground")                                                    // Check for grow
                        {
                            Script_Tile other_Script = hitColliders[i].gameObject.GetComponent <Script_Tile>(); // Get the Script
                            if (other_Script.goTree == null)                                                    // Check that it has no tree
                            {
                                other_Script.GenerateTree(false);
                            }
                        }
                    }



                    if (goTree_Script.infectionStage > 0)
                    {
                        if (hitColliders[i].tag == "Tree")
                        {
                            Script_Tree other_Script = hitColliders[i].gameObject.GetComponent <Script_Tree>(); // Get the Script
                            if (other_Script.infectionStage == 0)                                               // Check that it has no tree
                            {
                                other_Script.Resist();
                            }
                        }
                    }
                }
            }
        }
    }
Пример #11
0
    private void GenerateTile(GameObject referenceTile, int col, int row)
    {
        GameObject tile = (GameObject)Instantiate(referenceTile, transform);

        tiles[col, row] = tile;

        float posX = col * tileSize;
        float posY = row * -tileSize;

        tile.transform.position = new Vector2(posX, posY);


        Script_Tile script = tile.GetComponent <Script_Tile>(); // Get the script of the newly generated tile

        script.posX = col;                                      // Give it the X and Y Pos
        script.posY = row * -tileSize;
    }
Пример #12
0
    public void InstantiateGrid(int p_width, int p_height, int p_gridDepthPosition)
    {
        _width             = p_width;
        _height            = p_height;
        _gridDepthPosition = p_gridDepthPosition;

        System.Array.Resize(ref _grid, p_width * p_height);

        for (int z = 0; z < _height; z++)
        {
            for (int x = 0; x < _width; x++)
            {
                Vector3Int  tilePosition = new Vector3Int(x, _gridDepthPosition, z);
                Script_Tile myTile       = new Script_Tile(tilePosition);
                SetGridTile(x, z, myTile);
            }
        }
    }
Пример #13
0
    private Script_Tile GetNearestGrass(List <Script_Tile> p_grassList)
    {
        List <Script_Tile> nearestGrassWithinRange = new List <Script_Tile> ();

        if (p_grassList.Count > 0)
        {
            int[] distanceArray = new int[p_grassList.Count];


            for (int i = 0; i < p_grassList.Count; i++)
            {
                distanceArray [i] = (int)Vector3Int.Distance(GetCurrentGridPosition(), p_grassList [i].GetPositionAsInt());
            }

            int shortestDistance = int.MaxValue;
            for (int j = 0; j < p_grassList.Count; j++)
            {
                if (shortestDistance > distanceArray [j])
                {
                    nearestGrassWithinRange.Clear();
                    shortestDistance = distanceArray [j];
                }

                if (shortestDistance == distanceArray [j])
                {
                    nearestGrassWithinRange.Add(p_grassList [j]);
                }
            }

            Script_Tile randomCloseGrass = null;
            if (nearestGrassWithinRange != null)
            {
                int randomNumber = Random.Range(0, nearestGrassWithinRange.Count);
                randomCloseGrass = nearestGrassWithinRange [randomNumber];
            }

            if (randomCloseGrass != null)
            {
                return(randomCloseGrass);
            }
        }
        return(null);
    }
Пример #14
0
    private bool DecideToEatGrass()
    {
        int x = GetCurrentGridPosition().x;
        int z = GetCurrentGridPosition().z;

        if (_grassWithinRange != null)
        {
            foreach (Script_Tile grass in _grassWithinRange)
            {
                if (_grid.AccessGridTile(x, z) == grass && (grass.GetOccupiedBySheep() != true || _grid.AccessGridTile(_occupiedLocation.x, _occupiedLocation.z) == grass))
                {
                    Vector3Int occupyLocation = new Vector3Int(x, 0, z);
                    OccupyNewLocation(occupyLocation);
                    _grassBeingEaten = grass;
                    return(true);
                }
            }
        }

        return(false);
    }
Пример #15
0
    private void DecideGrassToSeek()
    {
        List <Script_Tile> tilesWithinRangeExcludingPosition = _grassWithinRange;

        Script_Tile tileToRemove = null;

        foreach (Script_Tile tile in _grassWithinRange)
        {
            if (GetCurrentGridPosition() == tile.GetPositionAsInt())
            {
                tileToRemove = tile;
            }
        }
        if (tileToRemove != null)
        {
            tilesWithinRangeExcludingPosition.Remove(tileToRemove);
        }


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

        foreach (Script_Tile tile in tilesWithinRangeExcludingPosition)
        {
            if (tile.GetOccupiedBySheep() == false)
            {
                availableGrass.Add(tile);
            }
        }

        if (availableGrass.Count > 0)
        {
            _targetLocation = GetNearestGrass(availableGrass).GetPositionAsInt();
            OccupyNewLocation(_targetLocation);
        }
        else
        {
            _targetLocation = GetCurrentGridPosition();
            OccupyNewLocation(_targetLocation);
        }
    }
Пример #16
0
    private void DecideWanderingLocation()
    {
        List <Script_Tile> tilesWithinRangeExcludingPosition = _tilesWithinRange;
        Script_Tile        tileToRemove = null;

        foreach (Script_Tile tile in _tilesWithinRange.ToList())
        {
            if (GetCurrentGridPosition() == tile.GetPositionAsInt())
            {
                tileToRemove = tile;
            }
        }
        if (tileToRemove != null)
        {
            tilesWithinRangeExcludingPosition.Remove(tileToRemove);
        }

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

        foreach (Script_Tile tile in tilesWithinRangeExcludingPosition)
        {
            if (tile.GetOccupiedByWolf() == false)
            {
                availableTiles.Add(tile);
            }
        }
        int randomNumber = Random.Range(0, availableTiles.Count - 1);

        if (availableTiles.Count > 0)
        {
            _targetLocation = availableTiles [randomNumber].GetPositionAsInt();
            OccupyNewLocation(_targetLocation);
        }
        else
        {
            _targetLocation = GetCurrentGridPosition();
            OccupyNewLocation(_targetLocation);
        }
    }
    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);
    }
Пример #18
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);
    }
Пример #19
0
 private void SetGridTile(int p_x, int p_z, Script_Tile p_tile)
 {
     _grid [p_z * _width + p_x] = p_tile;
 }
Пример #20
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);
 }
Пример #21
0
    private List <Script_Tile> GetGrassWithinRange(int p_range)
    {
        List <Script_Tile> grassWithinRange = new List <Script_Tile>();
        int xCurrent = GetCurrentGridPosition().x;
        int zCurrent = GetCurrentGridPosition().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 (_grid.AccessGridTile(x, z).GetState() == GrassStates.Grass)
                {
                    Script_Tile tile = _grid.AccessGridTile(x, z);
                    grassWithinRange.Add(tile);
                }
            }
        }

        return(grassWithinRange);
    }