Updated plant behavior to new system.

This commit is contained in:
2025-08-11 22:03:32 -07:00
parent 6632f02d1c
commit fdd71edba5
3 changed files with 139 additions and 117 deletions
+38 -21
View File
@@ -7,6 +7,7 @@ public class PlantController : MonoBehaviour
[SerializeField] GameObject plantPrefab;
[SerializeField] List<GameObject> plantModels;
List<GameObject> plants;
List<GameObject> plantQueue;
float xBound;
float zBound;
Terrain terrain;
@@ -15,6 +16,7 @@ public class PlantController : MonoBehaviour
void Start()
{
plants = new List<GameObject>();
plantQueue = new List<GameObject>();
terrain = Core.instance.ground;
xBound = terrain.GetComponent<Terrain>().terrainData.size.x;
zBound = terrain.GetComponent<Terrain>().terrainData.size.z;
@@ -23,7 +25,7 @@ public class PlantController : MonoBehaviour
{
GameObject newPlant = Instantiate(plantPrefab);
newPlant.GetComponent<Plant>().SetVisual(plantModels[Random.Range(0, plantModels.Count - 1)]);
newPlant.GetComponent<Plant>().SetValues(Random.Range(10f, 100f), Random.Range(0.5f, 1f), Random.Range(0.1f, 0.25f));
newPlant.GetComponent<Plant>().SetValues(Random.Range(0.1f, 0.2f), Random.Range(1.5f, 5), Random.Range(0.25f, 0.5f));
float x = Random.Range(0, xBound);
float z = Random.Range(0, zBound);
float y = terrain.SampleHeight(new Vector3(x, 0, z));
@@ -39,43 +41,58 @@ public class PlantController : MonoBehaviour
// Update is called once per frame
void Update()
{
for(int i = 0; i < plants.Count; i ++)
float reproAvg = 0;
for(int i = plants.Count - 1; i >= 0;i --)
{
GameObject currentPlant = plants[i];
Plant plantI = currentPlant.GetComponent<Plant>();
reproAvg += plantI.maxSize;
if (!plantI.isAlive)
{
currentPlant.SetActive(false);
plants.Remove(currentPlant);
Destroy(currentPlant);
i--;
}
else
{
if (plantI.isReproducing)
if(plantI.isReproducing)
{
GameObject child = Instantiate(plantPrefab);
Plant childPlant = child.GetComponent<Plant>();
childPlant.SetVisual(plantI.visualPrefab);
childPlant.SetValues(plantI.healthMax + Random.Range(-0.5f, 0.5f), plantI.nutrientNeed + Random.Range(-0.01f, 0.01f), plantI.growthRate + Random.Range(-0.01f, 0.01f));
if(plants.Count < 1000)
{
// Gather data and alter slightly
float childGrowthRate = plantI.growthRate + Random.Range(-0.1f, 0.1f);
float childMaxSize = plantI.maxSize + Random.Range(-0.1f, 0.1f);
float childReproductionChance = plantI.reproductionChance + Random.Range(-0.01f, 0.01f);
float x = plantI.childXPos;
float z = plantI.childYPos;
float y = terrain.SampleHeight(new Vector3(plantI.childXPos, 0, plantI.childYPos));
Vector3 childPos = new Vector3(x, y, z);
if (childReproductionChance > 1)
{
childReproductionChance = 1;
}
Vector3 normal = terrain.terrainData.GetInterpolatedNormal(plantI.childXPos / xBound, plantI.childYPos / zBound);
child.transform.LookAt(normal);
child.transform.Rotate(new Vector3(90, 0, 0));
child.transform.position = childPos;
plants.Add(child);
i++;
plantI.childMade();
GameObject childVisualPrefab = plantI.visualPrefab;
GameObject child = Instantiate(plantPrefab);
child.GetComponent<Plant>().SetVisual(childVisualPrefab);
child.GetComponent<Plant>().SetValues(childGrowthRate, childMaxSize, childReproductionChance);
float x = Random.Range(0, xBound);
float z = Random.Range(0, zBound);
float y = terrain.SampleHeight(new Vector3(x, 0, z));
Vector3 normal = terrain.terrainData.GetInterpolatedNormal(x / xBound, z / zBound);
child.transform.LookAt(normal);
child.transform.Rotate(new Vector3(90, 0, 0));
Vector3 newPlantPos = new Vector3(x, y, z);
child.transform.position = newPlantPos;
plantQueue.Add(child);
}
plantI.isReproducing = false;
}
}
}
Debug.Log($"Max Size Avg = {reproAvg / plants.Count}");
if(plantQueue.Count > 0)
{
plants.AddRange(plantQueue);
plantQueue.Clear();
}
}
public void Step(float dTime)