예제 #1
0
    public Script_Wolf(Vector3 p_position, Script_GameManager p_gameManager, Script_Grid p_grid, Quaternion p_rotation)
    {
        _wolfObject      = GameObject.CreatePrimitive(PrimitiveType.Cube);
        _wolfObject.name = "WolfObject";
        _wolfObject.transform.position = p_position;
        _wolfObject.transform.rotation = p_rotation;
        _material       = _wolfObject.GetComponent <Renderer>().material;
        _material.color = Color.black;



        _speed                   = 0.033f;
        _size                    = 0.5f;
        _movementDecay           = 0.01f;
        _actionDecay             = 0.1f;
        _sensingRange            = 2;
        _sheepBeingEaten         = null;
        _maxHealth               = 10.0f;
        _health                  = 5.0f;
        _healthNeededToReproduce = _maxHealth * 0.75f;


        _decidedAction    = DecidedAction.Wandering;
        _actDelegate      = null;
        _tilesWithinRange = new List <Script_Tile> ();
        _sheepWithinRange = new List <Script_Sheep> ();
        _surroundingTiles = new List <Script_Tile> ();
        _grid             = p_grid;

        _occupiedLocation = GetCurrentGridPosition();
        _targetLocation   = GetCurrentGridPosition();
        OccupyNewLocation(GetCurrentGridPosition());
    }
예제 #2
0
    public void InstantiateSheep(Vector3 p_position)
    {
        Quaternion   rotation = new Quaternion(0, 0, 0, 0);
        Script_Sheep mySheep  = new Script_Sheep(p_position, _gameManager, this, rotation);

        _sheepList.Add(mySheep);
    }
예제 #3
0
    private bool DecideToEatSheep()
    {
        int x = GetCurrentGridPosition().x;
        int z = GetCurrentGridPosition().z;

        if (_sheepWithinRange != null)
        {
            foreach (Script_Sheep sheep in _sheepWithinRange.ToList())
            {
                if (sheep != null && sheep.GetSheepObject() != null)
                {
                    int sheepX = sheep.GetCurrentGridPosition().x;
                    int sheepZ = sheep.GetCurrentGridPosition().z;
                    if (GetCurrentGridPosition() == sheep.GetCurrentGridPosition() && (_grid.AccessGridTile(sheepX, sheepZ).GetOccupiedByWolf() != true ||
                                                                                       _occupiedLocation.x == sheepX && _occupiedLocation.z == sheepZ))
                    {
                        Vector3Int occupyLocation = new Vector3Int(x, 0, z);
                        OccupyNewLocation(occupyLocation);
                        _sheepBeingEaten = sheep;
                        return(true);
                    }
                }
            }
        }

        return(false);
    }
예제 #4
0
    public void DestroySheep(Script_Sheep p_sheep, Material p_material)
    {
        GameObject sheepObject = p_sheep.GetSheepObject();

        _sheepList.Remove(p_sheep);
        _gameManager.DestroyObject(sheepObject);
        _gameManager.DestroyMaterial(p_material);
    }
예제 #5
0
    private void DecideWolfToSeek()
    {
        List <Script_Sheep> sheepWithinRangeExcludingPosition = _sheepWithinRange;

        foreach (Script_Sheep sheep in _sheepWithinRange.ToList())
        {
            if (sheep != null && sheep.GetSheepObject() != null)
            {
                sheepWithinRangeExcludingPosition.Add(sheep);
            }
        }

        Script_Sheep sheepToRemove = null;

        foreach (Script_Sheep sheep in _sheepWithinRange.ToList())
        {
            if (sheep != null && sheep.GetSheepObject() != null)
            {
                if (GetCurrentGridPosition() == sheep.GetCurrentGridPosition())
                {
                    sheepToRemove = sheep;
                }
            }
        }
        if (sheepToRemove != null)
        {
            sheepWithinRangeExcludingPosition.Remove(sheepToRemove);
        }

        List <Script_Sheep> availableSheep = new List <Script_Sheep>();

        foreach (Script_Sheep sheep in sheepWithinRangeExcludingPosition)
        {
            if (sheep != null && sheep.GetSheepObject() != null)
            {
                int sheepX = sheep.GetCurrentGridPosition().x;
                int sheepZ = sheep.GetCurrentGridPosition().z;
                if (_grid.AccessGridTile(sheepX, sheepZ).GetOccupiedByWolf() == false)
                {
                    availableSheep.Add(sheep);
                }
            }
        }
        if (availableSheep.Count > 0)
        {
            _targetLocation = GetNearestSheep(availableSheep).GetCurrentGridPosition();
            OccupyNewLocation(_targetLocation);
        }
        else
        {
            _targetLocation = GetCurrentGridPosition();
            OccupyNewLocation(_targetLocation);
        }
    }
