Ejemplo n.º 1
0
        public void Erase(Tilemap tilemap, Vector2 localPos)
        {
            int minGridX = m_brushTilemap.MinGridX;
            int minGridY = m_brushTilemap.MinGridY;
            int maxGridX = m_brushTilemap.MaxGridX;
            int maxGridY = m_brushTilemap.MaxGridY;

            if (IsUndoEnabled)
            {
#if UNITY_EDITOR
                Undo.RecordObject(tilemap, Tilemap.k_UndoOpName + tilemap.name);
                Undo.RecordObjects(tilemap.GetComponentsInChildren <TilemapChunk>(), Tilemap.k_UndoOpName + tilemap.name);
#endif
            }
            tilemap.IsUndoEnabled = IsUndoEnabled;
            int dstGy = BrushUtil.GetGridY(localPos, tilemap.CellSize);
            for (int gridY = minGridY; gridY <= maxGridY; ++gridY, ++dstGy)
            {
                int dstGx = BrushUtil.GetGridX(localPos, tilemap.CellSize);
                for (int gridX = minGridX; gridX <= maxGridX; ++gridX, ++dstGx)
                {
                    tilemap.SetTileData(dstGx, dstGy, Tileset.k_TileData_Empty);
                }
            }
            tilemap.UpdateMeshImmediate();
            tilemap.IsUndoEnabled = false;
        }
Ejemplo n.º 2
0
        public void Paint(Tilemap tilemap, Vector2 localPos)
        {
            int minGridX = m_brushTilemap.MinGridX;
            int minGridY = m_brushTilemap.MinGridY;
            int maxGridX = m_brushTilemap.MaxGridX;
            int maxGridY = m_brushTilemap.MaxGridY;

            if (IsUndoEnabled)
            {
#if UNITY_EDITOR
                Undo.RecordObject(tilemap, Tilemap.k_UndoOpName + tilemap.name);
                Undo.RecordObjects(tilemap.GetComponentsInChildren <TilemapChunk>(), Tilemap.k_UndoOpName + tilemap.name);
#endif
            }
            tilemap.IsUndoEnabled = IsUndoEnabled;
            int dstGy = BrushUtil.GetGridY(localPos, tilemap.CellSize);
            for (int gridY = minGridY; gridY <= maxGridY; ++gridY, ++dstGy)
            {
                int dstGx = BrushUtil.GetGridX(localPos, tilemap.CellSize);
                for (int gridX = minGridX; gridX <= maxGridX; ++gridX, ++dstGx)
                {
                    uint tileData = m_brushTilemap.GetTileData(gridX, gridY);
                    if (
                        tileData != Tileset.k_TileData_Empty || // don't copy empty tiles
                        m_brushTilemap.GridWidth == 1 && m_brushTilemap.GridHeight == 1    // unless the brush size is one
                        )
                    {
                        tilemap.SetTileData(dstGx, dstGy, tileData);
                    }
                }
            }
            tilemap.UpdateMeshImmediate();
            tilemap.IsUndoEnabled = false;
        }
Ejemplo n.º 3
0
        public static void DrawLineMirrored(Tilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData)
        {
            int w  = tileData.GetLength(0);
            int h  = tileData.GetLength(1);
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);

            TilemapDrawingUtils.Line(x0, y0, x1, y1,
                                     (x, y) =>
            {
                tilemap.SetTileData(x, y, tileData[(x % w + w) % w, (y % h + h) % h]);
                tilemap.SetTileData(x0 - x, y0 - y, tileData[((x0 - x) % w + w) % w, ((y0 - y) % h + h) % h]);
                return(true);
            }
                                     );
        }
