예제 #1
0
        public void SetPreviewTilemap(Unity_Map map, Unity_Tile[,] newTiles)
        {
            int w = newTiles.GetLength(0);
            int h = newTiles.GetLength(1);

            if (w <= 0 || h <= 0)
            {
                ClearPreviewTilemap();
                return;
            }
            int       cellSize = LevelEditorData.Level.CellSize;
            Texture2D tex      = new Texture2D(w * cellSize, h * cellSize);

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    Unity_Tile        newTile = newTiles[x, y];
                    Unity_TileTexture tile    = map.GetTile(newTile, LevelEditorData.CurrentSettings);
                    FillInTilePixels(tex, tile, newTile, x, y, cellSize, applyTexture: false);
                }
            }
            tex.filterMode = FilterMode.Point;
            tex.Apply();
            tilemapPreview.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0), LevelEditorData.Level.PixelsPerUnit, 0, SpriteMeshType.FullRect);
        }
예제 #2
0
 /// <summary>
 /// Creates an empty tileset with a single transparent tile
 /// </summary>
 public Unity_TileSet(int cellSize)
 {
     Tiles = new Unity_TileTexture[]
     {
         TextureHelpers.CreateTexture2D(cellSize, cellSize, true, true).CreateTile()
     };
 }
예제 #3
0
        /// <summary>
        /// Creates a tile set from a tile map
        /// </summary>
        /// <param name="tileMapColors">The tile map colors</param>
        /// <param name="tileMapWidth">The tile map width, in tiles</param>
        /// <param name="cellSize">The tile size</param>
        public Unity_TileSet(IList <BaseColor> tileMapColors, int tileMapWidth, int cellSize)
        {
            // Create the tile array
            Tiles = new Unity_TileTexture[tileMapColors.Count / (cellSize * cellSize)];

            // Create each tile
            for (var index = 0; index < Tiles.Length; index++)
            {
                // Create the texture
                Texture2D tex = TextureHelpers.CreateTexture2D(cellSize, cellSize);

                // Get the tile x and y
                var tileY = (int)Math.Floor(index / (double)tileMapWidth);
                var tileX = (index - (tileMapWidth * tileY));

                var tileOffset = (tileY * tileMapWidth * cellSize * cellSize) + (tileX * cellSize);

                // Set every pixel
                for (int y = 0; y < cellSize; y++)
                {
                    for (int x = 0; x < cellSize; x++)
                    {
                        tex.SetPixel(x, y, tileMapColors[(tileOffset + (y * cellSize * tileMapWidth + x))].GetColor());
                    }
                }

                // Apply the pixels
                tex.Apply();

                // Create a tile
                Tiles[index] = tex.CreateTile();
            }
        }
예제 #4
0
        private void Update()
        {
            if (Controller.LoadState != Controller.State.Finished)
            {
                return;
            }

            CheckPaletteChange();

            if (animatedTiles == null || !Settings.AnimateTiles)
            {
                return;
            }

            for (int mapIndex = 0; mapIndex < animatedTiles.Length; mapIndex++)
            {
                bool      changedTile = false;
                var       map         = LevelEditorData.Level.Maps[mapIndex];
                Texture2D tex         = GraphicsTilemaps[mapIndex].sprite.texture;
                foreach (var animatedTile in animatedTiles[mapIndex].Keys)
                {
                    var animSpeed = animatedTile.AnimationSpeed / 30f;
                    foreach (var at in animatedTiles[mapIndex][animatedTile])
                    {
                        //print("Updating " + at.x + " - " + at.y);
                        at.currentTimer += Time.deltaTime;

                        if (!(at.currentTimer >= animSpeed))
                        {
                            continue;
                        }

                        int frames       = Mathf.FloorToInt(at.currentTimer / animSpeed);
                        int oldTileIndex = at.tileIndex;
                        at.tileIndex = (oldTileIndex + frames) % at.animatedTile.TileIndices.Length;

                        int oldIndexInTileset = at.animatedTile.TileIndices[oldTileIndex];
                        int newIndexInTileset = at.animatedTile.TileIndices[at.tileIndex];
                        if (oldIndexInTileset != newIndexInTileset)
                        {
                            changedTile = true;
                            var newTile            = map.MapTiles[at.y * map.Width + at.x];
                            Unity_TileTexture tile = map.GetTile(newTile, LevelEditorData.CurrentSettings, tileIndexOverride: newIndexInTileset);

                            int cellSize = LevelEditorData.Level.CellSize;
                            FillInTilePixels(tex, tile, newTile, at.x, at.y, cellSize, applyTexture: false);
                        }
                        at.currentTimer -= frames * animSpeed;
                    }
                }
                if (changedTile)
                {
                    tex.Apply();
                }
            }
        }
예제 #5
0
        public static Unity_TileTexture CreateTile(this Texture2D tex, Rect?rect = null)
        {
            var t = new Unity_TileTexture()
            {
                rect    = rect ?? new Rect(0, 0, tex.width, tex.height),
                texture = tex
            };

            return(t);
        }
