Exemplo n.º 1
0
        // TODO Optimize so that we use Struct instead of classes.

        public static void SessionManagerClearRefs()
        {
            lastSelectedChunkPos        = new Vector4(0, 0, 0, 0);
            lastSelectedChunkClusterPos = new Vector4(0, 0, 0, 0);
            lastSelectedChunk           = null;
            lastSelectedChunkCluster    = null;
        }
Exemplo n.º 2
0
        // Get a reference to a chunk cluster by world position, then query the tiles to get the desired chunk cluster.
        public static ChunkCluster GetChunkCluster(Vector3 worldPosition, bool createChunkClusterIfNotFound = false)
        {
            // Convert and store the given position to a valid ChunkClusterPoisson.
            worldPosition = ChunkClusterPoissonId(worldPosition);

            // Check if last selected Chunk Cluster poisson is the same, if it is then just return the same Chunk Cluster.
            if (lastSelectedLayer == MapDataManager.mapDataCurrentLayer)
            {
                if (lastSelectedChunkCluster != null)
                {
                    if (lastSelectedChunkCluster.posId.x == worldPosition.x && lastSelectedChunkCluster.posId.y == worldPosition.z)
                    {
                        return(lastSelectedChunkCluster);
                    }
                }
            }

            curLayerIndex = lastSelectedLayer = MapDataManager.mapDataCurrentLayer;
            // Create a berrySystem vector 2 [ v2 ]

            V2 targetPosId = new V2();

            targetPosId.x = worldPosition.x;
            targetPosId.y = worldPosition.z;

            // Get the current layer and get all the chunk clusters on that maplayer and then scan for the correct posId.
            if (XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters != null)
            {
                for (int i = 0; i < XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters.Length; i++)
                {
                    if (XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters[i].posId.x == targetPosId.x && XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters[i].posId.y == targetPosId.y)
                    {
                        return(lastSelectedChunkCluster = XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters[i]);
                    }
                    if (i == XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters.Length - 1 && createChunkClusterIfNotFound)
                    {
                        // Create a new chunk cluster entire in mapdata.
                        Array.Resize(ref XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters, XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters.Length + 1);
                        ChunkCluster newCluster = new ChunkCluster();
                        newCluster.index = XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters.Length - 1;
                        newCluster.posId = targetPosId;

                        // Create an actual gameobject in the level.
                        newCluster.clusterObjRef = new GameObject(XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters.Length - 1 + " chunkCluster");
                        newCluster.clusterObjRef.transform.parent = XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].layerObjRef.transform;
                        newCluster.clusterObjRef.isStatic         = true;

                        // Applay the new ChunkCluster Array to the current layer.
                        return(lastSelectedChunkCluster = XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters[i + 1] = newCluster);
                    }
                }
            }
            else
            {
                if (createChunkClusterIfNotFound)
                {
                    // Create a new chunk cluster entire in mapdata
                    ChunkCluster[] newChunkCluster = new ChunkCluster[1];
                    newChunkCluster[0]       = new ChunkCluster();
                    newChunkCluster[0].posId = targetPosId;
                    newChunkCluster[0].index = 0;

                    // Create an actual gameobject in the level.
                    newChunkCluster[0].clusterObjRef = new GameObject("0" + " chunkCluster");
                    newChunkCluster[0].clusterObjRef.transform.parent = XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].layerObjRef.transform;
                    newChunkCluster[0].clusterObjRef.isStatic         = true;

                    // Applay the new ChunkCluster Array to the current layer.
                    XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters = newChunkCluster;

                    return(lastSelectedChunkCluster = XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[curLayerIndex].chunkClusters[0]);
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        // Get a reference to a chunk, then query the tiles to get the desired chunk.
        public static Chunk GetChunk(Vector3 worldPosition, ChunkCluster chunkCluster, bool createChunkIfNotFound, bool clear = false)
        {
            if (chunkCluster != null)
            {
                // Convert and store the given position to a valid Chunk Poisson.
                worldPosition = ChunkPoissonId(worldPosition);

                if (lastSelectedLayerChunk == MapDataManager.mapDataCurrentLayer)
                {
                    if (lastSelectedChunk != null)
                    {
                        if (lastSelectedChunk.posId.x == worldPosition.x && lastSelectedChunk.posId.y == worldPosition.z)
                        {
                            return(lastSelectedChunk);
                        }
                    }
                }

                lastSelectedLayer = lastSelectedLayerChunk = MapDataManager.mapDataCurrentLayer;

                V2 targetPosId = new V2();
                targetPosId.x = worldPosition.x;
                targetPosId.y = worldPosition.z;

                if (chunkCluster.chunks != null)
                {
                    for (int i = 0; i < chunkCluster.chunks.Length; i++)
                    {
                        if (chunkCluster.chunks[i].posId.x == worldPosition.x && chunkCluster.chunks[i].posId.y == worldPosition.z)
                        {
                            lastSelectedChunk = chunkCluster.chunks[i];
                            return(lastSelectedChunk);
                        }
                        if (i == chunkCluster.chunks.Length - 1 && createChunkIfNotFound)
                        {
                            // Create a new chunk entire in mapdata.
                            Array.Resize(ref chunkCluster.chunks, chunkCluster.chunks.Length + 1);
                            Chunk newChunk = new Chunk();
                            newChunk.posId = targetPosId;

                            // Create an actual chunk GameObject in the level.
                            newChunk.chunkObjRef                    = new GameObject(chunkCluster.chunks.Length - 1 + " chunk");
                            newChunk.chunkObjRef.isStatic           = true;
                            newChunk.chunkObjRef.transform.position = worldPosition;
                            newChunk.meshObjRef = newChunk.chunkObjRef.AddComponent <TileMesh2D>();
                            newChunk.chunkObjRef.transform.parent = chunkCluster.clusterObjRef.transform;

                            // Applay the new ChunkCluster Array to the current layer.
                            return(lastSelectedChunk = chunkCluster.chunks[chunkCluster.chunks.Length - 1] = newChunk);
                        }
                    }
                }
                else
                {
                    if (createChunkIfNotFound)
                    {
                        // Create a new chunk cluster entire in mapdata.
                        Chunk newChunk = new Chunk();
                        newChunk.posId = targetPosId;

                        // Create an actual gameobject in the level.
                        newChunk.chunkObjRef                    = new GameObject("0" + " chunk");
                        newChunk.chunkObjRef.isStatic           = true;
                        newChunk.chunkObjRef.transform.position = worldPosition;
                        newChunk.meshObjRef = newChunk.chunkObjRef.AddComponent <TileMesh2D>();
                        newChunk.chunkObjRef.transform.parent = chunkCluster.clusterObjRef.transform;

                        // Applay the new ChunkCluster Array to the current layer.
                        chunkCluster.chunks = new Chunk[1];
                        return(lastSelectedChunk = chunkCluster.chunks[0] = newChunk);
                    }
                }
            }
            return(null);
        }