Ejemplo n.º 4
0
        public static void DrawEllipse(Tilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData, bool isFilled)
        {
            int w  = tileData.GetLength(0);
            int h  = tileData.GetLength(1);
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);
            int xf = 0;
            int yf = 0;

            //fix for cases where x1 x2 y1 or y2 are negative or x1 > x2 or y1 > y2
            // NOTE: I tested this only for case x1 == y1 == 0
            if (x0 > x1)
            {
                Swap <int>(ref x0, ref x1);
            }
            if (y0 > y1)
            {
                Swap <int>(ref y0, ref y1);
            }
            if (x0 < 0)
            {
                xf = x0; x0 = 0; x1 -= xf;
            }
            if (y0 < 0)
            {
                yf = y0; y0 = 0; y1 -= yf;
            }
            //
            TilemapDrawingUtils.Ellipse(x0, y0, x1, y1, isFilled,
                                        (x, y) =>
            {
                tilemap.SetTileData(x + xf, y + yf, tileData[((x + xf) % w + w) % w, ((y + yf) % h + h) % h]);
                return(true);
            }
                                        );
        }
Ejemplo n.º 5
0
        public void CutRect(Tilemap tilemap, int startGridX, int startGridY, int endGridX, int endGridY)
        {
            if (IsUndoEnabled)
            {
#if UNITY_EDITOR
                Undo.RecordObject(tilemap, Tilemap.k_UndoOpName + tilemap.name);
                Undo.RecordObjects(tilemap.GetComponentsInChildren <TilemapChunk>(), Tilemap.k_UndoOpName + tilemap.name);
#endif
            }
            tilemap.IsUndoEnabled = IsUndoEnabled;

            for (int gridY = startGridY; gridY <= endGridY; ++gridY)
            {
                for (int gridX = startGridX; gridX <= endGridX; ++gridX)
                {
                    BrushTilemap.SetTileData(gridX - startGridX, gridY - startGridY, tilemap.GetTileData(gridX, gridY));
                    tilemap.SetTileData(gridX, gridY, Tileset.k_TileData_Empty);
                }
            }
            BrushTilemap.UpdateMeshImmediate();
            tilemap.UpdateMeshImmediate();

            tilemap.IsUndoEnabled = false;
        }
