Beispiel #1
0
        public static Sv3 TileVector(Vector3 worldPosition)
        {
            Vector3 offset = ChunkPoissonId(worldPosition) / 0.32f;

            worldPosition.x  = (int)Math.Ceiling(worldPosition.x / 0.32f) + mapWidth * 0.5f;
            worldPosition.x -= offset.x;

            worldPosition.z  = (int)Math.Ceiling(worldPosition.z / 0.32f) + mapWidth * 0.5f;
            worldPosition.z -= offset.z;


            Sv3 target = new Sv3();

            target.x  = (short)worldPosition.x;
            target.y  = (short)worldPosition.z;
            target.y -= 1;

            return(target);
        }
Beispiel #2
0
        // Get a reference to a chunk, then query the tiles to get the desired tile.
        public static void GetTile(Vector3 worldPos, Chunk targetChunk, bool createTileIfNotFound, int layer, int newTileId, bool removeIfFound = false)
        {
            if (targetChunk == null && removeIfFound)
            {
                return;
            }

            if (targetChunk.tileLayers == null)
            {
                if (createTileIfNotFound)
                {
                    targetChunk.tileLayers             = new BitArray[1];
                    targetChunk.textureId              = new ushort[1];
                    targetChunk.textureId[0]           = (ushort)newTileId;
                    targetChunk.dataLayers             = new DataLayer[1];
                    targetChunk.dataLayers[0].dataLine = new BitArray[32];
                }
            }

            if (targetChunk.tileLayers.Length - 1 <= layer)
            {
                Array.Resize(ref targetChunk.tileLayers, layer + 1);
                Array.Resize(ref targetChunk.textureId, layer + 1);
                Array.Resize(ref targetChunk.dataLayers, layer + 1);
            }

            Sv3 tileId = TileVector(worldPos);

            // Remove the target tile at the churrent layer.

            if (removeIfFound)
            {
                if (targetChunk.tileLayers[layer] == null || targetChunk == null)
                {
                    return;
                }
                if (targetChunk.tileLayers[layer][tileId.y])
                {
                    // If the vertical flag is true = 1
                    // Set the vertical line to 0 and generate a data line for this index.

                    targetChunk.tileLayers[layer][tileId.y]                    = false;
                    targetChunk.dataLayers[layer].dataLine[tileId.y]           = new BitArray(32, true);
                    targetChunk.dataLayers[layer].dataLine[tileId.y][tileId.x] = false;
                }
                else
                {
                    // If the vertical flag is false = 0
                    if (targetChunk.dataLayers[layer].dataLine[tileId.y] != null)
                    {
                        targetChunk.dataLayers[layer].dataLine[tileId.y][tileId.x] = false;
                    }
                    else
                    {
                        return;
                    }
                }

                if (targetChunk.meshObjRef == null)
                {
                    targetChunk.meshObjRef = targetChunk.chunkObjRef.GetComponent <TileMesh2D>();
                }

                if (targetChunk.meshObjRef.tileObjectLayers != null)
                {
                    if (targetChunk.meshObjRef.tileObjectLayers[layer] != null)
                    {
                        targetChunk.meshObjRef.tileObjectLayers[layer].currentLayer = (ushort)layer;
                        targetChunk.meshObjRef.tileObjectLayers[layer].curCunk      = targetChunk;
                        targetChunk.meshObjRef.tileObjectLayers[layer].StartGenerate();
                    }
                }
                else
                {
                    targetChunk.meshObjRef.StartGenerate();
                }
            }
            if (createTileIfNotFound)
            {
                // Get the current chunk and check if the current target layer dose exist inside
                // the map data, if not then register a new layer.
                if (targetChunk.tileLayers[layer] == null)
                {
                    targetChunk.tileLayers[layer] = new BitArray(32);
                    targetChunk.textureId[layer]  = (ushort)newTileId;
                }

                //
                if (!targetChunk.tileLayers[layer][tileId.y])
                {
                    if (targetChunk.dataLayers[layer].dataLine == null)
                    {
                        targetChunk.dataLayers[layer].dataLine = new BitArray[32];
                    }
                    if (targetChunk.dataLayers[layer].dataLine[tileId.y] != null)
                    {
                        if (targetChunk.dataLayers[layer].dataLine[tileId.y].Length < tileId.x)
                        {
                            targetChunk.dataLayers[layer].dataLine[tileId.y].Length = tileId.x;
                        }
                    }
                    else
                    {
                        targetChunk.dataLayers[layer].dataLine[tileId.y] = new BitArray(32);
                    }

                    targetChunk.dataLayers[layer].dataLine[tileId.y].Set(tileId.x, true);

                    if (targetChunk.dataLayers[layer].dataLine[tileId.y].Length == 32)
                    {
                        for (int i = 0; i < 32; i++)
                        {
                            if (targetChunk.dataLayers[layer].dataLine[tileId.y][i] == false)
                            {
                                break;
                            }
                            if (i == targetChunk.dataLayers[layer].dataLine[tileId.y].Length - 1)
                            {
                                targetChunk.dataLayers[layer].dataLine[tileId.y] = null;
                                targetChunk.tileLayers[layer][tileId.y]          = true;
                            }
                        }
                    }
                }

                // Get the cunk tilemesh component and check if the parrent mesh has any children an then find
                // if the current tile id exists as a layer.

                if (targetChunk.meshObjRef == null)
                {
                    targetChunk.meshObjRef = targetChunk.chunkObjRef.GetComponent <TileMesh2D>();
                }
                if (layer != 0)
                {
                    if (targetChunk.meshObjRef.tileObjectLayers == null)
                    {
                        targetChunk.meshObjRef.GenerateLayers();
                    }

                    if (targetChunk.tileLayers.Length != targetChunk.meshObjRef.tileObjectLayers.Length)
                    {
                        targetChunk.meshObjRef.GenerateLayers();
                    }
                    //Debug.Log(targetChunk.meshObjRef);
                    //Debug.Log(targetChunk.meshObjRef.tileObjectLayers.Length);
                    //Debug.Log(layer);
                    //Debug.Log(targetChunk.tileLayers.Length);
                    if (targetChunk.meshObjRef.tileObjectLayers[layer] == null)
                    {
                        targetChunk.meshObjRef.GenerateLayers();
                    }
                }

                if (targetChunk.meshObjRef.tileObjectLayers == null)
                {
                    //targetChunk.meshObjRef.currentLayer = (ushort) 0;
                    targetChunk.meshObjRef.curCunk = targetChunk;
                    targetChunk.meshObjRef.StartGenerate();
                }
                else
                {
                    if (targetChunk.meshObjRef.tileObjectLayers[layer] != null)
                    {
                        targetChunk.meshObjRef.tileObjectLayers[layer].currentLayer = (ushort)layer;
                        targetChunk.meshObjRef.tileObjectLayers[layer].curCunk      = targetChunk;
                        targetChunk.meshObjRef.tileObjectLayers[layer].StartGenerate();
                    }
                }
            }
        }