DotRecastNetSim/src/DotRecast.Recast.Demo/Tools/OffMeshConnectionTool.cs

129 lines
4.0 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
2023-03-15 17:00:29 +03:00
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
2023-03-14 08:02:43 +03:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
using Silk.NET.Windowing;
using DotRecast.Core;
using DotRecast.Recast.Demo.Builder;
using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Demo.Geom;
using static DotRecast.Recast.Demo.Draw.DebugDraw;
namespace DotRecast.Recast.Demo.Tools;
2023-03-16 19:48:49 +03:00
public class OffMeshConnectionTool : Tool
{
2023-03-14 08:02:43 +03:00
private Sample sample;
private bool hitPosSet;
private float[] hitPos;
private bool bidir;
2023-03-16 19:48:49 +03:00
public override void setSample(Sample m_sample)
{
2023-03-14 08:02:43 +03:00
sample = m_sample;
}
2023-03-16 19:48:49 +03:00
public override void handleClick(float[] s, float[] p, bool shift)
{
2023-03-14 08:02:43 +03:00
DemoInputGeomProvider geom = sample.getInputGeom();
2023-03-16 19:48:49 +03:00
if (geom == null)
{
2023-03-14 08:02:43 +03:00
return;
}
2023-03-16 19:48:49 +03:00
if (shift)
{
2023-03-14 08:02:43 +03:00
// Delete
// Find nearest link end-point
float nearestDist = float.MaxValue;
DemoOffMeshConnection nearestConnection = null;
2023-03-16 19:48:49 +03:00
foreach (DemoOffMeshConnection offMeshCon in geom.getOffMeshConnections())
{
2023-03-14 08:02:43 +03:00
float d = Math.Min(DemoMath.vDistSqr(p, offMeshCon.verts, 0), DemoMath.vDistSqr(p, offMeshCon.verts, 3));
2023-03-16 19:48:49 +03:00
if (d < nearestDist && Math.Sqrt(d) < sample.getSettingsUI().getAgentRadius())
{
2023-03-14 08:02:43 +03:00
nearestDist = d;
nearestConnection = offMeshCon;
}
}
2023-03-16 19:48:49 +03:00
if (nearestConnection != null)
{
2023-03-14 08:02:43 +03:00
geom.getOffMeshConnections().Remove(nearestConnection);
}
2023-03-16 19:48:49 +03:00
}
else
{
2023-03-14 08:02:43 +03:00
// Create
2023-03-16 19:48:49 +03:00
if (!hitPosSet)
{
2023-03-14 08:02:43 +03:00
hitPos = ArrayUtils.CopyOf(p, p.Length);
hitPosSet = true;
2023-03-16 19:48:49 +03:00
}
else
{
2023-03-14 08:02:43 +03:00
int area = SampleAreaModifications.SAMPLE_POLYAREA_TYPE_JUMP;
int flags = SampleAreaModifications.SAMPLE_POLYFLAGS_JUMP;
geom.addOffMeshConnection(hitPos, p, sample.getSettingsUI().getAgentRadius(), bidir, area, flags);
hitPosSet = false;
}
}
}
2023-03-16 19:48:49 +03:00
public override void handleRender(NavMeshRenderer renderer)
{
if (sample == null)
{
2023-03-14 08:02:43 +03:00
return;
}
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
RecastDebugDraw dd = renderer.getDebugDraw();
float s = sample.getSettingsUI().getAgentRadius();
2023-03-16 19:48:49 +03:00
if (hitPosSet)
{
2023-03-14 08:02:43 +03:00
dd.debugDrawCross(hitPos[0], hitPos[1] + 0.1f, hitPos[2], s, duRGBA(0, 0, 0, 128), 2.0f);
}
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
DemoInputGeomProvider geom = sample.getInputGeom();
2023-03-16 19:48:49 +03:00
if (geom != null)
{
2023-03-14 08:02:43 +03:00
renderer.drawOffMeshConnections(geom, true);
}
}
2023-03-16 19:48:49 +03:00
public override void layout(IWindow ctx)
{
2023-03-14 08:02:43 +03:00
// nk_layout_row_dynamic(ctx, 20, 1);
// bidir = !nk_option_label(ctx, "One Way", !bidir);
// nk_layout_row_dynamic(ctx, 20, 1);
// bidir = nk_option_label(ctx, "Bidirectional", bidir);
}
2023-03-16 19:48:49 +03:00
public override string getName()
{
2023-03-14 08:02:43 +03:00
return "Create Off-Mesh Links";
}
2023-03-16 19:48:49 +03:00
public override void handleUpdate(float dt)
{
2023-03-14 08:02:43 +03:00
// TODO Auto-generated method stub
}
2023-03-16 19:48:49 +03:00
}