public void OnSceneGUI_DoPaint()
        {
            Event e = Event.current;

            DrawToolbar();
            if (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
                (Tools.current != Tool.Rect && Tools.current != Tool.None))
            {
                is_tileSetBrush_visible = false;
                SceneView.RepaintAll();
                return;
            }

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

            HandleUtility.AddDefaultControl(control_id);
            EventType current_event_type = Event.current.GetTypeForControl(control_id);
            bool      is_skip            = false;
            int       save_control       = GUIUtility.hotControl;

            try
            {
                if (current_event_type == EventType.Layout)
                {
                    is_skip = true;
                }
                else if (current_event_type == EventType.ScrollWheel)
                {
                    is_skip = true;
                }

                if (tileMap.tileSet == null)
                {
                    return;
                }

                if (!is_skip)
                {
                    if (e.type == EventType.KeyDown)
                    {
                        switch (e.keyCode)
                        {
                        case TileToolbarConst.Key_FlipH:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).FlipH(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_FlipV:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).FlipV(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_Rot90:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).Rot90(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_Rot90Back:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).Rot90Back(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_PencilTool:
                        case TileToolbarConst.Key_LineTool:
                        case TileToolbarConst.Key_RectTool:
                        case TileToolbarConst.Key_EllipseTool:
                            switch (e.keyCode)
                            {
                            case TileToolbarConst.Key_PencilTool:
                                TileSetBrushBehaviour.instance.paint_mode = TileSetBrushPaintMode.Pencil;
                                break;

                            case TileToolbarConst.Key_LineTool:
                                TileSetBrushBehaviour.instance.paint_mode = TileSetBrushPaintMode.Line;
                                break;

                            case TileToolbarConst.Key_RectTool:
                                TileSetBrushBehaviour.instance.paint_mode =
                                    TileSetBrushBehaviour.instance.paint_mode == TileSetBrushPaintMode.Rect
                      ? TileSetBrushPaintMode.FilledRect
                      : TileSetBrushPaintMode.Rect;//在filledRect和Rect之间切换
                                break;

                            case TileToolbarConst.Key_EllipseTool:
                                TileSetBrushBehaviour.instance.paint_mode =
                                    TileSetBrushBehaviour.instance.paint_mode == TileSetBrushPaintMode.Ellipse
                      ? TileSetBrushPaintMode.FilledEllipse
                      : TileSetBrushPaintMode.Ellipse;//在FilledEllipse和Ellipse之间切换
                                break;
                            }

                            tileSetBrush_mode = TileMapEditorBrushMode.Paint;
                            TileToolbar.instance.tileSetBrushToolbar.TriggerButton((int)tileSetBrush_mode);
                            TileToolbar.instance.tileSetBrushPaintToolbar.TriggerButton((int)TileSetBrushBehaviour.instance
                                                                                        .paint_mode);
                            e.Use();
                            break;
                        }
                    }

                    EditorGUIUtility.AddCursorRect(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height),
                                                   MouseCursor.Arrow);
                    GUIUtility.hotControl = control_id;
                    {
                        Plane   tileMapChunk_plane = new Plane(tileMap.transform.forward, tileMap.transform.position);
                        Vector2 mouse_pos          = Event.current.mousePosition;
                        mouse_pos.y = Screen.height - mouse_pos.y;
                        Ray   ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                        float distance;
                        if (tileMapChunk_plane.Raycast(ray, out distance))
                        {
                            Rect tile_rect = new Rect(0, 0, tileMap.cell_size.x, tileMap.cell_size.y);
                            tile_rect.position = tileMap.transform.InverseTransformPoint(ray.GetPoint(distance));

                            Vector2 tile_pos = tile_rect.position;
                            if (tile_pos.x < 0)
                            {
                                tile_pos.x -= tileMap.cell_size.x;//因为负数是从-1开始的,正数是从0开始的
                            }
                            if (tile_pos.y < 0)
                            {
                                tile_pos.y -= tileMap.cell_size.y;                 //因为负数是从-1开始的,正数是从0开始的
                            }
                            tile_pos.x        -= tile_pos.x % tileMap.cell_size.x; //取整
                            tile_pos.y        -= tile_pos.y % tileMap.cell_size.y; //取整
                            tile_rect.position = tile_pos;


                            Vector2 start_pos = new Vector2(Mathf.Min(start_dragging_pos.x, end_dragging_pos.x),
                                                            Mathf.Min(start_dragging_pos.y, end_dragging_pos.y));
                            Vector2 end_pos = new Vector2(Mathf.Max(start_dragging_pos.x, end_dragging_pos.x),
                                                          Mathf.Max(start_dragging_pos.y, end_dragging_pos.y));
                            Vector2 selection_snapped_pos = TileSetBrushUtil.GetSnappedPosition(start_pos, tileMap.cell_size);
                            Vector2 selection_size        = TileSetBrushUtil.GetSnappedPosition(end_pos, tileMap.cell_size) - selection_snapped_pos + tileMap.cell_size;

                            TileSetBrushBehaviour tileSetBrushBehaviour = TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap);
                            // Update brush transform
                            local_tileSetBrushBehaviour_pos = (Vector2)tileMap.transform.InverseTransformPoint(ray.GetPoint(distance));//设置tileSetBrushBehaviour的position
                            Vector2 tileSetBrushBehaviour_snapped_pos = TileSetBrushUtil.GetSnappedPosition((tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos), tileMap.cell_size);
                            tileSetBrushBehaviour.transform.rotation   = tileMap.transform.rotation;
                            tileSetBrushBehaviour.transform.localScale = tileMap.transform.lossyScale;
                            if (!TileSetBrushBehaviour.instance.is_dragging)//不是拖动的时候
                            {
                                tileSetBrushBehaviour.transform.position =
                                    tileMap.transform.TransformPoint(new Vector3(tileSetBrushBehaviour_snapped_pos.x, tileSetBrushBehaviour_snapped_pos.y, -0.01f));
                            }

                            int pre_mouse_grid_x = mouse_grid_x;
                            int pre_mouse_grid_y = mouse_grid_y;
                            if (e.isMouse)
                            {
                                mouse_grid_x = TileSetBrushUtil.GetGridX(local_tileSetBrushBehaviour_pos, tileMap.cell_size);
                                mouse_grid_y = TileSetBrushUtil.GetGridY(local_tileSetBrushBehaviour_pos, tileMap.cell_size);
                            }

                            bool is_mouse_grid_changed = pre_mouse_grid_x != mouse_grid_x || pre_mouse_grid_y != mouse_grid_y;
                            //Update Fill Preview
                            if (GetTileSetBrushMode() == TileMapEditorBrushMode.Fill && is_mouse_grid_changed)
                            {
                                fill_preview_tile_pos_list.Clear();
                                TileMapDrawingUtil.FloodFillPreview(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos,
                                                                    tileSetBrushBehaviour.tileMap.GetTileData(0, 0), fill_preview_tile_pos_list);
                            }

                            bool is_modifiers_changed = false;
                            if (e.isKey)
                            {
                                EventModifiers filtered_modifiers = e.modifiers & (EventModifiers.Control | EventModifiers.Alt);
                                is_modifiers_changed = filtered_modifiers != m_prevEventModifiers;
                                m_prevEventModifiers = filtered_modifiers;
                            }

                            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 && is_mouse_grid_changed ||
                                 e.type == EventType.MouseUp || is_modifiers_changed)
                                )
                            {
                                if (e.button == 0)
                                {
                                    if (mouseDoubleClick.IsDoubleClick && tileSetBrushBehaviour.tileMap.GridWidth == 1 &&
                                        tileSetBrushBehaviour.tileMap.GridHeight == 1)
                                    {
                                        // Restore previous tiledata modified by Paint, because before the double click, a single click is done before
                                        tileMap.SetTileData(tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, flood_fill_restored_tileData);
                                        tileSetBrushBehaviour.FloodFill(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos);
                                    }
                                    // Do a brush paint action
                                    else
                                    {
                                        switch (GetTileSetBrushMode())
                                        {
                                        case TileMapEditorBrushMode.Paint:
                                            if (e.type == EventType.MouseDown)
                                            {
                                                flood_fill_restored_tileData = tileMap.GetTileData(mouse_grid_x, mouse_grid_y);
                                            }

                                            if (e.type == EventType.MouseDown)
                                            {
                                                tileSetBrushBehaviour.DoPaintPressed(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers);
                                            }
                                            else if (e.type == EventType.MouseDrag || is_modifiers_changed &&
                                                     TileSetBrushBehaviour.instance.paint_mode != TileSetBrushPaintMode.Pencil)
                                            {
                                                tileSetBrushBehaviour.DoPaintDragged(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers);
                                            }
                                            else if (e.type == EventType.MouseUp)
                                            {
                                                tileSetBrushBehaviour.DoPaintReleased(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers);
                                            }
                                            break;

                                        case TileMapEditorBrushMode.Erase:
                                            tileSetBrushBehaviour.Erase(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos);
                                            break;

                                        case TileMapEditorBrushMode.Fill:
                                            tileSetBrushBehaviour.FloodFill(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos);
                                            break;
                                        }
                                    }
                                }
                                else if (e.button == 1)
                                {
                                    if (e.type == EventType.MouseDown)
                                    {
                                        is_dragging = true;
                                        tileSetBrushBehaviour.tileMap.ClearMap();
                                        start_dragging_pos = end_dragging_pos = local_tileSetBrushBehaviour_pos;
                                    }
                                    else
                                    {
                                        end_dragging_pos = local_tileSetBrushBehaviour_pos;
                                    }
                                }
                            }

                            if (e.type == EventType.MouseUp)
                            {
                                if (e.button == 1) // right mouse button
                                {
                                    is_dragging = false;
                                    ResetTileSetBrushMode();
                                    // Copy one tile
                                    if (selection_size.x <= tileMap.cell_size.x && selection_size.y <= tileMap.cell_size.y)
                                    {
                                        uint tileData = tileMap.GetTileData(local_tileSetBrushBehaviour_pos);
                                        //Select the first tile not null if any and select the tilemap
                                        if (e.control && tileMap.parent_tileMapGroup)
                                        {
                                            for (int i = tileMap.parent_tileMapGroup.tileMap_list.Count - 1; i >= 0; --i)
                                            {
                                                var tileMap = this.tileMap.parent_tileMapGroup.tileMap_list[i];
                                                tileData = tileMap.GetTileData(local_tileSetBrushBehaviour_pos);
                                                if (tileData != TileSetConst.TileData_Empty)
                                                {
                                                    tileMap.parent_tileMapGroup.selected_tileMap = tileMap;
                                                    if (Selection.activeGameObject == this.tileMap.gameObject)
                                                    {
                                                        Selection.activeGameObject = tileMap.gameObject;
                                                    }
                                                    break;
                                                }
                                            }
                                        }

                                        if (tileData == TileSetConst.TileData_Empty)
                                        {
                                            tileMap.tileSet.selected_tileId = TileSetConst.TileId_Empty;
                                            tileSetBrushBehaviour.tileMap.SetTileData(0, 0, TileSetConst.TileData_Empty);
                                        }
                                        else
                                        {
                                            int brushId = TileSetUtil.GetTileSetBrushIdFromTileData(tileData);
                                            int tileId  = TileSetUtil.GetTileIdFromTileData(tileData);

                                            // Select the copied tile in the tileset
                                            if (brushId > 0 && !e.alt) //NOTE: if Alt is held, the tile is selected instead
                                            {
                                                tileMap.tileSet.selected_tileSetBrushId = brushId;
                                            }
                                            else
                                            {
                                                tileMap.tileSet.selected_tileId = tileId;
                                                tileSetBrushBehaviour.tileMap.SetTileData(0, 0,
                                                                                          tileData & ~TileSetConst.TileDataMask_TileSetBrushId); // keep tile flags
                                            }
                                        }

                                        // Cut tile if key shift is pressed
                                        if (e.shift)
                                        {
                                            int startGridX = TileSetBrushUtil.GetGridX(start_pos, tileMap.cell_size);
                                            int startGridY = TileSetBrushUtil.GetGridY(start_pos, tileMap.cell_size);
                                            tileSetBrushBehaviour.CutRect(tileMap, startGridX, startGridY, startGridX, startGridY);
                                        }

                                        tileSetBrushBehaviour.tileMap.UpdateMesh();
                                        tileSetBrushBehaviour.offset = Vector2.zero;
                                    }
                                    // copy a rect of tiles
                                    else
                                    {
                                        int startGridX = TileSetBrushUtil.GetGridX(start_pos, tileMap.cell_size);
                                        int startGridY = TileSetBrushUtil.GetGridY(start_pos, tileMap.cell_size);
                                        int endGridX   = TileSetBrushUtil.GetGridX(end_pos, tileMap.cell_size);
                                        int endGridY   = TileSetBrushUtil.GetGridY(end_pos, tileMap.cell_size);

                                        // Cut tile if key shift is pressed
                                        if (e.shift)
                                        {
                                            tileSetBrushBehaviour.CutRect(tileMap, startGridX, startGridY, endGridX, endGridY);
                                        }
                                        else
                                        {
                                            tileSetBrushBehaviour.CopyRect(tileMap, startGridX, startGridY, endGridX, endGridY);
                                        }

                                        tileSetBrushBehaviour.offset.x = end_dragging_pos.x > start_dragging_pos.x
                    ? -(endGridX - startGridX) * tileMap.cell_size.x
                    : 0f;
                                        tileSetBrushBehaviour.offset.y = end_dragging_pos.y > start_dragging_pos.y
                    ? -(endGridY - startGridY) * tileMap.cell_size.y
                    : 0f;
                                    }
                                }
                            }

                            if (is_dragging)
                            {
                                Rect rGizmo = new Rect(selection_snapped_pos, selection_size);
                                DrawUtil.HandlesDrawSolidRectangleWithOutline(rGizmo, new Color(), Color.white, tileMap.transform);
                            }
                            else // Draw brush border
                            {
                                Rect rBound = new Rect(tileSetBrushBehaviour.tileMap.tileMapBounds.min,
                                                       tileSetBrushBehaviour.tileMap.tileMapBounds.size);
                                Color fillColor;
                                switch (GetTileSetBrushMode())
                                {
                                case TileMapEditorBrushMode.Paint:
                                    fillColor = new Color(0, 0, 0, 0);
                                    break;

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

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

                                default:
                                    fillColor = new Color(0, 0, 0, 0);
                                    break;
                                }

                                DrawUtil.HandlesDrawSolidRectangleWithOutline(rBound, fillColor, new Color(1, 1, 1, 0.2f),
                                                                              tileSetBrushBehaviour.transform);
                            }
                        }
                    }

                    if (current_event_type == EventType.MouseDrag && Event.current.button < 2) // 2 is for central mouse button
                    {
                        // avoid dragging the map
                        Event.current.Use();
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.LogException(exception);
            }

            SceneView.RepaintAll();
            GUIUtility.hotControl = save_control;
        }
