예제 #1
0
        public TileDat getTileAtPosition(Vector2 Position, bool makeDirty = false)
        {
            // Get the position in the array of tiles.
            int pos = this.vectorToArrayPosition(Position);

            bool outsideRange = false;

            // end of the chunk
            if (Position.x < 0 || Position.y < 0 || Position.x >= this.chunkSize || Position.y >= this.chunkSize)
            {
                outsideRange = true;
            }

            if (pos >= this.tiles.Capacity || pos < 0 || outsideRange)
            {
                // Anti recursion check, catches invalid 'states'
                if (this.fetchTileInProgress)
                {
                    // bail! < TODO return default dat
                    TileDat transparent = new TileDat();
                    transparent.tile         = new Tile();
                    transparent.tile.prop    = TilePropType.prop_air;
                    this.fetchTileInProgress = false;
                    return(transparent);
                }

                // try and fetch from parent! slow af, mark fetch to handle fail state
                this.fetchTileInProgress = true;
                return(this.parent.getParent().getTileAtPosition((Vector2)this.position + Position, (LayerType)this.parent.LayerType, makeDirty));

                this.fetchTileInProgress = false;
            }

            return(this.tiles[pos]);
        }
예제 #2
0
        public void setTile(Vector2 Position, Tile Tile)
        {
            int pos = this.vectorToArrayPosition(Position);

            bool outsideRange = false;

            // end of the chunk
            if (Position.x < 0 || Position.y < 0 || Position.x >= this.chunkSize || Position.y >= this.chunkSize)
            {
                outsideRange = true;
            }

            if (pos >= this.tiles.Capacity || pos < 0 || outsideRange)
            {
                // Anti recursion check, catches invalid 'states'
                if (this.fetchTileInProgress)
                {
                    // bail! < do nothing
                    return;
                }

                this.fetchTileInProgress = true;
                this.parent.getParent().setTile((Vector2)this.position + Position, Tile);
                this.fetchTileInProgress = false;
                return;
            }

            // Call on destroy for behaviours
            foreach (TileBehaviour behaviour in this.tiles[pos].tile.behaviours)
            {
                behaviour.onDestroy(this.tiles[pos]);
            }

            // Get the tile data object.
            TileDat tileData = this.tiles[pos];

            tileData.flags = 0;
            tileData.tile  = Tile;

            updateAjacentTiles(Position);

            // Call on init for behaviours
            foreach (TileBehaviour behaviour in Tile.behaviours)
            {
                behaviour.onInit(tileData);
            }

            // set the buffer dirty.
            this.isDirty = true;
        }
예제 #3
0
        public void init()
        {
            float chunkOrigin = this.chunkSize * 0.5f;
            float unitScale   = this.Parent.getParent().tileUnitSize;

            this.mesh     = new Mesh();
            this.geometry = new GameObject("TileChunk");
            this.geometry.transform.parent     = this.parent.gameObject.transform;
            this.geometry.transform.position   = this.position + new Vector3(chunkOrigin * unitScale, chunkOrigin * unitScale, 0);
            this.geometry.transform.localScale = Vector3.one * unitScale;

            this.meshRenderer = this.geometry.AddComponent <MeshRenderer>();
            this.meshFilter   = this.geometry.AddComponent <MeshFilter>();

            this.meshRenderer.material = this.parent.getParent().textureMap.getTextureForID(0).material;

            int chunkOffset = -(int)this.chunkSize / 2;

            // Add row by row
            for (int y = 0; y < this.chunkSize; ++y)
            {
                for (int x = 0; x < this.chunkSize; ++x)
                {
                    // Create a new tile
                    TileDat tileToAdd = new TileDat();

                    // Parents layer parent is the tile map itself, default use first tile in map, which is air!
                    tileToAdd.tile   = this.parent.getParent().getTile(0);
                    tileToAdd.parent = this;

                    // tiles are offset by chunk offset, so the gameobjects center is in the center of the chunk
                    tileToAdd.position = new Vector3(x + chunkOffset, y + chunkOffset, 0) + this.geometry.transform.position;
                    tiles.Add(tileToAdd);
                }
            }

            // updates enture tile layer!
            updateAjacentTiles();

            // Call init on all tiles in  the map!
            foreach (TileDat Tile in tiles)
            {
                foreach (TileBehaviour behaviour in Tile.tile.behaviours)
                {
                    behaviour.onInit(Tile);
                }
            }
        }
예제 #4
0
        void updateAjacentTiles(Vector2 Position)
        {
            // Optimise this don't do a parent fetch if we know it is in bounds todo < will make call alot faster most the time
            TileDat current = this.getTileAtPosition(Position);
            TileDat up      = this.getTileAtPosition(Position + Vector2.up);
            TileDat down    = this.getTileAtPosition(Position - Vector2.up);
            TileDat right   = this.getTileAtPosition(Position + Vector2.right);
            TileDat left    = this.getTileAtPosition(Position - Vector2.right);

            current.left  = left;
            current.right = right;
            current.up    = up;
            current.down  = down;

            down.up    = current;
            up.down    = current;
            left.right = current;
            right.left = current;
        }