Ejemplo n.º 6
0
        private void DoPaintInspector()
        {
            Event e = Event.current;

            Tilemap tilemap = (Tilemap)target;

            if (DoToolBar() ||
                DragAndDrop.objectReferences.Length > 0 || // hide brush when user is dragging a prefab into the scene
                EditorWindow.mouseOverWindow != SceneView.currentDrawingSceneView)    // hide brush when it's not over the scene view
            {
                m_brushVisible = false;
                SceneView.RepaintAll();
                return;
            }

            int controlID = GUIUtility.GetControlID(FocusType.Passive);

            HandleUtility.AddDefaultControl(controlID);
            EventType currentEventType = Event.current.GetTypeForControl(controlID);
            bool      skip             = false;
            int       saveControl      = GUIUtility.hotControl;

            try
            {
                if (currentEventType == EventType.Layout)
                {
                    skip = true;
                }
                else if (currentEventType == EventType.ScrollWheel)
                {
                    skip = true;
                }

                if (tilemap.Tileset == null)
                {
                    return;
                }

                if (!skip)
                {
                    if (e.type == EventType.KeyDown)
                    {
                        if (e.keyCode == ShortcutKeys.k_FlipH)
                        {
                            BrushBehaviour.GetOrCreateBrush(tilemap).FlipH(!e.shift);
                            e.Use(); // Use key event
                        }
                        else if (e.keyCode == ShortcutKeys.k_FlipV)
                        {
                            BrushBehaviour.GetOrCreateBrush(tilemap).FlipV(!e.shift);
                            e.Use(); // Use key event
                        }
                        else if (e.keyCode == ShortcutKeys.k_Rot90)
                        {
                            BrushBehaviour.GetOrCreateBrush(tilemap).Rot90(!e.shift);
                            e.Use(); // Use key event
                        }
                        else if (e.keyCode == ShortcutKeys.k_Rot90Back)
                        {
                            BrushBehaviour.GetOrCreateBrush(tilemap).Rot90Back(!e.shift);
                            e.Use(); // Use key event
                        }
                    }

                    EditorGUIUtility.AddCursorRect(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), MouseCursor.Arrow);
                    GUIUtility.hotControl = controlID;
                    {
                        Plane   chunkPlane = new Plane(tilemap.transform.forward, tilemap.transform.position);
                        Vector2 mousePos   = Event.current.mousePosition; mousePos.y = Screen.height - mousePos.y;
                        Ray     ray        = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                        float   dist;
                        if (chunkPlane.Raycast(ray, out dist))
                        {
                            Rect rTile = new Rect(0, 0, m_tilemap.CellSize.x, m_tilemap.CellSize.y);
                            rTile.position = tilemap.transform.InverseTransformPoint(ray.GetPoint(dist));

                            Vector2 tilePos = rTile.position;
                            if (tilePos.x < 0)
                            {
                                tilePos.x -= m_tilemap.CellSize.x;
                            }
                            if (tilePos.y < 0)
                            {
                                tilePos.y -= m_tilemap.CellSize.y;
                            }
                            tilePos.x     -= tilePos.x % m_tilemap.CellSize.x;
                            tilePos.y     -= tilePos.y % m_tilemap.CellSize.y;
                            rTile.position = tilePos;


                            Vector2 startPos            = new Vector2(Mathf.Min(m_startDragging.x, m_endDragging.x), Mathf.Min(m_startDragging.y, m_endDragging.y));
                            Vector2 endPos              = new Vector2(Mathf.Max(m_startDragging.x, m_endDragging.x), Mathf.Max(m_startDragging.y, m_endDragging.y));
                            Vector2 selectionSnappedPos = BrushUtil.GetSnappedPosition(startPos, m_tilemap.CellSize);
                            Vector2 selectionSize       = BrushUtil.GetSnappedPosition(endPos, m_tilemap.CellSize) - selectionSnappedPos + m_tilemap.CellSize;

                            BrushBehaviour brush = BrushBehaviour.GetOrCreateBrush(tilemap);
                            // Update brush transform
                            m_localPaintPos = (Vector2)tilemap.transform.InverseTransformPoint(ray.GetPoint(dist));
                            Vector2 brushSnappedPos = BrushUtil.GetSnappedPosition(brush.Offset + m_localPaintPos, m_tilemap.CellSize);
                            brush.transform.rotation   = tilemap.transform.rotation;
                            brush.transform.localScale = tilemap.transform.lossyScale;
                            brush.transform.position   = tilemap.transform.TransformPoint(new Vector3(brushSnappedPos.x, brushSnappedPos.y, -0.01f));
                            //---

                            int prevMouseGridX = m_mouseGridX;
                            int prevMouseGridY = m_mouseGridY;
                            if (e.isMouse)
                            {
                                m_mouseGridX = BrushUtil.GetGridX(m_localPaintPos, tilemap.CellSize);
                                m_mouseGridY = BrushUtil.GetGridY(m_localPaintPos, tilemap.CellSize);
                            }
                            bool isMouseGridChanged = prevMouseGridX != m_mouseGridX || prevMouseGridY != m_mouseGridY;
                            //Update Fill Preview
                            if (GetBrushMode() == eBrushMode.Fill && isMouseGridChanged)
                            {
                                m_fillPreview.Clear();
                                TilemapDrawingUtils.FloodFillPreview(tilemap, brush.Offset + m_localPaintPos, brush.BrushTilemap.GetTileData(0, 0), m_fillPreview);
                            }

                            if (
                                (EditorWindow.focusedWindow == EditorWindow.mouseOverWindow) && // fix painting tiles when closing another window popup over the SceneView like GameObject Selection window
                                (e.type == EventType.MouseDown || e.type == EventType.MouseDrag && isMouseGridChanged)
                                )
                            {
                                if (e.button == 0)
                                {
                                    if (m_dblClick.IsDblClick && brush.BrushTilemap.GridWidth == 1 && brush.BrushTilemap.GridHeight == 1)
                                    {
                                        // Restore previous tiledata modified by Paint, because before the double click, a single click is done before
                                        tilemap.SetTileData(brush.Offset + m_localPaintPos, m_floodFillRestoredTileData);
                                        brush.FloodFill(tilemap, brush.Offset + m_localPaintPos, brush.BrushTilemap.GetTileData(0, 0));
                                    }
                                    // Do a brush paint action
                                    else
                                    {
                                        switch (GetBrushMode())
                                        {
                                        case eBrushMode.Paint:
                                            m_floodFillRestoredTileData = tilemap.GetTileData(m_mouseGridX, m_mouseGridY);
                                            brush.Paint(tilemap, brush.Offset + m_localPaintPos);
                                            break;

                                        case eBrushMode.Erase:
                                            brush.Erase(tilemap, brush.Offset + m_localPaintPos);
                                            break;

                                        case eBrushMode.Fill:
                                            brush.FloodFill(tilemap, brush.Offset + m_localPaintPos, brush.BrushTilemap.GetTileData(0, 0));
                                            break;
                                        }
                                    }
                                }
                                else if (e.button == 1)
                                {
                                    if (e.type == EventType.MouseDown)
                                    {
                                        m_isDragging = true;
                                        brush.BrushTilemap.ClearMap();
                                        m_startDragging = m_endDragging = m_localPaintPos;
                                    }
                                    else
                                    {
                                        m_endDragging = m_localPaintPos;
                                    }
                                }
                            }
                            else if (e.type == EventType.MouseUp)
                            {
                                if (e.button == 1) // right mouse button
                                {
                                    m_isDragging = false;
                                    ResetBrushMode();
                                    // Copy one tile
                                    if (selectionSize.x <= m_tilemap.CellSize.x && selectionSize.y <= m_tilemap.CellSize.y)
                                    {
                                        uint tileData = tilemap.GetTileData(m_localPaintPos);
                                        if (tileData == Tileset.k_TileData_Empty)
                                        {
                                            tilemap.Tileset.SelectedTileId = Tileset.k_TileId_Empty;
                                            brush.BrushTilemap.SetTileData(0, 0, Tileset.k_TileData_Empty);
                                        }
                                        else
                                        {
                                            int brushId = Tileset.GetBrushIdFromTileData(tileData);
                                            int tileId  = Tileset.GetTileIdFromTileData(tileData);

                                            // Select the copied tile in the tileset, alternating between the brush and the tile drawn by the brush
                                            if (brushId > 0 && brushId != tilemap.Tileset.SelectedBrushId)
                                            {
                                                tilemap.Tileset.SelectedBrushId = brushId;
                                            }
                                            else
                                            {
                                                tilemap.Tileset.SelectedTileId = tileId;
                                                brush.BrushTilemap.SetTileData(0, 0, tileData & ~Tileset.k_TileDataMask_BrushId); // keep tile flags
                                            }
                                        }

                                        // Cut tile if key shift is pressed
                                        if (e.shift)
                                        {
                                            int startGridX = BrushUtil.GetGridX(startPos, m_tilemap.CellSize);
                                            int startGridY = BrushUtil.GetGridY(startPos, m_tilemap.CellSize);
                                            brush.CutRect(tilemap, startGridX, startGridY, startGridX, startGridY);
                                        }

                                        brush.BrushTilemap.UpdateMesh();
                                        brush.Offset = Vector2.zero;
                                    }
                                    // copy a rect of tiles
                                    else
                                    {
                                        int startGridX = BrushUtil.GetGridX(startPos, m_tilemap.CellSize);
                                        int startGridY = BrushUtil.GetGridY(startPos, m_tilemap.CellSize);
                                        int endGridX   = BrushUtil.GetGridX(endPos, m_tilemap.CellSize);
                                        int endGridY   = BrushUtil.GetGridY(endPos, m_tilemap.CellSize);

                                        // Cut tile if key shift is pressed
                                        if (e.shift)
                                        {
                                            brush.CutRect(tilemap, startGridX, startGridY, endGridX, endGridY);
                                        }
                                        else
                                        {
                                            brush.CopyRect(tilemap, startGridX, startGridY, endGridX, endGridY);
                                        }
                                        brush.Offset.x = m_endDragging.x > m_startDragging.x ? -(endGridX - startGridX) * tilemap.CellSize.x : 0f;
                                        brush.Offset.y = m_endDragging.y > m_startDragging.y ? -(endGridY - startGridY) * tilemap.CellSize.y : 0f;
                                    }
                                }
                            }

                            if (m_isDragging)
                            {
                                Rect rGizmo = new Rect(selectionSnappedPos, selectionSize);
                                HandlesEx.DrawRectWithOutline(tilemap.transform, rGizmo, new Color(), Color.white);
                            }
                            else // Draw brush border
                            {
                                Rect  rBound = new Rect(brush.BrushTilemap.MapBounds.min, brush.BrushTilemap.MapBounds.size);
                                Color fillColor;
                                switch (GetBrushMode())
                                {
                                case eBrushMode.Paint:
                                    fillColor = new Color(0, 0, 0, 0);
                                    break;

                                case eBrushMode.Erase:
                                    fillColor = new Color(1f, 0f, 0f, 0.1f);
                                    break;

                                case eBrushMode.Fill:
                                    fillColor = new Color(1f, 1f, 0f, 0.2f);
                                    break;

                                default:
                                    fillColor = new Color(0, 0, 0, 0);
                                    break;
                                }
                                HandlesEx.DrawRectWithOutline(brush.transform, rBound, fillColor, new Color(1, 1, 1, 0.2f));
                            }
                        }
                    }

                    if (currentEventType == EventType.MouseDrag && Event.current.button < 2) // 2 is for central mouse button
                    {
                        // avoid dragging the map
                        Event.current.Use();
                    }
                }
            }
            // Avoid loosing the hotControl because of a triggered exception
            catch (System.Exception ex)
            {
                Debug.LogException(ex);
            }

            SceneView.RepaintAll();
            GUIUtility.hotControl = saveControl;
        }
        public void GenerateMap()
        {
            //Ground.ClearMap();
            GroundOverlay.ClearMap();

            float now;

            now = Time.realtimeSinceStartup;
            float fDiv = 25f;
            float xf   = Random.value * 100;
            float yf   = Random.value * 100;

            //*/ Rogue Demo (280ms with 180x180)
            uint tileWater       = (8 << 16);
            uint tileWaterPlants = (24 << 16);
            uint tileDarkGrass   = 66;
            uint tileGrass       = (9 << 16);
            uint tileFlowers     = (22 << 16);
            uint tileMountains   = (23 << 16);

            //*/

            // Oryx Demo

            /*/
             * uint tileWater = (9 << 16);
             * uint tileWaterPlants = (16 << 16);
             * uint tileDarkGrass = 757;
             * uint tileGrass = 756;
             * uint tileFlowers = (17 << 16);
             * uint tileMountains = (18 << 16);
             * //*/

            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    float fRand = Random.value;
                    float noise = Mathf.PerlinNoise((i + xf) / fDiv, (j + yf) / fDiv);
                    //Debug.Log( "noise: "+noise+"; i: "+i+"; j: "+j );
                    if (noise < 0.3) //water
                    {
                        Ground.SetTileData(i, j, tileWater);
                    }
                    else if (noise < 0.4) // water plants
                    {
                        Ground.SetTileData(i, j, tileWater);
                        if (fRand < noise / 3)
                        {
                            GroundOverlay.SetTileData(i, j, tileWaterPlants);
                        }
                    }
                    else if (noise < 0.5 && fRand < (1 - noise / 2)) // dark grass
                    {
                        Ground.SetTileData(i, j, tileDarkGrass);
                    }
                    else if (noise < 0.6 && fRand < (1 - 1.2 * noise)) // flowers
                    {
                        Ground.SetTileData(i, j, tileGrass);
                        GroundOverlay.SetTileData(i, j, tileFlowers);
                    }
                    else if (noise < 0.7) // grass
                    {
                        Ground.SetTileData(i, j, tileGrass);
                    }
                    else // mountains
                    {
                        Ground.SetTileData(i, j, tileGrass);
                        GroundOverlay.SetTileData(i, j, tileMountains);
                    }
                }
            }
            Debug.Log("Generation time(ms): " + (Time.realtimeSinceStartup - now) * 1000);

            now = Time.realtimeSinceStartup;
            Ground.UpdateMesh();
            GroundOverlay.UpdateMesh();
            Debug.Log("UpdateMesh time(ms): " + (Time.realtimeSinceStartup - now) * 1000);
        }
