Updated plant nutrient range and made base algorithm to dictate nutrient uptake
This commit is contained in:
@@ -7,8 +7,8 @@ using static UnityEngine.UIElements.UxmlAttributeDescription;
|
||||
public class NutrientController : MonoBehaviour
|
||||
{
|
||||
private float[,] nutrientMap;
|
||||
[SerializeField, Range(10, 100)] int sizeX;
|
||||
[SerializeField, Range(10, 100)] int sizeY;
|
||||
[SerializeField, Range(10, 256)] int sizeX;
|
||||
[SerializeField, Range(10, 256)] int sizeY;
|
||||
[SerializeField] MeshRenderer meshRenderer;
|
||||
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,13 +44,15 @@ public class Plant : MonoBehaviour
|
||||
float y = transform.position.z / terrain.terrainData.size.z;
|
||||
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;
|
||||
}
|
||||
@@ -65,6 +67,6 @@ public class Plant : MonoBehaviour
|
||||
{
|
||||
nutrientCont.AddNutrients(x, y, (nutrientsConsumed * 0.9f));
|
||||
isAlive = false;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user