using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Manages a collection of flower plants and attached flowers
///
public class FlowerArea : MonoBehaviour
{
// The diameter of the area where the agent and flowers can be
// used for observing relative distance from agent to flower
public const float AreaDiameter = 20f;
// The list of all flower plants in this flower area (flower plants have multiple flowers)
private List flowerPlants;
// A lookup dictionary for looking up a flower from a nectar collider
private Dictionary nectarFlowerDictionary;
///
/// The list of all flowers in the flower area
///
public List Flowers { get; private set; }
///
/// Reset the flowers and flower plants
///
public void ResetFlowers()
{
// Rotate each flower plant around the Y axis and subtly around X and Z
foreach (GameObject flowerPlant in flowerPlants)
{
float xRotation = UnityEngine.Random.Range(-5f, 5f);
float yRotation = UnityEngine.Random.Range(-180f, 180f);
float zRotation = UnityEngine.Random.Range(-5f, 5f);
flowerPlant.transform.localRotation = Quaternion.Euler(xRotation, yRotation, zRotation);
}
// Reset each flower
foreach (Flower flower in Flowers)
{
flower.ResetFlower();
}
}
///
/// Gets the that a nectar collider belongs to
///
/// The nectar collider
/// The matching flower
public Flower GetFlowerFromNectar(Collider collider)
{
return nectarFlowerDictionary[collider];
}
///
/// Called when the area wakes up
///
private void Awake()
{
// Initialize variables
flowerPlants = new List();
nectarFlowerDictionary = new Dictionary();
Flowers = new List();
}
///
/// Called when the game starts
///
private void Start()
{
// Find all flowers that are children of this GameObject/Transform
FindChildFlowers(transform);
}
///
/// Recursively finds all flowers and flower plants that are children of a parent transform
///
/// The parent of the children to check
private void FindChildFlowers(Transform parent)
{
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
if (child.CompareTag("flower_plant"))
{
// Found a flower plant, add it to the flowerPlants list
flowerPlants.Add(child.gameObject);
// Look for flowers within the flower plant
FindChildFlowers(child);
}
else
{
// Not a flower plant, look for a Flower component
Flower flower = child.GetComponent();
if (flower != null)
{
// Found a flower, add it to the Flowers list
Flowers.Add(flower);
// Add the nectar collider to the lookup dictionary
nectarFlowerDictionary.Add(flower.nectarCollider, flower);
// Note: there are no flowers that are children of other flowers
}
else
{
// Flower component not found, so check children
FindChildFlowers(child);
}
}
}
}
}