我正在开发一个用于映射和播放流行桌面游戏的程序D& D:D现在我正在努力获取基本功能,例如拖动UI元素,捕捉到网格并检查碰撞。 / p>
现在,从鼠标释放的每个对象都会立即捕捉到最近的网格点。当像玩家对象这样的东西捕捉到具有墙壁或其他相邻墙的网格点时,这会导致问题。所以基本上当玩家被摔倒时,他们最终会覆盖一些墙壁。这很好并按预期工作,但问题是,现在我的碰撞检测在你试图移动这个玩家时会被绊倒,因为它位于墙下,因此你不能再拖动玩家了。
以下是相关代码:
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元素。谢谢!
答案 0 :(得分:0)
为什么不包括您仍希望能够在碰撞检测代码中移动对象的情况。我在考虑碰撞检测代码中的简单if语句。如果没有看到碰撞检测代码,我无法告诉你。
此外,设置/更改“阻止”的内容是什么?
答案 1 :(得分:0)
如果墙砖与相邻的单元重叠并导致碰撞触发,则应使用较小的碰撞形状,这些形状不会重叠。
理想情况下,碰撞形状应该与被占用的网格单元的大小完全相同,与视觉外观无关,对吗?
答案 2 :(得分:0)
我想花点时间提出另一种解决方案......
使用现有经过良好测试的实施:MapTool!
当然,如果你特别需要或想要自己实现它(这绝对是一个有趣的个人项目),这个解决方案是没用的,但我认为应该提到它。 8)