예제 #6
0
        /// <summary>
        /// Creates a tileset with a single colored tile
        /// </summary>
        public Unity_TileSet(int cellSize, Color color)
        {
            var tex = TextureHelpers.CreateTexture2D(cellSize, cellSize);

            tex.SetPixels(Enumerable.Repeat(color, cellSize * cellSize).ToArray());

            tex.Apply();

            Tiles = new Unity_TileTexture[]
            {
                tex.CreateTile()
            };
        }
예제 #7
0
        public void SetTileAtPos(int x, int y, Unity_Tile newTile, bool applyTexture = true)
        {
            var map = LevelEditorData.Level.Maps[LevelEditorData.CurrentMap];

            if (y < 0 || y >= map.Height)
            {
                return;
            }
            if (x < 0 || x >= map.Width)
            {
                return;
            }

            // Update tile graphics
            for (int i = 0; i < CollisionTilemaps.Length; i++)
            {
                CollisionTilemaps[i].SetTile(new Vector3Int(x, y, 0), null);
                var type = LevelEditorData.Level.GetCollisionTypeGraphicFunc(newTile.Data.CollisionType);
                if (CurrentCollisionIcons.ContainsKey(type))
                {
                    CollisionTilemaps[i].SetTile(new Vector3Int(x, y, 0), CurrentCollisionIcons[type]);
                }
            }
            for (int i = 0; i < GraphicsTilemaps.Length; i++)
            {
                Texture2D         tex  = GraphicsTilemaps[i].sprite.texture;
                Unity_TileTexture tile = map.GetTile(newTile, LevelEditorData.CurrentSettings);

                int cellSize = LevelEditorData.Level.CellSize;
                FillInTilePixels(tex, tile, newTile, x, y, cellSize, applyTexture: applyTexture);
            }

            // Get the tile to set
            var destTile = map.MapTiles[y * map.Width + x];

            // Update destination tile values
            destTile.Data.CollisionType = newTile.Data.CollisionType;
            destTile.PaletteIndex       = newTile.PaletteIndex;
            destTile.Data.TileMapX      = newTile.Data.TileMapX;
            destTile.Data.TileMapY      = newTile.Data.TileMapY;
            destTile.Data.PC_Unk1       = newTile.Data.PC_Unk1;
            destTile.Data.PC_Unk2       = newTile.Data.PC_Unk2;

            // Get the correct transparency mode to set if available
            if (map.TileSetTransparencyModes != null)
            {
                destTile.Data.PC_TransparencyMode = map.TileSetTransparencyModes[(map.TileSetWidth * newTile.Data.TileMapY) + newTile.Data.TileMapX];
            }
        }
예제 #8
0
        /// <summary>
        /// Creates a tile set from a tile-set texture
        /// </summary>
        /// <param name="tileSet">The tile-set texture</param>
        /// <param name="cellSize">The tile size</param>
        public Unity_TileSet(Texture2D tileSet, int cellSize)
        {
            // Create the tile array
            Tiles = new Unity_TileTexture[(tileSet.width / cellSize) * (tileSet.height / cellSize)];

            // Keep track of the index
            var index = 0;

            // Extract every tile
            for (int y = 0; y < tileSet.height; y += cellSize)
            {
                for (int x = 0; x < tileSet.width; x += cellSize)
                {
                    // Create a tile
                    Tiles[index] = tileSet.CreateTile(new Rect(x, y, cellSize, cellSize));

                    index++;
                }
            }
        }
예제 #9
0
        private void FillInTilePixels(Texture2D tex, Unity_TileTexture tile, Unity_Tile t, int x, int y, int cellSize, bool applyTexture = false)
        {
            int  texX  = x * cellSize;
            int  texY  = y * cellSize;
            bool flipY = (t?.Data.VerticalFlip ?? false);
            bool flipX = (t?.Data.HorizontalFlip ?? false);

            if (tile?.texture == null)
            {
                tex.SetPixels(texX, texY, cellSize, cellSize, new Color[cellSize * cellSize]);
            }
            else
            {
                var tileTex = tile.texture;
                tex.SetPixels(texX, texY, cellSize, cellSize, tile.GetPixels(flipX, flipY));
            }
            if (applyTexture)
            {
                tex.Apply();
            }
        }
예제 #10
0
        public Unity_MapTileMap LoadTileSet(PCX pcx)
        {
            // Get the tilemap texture
            var tileMap = pcx.ToTexture();

            var tileSetWidth  = tileMap.width / Settings.CellSize;
            var tileSetHeight = tileMap.height / Settings.CellSize;

            // Create the tile array
            var tiles = new Unity_TileTexture[tileSetWidth * tileSetHeight];

            // Get the transparency color
            var transparencyColor = tileMap.GetPixel(0, 0);

            // Replace the transparency color with true transparency
            for (int y = 0; y < tileMap.height; y++)
            {
                for (int x = 0; x < tileMap.width; x++)
                {
                    if (tileMap.GetPixel(x, y) == transparencyColor)
                    {
                        tileMap.SetPixel(x, y, new Color(0, 0, 0, 0));
                    }
                }
            }

            tileMap.Apply();

            // Split the .pcx into a tile-set
            for (int ty = 0; ty < tileSetHeight; ty++)
            {
                for (int tx = 0; tx < tileSetWidth; tx++)
                {
                    // Create a tile
                    tiles[ty * tileSetWidth + tx] = tileMap.CreateTile(new Rect(tx * Settings.CellSize, ty * Settings.CellSize, Settings.CellSize, Settings.CellSize));
                }
            }

            return(new Unity_MapTileMap(tiles));
        }
