c# - 在固定网格上拖动对象的算法

标签 c# grid drag

我正在开发一个用于绘制和播放流行桌面游戏 D&D 的程序:D 现在我正在开发基本功能,例如四处拖动 UI 元素、捕捉到网格和检查碰撞。

现在,从鼠标中释放每个对象后,它会立即捕捉到最近的网格点。当玩家对象之类的东西捕捉到有墙或其他相邻的网格点时,这会导致问题。所以基本上当玩家掉落时,他们最终会被一些墙覆盖。这很好并且按预期工作,但问题是现在每当您尝试移动此播放器时我的碰撞检测都会被触发,因为它位于墙下并且因此您不能再拖动播放器。

相关代码如下:

void UIObj_MouseMove(object sender, MouseEventArgs e)  
{  
            blocked = false;  
            if (dragging)  
            {  
                foreach (UIElement o in ((Floor)Parent).Children)  
                {  
                    if (o.GetType() != GetType() && o.GetType().BaseType == typeof(UIObj) &&  
                        Math.Sqrt(Math.Pow(((UIObj)o).cX - cX, 2) + Math.Pow(((UIObj)o).cY - cY, 2)) <  
                        Math.Max(r.Height + ((UIObj)o).r.Height, r.Width + ((UIObj)o).r.Width))  
                    {  
                        double Y = e.GetPosition((Floor)Parent).Y;  
                        double X = e.GetPosition((Floor)Parent).X;  
                        Geometry newRect = new RectangleGeometry(new Rect(Margin.Left + (X - prevX),  
                        Margin.Top + (Y - prevY), Margin.Right + (X - prevX), Margin.Bottom + (Y - prevY)));  
                        GeometryHitTestParameters ghtp = new GeometryHitTestParameters(newRect);  
                        VisualTreeHelper.HitTest(o, null, new HitTestResultCallback(MyHitTestResultCallback), ghtp);  
                    }  
                }  
                if (!blocked)  
                {  
                    Margin = new Thickness(Margin.Left + (e.GetPosition((Floor)Parent).X - prevX),  
                        Margin.Top + (e.GetPosition((Floor)Parent).Y - prevY),  
                        Margin.Right + (e.GetPosition((Floor)Parent).X - prevX),  
                        Margin.Bottom + (e.GetPosition((Floor)Parent).Y - prevY));  
                    InvalidateVisual();  
                }
                prevX = e.GetPosition((Floor)Parent).X;  
                prevY = e.GetPosition((Floor)Parent).Y;  
                cX = Margin.Left + r.Width / 2;  
                cY = Margin.Top + r.Height / 2;  
            }  
        }  

internal virtual void SnapToGrid()  
        {  
            double xPos = Margin.Left;  
            double yPos = Margin.Top;  
            double xMarg =  xPos % ((Floor)Parent).cellDim;  
            double yMarg =  yPos % ((Floor)Parent).cellDim;  
            if (xMarg < ((Floor)Parent).cellDim / 2)  
            {  
                if (yMarg < ((Floor)Parent).cellDim / 2)  
                {  
                    Margin = new Thickness(xPos - xMarg, yPos - yMarg, xPos - xMarg + r.Width, yPos - yMarg + r.Height);
                    }  
                else  
                {  
                    Margin = new Thickness(xPos - xMarg, yPos - yMarg + ((Floor)Parent).cellDim, xPos - xMarg + r.Width,

                        yPos - yMarg + ((Floor)Parent).cellDim + r.Height);  
                }  
            }  
            else  
            {
                if (yMarg < ((Floor)Parent).cellDim / 2)
                {
                    Margin = new Thickness(xPos - xMarg + ((Floor)Parent).cellDim, yPos - yMarg,
                        xPos - xMarg + ((Floor)Parent).cellDim + r.Width, yPos - yMarg + r.Height);
                }
                else
                {
                    Margin = new Thickness(xPos - xMarg + ((Floor)Parent).cellDim, yPos - yMarg + ((Floor)Parent).cellDim,
                        xPos - xMarg + ((Floor)Parent).cellDim + r.Width, yPos - yMarg + ((Floor)Parent).cellDim + r.Height);
                }
            }
        }

本质上,我正在寻找一种简单的方法来修改现有代码,以允许移动一个 UI 元素,而另一个元素位于它之上。谢谢!

最佳答案

我只想花点时间提出一个替代解决方案...
使用现有的经过良好测试的实现:MapTool!

当然,如果你特别需要或想自己实现它(绝对是一个有趣的个人项目),这个解决方案是没有用的,但我认为应该提到它。 8)

关于c# - 在固定网格上拖动对象的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3063014/

相关文章:

ios - 多个动态绘制的多边形拖动

android - Libgdx 触摸并拖动屏幕上的任意位置以移动 Sprite

javascript - 限制可拖动元素 JS

c# - 关于处理表单的代码分析警告

c# - 我们如何在 MSTest 中运行具有多个参数的测试方法?

python - 框架内的网格?

由于使用分页按钮,extjs 为网格传递额外参数

c# - xamarin 形式 : Couldn't change the background color of selected item on ListView

c# - 是否可以隐藏继承的数据成员?

css - 将 2 个内联列垂直放置到背景中间