Ejemplo n.º 8
0
        public void SetTileData(int locGridX, int locGridY, uint tileData)
        {
            if (locGridX >= 0 && locGridX < m_width && locGridY >= 0 && locGridY < m_height)
            {
                int tileIdx = locGridY * m_width + locGridX;

                int  tileId = (int)(tileData & Tileset.k_TileDataMask_TileId);
                Tile tile   = tileId != Tileset.k_TileId_Empty? Tileset.Tiles[tileId] : null;

                int  prevTileId = (int)(m_tileDataList[tileIdx] & Tileset.k_TileDataMask_TileId);
                Tile prevTile   = prevTileId != Tileset.k_TileId_Empty? Tileset.Tiles[prevTileId] : null;

                int brushId     = Tileset.GetBrushIdFromTileData(tileData);
                int prevBrushId = Tileset.GetBrushIdFromTileData(m_tileDataList[tileIdx]);

                if (brushId != prevBrushId)
                {
                    TilesetBrush brush     = ParentTilemap.Tileset.FindBrush(brushId);
                    TilesetBrush prevBrush = ParentTilemap.Tileset.FindBrush(prevBrushId);
                    if (prevBrush != null)
                    {
                        prevBrush.OnErase(this, locGridX, locGridY, tileData);
                    }
                    if (brush != null)
                    {
                        tileData = brush.OnPaint(this, locGridX, locGridY, tileData);
                    }

                    // Refresh Neighbors ( and itself if needed )
                    for (int yf = -1; yf <= 1; ++yf)
                    {
                        for (int xf = -1; xf <= 1; ++xf)
                        {
                            if ((xf | yf) == 0)
                            {
                                if (brushId > 0)
                                {
                                    // Refresh itself
                                    tileData = (tileData & ~Tileset.k_TileFlag_Updated);
                                }
                            }
                            else
                            {
                                int          gx               = (locGridX + xf);
                                int          gy               = (locGridY + yf);
                                int          idx              = gy * m_width + gx;
                                bool         isInsideChunk    = (gx >= 0 && gx < m_width && gy >= 0 && gy < m_height);
                                uint         neighborTileData = isInsideChunk ? m_tileDataList[idx] : ParentTilemap.GetTileData(GridPosX + locGridX + xf, GridPosY + locGridY + yf);
                                int          neighborBrushId  = (int)((neighborTileData & Tileset.k_TileDataMask_BrushId) >> 16);
                                TilesetBrush neighborBrush    = ParentTilemap.Tileset.FindBrush(neighborBrushId);
                                //if (brush != null && brush.AutotileWith(brushId, neighborBrushId) || prevBrush != null && prevBrush.AutotileWith(prevBrushId, neighborBrushId))
                                if (neighborBrush != null &&
                                    (neighborBrush.AutotileWith(neighborBrushId, brushId) || neighborBrush.AutotileWith(neighborBrushId, prevBrushId)))
                                {
                                    neighborTileData = (neighborTileData & ~Tileset.k_TileFlag_Updated); // force a refresh
                                    if (isInsideChunk)
                                    {
                                        m_tileDataList[idx] = neighborTileData;
                                    }
                                    else
                                    {
                                        ParentTilemap.SetTileData(GridPosX + gx, GridPosY + gy, neighborTileData);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (brushId > 0)
                {
                    // Refresh itself
                    tileData = (tileData & ~Tileset.k_TileFlag_Updated);
                }

                m_needsRebuildMesh      |= (m_tileDataList[tileIdx] != tileData) || (tileData & Tileset.k_TileDataMask_TileId) == Tileset.k_TileId_Empty;
                m_needsRebuildColliders |= m_needsRebuildMesh &&
                                           (
                    (prevBrushId > 0) || (brushId > 0) || // there is a brush (a brush could change the collider data later)
                    (tile != null && tile.collData.type != eTileCollider.None) || (prevTile != null && prevTile.collData.type != eTileCollider.None)    // prev. or new tile has colliders
                                           );

                if (ParentTilemap.ColliderType != eColliderType.None && m_needsRebuildColliders)
                {
                    // Refresh Neighbors tilechunk colliders, to make the collider autotiling
                    // Only if neighbor is outside this tilechunk
                    for (int yf = -1; yf <= 1; ++yf)
                    {
                        for (int xf = -1; xf <= 1; ++xf)
                        {
                            if ((xf | yf) != 0) // skip this tile position xf = yf = 0
                            {
                                int  gx            = (locGridX + xf);
                                int  gy            = (locGridY + yf);
                                bool isInsideChunk = (gx >= 0 && gx < m_width && gy >= 0 && gy < m_height);
                                if (!isInsideChunk)
                                {
                                    ParentTilemap.InvalidateChunkAt(GridPosX + gx, GridPosY + gy, false, true);
                                }
                            }
                        }
                    }
                }

                // Update tile data
                m_tileDataList[tileIdx] = tileData;

                if (!Tilemap.DisableTilePrefabCreation)
                {
                    // Create tile Objects
                    if (tile != null && tile.prefabData.prefab != null)
                    {
                        CreateTileObject(tileIdx, tile.prefabData);
                    }
                    else
                    {
                        DestroyTileObject(tileIdx);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        //https://social.msdn.microsoft.com/Forums/en-US/9d926a16-0051-4ca3-b77c-8095fb489ae2/flood-fill-c?forum=csharplanguage
        public static void FloodFill(Tilemap tilemap, int gridX, int gridY, uint[,] tileData)
        {
            float timeStamp;

            timeStamp = Time.realtimeSinceStartup;
            //float callTimeStamp = timeStamp;

            int patternW             = tileData.GetLength(0);
            int patternH             = tileData.GetLength(1);
            LinkedList <Point> check = new LinkedList <Point>();
            uint floodFrom           = tilemap.GetTileData(gridX, gridY);

            tilemap.SetTileData(gridX, gridY, tileData[(gridX % patternW + patternW) % patternW, (gridY % patternH + patternH) % patternH]);
            bool isBrush = Tileset.GetBrushIdFromTileData(floodFrom) != 0;

            //Debug.Log(" Flood Fill Starts +++++++++++++++ ");
            if (
                (patternW > 0 && patternH > 0) &&
                isBrush?
                Tileset.GetBrushIdFromTileData(floodFrom) != Tileset.GetBrushIdFromTileData(tileData[0, 0])
                :
                floodFrom != tileData[0, 0]
                )
            {
                check.AddLast(new Point(gridX, gridY));
                while (check.Count > 0)
                {
                    Point cur = check.First.Value;
                    check.RemoveFirst();

                    foreach (Point off in new Point[] {
                        new Point(0, -1), new Point(0, 1),
                        new Point(-1, 0), new Point(1, 0)
                    })
                    {
                        Point next         = new Point(cur.X + off.X, cur.Y + off.Y);
                        uint  nextTileData = tilemap.GetTileData(next.X, next.Y);
                        if (
                            next.X >= tilemap.MinGridX && next.X <= tilemap.MaxGridX &&
                            next.Y >= tilemap.MinGridY && next.Y <= tilemap.MaxGridY
                            )
                        {
                            if (
                                isBrush?
                                Tileset.GetBrushIdFromTileData(floodFrom) == Tileset.GetBrushIdFromTileData(nextTileData)
                                :
                                floodFrom == nextTileData
                                )
                            {
                                check.AddLast(next);
                                tilemap.SetTileData(next.X, next.Y, tileData[(next.X % patternW + patternW) % patternW, (next.Y % patternH + patternH) % patternH]);
                            }
                        }
                    }

                    float timePast = Time.realtimeSinceStartup - timeStamp;
                    if (timePast > k_timeToAbortFloodFill)
                    {
#if UNITY_EDITOR
                        int result = UnityEditor.EditorUtility.DisplayDialogComplex("FloodFill is taking too much time", "Do you want to continue for another " + k_timeToAbortFloodFill + " seconds?", "Wait", "Cancel", "Wait and Don't ask again");
                        if (result == 0)
                        {
                            timeStamp = Time.realtimeSinceStartup;
                        }
                        else if (result == 1)
                        {
                            break;
                        }
                        else if (result == 2)
                        {
                            timeStamp = float.MaxValue;
                        }
#else
                        check.Clear();
#endif
                    }
                }
            }

            //Debug.Log("FloodFill Time " + (int)((Time.realtimeSinceStartup - callTimeStamp) * 1000) + "ms");
        }
Ejemplo n.º 10
0
        public static void DrawRect(Tilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData, bool isFilled, bool is9Sliced = false)
        {
            int w  = tileData.GetLength(0);
            int h  = tileData.GetLength(1);
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);

            if (x0 > x1)
            {
                Swap <int>(ref x0, ref x1);
            }
            if (y0 > y1)
            {
                Swap <int>(ref y0, ref y1);
            }
            TilemapDrawingUtils.Rect(x0, y0, x1, y1, isFilled,
                                     (x, y) =>
            {
                if (is9Sliced)
                {
                    if (x == x0 && y == y0)
                    {
                        tilemap.SetTileData(x, y, tileData[0, 0]);
                    }
                    else if (x == x0 && y == y1)
                    {
                        tilemap.SetTileData(x, y, tileData[0, h - 1]);
                    }
                    else if (x == x1 && y == y0)
                    {
                        tilemap.SetTileData(x, y, tileData[w - 1, 0]);
                    }
                    else if (x == x1 && y == y1)
                    {
                        tilemap.SetTileData(x, y, tileData[w - 1, h - 1]);
                    }
                    else
                    {
                        int cw = w - 2;
                        int ch = h - 2;
                        int cx = cw >= 1 ? 1 + (x % cw + cw) % cw : (x % w + w) % w;
                        int cy = ch >= 1 ? 1 + (y % ch + ch) % ch : (y % h + h) % h;
                        if (x == x0)
                        {
                            tilemap.SetTileData(x, y, tileData[0, cy]);
                        }
                        else if (x == x1)
                        {
                            tilemap.SetTileData(x, y, tileData[w - 1, cy]);
                        }
                        else if (y == y0)
                        {
                            tilemap.SetTileData(x, y, tileData[cx, 0]);
                        }
                        else if (y == y1)
                        {
                            tilemap.SetTileData(x, y, tileData[cx, h - 1]);
                        }
                        else
                        {
                            tilemap.SetTileData(x, y, tileData[cx, cy]);
                        }
                    }
                }
                else
                {
                    tilemap.SetTileData(x, y, tileData[(x % w + w) % w, (y % h + h) % h]);
                }
                return(true);
            }
                                     );
        }