/* * 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); } }
private void Awake() { Hungry = false; WantToMate = false; TargetFood = null; TargetMate = null; }
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); }
/** * Removes all the carrots from the island */ public void Clear() { foreach (Vector2Int carrotCell in _carrots.Keys) { CarrotController carrot = _carrots[carrotCell]; Destroy(carrot); } _carrots.Clear(); }
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; } } } }
/* * 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); }
/* * 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); } }
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); }
/* * 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); }
public void Merge(CarrotController other) { FoodSource.Merge(other.FoodSource); onMerge?.Invoke(this, EventArgs.Empty); }