/Scripts/InfiniteWorld.cs
https://bitbucket.org/Tsarpf/yacmbcp · C# · 379 lines · 287 code · 59 blank · 33 comment · 61 complexity · ba65f7faefe4d42490cd1f627a1d3734 MD5 · raw file
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading;
-
-
- public static class InfiniteWorld
- {
- static int3 chunkSize = new int3
- ((int)WorldGameObject.chunkSize.x,
- (int)WorldGameObject.chunkSize.y,
- (int)WorldGameObject.chunkSize.z);
-
- static int3 worldChunkSize = new int3
- ((int)WorldGameObject.worldChunkSize.x,
- (int)WorldGameObject.worldChunkSize.y,
- (int)WorldGameObject.worldChunkSize.z);
-
- public static Vector3 playerPosLast;
- public static Vector3 playerPosNew;
-
- public static int3 ChunkArrayOffset = new int3(0, 0, 0);
-
- public static GameObject player;
-
- public static GUIText guiTextTarget;
-
- //public static GUIText guiText2;
-
- static public Vector3 getPlayerChunkPos()
- {
- Vector3 playerPos = player.transform.position;
-
- int x = Mathf.CeilToInt(playerPos.x) - 1;
- int y = Mathf.CeilToInt(playerPos.y) - 1;
- int z = Mathf.CeilToInt(playerPos.z) - 1;
-
- x = x / chunkSize.x;
- y = y / chunkSize.y;
- z = z / chunkSize.z;
-
- //int3 offset = ChunkRowUtils.ChunkArrayOffset;
-
- return new Vector3(x, y, z);
- }
-
- static private Vector3 getPlayerBlockPos()
- {
- int x = Mathf.CeilToInt(player.transform.position.x) - 1;
- int y = Mathf.CeilToInt(player.transform.position.y) - 1;
- int z = Mathf.CeilToInt(player.transform.position.z) - 1;
-
- Vector3 blockCoords = new Vector3();
- blockCoords.x = x % chunkSize.x;
- blockCoords.y = y % chunkSize.y;
- blockCoords.z = z % chunkSize.z;
-
- return blockCoords;
- }
-
- public static void playerPositionChunkwiseChecking()
- {
-
- playerPosNew = getPlayerChunkPos();
-
- if (playerPosNew != playerPosLast)
- {
-
- //We have moved to a new chunk compared to last frame
- int x = (int)(playerPosNew.x - playerPosLast.x);
- int y = (int)(playerPosNew.y - playerPosLast.y);
- int z = (int)(playerPosNew.z - playerPosLast.z);
- //Debug.Log( x + " " + y + " " + z);
- InfiniteWorld.AddToDirQueue(new int3(x, y, z));
-
- ThreadPool.QueueUserWorkItem(new WaitCallback(shiftAndReplace));
- //Thread shiftThread = new Thread(new ThreadStart(shiftAndReplace));
- //shiftThread.Start();
-
- }
-
- playerPosLast = playerPosNew;
- }
-
- public static void shiftAndReplace(System.Object stateInfo)
- {
-
- //ThreadPool.SetMaxThreads(8, 8);
- ThreadPool.SetMinThreads(4, 4);
-
- int3 dir = GetFromDirQueue();
-
- Chunk[, ,] chunks = WorldGameObject.chunks;
- lock (WorldGameObject.chunks)
- {
- xdirFunction(dir.x, ChunkArrayOffset, chunks);
- ydirFunction(dir.y, ChunkArrayOffset, chunks);
- zdirFunction(dir.z, ChunkArrayOffset, chunks);
- }
-
- //foreach (Thread thread in threadList)
- //{
- // thread.Join();
- //}
-
- //threadList.Clear();
-
- //ThreadPool.SetMinThreads(0, 0);
- }
-
- private static void xdirFunction(int xdir, int3 offset, Chunk[, ,] chunks)
- {
- if (xdir == 1)
- {
- ChunkArrayOffset.x += 1;
- for (int x = 0; x < worldChunkSize.x; x++)
- {
- for (int y = 0; y < worldChunkSize.y; y++)
- {
- for (int z = 0; z < worldChunkSize.z; z++)
- {
- if (x == 0)
- {
- AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x + 1, y, z].addChunkDrawData));
-
- }
- if (x == worldChunkSize.x - 1)
- {
- int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
-
- WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
- ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x - 1, y, z].addChunkDrawData));
- }
- else
- {
- chunks[x, y, z] = chunks[x + 1, y, z];
- }
- /*
- //newstuff after this
-
- if (x == worldChunkSize.x - 1)
- {
-
- }
- */
- }
- }
- }
- }
-
- else if (xdir == -1)
- {
- ChunkArrayOffset.x -= 1;
- for (int x = worldChunkSize.x - 1; x >= 0; x--)
- {
- for (int y = worldChunkSize.y - 1; y >= 0; y--)
- {
- for (int z = worldChunkSize.z - 1; z >= 0; z--)
- {
- if (x == (worldChunkSize.x - 1))
- {
- AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
-
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x - 1, y, z].addChunkDrawData));
- }
- if (x == 0)
- {
- int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
- WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
- ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x + 1, y, z].addChunkDrawData));
-
- }
- else
- {
- chunks[x, y, z] = chunks[x - 1, y, z];
- }
- }
- }
- }
- }
-
- }
-
- private static void ydirFunction(int ydir, int3 offset, Chunk[, ,] chunks)
- {
- if (ydir == 1)
- {
- ChunkArrayOffset.y += 1;
- for (int x = 0; x < worldChunkSize.x; x++)
- {
- for (int y = 0; y < worldChunkSize.y; y++)
- {
- for (int z = 0; z < worldChunkSize.z; z++)
- {
- if (y == 0)
- {
- AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
-
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y + 1, z].addChunkDrawData));
-
- }
- if (y == worldChunkSize.y - 1)
- {
- int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
-
- WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
- ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
-
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y - 1, z].addChunkDrawData));
-
- }
- else
- {
- chunks[x, y, z] = chunks[x, y + 1, z];
- }
- }
- }
- }
- }
- else if (ydir == -1)
- {
- ChunkArrayOffset.y -= 1;
- for (int x = worldChunkSize.x - 1; x >= 0; x--)
- {
- for (int y = worldChunkSize.y - 1; y >= 0; y--)
- {
- for (int z = worldChunkSize.z - 1; z >= 0; z--)
- {
- if (y == (worldChunkSize.y - 1))
- {
- AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y - 1, z].addChunkDrawData));
-
- }
- if (y == 0)
- {
- int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
- WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
- ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
-
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y + 1, z].addChunkDrawData));
-
- }
- else
- {
- chunks[x, y, z] = chunks[x, y - 1, z];
- }
- }
- }
- }
- }
- }
-
- private static void zdirFunction(int zdir, int3 offset, Chunk[, ,] chunks)
- {
- if (zdir == 1)
- {
- ChunkArrayOffset.z += 1;
- for (int x = 0; x < worldChunkSize.x; x++)
- {
- for (int y = 0; y < worldChunkSize.y; y++)
- {
- for (int z = 0; z < worldChunkSize.z; z++)
- {
- if (z == 0)
- {
- AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z + 1].addChunkDrawData));
-
- }
- if (z == worldChunkSize.z - 1)
- {
- int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
-
- WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
- ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
-
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z - 1].addChunkDrawData));
- }
- else
- {
- chunks[x, y, z] = chunks[x, y, z + 1];
- }
- }
- }
- }
- }
- else if (zdir == -1)
- {
- ChunkArrayOffset.z -= 1;
- for (int x = worldChunkSize.x - 1; x >= 0; x--)
- {
- for (int y = worldChunkSize.y - 1; y >= 0; y--)
- {
- for (int z = worldChunkSize.z - 1; z >= 0; z--)
- {
- if (z == (worldChunkSize.z - 1))
- {
- AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
-
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z - 1].addChunkDrawData));
-
- }
- if (z == 0)
- {
- int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
- WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
- ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
- //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z + 1].addChunkDrawData));
-
- }
- else
- {
- chunks[x, y, z] = chunks[x, y, z - 1];
- }
- }
- }
- }
- }
- }
-
- public static Queue qRemoveChunk = new Queue(32);
- public static void AddToRemoveQueue(Chunk chunk)
- {
- lock (qRemoveChunk)
- {
- qRemoveChunk.Enqueue(chunk);
- }
- }
-
- public static Chunk GetFromRemoveQueue()
- {
- Chunk chunk;
-
- lock (qRemoveChunk)
- {
- if (qRemoveChunk.Count > 0)
- {
- chunk = (Chunk)qRemoveChunk.Dequeue();
- }
- else
- {
- chunk = null;
- }
- }
- return chunk;
- }
-
- public static Queue qShiftDirection = new Queue((int)WorldGameObject.worldChunkSize.x * (int)worldChunkSize.z + 10);
- public static void AddToDirQueue(int3 dir)
- {
- lock (qShiftDirection)
- {
- qShiftDirection.Enqueue(dir);
- }
- }
-
- public static int3 GetFromDirQueue()
- {
- int3 dir;
-
- lock (qShiftDirection)
- {
- if (qShiftDirection.Count > 0)
- {
- dir = (int3)qShiftDirection.Dequeue();
- }
- else
- {
- Debug.Log("GetFromDirQueue was null");
- dir = new int3(0, 0, 0);
- }
- }
- return dir;
- }
- }