103 lines
2.5 KiB
C#
103 lines
2.5 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class OctreeManager : MonoBehaviour
|
||
|
|
{
|
||
|
|
|
||
|
|
public GameObject[] parents;
|
||
|
|
private float cellSize = 250f;
|
||
|
|
|
||
|
|
private Dictionary<Vector2Int, List<GameObject>> grid = new Dictionary<Vector2Int, List<GameObject>>();
|
||
|
|
|
||
|
|
// Start is called before the first frame update
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
//get all objects with mapzone tag, assign to parents array
|
||
|
|
parents = GameObject.FindGameObjectsWithTag("mapzone");
|
||
|
|
|
||
|
|
|
||
|
|
for (int i = 0; i < parents.Length; i++)
|
||
|
|
{
|
||
|
|
GameObject[] children = getChildren(parents[i]);
|
||
|
|
if (children == null) return;
|
||
|
|
//get all children of parents[i]
|
||
|
|
for (int j = 0; j < children.Length; j++)
|
||
|
|
{
|
||
|
|
Vector3 position = children[j].transform.position;
|
||
|
|
Vector2Int cell = new Vector2Int(
|
||
|
|
Mathf.FloorToInt(position.x / cellSize),
|
||
|
|
Mathf.FloorToInt(position.z / cellSize));
|
||
|
|
|
||
|
|
if (!grid.ContainsKey(cell))
|
||
|
|
{
|
||
|
|
grid[cell] = new List<GameObject>();
|
||
|
|
}
|
||
|
|
|
||
|
|
grid[cell].Add(children[j]);
|
||
|
|
children[j].SetActive(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
Vector3 playerPosition = Player.Instance.transform.position;
|
||
|
|
Vector2Int playerCell = new Vector2Int(
|
||
|
|
Mathf.FloorToInt(playerPosition.x / cellSize),
|
||
|
|
Mathf.FloorToInt(playerPosition.z / cellSize));
|
||
|
|
|
||
|
|
// Iterate through neighboring cells
|
||
|
|
for (int x = playerCell.x - 1; x <= playerCell.x + 1; x++)
|
||
|
|
{
|
||
|
|
for (int y = playerCell.y - 1; y <= playerCell.y + 1; y++)
|
||
|
|
{
|
||
|
|
|
||
|
|
Vector2Int cell = new Vector2Int(x, y);
|
||
|
|
if (grid.ContainsKey(cell))
|
||
|
|
{
|
||
|
|
foreach (var obj in grid[cell])
|
||
|
|
{
|
||
|
|
// Compare squared distances for faster calculation
|
||
|
|
Vector2 offset = new Vector2(obj.transform.position.x, obj.transform.position.z) - new Vector2(playerPosition.x, playerPosition.z);
|
||
|
|
float distanceSquared = Vector2.SqrMagnitude(offset);
|
||
|
|
|
||
|
|
// Set your threshold distance squared here
|
||
|
|
float hideDistanceSquared = 15000f;
|
||
|
|
|
||
|
|
if (distanceSquared <= hideDistanceSquared)
|
||
|
|
{
|
||
|
|
obj.SetActive(true); // Show the object
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
obj.SetActive(false); // Hide the object
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private GameObject[] getChildren(GameObject g)
|
||
|
|
{
|
||
|
|
|
||
|
|
if (g.transform.childCount == 0)
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
GameObject[] children = new GameObject[g.transform.childCount];
|
||
|
|
for (int i = 0; i < g.transform.childCount; i++)
|
||
|
|
{
|
||
|
|
children[i] = g.transform.GetChild(i).gameObject;
|
||
|
|
}
|
||
|
|
return children;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|