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
+20 -29
View File
@@ -9,11 +9,13 @@ public class NutrientController : MonoBehaviour
private float[,] nutrientMap;
[SerializeField, Range(10, 256)] int sizeX;
[SerializeField, Range(10, 256)] int sizeY;
[SerializeField, Range(0f, 1f)] float recoveryRate;
[SerializeField] MeshRenderer meshRenderer;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
nutrientMap = new float[sizeX, sizeY];
for(int r = 0; r < sizeX; r ++)
{
@@ -38,30 +40,27 @@ public class NutrientController : MonoBehaviour
{
if (nutrientMap[r,c] < 1)
{
nutrientMap[r, c] += (0.1f * dTime);
nutrientMap[r, c] += (recoveryRate/60.0f * dTime);
if(nutrientMap[r, c] > 1)
{
nutrientMap[r, c] = 1;
}
}
}
}
}
public float PlantNutrientUse(float x, float y, float radius, float nutrientsNeeded)
public float PlantNutrientUse(float x, float z, float radius, float nutrientsNeeded)
{
int locX = Mathf.FloorToInt(x * sizeX);
int locY = Mathf.FloorToInt(y * sizeY);
int locZ = Mathf.FloorToInt(z * sizeY);
int flooredRadius = Mathf.FloorToInt(radius);
float totalNutrients = nutrientMap[locX, locY];
float totalNutrients = nutrientMap[locX, locZ];
float[,] nutrientsAvailable;
try
{
nutrientsAvailable = new float[flooredRadius * 2 + 1, flooredRadius * 2 + 1];
} catch (Exception e)
{
Debug.Log($"x : {x} y : {y} radius : {radius} nutrients needed : {nutrientsNeeded}");
return 2000000;
}
nutrientsAvailable = new float[flooredRadius * 2 + 1, flooredRadius * 2 + 1];
// Calculate total amount of nutrients available to the plant, at a 1/ (2 * steps from origin) ratio.
for (int r = -flooredRadius; r <= flooredRadius; r++)
@@ -70,21 +69,22 @@ public class NutrientController : MonoBehaviour
for(int c = -flooredRadius; c <= flooredRadius; c++)
{
int currentLocY = locY + c;
if (currentLocX < 0 || currentLocX >= nutrientMap.GetLength(0) || currentLocY < 0 || currentLocY >= nutrientMap.GetLength(1))
int currentLocZ = locZ + c;
// 26 , 255
if (currentLocX < 0 || currentLocX >= nutrientMap.GetLength(0) || currentLocZ < 0 || currentLocZ >= nutrientMap.GetLength(1))
{
nutrientsAvailable[flooredRadius, flooredRadius] = 0;
nutrientsAvailable[flooredRadius + r, flooredRadius + c] = 0;
}
else
{
if (r == 0 && c == 0)
{
nutrientsAvailable[flooredRadius, flooredRadius] = nutrientMap[currentLocX, currentLocY];
nutrientsAvailable[flooredRadius, flooredRadius] = nutrientMap[currentLocX, currentLocZ];
}
else
{
nutrientsAvailable[flooredRadius + r, flooredRadius + c] = nutrientMap[currentLocX, currentLocY] / ((Mathf.Abs(r) + Mathf.Abs(c)) * 2);
totalNutrients += nutrientMap[currentLocX, currentLocY] / ((Mathf.Abs(r) + Mathf.Abs(c)) * 2);
nutrientsAvailable[flooredRadius + r, flooredRadius + c] = nutrientMap[currentLocX, currentLocZ] / ((Mathf.Abs(r) + Mathf.Abs(c)) * 2);
totalNutrients += nutrientMap[currentLocX, currentLocZ] / ((Mathf.Abs(r) + Mathf.Abs(c)) * 2);
}
}
}
@@ -100,7 +100,7 @@ public class NutrientController : MonoBehaviour
for (int c = -flooredRadius; c <= flooredRadius; c++)
{
int currentLocY = locY + c;
int currentLocY = locZ + c;
if (currentLocX >= 0 && currentLocX < nutrientMap.GetLength(0) && currentLocY >= 0 && currentLocY < nutrientMap.GetLength(1))
{
nutrientMap[currentLocX, currentLocY] -= (nutrientsAvailable[flooredRadius + r, flooredRadius + c]);
@@ -109,14 +109,13 @@ public class NutrientController : MonoBehaviour
}
return nutrientsNeeded - totalNutrients;
}
for (int r = -flooredRadius; r <= flooredRadius; r++)
{
int currentLocX = locX + r;
for (int c = -flooredRadius; c <= flooredRadius; c++)
{
int currentLocY = locY + c;
int currentLocY = locZ + c;
if (currentLocX >= 0 && currentLocX < nutrientMap.GetLength(0) && currentLocY >= 0 && currentLocY < nutrientMap.GetLength(1))
{
nutrientMap[currentLocX, currentLocY] -= (nutrientsAvailable[flooredRadius + r, flooredRadius + c] / totalNutrients) * nutrientsNeeded;
@@ -162,14 +161,6 @@ public class NutrientController : MonoBehaviour
return totalNutrients;
}
public void UseNutrients(float x, float y, float radius, float used)
{
int locX = Mathf.FloorToInt(x * sizeX);
int locY = Mathf.FloorToInt(y * sizeY);
nutrientMap[locX, locY] -= used;
}
public void AddNutrients(float x, float y, float added, float radius)
{
int locX = Mathf.FloorToInt(x * sizeX);