예제 #5
0
        // returns a reference, get triggers the buffer to be marked as dirty?
        public TileDat getTileAtPosition(Vector2 Position, LayerType layer, bool dirtyBuffer = false)
        {
            // Work out which chunk the tile resides in, loses float precision however should be caught by out of range guard, regardless.
            uint chunkX = ((uint)Position.x / this.chunkSize);
            uint chunkY = ((uint)Position.y / this.chunkSize);

            // Optimise precalculate this
            uint _chunkDimensionXZ = this.dimensions / this.chunkSize;

            uint pos = (chunkX) + (chunkY * _chunkDimensionXZ);

            // Normalise the position to make relative to parent chunk.
            Vector2 normalisedPosition = Position - new Vector2(chunkX * this.chunkSize, chunkY * this.chunkSize);

            bool outOfRange = false;

            if (Position.x < 0 || Position.y < 0 || Position.x >= this.dimensions || Position.y >= this.dimensions)
            {
                outOfRange = true;
            }

            List <TileChunk> chunks = this.layers[(int)layer].getChunks();

            if (pos > chunks.Count - 1 || outOfRange || pos < 0)
            {
                TileDat transparent = new TileDat();
                transparent.tile      = new Tile();
                transparent.tile.prop = TilePropType.prop_air;
                return(transparent);
            }

            // Set the buffer dirty
            if (dirtyBuffer)
            {
                chunks[(int)pos].setDirty();
            }

            return(chunks[(int)pos].getTileAtPosition(normalisedPosition));
        }
예제 #6
0
 /// <summary>
 ///  Called by the tile system when a Tool is used on a tile
 /// </summary>
 /// <param name="Tile"> Tile that is being used. </param>
 /// <param name="Tool"> Tool that is being used </param>
 public virtual void onUse(TileDat Tile, Tool Tool)
 {
 }
예제 #7
0
        // Examples below havent been thought out and may be subject to change

        /// <summary>
        /// Called by the tile system when a Tool is hit on a tile
        /// </summary>
        /// <param name="Tile"> Tile that is being hit. </param>
        /// <param name="Tool"> Tool thats is doing the hitting </param>
        public virtual void onHit(TileDat Tile, Tool Tool)
        {
        }
예제 #8
0
 /// <summary>
 /// Called by the tile system every X ticks
 /// </summary>
 /// <param name="Tile"> The Tile it is operating on is passed into the update method. </param>
 public virtual void onUpdate(TileDat Tile, float fDelta)
 {
 }
예제 #9
0
 /// <summary>
 /// Called by the tile system every X ticks
 /// </summary>
 /// <param name="Tile"> The Tile it is operating on is passed into the update method. </param>
 public virtual void onUpdate(TileDat Tile)
 {
 }
예제 #10
0
 /// <summary>
 /// Called when the tile is removed from the map
 /// </summary>
 public virtual void onDestroy(TileDat Tile)
 {
 }
예제 #11
0
 /// <summary>
 /// Called when the tile is added to the map!
 /// </summary>
 /// <param name="Tile"></param>
 public virtual void onInit(TileDat Tile)
 {
 }
예제 #12
0
        void renderPass(RenderFlags RenderPass)
        {
            // Calculate vertex buffer size. 6 per face
            uint vertexBufferSize = 6 * (this.chunkSize * this.chunkSize);

            List <Vector3> vertices = new List <Vector3>();
            List <Vector2> uvs      = new List <Vector2>();
            List <Vector3> normals  = new List <Vector3>();

            int vertexOffset = 0;
            int chunkOffset  = -(int)this.chunkSize / 2;


            for (int x = 0; x < this.chunkSize; ++x)
            {
                for (int y = 0; y < this.chunkSize; ++y)
                {
                    int z = 0;

                    // Tile were rendering
                    TileDat tile = this[x, y];

                    // DON'T render air -> this should be figured out earlier then pushed to be rendered or simplified
                    if (!tile.tile.rendered || tile.tile.prop == TilePropType.prop_air || tile.tile.prop == TilePropType.prop_vacumn)
                    {
                        continue;
                    }

                    // Used to determine the vertex offset & whether we need to add to the sort list ( not used )
                    byte _offset = 0;

                    // Back
                    vertices.Add(new Vector3(x + chunkOffset, y + chunkOffset, z));
                    vertices.Add(new Vector3(x + chunkOffset, y + chunkOffset + 1, z));
                    vertices.Add(new Vector3(x + chunkOffset + 1, y + chunkOffset + 1, z));
                    vertices.Add(new Vector3(x + chunkOffset, y + chunkOffset, z));
                    vertices.Add(new Vector3(x + chunkOffset + 1, y + chunkOffset + 1, z));
                    vertices.Add(new Vector3(x + chunkOffset + 1, y + chunkOffset, z));

                    uvs.Add(tile.tile.material.textureCoords[0]);
                    uvs.Add(tile.tile.material.textureCoords[1]);
                    uvs.Add(tile.tile.material.textureCoords[2]);
                    uvs.Add(tile.tile.material.textureCoords[0]);
                    uvs.Add(tile.tile.material.textureCoords[2]);
                    uvs.Add(tile.tile.material.textureCoords[3]);

                    normals.Add(Vector3.forward);
                    normals.Add(Vector3.forward);
                    normals.Add(Vector3.forward);
                    normals.Add(Vector3.forward);
                    normals.Add(Vector3.forward);
                    normals.Add(Vector3.forward);

                    _offset += 6;

                    vertexOffset += _offset;
                }
            }

            int[] triangles = new int[vertexOffset];
            for (int i = 0; i < vertexOffset; ++i)
            {
                triangles[i] = i;
            }

            this.mesh.vertices = vertices.ToArray();
            this.mesh.uv       = uvs.ToArray();
            //this.mesh.triangles = triangles;
            this.mesh.SetTriangles(triangles, 0, false);
            this.mesh.normals = normals.ToArray();
        }