2020-04-20 03:14:45 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class LoraxCuller : MonoBehaviour {
|
|
|
|
|
public Lorax[] Loraces;
|
2020-04-21 02:40:23 +02:00
|
|
|
|
public int[] Radius;
|
|
|
|
|
public bool[] Omnidirectional;
|
2020-04-29 23:31:45 +02:00
|
|
|
|
public bool CullingStatusChanged = false;
|
2020-04-20 03:14:45 +02:00
|
|
|
|
|
2020-04-21 02:40:23 +02:00
|
|
|
|
private HashSet<GameObject> OldChunks = new HashSet<GameObject>();
|
|
|
|
|
private int LastX = int.MaxValue;
|
|
|
|
|
private int LastY = int.MaxValue;
|
|
|
|
|
private int LastAngle = int.MaxValue;
|
2020-04-20 03:14:45 +02:00
|
|
|
|
|
2020-04-21 02:40:23 +02:00
|
|
|
|
private void Update() {
|
2020-06-12 21:28:40 +02:00
|
|
|
|
if (Loraces.Length == 0) {
|
|
|
|
|
// Not in the world of the lorax yet.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 02:40:23 +02:00
|
|
|
|
int BaseX = Mathf.FloorToInt(transform.position.x / 20) * 20 + 10;
|
|
|
|
|
int BaseY = Mathf.FloorToInt(transform.position.z / 20) * 20 + 10;
|
|
|
|
|
int Angle = Mathf.FloorToInt(transform.localEulerAngles.y / 5) * 5;
|
|
|
|
|
if (BaseX == LastX && BaseY == LastY && Angle == LastAngle) {
|
|
|
|
|
return;
|
2020-04-20 11:31:15 +02:00
|
|
|
|
}
|
2020-04-21 02:40:23 +02:00
|
|
|
|
LastX = BaseX;
|
|
|
|
|
LastY = BaseY;
|
|
|
|
|
LastAngle = Angle;
|
2020-04-20 03:14:45 +02:00
|
|
|
|
|
2020-04-21 02:40:23 +02:00
|
|
|
|
HashSet<GameObject> EnabledChunks = new HashSet<GameObject>();
|
2020-04-20 03:14:45 +02:00
|
|
|
|
for (int LoraxIndex = 0; LoraxIndex < Loraces.Length; LoraxIndex++) {
|
|
|
|
|
Lorax Lorax = Loraces[LoraxIndex];
|
2020-04-21 02:40:23 +02:00
|
|
|
|
for (float X = BaseX - Radius[LoraxIndex]; X <= BaseX + Radius[LoraxIndex]; X += 10) {
|
|
|
|
|
for (float Y = BaseY - Radius[LoraxIndex]; Y <= BaseY + Radius[LoraxIndex]; Y += 10) {
|
|
|
|
|
if (!Omnidirectional[LoraxIndex]) {
|
|
|
|
|
Vector3 Delta = new Vector3(X - BaseX, 0, Y - BaseY);
|
|
|
|
|
if (Delta.magnitude > Mathf.Sqrt(Mathf.Pow(20, 2) * 2)) {
|
|
|
|
|
if (Vector3.Dot(Delta.normalized, transform.forward) < -0.2) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-20 20:22:14 +02:00
|
|
|
|
|
2020-04-21 02:40:23 +02:00
|
|
|
|
GameObject Chunk = Lorax.GetChunkAt(X, Y);
|
|
|
|
|
if (Chunk != null) {
|
|
|
|
|
Chunk.SetActive(true);
|
|
|
|
|
EnabledChunks.Add(Chunk);
|
2020-04-20 20:22:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-20 03:14:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-20 20:22:14 +02:00
|
|
|
|
|
2020-04-29 23:31:45 +02:00
|
|
|
|
CullingStatusChanged = false;
|
2020-04-21 02:40:23 +02:00
|
|
|
|
foreach (GameObject OldChunk in OldChunks) {
|
|
|
|
|
if (!EnabledChunks.Contains(OldChunk)) {
|
|
|
|
|
OldChunk.SetActive(false);
|
2020-04-29 23:31:45 +02:00
|
|
|
|
CullingStatusChanged = true;
|
2020-04-20 20:22:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-29 23:31:45 +02:00
|
|
|
|
if (EnabledChunks.Count > OldChunks.Count) {
|
|
|
|
|
CullingStatusChanged = true;
|
|
|
|
|
}
|
2020-04-21 02:40:23 +02:00
|
|
|
|
OldChunks = EnabledChunks;
|
2020-04-20 03:14:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|