Esempio n. 1
0
        public override List <Point> GetRegion(Point point, ScriptableTile tile, TileMap map)
        {
            region = new List <Point>();
            if (end == start)
            {
                return(base.GetRegion(point, tile, map));
            }

            int x0 = Mathf.Min(start.x, end.x),
                x1 = Mathf.Max(start.x, end.x),
                y0 = Mathf.Min(start.y, end.y),
                y1 = Mathf.Max(start.y, end.y);

            for (int x = x0; x <= x1; x++)
            {
                for (int y = y0; y <= y1; y++)
                {
                    if (filled || (x == x0 || x == x1 || y == y0 || y == y1))
                    {
                        region.Add(new Point(x, y));
                    }
                }
            }

            return(region);
        }
Esempio n. 2
0
 //The region to draw a tool preview for
 public virtual List <Point> GetRegion(Point point, ScriptableTile tile, TileMap map)
 {
     region = new List <Point> {
         point
     };
     return(region);
 }
Esempio n. 3
0
        public void UpdateTile(int x, int y)
        {
            int            index   = x + y * tileMap.Width;
            SpriteRenderer current = spriteMap [index];

            if (current == null)
            {
                current = new GameObject(string.Format("[{0}, {1}]", x, y), typeof(PolygonCollider2D)).AddComponent <SpriteRenderer>();
                current.transform.position   = new Vector2(x, y);
                current.transform.localScale = Vector2.one;
                current.transform.SetParent(transform);
                current.gameObject.hideFlags = HideFlags.HideInHierarchy;

                current.sharedMaterial = material;
                current.color          = color;
                spriteMap [index]      = current;
            }
            ScriptableTile tile = tileMap.GetTileAt(x, y);

            if (current.GetComponent <PolygonCollider2D> ())
            {
                DestroyImmediate(current.GetComponent <PolygonCollider2D> ());
            }

            current.sprite = tile ? tile.GetSprite(tileMap, new Point(x, y)) : null;

            if (current.sprite)
            {
                current.gameObject.AddComponent <PolygonCollider2D> ();
            }
        }
Esempio n. 4
0
        //Called when the left mouse button is initially held down
        public override bool OnClickDown(Point point, ScriptableTile tile, TileMap map)
        {
            //Return if the tilemap is null/empty
            if (map == null)
            {
                return(false);
            }
            //Gets the tile where you clicked
            ScriptableTile start = map.GetTileAt(point);

            //Return if ther tile specified is null
            if (tile == null)
            {
                return(false);
            }
            //The queue of points that need to be changed to the specified tile
            Queue <Point> open = new Queue <Point> ();
            //The list of points already changed to the specified tile
            List <Point> closed = new List <Point> ();
            //A number larger than the amount of tiles available
            int maxLoops = map.Width * map.Height * 10;

            //Add the specified point to the open queue
            open.Enqueue(point);
            //As long as there are items in the queue, keep this running
            while (open.Count > 0)
            {
                //Decrement the max loops value
                maxLoops--;
                //If we've executed this code more than the max loops then we've done something wrong :/
                if (maxLoops <= 0)
                {
                    Debug.LogError("Fill tool, max loops reached!");
                    return(false);
                }

                Point p = open.Dequeue();
                if (closed.Contains(p))
                {
                    continue;
                }

                closed.Add(p);
                ScriptableTile t = map.GetTileAt(p);

                if (!map.IsInBounds(p) || t != start)
                {
                    continue;
                }

                open.Enqueue(p.Up);
                open.Enqueue(p.Right);
                open.Enqueue(p.Down);
                open.Enqueue(p.Left);

                map.SetTileAt(p, tile);
            }

            return(true);
        }
Esempio n. 5
0
        public bool SetTileAt(int x, int y, ScriptableTile to)
        {
            ScriptableTile from = GetTileAt(x, y);

            //Conditions for returning
            if (IsInBounds(x, y) &&
                !(from == null && to == null) &&
                (((from == null || to == null) && (from != null || to != null)) ||
                 from.ID != to.ID))
            {
                map [x + y * width] = to;

                OnTileChanged.Invoke(x, y);

                if (debugMode)
                {
                    Debug.LogFormat("Set [{0}, {1}] from {2} to {3}", x, y, from ? from.Name : "nothing", to ? to.Name : "nothing");
                }

                CurrentEdit.Add(new ChangeElement(x, y, from, to));


                return(true);
            }
            return(false);
        }
