VoxelUnity/Assets/Scripts/WorldManager.cs

110 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WorldManager : MonoBehaviour {
public Vector3Int min_range;
public Vector3Int max_range;
public Vector3Int cache_range;
public string server_host;
public int server_port;
public Chunk chunkPrefab;
public int max_geometry_updates_per_frame = 15;
public int n_chunk_build_threads = 3;
private int worldHeight;
private NetworkThread network_thread;
private static WorldManager instance;
public Transform position_reference;
private Vector3Int current_chunk_position;
private ChunkManager chunk_manager;
public static WorldManager Instance
{
get
{
return instance;
}
}
public int WorldHeight
{
get
{
return worldHeight;
}
}
// Use this for initialization
void Start () {
WorldManager.instance = this;
worldHeight = 3 * Chunk.N_CHUNK_SIDELENGTH;
network_thread = new NetworkThread(server_host, server_port);
chunk_manager = new ChunkManager(min_range,
max_range,
cache_range,
this,
network_thread,
position_reference,
n_chunk_build_threads);
}
// Update is called once per frame
void Update () {
while (network_thread.inbox_count > 0)
{
MessageObject mo = network_thread.PopMessage();
chunk_manager.processServerMessage(mo);
}
current_chunk_position = Vector3Int.RoundToInt(position_reference.position);
current_chunk_position.x /= Chunk.N_CHUNK_SIDELENGTH;
current_chunk_position.y /= Chunk.N_CHUNK_SIDELENGTH;
current_chunk_position.z /= Chunk.N_CHUNK_SIDELENGTH;
chunk_manager.ApplyUpdates(max_geometry_updates_per_frame);
chunk_manager.UpdateViewRange();
}
public Chunk InstantiateNewChunk(Vector3Int chunk_position)
{
Chunk c = (Chunk)Instantiate(chunkPrefab, transform);
c.transform.localPosition = new Vector3(chunk_position.x * Chunk.N_CHUNK_SIDELENGTH,chunk_position.y * Chunk.N_CHUNK_SIDELENGTH,chunk_position.z * Chunk.N_CHUNK_SIDELENGTH);
c.ChunkPosition = new int[] {chunk_position.x, chunk_position.y, chunk_position.z};
return c;
}
void OnDestroy()
{
network_thread.end_thread = true;
chunk_manager.end_builder_threads = true;
// TODO: waiting for answer
System.Threading.Thread.Sleep(200);
}
}