Updated nutrient lookup to include radius around plant based on size of plant

This commit is contained in:
2025-08-06 22:58:01 -07:00
parent a30cbca5c9
commit 7097c8ce53
7 changed files with 63 additions and 35 deletions
+34 -23
View File
@@ -1,3 +1,4 @@
using System;
using UnityEditor.TextCore.Text;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;
@@ -29,15 +30,43 @@ public class NutrientController : MonoBehaviour
}
public float GetNutrients(float x, float y)
public float GetNutrients(float x, float y, float radius)
{
int locX = Mathf.FloorToInt(x * sizeX);
int locY = Mathf.FloorToInt(y * sizeY);
return nutrientMap[locX, locY];
int ceilRad = Mathf.CeilToInt(radius);
float totalNutrients = 0;
for(int r = -ceilRad; r < ceilRad; r++)
{
for (int c = -ceilRad; c < ceilRad; c++)
{
if((locX + ceilRad > 0 && locX + ceilRad < sizeX) && (locY + ceilRad > 0 && locY + ceilRad < sizeY))
{
int fraction = Mathf.Abs(r) + Mathf.Abs(c);
if(fraction == 0)
{
totalNutrients += nutrientMap[locX + ceilRad, locY + ceilRad];
}
else
{
totalNutrients += nutrientMap[locX + ceilRad, locY + ceilRad] / (fraction * 2);
}
}
}
}
if(totalNutrients == 0)
{
float thing = nutrientMap[locX, locY];
Debug.Log("HERE" + thing);
}
return totalNutrients;
}
public void UseNutrients(float x, float y, float used)
public void UseNutrients(float x, float y, float radius, float used)
{
int locX = Mathf.FloorToInt(x * sizeX);
int locY = Mathf.FloorToInt(y * sizeY);
@@ -61,26 +90,13 @@ public class NutrientController : MonoBehaviour
for (int c = 0; c < sizeY; c++)
{
float colorValue = nutrientMap[r, c];
texture.SetPixel(r, c, new Color(colorValue, colorValue, colorValue));
texture.SetPixel(r, c, new Color(colorValue, colorValue, colorValue, 0.5f));
}
}
texture.Apply();
meshRenderer.material.mainTexture = texture;
}
private void ConsoleDump()
{
string toPrint = "\n";
for (int r = 0; r < sizeX; r++)
{
for (int c = 0; c < sizeY; c++)
{
toPrint += nutrientMap[r, c] + " ";
}
toPrint += "\n";
}
Debug.Log(toPrint);
meshRenderer.gameObject.SetActive(true);
}
#if UNITY_EDITOR
@@ -95,11 +111,6 @@ public class NutrientController : MonoBehaviour
{
(target as NutrientController).Render();
}
if(GUILayout.Button("Dump it"))
{
(target as NutrientController).ConsoleDump();
}
}
}
#endif
+7 -2
View File
@@ -42,9 +42,14 @@ public class Plant : MonoBehaviour
float x = transform.position.x / terrain.terrainData.size.x;
float y = transform.position.z / terrain.terrainData.size.z;
float size = visual.transform.localScale.x;
float nutrientsAvailable = nutrientCont.GetNutrients(x, y, size);
Debug.Log($"Nutrients available {nutrientsAvailable}");
float nutrientsAvailable = nutrientCont.GetNutrients(x, y);
float nutrientsNeededFrame = (nutrientNeed * Time.deltaTime);
if (nutrientsNeededFrame > nutrientsAvailable)
{
healthCurrent -= Time.deltaTime * 0.1f * healthMax;
@@ -52,7 +57,7 @@ public class Plant : MonoBehaviour
else
{
visual.transform.localScale += visual.transform.localScale * (growthRate * Time.deltaTime);
nutrientCont.UseNutrients(x, y, nutrientsNeededFrame);
nutrientCont.UseNutrients(x, y, size, nutrientsNeededFrame);
nutrientsConsumed += nutrientsNeededFrame;
}
+1 -1
View File
@@ -18,7 +18,7 @@ public class PlantController : MonoBehaviour
xBound = terrain.GetComponent<Terrain>().terrainData.size.x;
zBound = terrain.GetComponent<Terrain>().terrainData.size.z;
for (int i = 0; i < 100; i++)
for (int i = 0; i < 1; i++)
{
GameObject newPlant = Instantiate(plantPrefab);
newPlant.GetComponent<Plant>().SetVisual(plantModels[Random.Range(0, plantModels.Count - 1)]);