Пример #1
0
        private void Initialize(Map map, uint[] data, bool makeUnique, List <Material> materials)
        {
            Tiles = new TileGrid(Width, Height);

            // data is left-to-right, top-to-bottom
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    uint index = data[y * Width + x];

                    // compute the SpriteEffects to apply to this tile
                    SpriteEffects spriteEffects = SpriteEffects.None;
                    if ((index & FlippedHorizontallyFlag) != 0)
                    {
                        spriteEffects |= SpriteEffects.FlipHorizontally;
                    }
                    if ((index & FlippedVerticallyFlag) != 0)
                    {
                        spriteEffects |= SpriteEffects.FlipVertically;
                    }

                    // strip out the flip flags to get the real ID
                    int id = (int)(index & ~(FlippedVerticallyFlag | FlippedHorizontallyFlag));
                    //Debug.Log("Tile ID: " + id + " (index : " + index + ")");
                    // get the tile
                    Tile t = null;
                    map.Tiles.TryGetValue(id, out t);

                    // if the tile is non-null...
                    if (t != null)
                    {
                        // if we want unique instances, clone it
                        if (makeUnique)
                        {
                            t = t.Clone();
                            t.SpriteEffects = spriteEffects;
                        }

                        // otherwise we may need to clone if the tile doesn't have the correct effects
                        // in this world a flipped tile is different than a non-flipped one; just because
                        // they have the same source rect doesn't mean they're equal.
                        else if (t.SpriteEffects != spriteEffects)
                        {
                            t = t.Clone();
                            t.SpriteEffects = spriteEffects;
                        }
                    }

                    // put that tile in our grid
                    Tiles[x, y] = t;
                }
            }

            GenerateLayerMesh(map, materials);
        }
Пример #2
0
        public void ReplaceTile(int id, int x, int y, Map map, Material material)
        {
            GameObject tileObject = Tiles[x, y].TileObject;

            //Debug.Log("Tile ID: " + id + " (index : " + index + ")");
            // get the tile
            Tile t = null;

            map.Tiles.TryGetValue(id, out t);

            // if the tile is non-null...
            if (t != null)
            {
                t = t.Clone();
            }

            Tiles[x, y] = t;

            List <Material> materials = new List <Material>();

            materials.Add(material);

            // Create Tile's GameObject
            t.CreateTileObject(Name + "[" + x + ", " + y + "]",
                               LayerGameObject.transform,
                               Name,
                               tileObject.transform.localPosition,
                               materials,
                               Opacity);

            GameObject.Destroy(tileObject);
        }
Пример #3
0
        /// <summary>
        /// Sets a Tile in position x and y to be Tile with ID equals newTileID (a Global Tile ID)
        /// This does not works if the layer is not made of Unique Tiles.
        /// </summary>
        /// <param name="x">Tile X index</param>
        /// <param name="y">Tile Y index</param>
        /// <param name="newTileID">Global Tile ID to change existing tile to. If -1 is passed, erase current Tile</param>
        /// <returns>true if newTileID was found and change succeded, false otherwise</returns>
        public bool SetTile(int x, int y, int newTileID, TileSet tileSet = null)
        {
            if (x < 0 || x >= Width || y < 0 || y >= Height || !MakeUniqueTiles)
            {
                return(false);
            }

            if (newTileID < 0)
            {
                if (Tiles[x, y] != null)
                {
                    GameObject.Destroy(Tiles[x, y].TileGameObject);
                }
                Tiles[x, y] = null;
                return(true);
            }

            Tile t = null;

            if ((tileSet == null ? BaseMap.Tiles : tileSet.Tiles).TryGetValue(newTileID, out t))
            {
                if (Tiles[x, y] != null)
                {
                    Tiles[x, y].TileSprite = t.TileSprite;
                    Tiles[x, y].CurrentID  = t.OriginalID;
                    (Tiles[x, y].TileGameObject.GetComponent <Renderer>() as SpriteRenderer).sprite = t.TileSprite;
                }
                else
                {
                    Tile newTile = t.Clone();
                    CreateTileGameObject(newTile, x, y);
                    Tiles[x, y] = newTile;
                }
                return(true);
            }

            return(false);
        }
Пример #4
0
        private void Initialize(Map map, uint[] data, List <Material> materials)
        {
            Tiles         = new TileGrid(Width, Height);
            BaseMap       = map;
            BaseMaterials = materials;
            LayerTileSets = new List <TileSet>();
            // data is left-to-right, top-to-bottom
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    uint id = data[y * Width + x];

                    // compute the SpriteEffects to apply to this tile
                    SpriteEffects spriteEffects = new SpriteEffects();

                    // MARIO: new method to verify flipped tiles
                    spriteEffects.flippedHorizontally   = (id & FlippedHorizontallyFlag) == FlippedHorizontallyFlag;
                    spriteEffects.flippedVertically     = (id & FlippedVerticallyFlag) == FlippedVerticallyFlag;
                    spriteEffects.flippedAntiDiagonally = (id & FlippedAntiDiagonallyFlag) == FlippedAntiDiagonallyFlag;

                    // MARIO: new strip out the flip flags to get the real ID
                    // Fixed for AntiDiagonallyFlgs
                    id &= ~(FlippedHorizontallyFlag |
                            FlippedVerticallyFlag |
                            FlippedAntiDiagonallyFlag);

                    // get the tile
                    Tile t = null;
                    BaseMap.Tiles.TryGetValue((int)id, out t);

                    // if the tile is non-null...
                    if (t != null)
                    {
                        // if we want unique instances, clone it
                        if (MakeUniqueTiles)
                        {
                            t = t.Clone();
                            t.SpriteEffects = spriteEffects;
                        }

                        // otherwise we may need to clone if the tile doesn't have the correct effects
                        // in this world a flipped tile is different than a non-flipped one; just because
                        // they have the same source rect doesn't mean they're equal.
                        else if (t.SpriteEffects != spriteEffects)
                        {
                            t = t.Clone();
                            t.SpriteEffects = spriteEffects;
                        }

                        // Add this Tile's TileSet to LayerTileSets list, if it is not there yet
                        if (!LayerTileSets.Contains(t.TileSet))
                        {
                            LayerTileSets.Add(t.TileSet);
                        }
                    }

                    // put that tile in our grid
                    Tiles[x, y] = t;
                }
            }

            GenerateLayer();
        }