示例#2
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, bool randomize = false)
        {
            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);
            int  dataIdx0            = randomize ? Random.Range(0, patternW) : (gridX % patternW + patternW) % patternW;
            int  dataIdx1            = randomize ? Random.Range(0, patternH) : (gridY % patternH + patternH) % patternH;

            tilemap.SetTileData(gridX, gridY, tileData[dataIdx0, dataIdx1]);
            bool isBrush = TileSetUtil.GetTileSetBrushIdFromTileData(floodFrom) != 0;

            //Debug.Log(" Flood Fill Starts +++++++++++++++ ");
            if (
                (patternW > 0 && patternH > 0) &&
                isBrush ?
                TileSetUtil.GetTileSetBrushIdFromTileData(floodFrom) != TileSetUtil.GetTileSetBrushIdFromTileData(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.min_grid_x && next.X <= tilemap.max_grid_x &&
                            next.Y >= tilemap.min_grid_y && next.Y <= tilemap.max_grid_y
                            )
                        {
                            if (
                                isBrush ?
                                TileSetUtil.GetTileSetBrushIdFromTileData(floodFrom) == TileSetUtil.GetTileSetBrushIdFromTileData(nextTileData)
                  :
                                floodFrom == nextTileData
                                )
                            {
                                check.AddLast(next);
                                dataIdx0 = randomize ? Random.Range(0, patternW) : (next.X % patternW + patternW) % patternW;
                                dataIdx1 = randomize ? Random.Range(0, patternH) : (next.Y % patternH + patternH) % patternH;
                                tilemap.SetTileData(next.X, next.Y, tileData[dataIdx0, dataIdx1]);
                            }
                        }
                    }

                    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");
        }
