Make some changes to the lorax

This commit is contained in:
Sofia 2020-04-20 05:24:01 +03:00
parent 617c48d4ff
commit 6c232fd391
2 changed files with 20 additions and 18 deletions

View File

@ -18,8 +18,10 @@ public class FGAssetModificationProcessor : UnityEditor.AssetModificationProcess
foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Generator")) { foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Generator")) {
Lorax Lorax = obj.GetComponent<Lorax>(); Lorax Lorax = obj.GetComponent<Lorax>();
Lorax.DestroyGenerated(); if (Lorax.ShowInEditor) {
Lorax.Generate(); Lorax.DestroyGenerated();
Lorax.Generate();
}
} }
} }
} }

View File

@ -10,7 +10,7 @@ public class Lorax : MonoBehaviour {
[Header("Generic Gen")] [Header("Generic Gen")]
public int Seed; public int Seed;
public bool Regen = false; public bool Regen = false;
public bool Destroy = false; public bool ShowInEditor = true;
public List<GameObject> SpawnableTrees; public List<GameObject> SpawnableTrees;
public List<int> Chances; public List<int> Chances;
@ -26,22 +26,24 @@ public class Lorax : MonoBehaviour {
private int LastSeed = -1; private int LastSeed = -1;
private Vector3[][] TreePositions; private Vector3[][] TreePositions;
private bool Generated = false;
private int ChunkSize = 20; private int ChunkSize = 20;
private GameObject[] Chunks; private GameObject[] Chunks;
void Start() { void Start() {
Debug.Log((int)((Mathf.Floor((228.55f) / ChunkSize) * 25) + Mathf.Floor(245.27f / ChunkSize))); Generate();
} }
void Update() { void Update() {
if (Application.isEditor) { if (!Application.isPlaying) {
if (Destroy) { if (!ShowInEditor) {
Destroy = false; if (Generated) {
DestroyGenerated(); DestroyGenerated();
}
return;
} }
if (LastSeed == Seed && !Regen) { if (Generated && LastSeed == Seed && !Regen) {
return; return;
} }
Regen = false; Regen = false;
@ -52,13 +54,6 @@ public class Lorax : MonoBehaviour {
} }
public GameObject GetChunkAt(float x, float y) { public GameObject GetChunkAt(float x, float y) {
/*Debug.Log("----------------");
Debug.Log("y: " + (y + 250f));
Debug.Log("x: " + (x + 250f));
Debug.Log("y divided: " + (y + 250f) / ChunkSize);
Debug.Log("x divided: " + (x + 250f) / ChunkSize);
Debug.Log("y floored: " + (Mathf.Floor((y + 250f) / ChunkSize)));
Debug.Log("x floored: " + (Mathf.Floor((x + 250f) / ChunkSize)));*/
int CurrentChunk = (int)((Mathf.Floor((y + 250f) / ChunkSize) * 25f) + Mathf.Floor((x + 250f) / ChunkSize)); int CurrentChunk = (int)((Mathf.Floor((y + 250f) / ChunkSize) * 25f) + Mathf.Floor((x + 250f) / ChunkSize));
return Chunks[CurrentChunk]; return Chunks[CurrentChunk];
} }
@ -68,10 +63,13 @@ public class Lorax : MonoBehaviour {
DestroyImmediate(transform.GetChild(0).gameObject); DestroyImmediate(transform.GetChild(0).gameObject);
} }
Chunks = new GameObject[(500 / ChunkSize) * (500 / ChunkSize)]; Chunks = new GameObject[(500 / ChunkSize) * (500 / ChunkSize)];
Generated = false;
} }
public void Generate() { public void Generate() {
Random.InitState(Seed); Random.InitState(Seed);
Generated = true;
Chunks = new GameObject[(500 / ChunkSize) * (500 / ChunkSize)];
int[] TotalChances = new int[SpawnableTrees.Count + 1]; int[] TotalChances = new int[SpawnableTrees.Count + 1];
int Counter = 0; int Counter = 0;
for (int x = 0; x < TotalChances.Length - 1; x++) { for (int x = 0; x < TotalChances.Length - 1; x++) {
@ -87,7 +85,9 @@ public class Lorax : MonoBehaviour {
if (Chunks[CurrentChunk] == null) { if (Chunks[CurrentChunk] == null) {
Chunks[CurrentChunk] = new GameObject("Chunk " + CurrentChunk); Chunks[CurrentChunk] = new GameObject("Chunk " + CurrentChunk);
Chunks[CurrentChunk].transform.parent = transform; Chunks[CurrentChunk].transform.parent = transform;
Chunks[CurrentChunk].SetActive(false); if (Application.isPlaying) {
Chunks[CurrentChunk].SetActive(false);
}
} }
if (Random.value > TreeChance) { if (Random.value > TreeChance) {
continue; continue;