Files
EcoSim/Assets/GameAssets/Scripts/Plant.cs
T
Alex 3a909fda70 Initial commit
Basic plant generation and lifecycle implemented.
2025-08-06 21:04:49 -07:00

60 lines
1.7 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class Plant : MonoBehaviour
{
private float healthMax;
private float healthCurrent;
private float nutrientNeed;
private float growthRate;
public bool isAlive;
private GameObject visualPrefab;
public GameObject visual;
public void SetVisual(GameObject visualPrefab)
{
this.visualPrefab = visualPrefab;
}
public void SetValues(float healthMax, float nutrientNeed, float growthRate)
{
this.healthMax = healthMax;
this.nutrientNeed = nutrientNeed;
this.growthRate = growthRate;
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Start()
{
visual = Instantiate(visualPrefab, transform);
healthCurrent = healthMax;
isAlive = true;
}
// Update is called once per frame
void Update()
{
Terrain terrain = Core.instance.ground;
NutrientController nutrientCont = Core.instance.nutrientCont;
float x = transform.position.x / terrain.terrainData.size.x;
float y = transform.position.y / terrain.terrainData.size.y;
float nutrientsAvailable = nutrientCont.GetNutrients(x, y);
if(nutrientNeed > nutrientsAvailable)
{
healthCurrent -= Time.deltaTime * (0.1f * healthMax);
}
else
{
visual.transform.localScale += visual.transform.localScale * (growthRate * Time.deltaTime);
nutrientCont.UseNutrients(x, y, (nutrientNeed * Time.deltaTime));
}
if (healthCurrent <= 0)
{
isAlive = false;
}
}
}