campfire/Assets/Scripts/Lorax.cs
2020-04-20 02:20:21 +03:00

88 lines
3.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class Lorax : MonoBehaviour {
public Terrain Terrain;
[Header("Generic Gen")]
public int Seed;
public bool Regen = false;
public List<GameObject> SpawnableTrees;
public List<int> Chances;
[Header("Generation Details")]
public float HeightMin = 7.5f;
public float HeightMax = 9;
public float TreeChance = 0.9f;
public float CampSize = 10f;
public Vector3 CampMiddle;
public float RandomNudge = 2f;
public float Denseness = 4;
public float MinimumAdjacency = 1.5f;
private int LastSeed = -1;
private Vector3[][] TreePositions;
void Start() {
}
void Update() {
if (Application.isEditor) {
if (LastSeed == Seed && !Regen) {
return;
}
Regen = false;
LastSeed = Seed;
Random.InitState(Seed);
for (int x = transform.childCount - 1; x >= 0; x--) {
DestroyImmediate(transform.GetChild(0).gameObject);
}
int cap = (int)Mathf.Floor(500 / Denseness);
int[] TotalChances = new int[SpawnableTrees.Count + 1];
int Counter = 0;
for (int x = 0; x < TotalChances.Length - 1; x++) {
TotalChances[x + 1] = Counter + Chances[x];
Counter = TotalChances[x + 1];
}
TreePositions = new Vector3[cap][];
for (int y = 0; y < cap; y++) {
TreePositions[y] = new Vector3[cap];
for (int x = 0; x < cap; x++) {
if (Random.value > TreeChance) {
continue;
}
var pos = new Vector3(x * Denseness - 250 + Random.value * RandomNudge - RandomNudge / 2, 0, y * Denseness - 250 + Random.value * RandomNudge - RandomNudge / 2);
TreePositions[y][x] = pos;
if (x > 0 && (TreePositions[y][x - 1] - pos).magnitude < MinimumAdjacency) {
continue;
}
if (y > 0 && (TreePositions[y - 1][x] - pos).magnitude < MinimumAdjacency) {
continue;
}
if ((pos - CampMiddle).magnitude < CampSize) {
continue;
}
var height = Terrain.SampleHeight(pos);
if (height >= HeightMin && height <= HeightMax) {
pos.y = Terrain.transform.position.y + height;
var rand = Random.Range(0, TotalChances[TotalChances.Length - 1]);
var Chosen = SpawnableTrees[0];
for (int curr = TotalChances.Length - 2; curr > 0; curr--) {
if (rand > TotalChances[curr]) {
Chosen = SpawnableTrees[curr];
break;
}
}
var obj = GameObject.Instantiate(Chosen, pos, new Quaternion(), gameObject.transform);
}
}
}
}
}
}