예제 #6
0
    public void Decide()
    {
        _sheepBeingEaten = null;

        if (DecideToDie())
        {
            _decidedAction = DecidedAction.dying;
            _actDelegate   = new ActDelegate(Die);
        }

        if (DecideToReproduce() && !DecideToDie())
        {
            _reproductionLocation = DecideReproductionTile();
            Script_Tile currentlyOccupiedTile = _grid.AccessGridTile(_reproductionLocation.x, _reproductionLocation.z);
            currentlyOccupiedTile.SetOccupiedByWolf(false);
            _decidedAction = DecidedAction.Reproducing;
            _actDelegate   = new ActDelegate(Reproduce);
        }

        if (DecideToEatSheep() && !DecideToDie() && !DecideToReproduce())
        {
            OccupyNewLocation(GetCurrentGridPosition());
            _decidedAction = DecidedAction.Eating;
            _actDelegate   = new ActDelegate(EatSheep);
        }

        if (DecideToSeekGrass() && !DecideToEatSheep() && !DecideToReproduce() && !DecideToDie())
        {
            if (AtTargetLocation() || _decidedAction != DecidedAction.Seeking)
            {
                DecideWolfToSeek();
            }
            _decidedAction = DecidedAction.Seeking;
            _actDelegate   = new ActDelegate(MoveToLocation);
        }


        if (DecideToWander() && !DecideToSeekGrass() && !DecideToEatSheep() && !DecideToReproduce() && !DecideToDie())
        {
            if (AtTargetLocation() || _decidedAction != DecidedAction.Wandering)
            {
                DecideWanderingLocation();
            }

            _decidedAction = DecidedAction.Wandering;
            _actDelegate   = new ActDelegate(MoveToLocation);
        }
    }
예제 #7
0
    private Script_Sheep GetNearestSheep(List <Script_Sheep> p_sheepList)
    {
        List <Script_Sheep> nearestSheepWithinRange = new List <Script_Sheep> ();

        if (p_sheepList != null && p_sheepList.Count > 0)
        {
            int[] distanceArray = new int[p_sheepList.Count];


            for (int i = 0; i < p_sheepList.Count; i++)
            {
                if (p_sheepList [i] != null && p_sheepList [i].GetSheepObject() != null)
                {
                    distanceArray [i] = (int)Vector3Int.Distance(GetCurrentGridPosition(), p_sheepList [i].GetCurrentGridPosition());
                }
            }

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

                if (shortestDistance == distanceArray [j])
                {
                    nearestSheepWithinRange.Add(p_sheepList [j]);
                }
            }

            Script_Sheep randomCloseSheep = null;
            if (nearestSheepWithinRange != null)
            {
                int randomNumber = Random.Range(0, nearestSheepWithinRange.Count);
                randomCloseSheep = nearestSheepWithinRange [randomNumber];
            }

            if (randomCloseSheep != null)
            {
                return(randomCloseSheep);
            }
        }
        return(null);
    }
예제 #8
0
    public void CreateSheep(GameObject p_tileObject, int p_width, int p_height, float p_yOffset, Quaternion p_rotation, int p_numberOfSheep)
    {
        int currentAmountOfSheep = 0;

        while (currentAmountOfSheep < p_numberOfSheep)
        {
            int x = Random.Range(0, p_width);
            int z = Random.Range(0, p_height);

            while (AccessGridTile(x, z).GetOccupiedBySheep() == true)
            {
                x = Random.Range(0, p_width);
                z = Random.Range(0, p_height);
            }

            Vector3      entityPosition = new Vector3(x, p_yOffset, z);
            Script_Sheep mySheep        = new Script_Sheep(entityPosition, _gameManager, this, p_rotation);
            _sheepList.Add(mySheep);
            currentAmountOfSheep++;
        }
    }