From 97c426966b93224acbac34e770683af22db25f11 Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 28 Jun 2023 23:12:32 +0900 Subject: [PATCH] OffMeshConnectionTool, impl --- .../Tools/OffMeshConnectionTool.cs | 31 ++-------- .../Tools/OffMeshConnectionToolImpl.cs | 59 ++++++++++++++++++- .../Tools/OffMeshConnectionToolOptions.cs | 7 +++ 3 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolOptions.cs diff --git a/src/DotRecast.Recast.Demo/Tools/OffMeshConnectionTool.cs b/src/DotRecast.Recast.Demo/Tools/OffMeshConnectionTool.cs index 6c5b81c..0e00c0e 100644 --- a/src/DotRecast.Recast.Demo/Tools/OffMeshConnectionTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/OffMeshConnectionTool.cs @@ -35,7 +35,6 @@ public class OffMeshConnectionTool : IRcTool private readonly OffMeshConnectionToolImpl _impl; private bool hitPosSet; private RcVec3f hitPos; - private int bidir; public OffMeshConnectionTool() { @@ -46,7 +45,7 @@ public class OffMeshConnectionTool : IRcTool { return _impl; } - + public void OnSampleChanged() { // .. @@ -62,24 +61,7 @@ public class OffMeshConnectionTool : IRcTool if (shift) { - // Delete - // Find nearest link end-point - float nearestDist = float.MaxValue; - DemoOffMeshConnection nearestConnection = null; - foreach (DemoOffMeshConnection offMeshCon in geom.GetOffMeshConnections()) - { - float d = Math.Min(RcVec3f.DistSqr(p, offMeshCon.verts, 0), RcVec3f.DistSqr(p, offMeshCon.verts, 3)); - if (d < nearestDist && Math.Sqrt(d) < _impl.GetSample().GetSettings().agentRadius) - { - nearestDist = d; - nearestConnection = offMeshCon; - } - } - - if (nearestConnection != null) - { - geom.GetOffMeshConnections().Remove(nearestConnection); - } + _impl.Remove(p); } else { @@ -91,9 +73,7 @@ public class OffMeshConnectionTool : IRcTool } else { - int area = SampleAreaModifications.SAMPLE_POLYAREA_TYPE_JUMP; - int flags = SampleAreaModifications.SAMPLE_POLYFLAGS_JUMP; - geom.AddOffMeshConnection(hitPos, p, _impl.GetSample().GetSettings().agentRadius, 0 == bidir, area, flags); + _impl.Add(hitPos, p); hitPosSet = false; } } @@ -123,8 +103,9 @@ public class OffMeshConnectionTool : IRcTool public void Layout() { - ImGui.RadioButton("One Way", ref bidir, 0); - ImGui.RadioButton("Bidirectional", ref bidir, 1); + var options = _impl.GetOptions(); + ImGui.RadioButton("One Way", ref options.bidir, 0); + ImGui.RadioButton("Bidirectional", ref options.bidir, 1); } diff --git a/src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolImpl.cs b/src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolImpl.cs index c418ecc..4818c38 100644 --- a/src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolImpl.cs +++ b/src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolImpl.cs @@ -1,14 +1,25 @@ -namespace DotRecast.Recast.DemoTool.Tools +using System; +using DotRecast.Core; +using DotRecast.Recast.DemoTool.Builder; +using DotRecast.Recast.DemoTool.Geom; + +namespace DotRecast.Recast.DemoTool.Tools { public class OffMeshConnectionToolImpl : ISampleTool { private Sample _sample; - + private readonly OffMeshConnectionToolOptions _options; + + public OffMeshConnectionToolImpl() + { + _options = new OffMeshConnectionToolOptions(); + } + public string GetName() { return "Create Off-Mesh Links"; } - + public void SetSample(Sample sample) { _sample = sample; @@ -19,5 +30,47 @@ return _sample; } + + public OffMeshConnectionToolOptions GetOptions() + { + return _options; + } + + public void Add(RcVec3f start, RcVec3f end) + { + DemoInputGeomProvider geom = _sample.GetInputGeom(); + if (null == geom) + return; + + int area = SampleAreaModifications.SAMPLE_POLYAREA_TYPE_JUMP; + int flags = SampleAreaModifications.SAMPLE_POLYFLAGS_JUMP; + geom.AddOffMeshConnection(start, end, _sample.GetSettings().agentRadius, 0 == _options.bidir, area, flags); + } + + public void Remove(RcVec3f p) + { + DemoInputGeomProvider geom = _sample.GetInputGeom(); + if (null == geom) + return; + + // Delete + // Find nearest link end-point + float nearestDist = float.MaxValue; + DemoOffMeshConnection nearestConnection = null; + foreach (DemoOffMeshConnection offMeshCon in geom.GetOffMeshConnections()) + { + float d = Math.Min(RcVec3f.DistSqr(p, offMeshCon.verts, 0), RcVec3f.DistSqr(p, offMeshCon.verts, 3)); + if (d < nearestDist && Math.Sqrt(d) < _sample.GetSettings().agentRadius) + { + nearestDist = d; + nearestConnection = offMeshCon; + } + } + + if (nearestConnection != null) + { + geom.GetOffMeshConnections().Remove(nearestConnection); + } + } } } \ No newline at end of file diff --git a/src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolOptions.cs b/src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolOptions.cs new file mode 100644 index 0000000..5742fe0 --- /dev/null +++ b/src/DotRecast.Recast.DemoTool/Tools/OffMeshConnectionToolOptions.cs @@ -0,0 +1,7 @@ +namespace DotRecast.Recast.DemoTool.Tools +{ + public class OffMeshConnectionToolOptions + { + public int bidir; + } +} \ No newline at end of file