Updated plant nutrient range and made base algorithm to dictate nutrient uptake

This commit is contained in:
2025-08-07 22:54:46 -07:00
parent 7097c8ce53
commit 927a119075
4 changed files with 73 additions and 17 deletions
@@ -7,8 +7,8 @@ using static UnityEngine.UIElements.UxmlAttributeDescription;
public class NutrientController : MonoBehaviour public class NutrientController : MonoBehaviour
{ {
private float[,] nutrientMap; private float[,] nutrientMap;
[SerializeField, Range(10, 100)] int sizeX; [SerializeField, Range(10, 256)] int sizeX;
[SerializeField, Range(10, 100)] int sizeY; [SerializeField, Range(10, 256)] int sizeY;
[SerializeField] MeshRenderer meshRenderer; [SerializeField] MeshRenderer meshRenderer;
// Start is called once before the first execution of Update after the MonoBehaviour is created // Start is called once before the first execution of Update after the MonoBehaviour is created
@@ -30,6 +30,66 @@ public class NutrientController : MonoBehaviour
} }
public bool PlantNutrientUse(float x, float y, float radius, float nutrientsNeeded)
{
int locX = Mathf.FloorToInt(x * sizeX);
int locY = Mathf.FloorToInt(y * sizeY);
nutrientMap[locX - 1, locY] = 0;
int flooredRadius = Mathf.FloorToInt(radius);
float totalNutrients = nutrientMap[locX, locY];
float[] nutrientsAvailable = new float[flooredRadius * 2 + 1];
float[] amountToUse = new float[flooredRadius * 2 + 1];
for (int r = -flooredRadius; r <= flooredRadius; r++)
{
int currentLocX = locX + r;
if(currentLocX > 0 && currentLocX < sizeX)
{
if(r != 0)
{
nutrientsAvailable[flooredRadius + r] = nutrientMap[currentLocX, locY] / (Mathf.Abs(r) * 2);
totalNutrients += nutrientMap[currentLocX, locY] / (Mathf.Abs(r) * 2);
}
else
{
nutrientsAvailable[flooredRadius] = nutrientMap[currentLocX, locY];
}
}
}
if (totalNutrients < nutrientsNeeded)
{
return false;
}
/*string output = "NUTRIENTS AVAILABLE\n";
foreach (float n in nutrientsAvailable)
{
output += n + " ";
}
Debug.Log(output);
*/
//Normalizing
for(int r = 0; r < nutrientsAvailable.Length; r++)
{
amountToUse[r] = (nutrientsAvailable[r] / totalNutrients) * nutrientsNeeded;
}
/*
string output2 = "PULLING\n";
foreach (float n in amountToUse)
{
output2 += n + " ";
}
Debug.Log(output2);
*/
return true;
}
public float GetNutrients(float x, float y, float radius) public float GetNutrients(float x, float y, float radius)
{ {
int locX = Mathf.FloorToInt(x * sizeX); int locX = Mathf.FloorToInt(x * sizeX);
@@ -57,12 +117,6 @@ public class NutrientController : MonoBehaviour
} }
} }
if(totalNutrients == 0)
{
float thing = nutrientMap[locX, locY];
Debug.Log("HERE" + thing);
}
return totalNutrients; return totalNutrients;
} }
+7 -5
View File
@@ -44,13 +44,15 @@ public class Plant : MonoBehaviour
float y = transform.position.z / terrain.terrainData.size.z; float y = transform.position.z / terrain.terrainData.size.z;
float size = visual.transform.localScale.x; float size = visual.transform.localScale.x;
float nutrientsAvailable = nutrientCont.GetNutrients(x, y, size); //float nutrientsAvailable = nutrientCont.GetNutrients(x, y, size);
Debug.Log($"Nutrients available {nutrientsAvailable}"); //float nutrientsNeededFrame = (nutrientNeed * Time.deltaTime);
float nutrientsNeededFrame = (nutrientNeed * Time.deltaTime); float nutrientsNeededFrame = (nutrientNeed);
if (nutrientsNeededFrame > nutrientsAvailable) bool CanStayAlive = nutrientCont.PlantNutrientUse(x, y, size, nutrientsNeededFrame);
/*if (nutrientsNeededFrame > nutrientsAvailable)
{ {
healthCurrent -= Time.deltaTime * 0.1f * healthMax; healthCurrent -= Time.deltaTime * 0.1f * healthMax;
} }
@@ -65,6 +67,6 @@ public class Plant : MonoBehaviour
{ {
nutrientCont.AddNutrients(x, y, (nutrientsConsumed * 0.9f)); nutrientCont.AddNutrients(x, y, (nutrientsConsumed * 0.9f));
isAlive = false; isAlive = false;
} }*/
} }
} }
+4 -4
View File
@@ -152,7 +152,7 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
plantPrefab: {fileID: 4454294744824292065, guid: 20d627f37ea26f346bf0c20e82a4152a, type: 3} plantPrefab: {fileID: 4454294744824292065, guid: 20d627f37ea26f346bf0c20e82a4152a, type: 3}
plantModels: plantModels:
- {fileID: 919132149155446097, guid: e3d2efed8d0c55a4ca5bfa35b9f6c6c7, type: 3} - {fileID: 8704148140120826694, guid: f89e947d8ef527b4a939de047e8e2817, type: 3}
--- !u!114 &23266656 --- !u!114 &23266656
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -165,8 +165,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 881bafe4bc0f5d747a4fa68fb5fc9126, type: 3} m_Script: {fileID: 11500000, guid: 881bafe4bc0f5d747a4fa68fb5fc9126, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
sizeX: 100 sizeX: 256
sizeY: 100 sizeY: 256
meshRenderer: {fileID: 823137140} meshRenderer: {fileID: 823137140}
--- !u!114 &23266657 --- !u!114 &23266657
MonoBehaviour: MonoBehaviour:
@@ -567,7 +567,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!64 &823137139 --- !u!64 &823137139
MeshCollider: MeshCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0