3

私は、人気の卓上ゲーム 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 要素を移動できるようにする簡単な方法を探しています。ありがとう!

4

3 に答える 3

0

オブジェクトを移動できるようにしたい場合を、衝突検出コードに含めないでください。衝突検出コードの単純な if ステートメントを考えています。衝突検出コードを見ずして、mroe とは言えません。

また、「ブロックされた」設定/変更は何ですか?

于 2010-06-30T13:33:40.473 に答える
0

壁のタイルが隣接するセルと重なって衝突が発生する場合は、重ならない小さな衝突形状を使用する必要があります。

理想的には、衝突形状は、視覚的な外観とは関係なく、占有されているグリッド セルのサイズと正確に一致する必要があります。

于 2010-07-05T11:16:55.827 に答える
0

別の解決策を提案するために少し時間を取りたいと思います...
既存の十分にテストされた実装を使用してください: MapTool!

もちろん、このソリューションは、特に自分で実装する必要がある場合、または実装したい場合 (間違いなく楽しい個人的なプロジェクト) には役に立ちませんが、言及する必要があると思います。8 )

于 2010-07-06T15:09:41.243 に答える