Esempio n. 6
0
 public ChangeElement(int _x, int _y, ScriptableTile _from, ScriptableTile _to)
 {
     x    = _x;
     y    = _y;
     from = _from;
     to   = _to;
 }
Esempio n. 7
0
        public override void UpdateTileAt(int x, int y)
        {
            int            index   = x + y * tileMap.Width;
            SpriteRenderer current = spriteMap[index];

            if (current == null)
            {
                current = new GameObject(string.Format("[{0}, {1}]", x, y)).AddComponent <SpriteRenderer>();
                current.transform.SetParent(parent);

                spriteMap[index] = current;
            }
            ScriptableTile tile = tileMap.GetTileAt(x, y);

            current.sprite = tile ? tile.GetSprite(tileMap, new Point(x, y)) : null;

            current.transform.localPosition = new Vector2(x, y) + (tile ?
                                                                   new Vector2(current.sprite.pivot.x / current.sprite.rect.width,
                                                                               current.sprite.pivot.y / current.sprite.rect.height) : Vector2.zero);

            current.transform.localScale = Vector2.one;
            current.sharedMaterial       = material;
            current.color          = color;
            current.sortingLayerID = sortingLayer;
            current.sortingOrder   = orderInLayer;

            current.gameObject.SetActive(tile != null);
        }
        public void Resize(int newWidth, int newHeight)
        {
            if ((newWidth <= 0 || newHeight <= 0) || (width == newWidth && height == newHeight))
            {
                return;
            }

            int oldWidth = width; //, oldHeight = height;

            ScriptableTile[] oldMap = map;

            map    = new ScriptableTile[newWidth * newHeight];
            width  = newWidth;
            height = newHeight;
            OnResize.Invoke(newWidth, newHeight);

            for (int i = 0; i < oldMap.Length; i++)
            {
                int            x    = i % oldWidth;
                int            y    = i / oldWidth;
                ScriptableTile tile = oldMap[i];
                if (tile && IsInBounds(x, y))
                {
                    SetTileAt(x, y, tile);
                }
            }
        }
Esempio n. 9
0
        public override bool OnClick(Point point, ScriptableTile tile, TileMap map)
        {
            if (tile == null && map == null)
            {
                return(false);
            }

            int correctedRadius = radius - 1;

            bool result = false;

            for (int x = -correctedRadius; x <= correctedRadius; x++)
            {
                for (int y = -correctedRadius; y <= correctedRadius; y++)
                {
                    Point offsetPoint = point + new Point(x, y);

                    if (map.SetTileAt(offsetPoint, null))
                    {
                        result = true;
                    }
                }
            }
            return(result);
        }
        public bool SetTileAt(int x, int y, ScriptableTile to)
        {
            ScriptableTile from = GetTileAt(x, y);

            //Conditions for returning
            if (IsInBounds(x, y) &&
                !(from == null && to == null) &&
                (((from == null || to == null) && (from != null || to != null)) ||
                 from.ID != to.ID))
            {
                map[x + y * width] = to;

                if (CurrentEdit == null)
                {
                    CurrentEdit = new List <ChangeElement>();
                }
                CurrentEdit.Add(new ChangeElement(x, y, from, to));

                UpdateTileAt(x, y);
                UpdateNeighbours(x, y, true);

                return(true);
            }
            return(false);
        }
