65 lines
1.6 KiB
C#
65 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class ChunkManager : MonoBehaviour
|
|
{
|
|
private List<Chunk> activeChunks = new List<Chunk>();
|
|
private Camera viewer;
|
|
[SerializeField] private MeshGeneration _meshGeneration;
|
|
|
|
public Material material;
|
|
public int viewableChunks = 3;
|
|
public int chunkSize = 20;
|
|
public float isoLevel = 0.4f;
|
|
|
|
public InputAction action;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
viewer = Camera.main;
|
|
action.Enable();
|
|
action.performed += _ => Generate();
|
|
|
|
Generate();
|
|
}
|
|
|
|
private void Generate()
|
|
{
|
|
foreach (var c in activeChunks)
|
|
{
|
|
Destroy(c);
|
|
}
|
|
activeChunks.Clear();
|
|
|
|
for (int i = 0; i < viewableChunks; i++)
|
|
{
|
|
for (int j = 0; j < viewableChunks; j++)
|
|
{
|
|
for (int k = 0; k < viewableChunks; k++)
|
|
{
|
|
CreateChunks(new Vector3(i, j, k) * (chunkSize - 1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
void CreateChunks(Vector3 offset)
|
|
{
|
|
var mesh = _meshGeneration.March(chunkSize, offset, isoLevel);
|
|
|
|
GameObject chunkContainer = new GameObject($"chunk {offset.x} {offset.y} {offset.z}");
|
|
var chunk = chunkContainer.AddComponent<Chunk>();
|
|
chunk.Init(offset, mesh, material);
|
|
|
|
activeChunks.Add(chunk);
|
|
}
|
|
}
|