예제 #11
0
        // Used to redraw all tiles with different palette (0 = auto, 1-3 = palette)
        public void RefreshTiles(int palette)
        {
            Debug.Log($"Refreshing tiles with palette {palette}");

            // Change button visibility
            _currentPalette = palette;
            for (int i = 0; i < paletteButtons.Length; i++)
            {
                ColorBlock b = paletteButtons[i].colors;
                b.normalColor            = _currentPalette == i ? new Color(1, 1, 1) : new Color(0.5f, 0.5f, 0.5f);
                paletteButtons[i].colors = b;
            }

            // Get the current level and map
            var lvl = LevelEditorData.Level;


            // If auto, refresh indexes
            if (palette == 0)
            {
                lvl.AutoApplyPalette();
            }

            // Refresh tiles for every map
            if (GraphicsTilemaps.Length != LevelEditorData.Level.Maps.Length)
            {
                Array.Resize(ref GraphicsTilemaps, LevelEditorData.Level.Maps.Length);
                for (int i = 1; i < GraphicsTilemaps.Length; i++)
                {
                    GraphicsTilemaps[i] = Instantiate <SpriteRenderer>(GraphicsTilemaps[0], new Vector3(0, 0, -i), Quaternion.identity, GraphicsTilemaps[0].transform.parent);
                    GraphicsTilemaps[i].gameObject.name = "Tilemap Graphics " + i;
                    if (lvl.Maps[i].IsForeground)
                    {
                        Debug.Log($"{i} is in front");
                        //TilemapRenderer tr = GraphicsTilemaps[i].GetComponent<TilemapRenderer>();
                        SpriteRenderer tr = GraphicsTilemaps[i];
                        tr.sortingLayerName = "Tiles Front";
                    }
                }
            }
            animatedTiles = new Dictionary <Unity_AnimatedTile, List <Unity_AnimatedTile.Instance> > [GraphicsTilemaps.Length];
            for (int mapIndex = 0; mapIndex < LevelEditorData.Level.Maps.Length; mapIndex++)
            {
                var map = lvl.Maps[mapIndex];
                animatedTiles[mapIndex] = new Dictionary <Unity_AnimatedTile, List <Unity_AnimatedTile.Instance> >();
                if (map.IsAdditive)
                {
                    GraphicsTilemaps[mapIndex].material = additiveMaterial;
                }
                if (map.Alpha.HasValue)
                {
                    GraphicsTilemaps[mapIndex].color = new Color(1f, 1f, 1f, map.Alpha.Value);
                }
                int       cellSize = LevelEditorData.Level.CellSize;
                Texture2D tex      = TextureHelpers.CreateTexture2D(map.Width * cellSize, map.Height * cellSize);

                for (int y = 0; y < map.Height; y++)
                {
                    for (int x = 0; x < map.Width; x++)
                    {
                        var t = map.MapTiles[y * map.Width + x];

                        if (palette != 0)
                        {
                            t.PaletteIndex = palette;
                        }

                        if (t.PaletteIndex - 1 >= map.TileSet.Length)
                        {
                            t.PaletteIndex = 1;
                        }

                        Unity_TileTexture tile = map.GetTile(t, LevelEditorData.CurrentSettings);
                        var atInstance         = map.GetAnimatedTile(t, LevelEditorData.CurrentSettings);
                        if (atInstance != null)
                        {
                            atInstance.x = x;
                            atInstance.y = y;
                            var at = atInstance.animatedTile;
                            if (!animatedTiles[mapIndex].ContainsKey(at))
                            {
                                animatedTiles[mapIndex][at] = new List <Unity_AnimatedTile.Instance>();
                            }
                            animatedTiles[mapIndex][at].Add(atInstance);
                        }
                        FillInTilePixels(tex, tile, t, x, y, cellSize);

                        /*GraphicsTilemaps[mapIndex].SetTile(new Vector3Int(x, y, 0), map.GetTile(t, LevelEditorData.CurrentSettings));
                         * GraphicsTilemaps[mapIndex].SetTransformMatrix(new Vector3Int(x, y, 0), GraphicsTilemaps[mapIndex].GetTransformMatrix(new Vector3Int(x, y, 0)) * Matrix4x4.Scale(new Vector3(t.Data.HorizontalFlip ? -1 : 1, t.Data.VerticalFlip ? -1 : 1, 1)));*/
                    }
                }
                tex.filterMode = FilterMode.Point;
                tex.Apply();
                // Note: FullRect is important, otherwise when editing you need to create a new sprite
                GraphicsTilemaps[mapIndex].sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0), LevelEditorData.Level.PixelsPerUnit, 0, SpriteMeshType.FullRect);
            }

            CreateTilemapFull();
        }