示例#3
0
        public void SetTileData(int local_grid_x, int local_grid_y, uint tileData)
        {
            if (local_grid_x >= 0 && local_grid_x < width && local_grid_y >= 0 && local_grid_y < height)
            {
                int tile_index = local_grid_y * width + local_grid_x;

                int  tileId = (int)(tileData & TileSetConst.TileDataMask_TileId);
                Tile tile   = tileSet.GetTile(tileId);

                int  prev_tileId = (int)(tileData_list[tile_index] & TileSetConst.TileDataMask_TileId);
                Tile prev_tile   = tileSet.GetTile(prev_tileId);

                int brushId      = TileSetUtil.GetTileSetBrushIdFromTileData(tileData);
                int prev_brushId = TileSetUtil.GetTileSetBrushIdFromTileData(tileData_list[tile_index]);

                if (brushId != prev_brushId)
                {
                    if (!current_updated_tileMapChunk) // avoid this is chunks is being Updated from FillMeshData
                    {
                        // Refresh Neighbors ( and itself if needed )
                        for (int y_offset = -1; y_offset <= 1; ++y_offset)
                        {
                            for (int x_offset = -1; x_offset <= 1; ++x_offset)
                            {
                                if ((x_offset | y_offset) == 0)
                                {
                                    if (brushId > 0)
                                    {
                                        // Refresh itself
                                        tileData = (tileData & ~TileSetConst.TileFlag_Updated);
                                    }
                                }
                                else
                                {
                                    int          grid_x                = (local_grid_x + x_offset);
                                    int          grid_y                = (local_grid_y + y_offset);
                                    int          index                 = grid_y * width + grid_x;
                                    bool         is_inside_chunk       = (grid_x >= 0 && grid_x < width && grid_y >= 0 && grid_y < height);
                                    uint         neighbor_tileData     = is_inside_chunk ? tileData_list[index] : parent_tileMap.GetTileData(offset_grid_x + local_grid_x + x_offset, offset_grid_y + local_grid_y + y_offset);
                                    int          neighbor_brushId      = (int)((neighbor_tileData & TileSetConst.TileDataMask_TileSetBrushId) >> 16);
                                    TileSetBrush neighbor_tileSetBrush = parent_tileMap.tileSet.FindTileSetBrush(neighbor_brushId);
                                    //if (brush != null && brush.AutotileWith(brushId, neighborBrushId) || prevBrush != null && prevBrush.AutotileWith(prevBrushId, neighborBrushId))
                                    if (neighbor_tileSetBrush != null &&
                                        (neighbor_tileSetBrush.AutoTileWith(neighbor_brushId, brushId) || neighbor_tileSetBrush.AutoTileWith(neighbor_brushId, prev_brushId)))
                                    {
                                        neighbor_tileData = (neighbor_tileData & ~TileSetConst.TileFlag_Updated); // force a refresh
                                        if (is_inside_chunk)
                                        {
                                            tileData_list[index] = neighbor_tileData;
                                        }
                                        else
                                        {
                                            parent_tileMap.SetTileData(offset_grid_x + grid_x, offset_grid_y + grid_y, neighbor_tileData);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (brushId > 0)
                {
                    // Refresh itself
                    tileData = (tileData & ~TileSetConst.TileFlag_Updated);
                }

                is_need_rebuild_mesh      |= (tileData_list[tile_index] != tileData) || (tileData & TileSetConst.TileDataMask_TileId) == TileSetConst.TileId_Empty;
                is_need_rebuild_colliders |= is_need_rebuild_mesh &&
                                             (
                    (prev_brushId > 0) || (brushId > 0) || // there is a brush (a brush could change the collider data later)
                    (tile != null && tile.tileColliderData.type != TileColliderType.None) || (prev_tile != null && prev_tile.tileColliderData.type != TileColliderType.None) // prev. or new tile has colliders
                                             );

                if (parent_tileMap.tileMapColliderType != TileMapColliderType.None && is_need_rebuild_colliders)
                {
                    // Refresh Neighbors tilechunk colliders, to make the collider autotiling
                    // Only if neighbor is outside this tilechunk
                    for (int y_offset = -1; y_offset <= 1; ++y_offset)
                    {
                        for (int x_offset = -1; x_offset <= 1; ++x_offset)
                        {
                            if ((x_offset | y_offset) != 0) // skip this tile position xf = yf = 0
                            {
                                int  grid_x          = (local_grid_x + x_offset);
                                int  grid_y          = (local_grid_y + y_offset);
                                bool is_inside_chunk = (grid_x >= 0 && grid_x < width && grid_y >= 0 && grid_y < height);
                                if (!is_inside_chunk)
                                {
                                    parent_tileMap.InvalidateChunkAt(offset_grid_x + grid_x, offset_grid_y + grid_y, false, true);
                                }
                            }
                        }
                    }
                }

                // Update tile data
                tileData_list[tile_index] = tileData;

                if (!TileMap.Is_Disable_Tile_Prefab_Creation)
                {
                    // Create tile Objects
                    if (tile != null && tile.tilePrefabData.prefab != null)
                    {
                        CreateTileObject(tile_index, tile.tilePrefabData);
                    }
                    else
                    {
                        DestroyTileObject(tile_index);
                    }
                }

                TileSetBrush brush = parent_tileMap.tileSet.FindTileSetBrush(brushId);
                if (brushId != prev_brushId)
                {
                    TileSetBrush prevBrush = parent_tileMap.tileSet.FindTileSetBrush(prev_brushId);
                    if (prevBrush != null)
                    {
                        prevBrush.OnErase(this, local_grid_x, local_grid_y, tileData, prev_brushId);
                    }
                }
                if (brush != null)
                {
                    tileData = brush.OnPaint(this, local_grid_x, local_grid_y, tileData);
                }
            }
        }
示例#4
0
        //Note: this is doing the same as FloodFill but not saving data in the tilemap, only saving the filled points and returning a list
        public static void FloodFillPreview(TileMap tilemap, int gridX, int gridY, uint tileData, List <Vector2> outFilledPoints, uint maxPoints = uint.MaxValue)
        {
            if (
                gridX >= tilemap.min_grid_x && gridX <= tilemap.max_grid_x &&
                gridY >= tilemap.min_grid_y && gridY <= tilemap.max_grid_y
                )
            {
                bool[]             filledPoints = new bool[tilemap.GridWidth * tilemap.GridHeight];
                LinkedList <Point> check        = new LinkedList <Point>();
                uint floodFrom = tilemap.GetTileData(gridX, gridY);
                outFilledPoints.Add(Vector2.Scale(new Vector2(gridX, gridY), tilemap.cell_size));
                filledPoints[(gridY - tilemap.min_grid_y) * tilemap.GridWidth + gridX - tilemap.min_grid_x] = true;
                bool isBrush = TileSetUtil.GetTileSetBrushIdFromTileData(floodFrom) != 0;
                if (
                    isBrush ?
                    TileSetUtil.GetTileSetBrushIdFromTileData(floodFrom) != TileSetUtil.GetTileSetBrushIdFromTileData(tileData)
            :
                    floodFrom != tileData
                    )
                {
                    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);

                            if (
                                next.X >= tilemap.min_grid_x && next.X <= tilemap.max_grid_x &&
                                next.Y >= tilemap.min_grid_y && next.Y <= tilemap.max_grid_y
                                )
                            {
                                if (filledPoints[(next.Y - tilemap.min_grid_y) * tilemap.GridWidth + next.X - tilemap.min_grid_x])
                                {
                                    continue;                                                                                // skip already filled points
                                }
                                uint nextTileData = tilemap.GetTileData(next.X, next.Y);
                                if (
                                    isBrush ?
                                    TileSetUtil.GetTileSetBrushIdFromTileData(floodFrom) == TileSetUtil.GetTileSetBrushIdFromTileData(nextTileData)
                    :
                                    floodFrom == nextTileData
                                    )
                                {
                                    check.AddLast(next);
                                    filledPoints[(next.Y - tilemap.min_grid_y) * tilemap.GridWidth + next.X - tilemap.min_grid_x] = true;
                                    outFilledPoints.Add(Vector2.Scale(new Vector2(next.X, next.Y), tilemap.cell_size));
                                    if (outFilledPoints.Count >= maxPoints)
                                    {
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }