campfire/Assets/Scripts/LoraxCuller.cs

63 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoraxCuller : MonoBehaviour {
public Lorax[] Loraces;
public int[] Radius;
public bool[] Omnidirectional;
public bool CullingStatusChanged = false;
private HashSet<GameObject> OldChunks = new HashSet<GameObject>();
private int LastX = int.MaxValue;
private int LastY = int.MaxValue;
private int LastAngle = int.MaxValue;
private void Update() {
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;
}
LastX = BaseX;
LastY = BaseY;
LastAngle = Angle;
HashSet<GameObject> EnabledChunks = new HashSet<GameObject>();
for (int LoraxIndex = 0; LoraxIndex < Loraces.Length; LoraxIndex++) {
Lorax Lorax = Loraces[LoraxIndex];
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;
}
}
}
GameObject Chunk = Lorax.GetChunkAt(X, Y);
if (Chunk != null) {
Chunk.SetActive(true);
EnabledChunks.Add(Chunk);
}
}
}
}
CullingStatusChanged = false;
foreach (GameObject OldChunk in OldChunks) {
if (!EnabledChunks.Contains(OldChunk)) {
OldChunk.SetActive(false);
CullingStatusChanged = true;
}
}
if (EnabledChunks.Count > OldChunks.Count) {
CullingStatusChanged = true;
}
OldChunks = EnabledChunks;
}
}