Esempio n. 11
0
 public override bool OnClickUp(Point point, ScriptableTile tile, TileMap map)
 {
     //For undo/ redo
     map.FinishOperation();
     map.UpdateTileMap();
     return(false);
 }
        private void RefreshScriptableTileCache(EditorType type = EditorType.All)
        {
            tileMap.scriptableTileCache.Clear();
            List <ScriptableTile> toRemove = new List <ScriptableTile> ();

            for (int i = 0; i < tileMap.scriptableTileCache.Count; i++)
            {
                if (tileMap.scriptableTileCache [i] == null)
                {
                    toRemove.Add(tileMap.scriptableTileCache [i]);
                }
            }
            AssetDatabase.Refresh();
            tileMap.scriptableTileCache = tileMap.scriptableTileCache.Except(toRemove).ToList();
            string [] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(ScriptableTile)));
            for (int i = 0; i < guids.Length; i++)
            {
                string         assetPath = AssetDatabase.GUIDToAssetPath(guids [i]);
                ScriptableTile asset     = AssetDatabase.LoadAssetAtPath <ScriptableTile> (assetPath);
                if (asset != null && !tileMap.scriptableTileCache.Contains(asset))
                {
                    if (asset is DungeonTileBase && (((DungeonTileBase)asset).editorType == type || type == EditorType.All))
                    {
                        tileMap.scriptableTileCache.Add(asset);
                    }
                }
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Called by the tilemap editor to paint tiles
        /// </summary>
        /// <param name="point">Where you want to use the tool</param>
        /// <param name="tile">The ScriptableTile you want to use</param>
        /// <param name="map">What you want to use the tool on</param>
        public override bool OnClick(Point point, ScriptableTile tile, TileMap map)
        {
            if (tile == null || map == null)
            {
                return(false);
            }

            int correctedRadius = radius - 1;

            bool result = false;

            for (int x = -correctedRadius; x <= correctedRadius; x++)
            {
                for (int y = -correctedRadius; y <= correctedRadius; y++)
                {
                    Point offsetPoint = point + new Point(x, y);

                    if (shape == BrushShape.Circle)
                    {
                        Vector2 delta = (Vector2)(offsetPoint - point);
                        if (delta.sqrMagnitude > radius * radius)
                        {
                            continue;
                        }
                    }

                    if (map.SetTileAt(offsetPoint, tile))
                    {
                        result = true;
                    }
                }
            }
            return(result);
        }
Esempio n. 14
0
        public override bool OnClick(Point point, ScriptableTile tile, TileMap map)
        {
            if (tile == null && map == null)
            {
                return(false);
            }

            //If we haven't already started an operation, start one now
            //This is for undo/ redo support
            if (!map.OperationInProgress())
            {
                map.BeginOperation();
            }

            int correctedRadius = radius - 1;

            bool result = false;

            for (int x = -correctedRadius; x <= correctedRadius; x++)
            {
                for (int y = -correctedRadius; y <= correctedRadius; y++)
                {
                    Point offsetPoint = point + new Point(x, y);

                    if (map.SetTileAt(offsetPoint, null))
                    {
                        result = true;
                    }
                }
            }
            return(result);
        }
Esempio n. 15
0
 //Called when the left mouse button is held down
 public override bool OnClick(Point point, ScriptableTile tile, TileMap map)
 {
     //Return if the tilemap is null/empty
     if (map == null)
     {
         return(false);
     }
     //Set the tile at the specified point to the specified tile
     return(map.SetTileAt(point, tile));
 }
Esempio n. 16
0
 public override void OnClickUp(Point point, ScriptableTile tile, TileMap map)
 {
     base.OnClickUp(point, tile, map);
     for (int i = 0; i < region.Count; i++)
     {
         map.SetTileAt(region[i], tile);
     }
     start  = end = point;
     region = new List <Point>();
 }
Esempio n. 17
0
        public override void UpdateTileAt(int x, int y)
        {
            //var currentTexture = textureAtlas;
            ScriptableTile tile = tileMap.GetTileAt(x, y);

            int quadIndex = x + y * tileMap.Width;

            quadIndex *= 4;
            var uv = meshFilter.sharedMesh.uv;

            if (tile == null)
            {
                uv[quadIndex] = uv[quadIndex + 1] = uv[quadIndex + 2] = uv[quadIndex + 3] = Vector2.zero;

                meshFilter.sharedMesh.uv = uv;
                return;
            }

            Vector2[] prevUVs = new Vector2[] {
                uv[quadIndex],
                uv[quadIndex + 1],
                uv[quadIndex + 2],
                uv[quadIndex + 3]
            };

            Sprite sprite       = tile.GetSprite(tileMap, new Point(x, y));
            bool   atlasUpdated = AddSpriteToAtlas(sprite);

            // assign four uv coordinates to change the texture of one tile (one quad, two triangels)
            Rect spriteRect = rects[spriteList.IndexOf(sprite)];

            Vector2[] newUVs = new Vector2[] {
                new Vector2(spriteRect.xMin, spriteRect.yMin),
                new Vector2(spriteRect.xMax, spriteRect.yMin),
                new Vector2(spriteRect.xMin, spriteRect.yMax),
                new Vector2(spriteRect.xMax, spriteRect.yMax)
            };

            if (!newUVs.SequenceEqual(prevUVs))
            {
                uv[quadIndex]            = newUVs[0];
                uv[quadIndex + 1]        = newUVs[1];
                uv[quadIndex + 2]        = newUVs[2];
                uv[quadIndex + 3]        = newUVs[3];
                meshFilter.sharedMesh.uv = uv;

                if (atlasUpdated)
                {
                    //FIXME so that it only updates tiles who have changed uvs
                    tileMap.UpdateType(tile);
                }
            }
        }
 public void UpdateType(ScriptableTile type)
 {
     for (int x = 0; x <= Width; x++)
     {
         for (int y = 0; y <= Height; y++)
         {
             if (GetTileAt(x, y) == type)
             {
                 UpdateTileAt(x, y);
             }
         }
     }
 }
 private void Update()
 {
     for (int x = 0; x < Width; x++)
     {
         for (int y = 0; y < Height; y++)
         {
             ScriptableTile tile = GetTileAt(x, y);
             if (tile && tile.CheckIfCanTick())
             {
                 UpdateTileAt(x, y);
             }
         }
     }
 }
Esempio n. 20
0
        //Called when the left mouse button is initially held down
        public override void OnClickDown(Point point, ScriptableTile tile, TileMap map)
        {
            //Return if the tilemap is null/empty
            if (map == null)
            {
                return;
            }

            base.OnClickDown(point, tile, map);

            for (int i = 0; i < region.Count; i++)
            {
                Point offsetPoint = region[i];
                map.SetTileAt(offsetPoint, tile);
            }
            region = new List <Point>();
        }
        public List <ScriptableTile> GetAllTileTypes()
        {
            List <ScriptableTile> result = new List <ScriptableTile>();

            for (int x = 0; x <= Width; x++)
            {
                for (int y = 0; y <= Height; y++)
                {
                    ScriptableTile tile = GetTileAt(x, y);
                    if (!result.Contains(tile) && tile != null)
                    {
                        result.Add(tile);
                    }
                }
            }
            return(result);
        }
Esempio n. 22
0
        //Called when the left mouse button is held down
        public override bool OnClick(Point point, ScriptableTile tile, TileMap map)
        {
            //Return if the tilemap is null/empty
            if (map == null)
            {
                return(false);
            }

            //If we haven't already started an operation, start one now
            //This is for undo/ redo support
            if (!map.OperationInProgress())
            {
                map.BeginOperation();
            }

            //Set the tile at the specified point to the specified tile
            return(map.SetTileAt(point, tile));
        }
Esempio n. 23
0
        public override Sprite GetSprite(TileMap tilemap = null, Point position = default(Point))
        {
            if (tilemap == null)
            {
                return(bitmaskSprites[15]);
            }

            ScriptableTile[] tiles = new ScriptableTile[] {
                tilemap.GetTileAt(position.Up),
                tilemap.GetTileAt(position.Left),
                tilemap.GetTileAt(position.Down),
                tilemap.GetTileAt(position.Right),
            };
            int[] bitmasks = new int[] {
                1, 2, 4, 8
            };
            Point[] points = new Point[] {
                position.Up,
                position.Left,
                position.Down,
                position.Right,
            };

            int index = 0;

            for (int i = 0; i < 4; i++)
            {
                bool exists = tiles[i] != null;
                bool isSame = exists && tiles[i].ID == this.ID;
                bool isEdge = !tilemap.IsInBounds(points[i]);

                if ((isEdge && edgesAreFull) || (exists && mode == AutoTileMode.Everything) || (exists && (mode == AutoTileMode.SameTile && isSame)))
                {
                    index += bitmasks[i];
                }
            }

            if (index < bitmaskSprites.Length && bitmaskSprites [index])
            {
                return(bitmaskSprites [index]);
            }

            return(bitmaskSprites [15]);
        }
Esempio n. 24
0
        public override List <Point> GetRegion(Point point, ScriptableTile tile, TileMap map)
        {
            region = new List <Point>();
            //Arbitrary clamping of brush size
            radius = Mathf.Clamp(radius, 1, 64);
            int correctedRadius = radius - 1;

            for (int x = -correctedRadius; x <= correctedRadius; x++)
            {
                for (int y = -correctedRadius; y <= correctedRadius; y++)
                {
                    Point offsetPoint = point + new Point(x, y);
                    if (shape == BrushShape.Square || ((Vector2)(offsetPoint - point)).sqrMagnitude <= correctedRadius * correctedRadius)
                    {
                        region.Add(offsetPoint);
                    }
                }
            }
            return(region);
        }
Esempio n. 25
0
        /// <summary>
        /// Called by the tilemap editor to paint tiles
        /// </summary>
        /// <param name="point">Where you want to use the tool</param>
        /// <param name="tile">The ScriptableTile you want to use</param>
        /// <param name="map">What you want to use the tool on</param>
        public override bool OnClick(Point point, ScriptableTile tile, TileMap map)
        {
            if (tile == null || map == null)
            {
                return(false);
            }

            //If we haven't already started an operation, start one now
            //This is for undo/ redo support
            if (!map.OperationInProgress())
            {
                map.BeginOperation();
            }

            int correctedRadius = radius - 1;

            bool result = false;

            for (int x = -correctedRadius; x <= correctedRadius; x++)
            {
                for (int y = -correctedRadius; y <= correctedRadius; y++)
                {
                    Point offsetPoint = point + new Point(x, y);

                    if (shape == BrushShape.Circle)
                    {
                        Vector2 delta = (Vector2)(offsetPoint - point);
                        if (delta.sqrMagnitude > radius * radius)
                        {
                            continue;
                        }
                    }

                    if (map.SetTileAt(offsetPoint, tile))
                    {
                        result = true;
                    }
                }
            }
            return(result);
        }
Esempio n. 26
0
        // Called by the tilemap editor to paint tiles
        public override void OnClick(Point point, ScriptableTile tile, TileMap map)
        {
            if (map == null)
            {
                return;
            }

            //If we haven't already started an operation, start one now
            //This is for undo/ redo support
            if (!map.OperationInProgress())
            {
                map.BeginOperation();
            }

            for (int i = 0; i < region.Count; i++)
            {
                Point offsetPoint = region[i];

                map.SetTileAt(offsetPoint, tile);
            }
        }
Esempio n. 27
0
        public override Sprite GetSprite(TileMap tilemap = null, Point position = default(Point))
        {
            if (tilemap == null)
            {
                return(bitmaskSprites[15]);
            }

            ScriptableTile left  = tilemap.GetTileAt(position.Left);
            ScriptableTile up    = tilemap.GetTileAt(position.Up);
            ScriptableTile right = tilemap.GetTileAt(position.Right);
            ScriptableTile down  = tilemap.GetTileAt(position.Down);

            int index = 0;

            if ((!tilemap.IsInBounds(position.Up) && defaultIsFull) || (up && up.ID == this.ID))
            {
                index += 1;
            }
            if ((!tilemap.IsInBounds(position.Right) && defaultIsFull) || (right && right.ID == this.ID))
            {
                index += 8;
            }
            if ((!tilemap.IsInBounds(position.Down) && defaultIsFull) || (down && down.ID == this.ID))
            {
                index += 4;
            }
            if ((!tilemap.IsInBounds(position.Left) && defaultIsFull) || (left && left.ID == this.ID))
            {
                index += 2;
            }

            if (index < bitmaskSprites.Length && bitmaskSprites [index])
            {
                return(bitmaskSprites [index]);
            }

            return(bitmaskSprites [15]);
        }
Esempio n. 28
0
        protected override void OnHeaderGUI()
        {
            scriptableTile = target as ScriptableTile;

            Rect headerRect = new Rect(0, 0, Screen.width, 50);

            GUILayout.BeginArea(headerRect, new GUIStyle("IN ThumbnailShadow"));
            Rect contentRect = new Rect(10, 10, Screen.width - 20, 30);

            GUI.DrawTexture(new Rect(contentRect.x, contentRect.y, contentRect.height, contentRect.height), scriptableTile.GetTexture(null, Point.zero), ScaleMode.ScaleAndCrop);
            contentRect.x     += contentRect.height + 10;
            contentRect.width -= contentRect.x;
            GUI.Label(new Rect(contentRect.x, contentRect.y, contentRect.width, contentRect.height / 2), scriptableTile.name, CustomStyles.leftBoldLabel);
            GUI.Label(new Rect(contentRect.x, contentRect.y + contentRect.height / 2, contentRect.width, contentRect.height / 2), "Tile Type: " + scriptableTile.GetType().Name, CustomStyles.leftMiniLabel);
            GUIContent content = new GUIContent(EditorGUIUtility.FindTexture("_Help"), "Open Reference for " + scriptableTile.GetType().Name);

            if (GUI.Button(new Rect(contentRect.x + contentRect.width - 10, contentRect.y, 20, contentRect.height), content, CustomStyles.centerBoldLabel))
            {
                Application.OpenURL("https://github.com/toinfiniityandbeyond/Tilemap/wiki/" + scriptableTile.GetType().Name);
            }

            GUILayout.EndArea();
            GUILayout.Space(headerRect.height + 10);
        }
Esempio n. 29
0
 //Called when the left mouse button is held down
 public override bool OnClick(Point point, ScriptableTile tile, TileMap map)
 {
     return(false);
 }
Esempio n. 30
0
 public override bool OnClickUp(Point point, ScriptableTile tile, TileMap map)
 {
     map.UpdateTileMap();
     return(false);
 }