예제 #1
0
    /*
     * Plants an existing carrot on the ground at a certain position
     * 3 Cases:
     *	- If the destination cell is empty, the carrot is planted there, returns the planted carrot.
     *	- If the destination cell is invalid (out of bounds), the carrot is destroyed, returns null.
     *	- If the destination cell already contains a carrot, the planted carrot merges into the other one, returns the already planted carrot.
     *		The merge adds the food amount to the already planted carrot, the merged carrot is destroyed
     */
    private void PlantCarrot(object source, Grabbable.DropData data)
    {
        if (source is Grabbable gb)
        {
            CarrotController carrot = gb.GetComponent <CarrotController>();
            if (!carrot)
            {
                return;
            }

            Instantiate(DestroyVFXPrefab, carrot.transform.position, Quaternion.identity);

            Vector2Int cell = data.Cell;

            if (!_grid.IsValidCell(cell))
            {
                Destroy(carrot.gameObject);
                return;
            }

            if (_carrots.ContainsKey(cell))
            {
                CarrotController placedCarrot = _carrots[cell];
                placedCarrot.Merge(carrot);
                Destroy(carrot.gameObject);
                return;
            }

            _carrots.Add(cell, carrot);
        }
    }
예제 #2
0
 private void Awake()
 {
     Hungry     = false;
     WantToMate = false;
     TargetFood = null;
     TargetMate = null;
 }
예제 #3
0
    public override Brain.Action TakeDecision(Brain brain)
    {
        if (brain.Movement.PositionReached() && !brain.HasCheckedArea)
        {
            brain.HasCheckedArea = true;
            List <CarrotController> carrots = brain.Eyes.GetCarrotsInSight();

            CarrotController closestCarrot = null;
            float            minDistance   = float.MaxValue;

            foreach (CarrotController carrot in carrots)
            {
                if (carrot.Grabbable.Grabbed)
                {
                    continue;
                }

                float distance = (brain.transform.position - carrot.transform.position).sqrMagnitude;
                if (distance < minDistance && brain.Movement.CanReachPosition(carrot.transform.position))
                {
                    closestCarrot = carrot;
                    minDistance   = distance;
                }
            }

            if (closestCarrot)
            {
                brain.TargetFood = closestCarrot;
                return(Brain.Action.ReachFood);
            }
        }
        return(brain.CurrentAction);
    }
예제 #4
0
 /**
  * Removes all the carrots from the island
  */
 public void Clear()
 {
     foreach (Vector2Int carrotCell in _carrots.Keys)
     {
         CarrotController carrot = _carrots[carrotCell];
         Destroy(carrot);
     }
     _carrots.Clear();
 }
예제 #5
0
    private void OnTargetCancelled(object sender, EventArgs data)
    {
        if (sender is MonoBehaviour behaviour)
        {
            CarrotController carrot = behaviour.GetComponent <CarrotController>();
            foreach (RabbitController rabbit in carrot.TargetedBy)
            {
                Brain brain = rabbit.GetComponent <Brain>();

                if (brain)
                {
                    brain.TargetCancelled = true;
                }
            }
        }
    }
예제 #6
0
    /*
     * Removes a carrot from the manager
     */
    private void RemoveCarrot(CarrotController carrot)
    {
        if (!carrot)
        {
            return;
        }
        Destroy(carrot.gameObject);

        Vector2Int cell = _grid.GetCell(carrot.transform.position);

        if (!_carrots.ContainsKey(cell))
        {
            return;
        }
        _carrots.Remove(cell);
    }
예제 #7
0
    /*
     * Uproots an existing carrot of the grid
     */
    private void UprootCarrot(object source, Grabbable.GrabData data)
    {
        if (source is Grabbable gb)
        {
            CarrotController carrot = gb.GetComponent <CarrotController>();
            if (!carrot)
            {
                return;
            }

            if (!_carrots.ContainsKey(data.Cell))
            {
                return;
            }
            _carrots.Remove(data.Cell);
        }
    }
예제 #8
0
    public List <CarrotController> GetCarrotsInSight()
    {
        float distance = Data.PerceptionMaxDistance;
        List <CarrotController> results = new List <CarrotController>();

        Collider[] colliders = Physics.OverlapSphere(transform.position, distance);

        foreach (Collider collider in colliders)
        {
            CarrotController carrot = collider.GetComponent <CarrotController>();
            if (carrot && carrot.State != CarrotController.GrowState.Rotten)
            {
                results.Add(carrot);
            }
        }

        return(results);
    }
예제 #9
0
    /*
     * Spawns a carrot on a cell position
     * Returns the new carrot or null if there is already a carrot on the cell or the cell is invalid
     */
    private CarrotController SpawnCarrot(Vector2Int cell)
    {
        if (!_grid.IsValidCell(cell))
        {
            return(null);
        }
        if (_carrots.ContainsKey(cell))
        {
            return(null);
        }

        // Check carrots count limit
        int maxAllowed = _rabbitsManager.RabbitCount * MaxCarrotsPerRabbit;

        if (_carrots.Count >= maxAllowed)
        {
            return(null);
        }

        Vector3    position = _grid.GetRandomPosition(cell);
        GameObject obj      = Instantiate(CarrotPrefab, position, Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), Vector3.up));

        CarrotController carrot = obj.GetComponent <CarrotController>();

        _carrots.Add(cell, carrot);

        CarrotSpread spread    = carrot.Spread;
        Grabbable    grabbable = carrot.Grabbable;

        carrot.onRot += OnCarrotDestroy;
        carrot.FoodSource.onEaten += OnCarrotDestroy;
        spread.onSpread           += SpreadCarrot;
        grabbable.onGrab          += UprootCarrot;
        grabbable.onDrop          += PlantCarrot;

        return(carrot);
    }
예제 #10
0
 public void Merge(CarrotController other)
 {
     FoodSource.Merge(other.FoodSource);
     onMerge?.Invoke(this, EventArgs.Empty);
 }