我正在开发一个用于映射和播放流行桌面游戏 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 元素,而另一个 UI 元素位于它之上。谢谢!