Skip to content

Commit

Permalink
#35: made PathRequest generic in preparation for dynamic pathfinding …
Browse files Browse the repository at this point in the history
…support.
  • Loading branch information
Barsonax committed Nov 13, 2017
1 parent 98eab24 commit 0fa3976
Show file tree
Hide file tree
Showing 43 changed files with 488 additions and 397 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ICmpInitializable.OnShutdown(ShutdownContext context)
DualityApp.Mouse.Move -= Mouse_Move;
}

private void PathSolved(PathRequest pathRequest)
private void PathSolved(PathRequest<Path> pathRequest)
{
Path = pathRequest.CompletedPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ICmpInitializable.OnShutdown(ShutdownContext context)
DualityApp.Mouse.Move -= Mouse_Move;
}

private void PathSolved(PathRequest pathRequest)
private void PathSolved(PathRequest<Path> pathRequest)
{
Path = pathRequest.CompletedPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void Mouse_ButtonDown(object sender, MouseButtonEventArgs e)
request.AddCallback(OnRequestCompleted);
}

private void OnRequestCompleted(PathRequest pathRequest)
private void OnRequestCompleted(PathRequest<Path> pathRequest)
{
Path = pathRequest.CompletedPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ICmpUpdatable.OnUpdate()
_frameCounter++;
}

private void PathSolved(PathRequest pathRequest)
private void PathSolved(PathRequest<Path> pathRequest)
{
Path = pathRequest.CompletedPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Duality.Input;
using Duality.Plugins.Pathfindax.Components;
using Duality.Plugins.Pathfindax.PathfindEngine;
using Duality.Resources;
using Pathfindax.Nodes;
using Pathfindax.PathfindEngine;
using Pathfindax.Paths;
Expand All @@ -23,15 +22,15 @@ public class PhysicsParticleFollower : Component, ICmpUpdatable, ICmpInitializab
public PathfindaxCollisionCategory CollisionCategory { get; set; }
public Camera Camera { get; set; }
public IPath Path { get; private set; }
private PathfinderProxy _pathfinderProxy;
private PotentialFieldPathfinderProxy _pathfinderProxy;
private RigidBody _rigidBody;

void ICmpInitializable.OnInit(InitContext context)
{
if (context == InitContext.Activate && DualityApp.ExecContext == DualityApp.ExecutionContext.Game)
{
DualityApp.Mouse.ButtonDown += Mouse_ButtonDown;
_pathfinderProxy = new PathfinderProxy();
_pathfinderProxy = new PotentialFieldPathfinderProxy();
_rigidBody = GameObj.GetComponent<RigidBody>();
}
}
Expand Down Expand Up @@ -59,7 +58,7 @@ private void Mouse_ButtonDown(object sender, MouseButtonEventArgs e)
request.AddCallback(OnRequestCompleted);
}

private void OnRequestCompleted(PathRequest pathRequest)
private void OnRequestCompleted(PathRequest<PotentialField> pathRequest)
{
Path = pathRequest.CompletedPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ namespace Duality.Plugins.Pathfindax.Components
[RequiredComponent(typeof(IDefinitionNodeNetworkProvider<IDefinitionNodeNetwork>))]
public class AstarPathfinderComponent : PathfinderComponentBase<IDefinitionNodeNetwork>
{
private readonly List<AstarNodeNetwork> _astarNodeNetworks = new List<AstarNodeNetwork>();
public IReadOnlyList<AstarNodeNetwork> AstarNodeNetworks => _astarNodeNetworks;

/// <summary>
/// The max calculated clearance. Any clearance value higher than will be set to this.
/// Try to keep this as low as possible to prevent wasting time calculating clearance values that will never be used.
Expand All @@ -37,9 +34,8 @@ public override void OnInit(InitContext context)
if (definitionNodeNetwork is IDefinitionNodeGrid sourceNodeGrid)
nodeGenerators.Add(new GridClearanceGenerator(sourceNodeGrid, MaxClearance));
var astarNodeNetwork = new AstarNodeNetwork(definitionNodeNetwork, nodeGenerators.ToArray());
_astarNodeNetworks.Add(astarNodeNetwork);
return PathfinderFactory.CreateRequestProcesser(astarNodeNetwork, algorithm);
});
}, PathfinderId, AmountOfThreads);
Pathfinder.Start();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Duality.Editor;
using Duality.Editor;
using Pathfindax.Algorithms;
using Pathfindax.Factories;
using Pathfindax.Grid;
Expand All @@ -14,9 +13,6 @@ namespace Duality.Plugins.Pathfindax.Components
[RequiredComponent(typeof(IDefinitionNodeNetworkProvider<DefinitionNodeGrid>))]
public class FlowFieldPathfinderComponent : PathfinderComponentBase<DefinitionNodeGrid>
{
private readonly List<DijkstraNodeGrid> _dijkstraNodeGrids = new List<DijkstraNodeGrid>();
public IReadOnlyList<DijkstraNodeGrid> DijkstraNodeGrids => _dijkstraNodeGrids;

/// <summary>
/// The max calculated clearance. Any clearance value higher than will be set to this.
/// Try to keep this as low as possible to prevent wasting time calculating clearance values that will never be used.
Expand All @@ -34,9 +30,8 @@ public override void OnInit(InitContext context)
Pathfinder = PathfinderFactory.CreatePathfinder(sourceNodeNetwork, new PotentialFieldAlgorithm(100), (definitionNodeGrid, algorithm) =>
{
var dijkstraNodeGrid = new DijkstraNodeGrid(definitionNodeGrid, MaxClearance);
_dijkstraNodeGrids.Add(dijkstraNodeGrid);
return PathfinderFactory.CreateRequestProcesser(dijkstraNodeGrid, algorithm);
});
}, PathfinderId, AmountOfThreads);
Pathfinder.Start();
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Duality.Drawing;
using System.Collections.Generic;
using Duality.Drawing;
using Duality.Editor;
using Pathfindax.Grid;
using Pathfindax.Nodes;
using Pathfindax.PathfindEngine;
using Pathfindax.Utils;

namespace Duality.Plugins.Pathfindax.Components
Expand All @@ -13,6 +15,9 @@ namespace Duality.Plugins.Pathfindax.Components
[RequiredComponent(typeof(FlowFieldPathfinderComponent))]
public class NodeNetworkVisualizer : Component, ICmpRenderer
{
/// <summary>
///
/// </summary>
public int Thread { get; set; }

/// <summary>
Expand Down Expand Up @@ -46,34 +51,39 @@ bool ICmpRenderer.IsVisible(IDrawDevice device)
void ICmpRenderer.Draw(IDrawDevice device)
{
if (!Visualize) return;
var pathfinderComponent = GameObj.GetComponent<FlowFieldPathfinderComponent>();
if (pathfinderComponent?.Pathfinder?.SourceNodeNetwork != null)
var pathfinder = PathfindaxEngine.GetPathfinder();
if (pathfinder?.DefinitionNodeNetwork != null)
{
if (Thread > pathfinderComponent.DijkstraNodeGrids.Count) return;
var network = pathfinderComponent.DijkstraNodeGrids[Thread].GetCollisionLayerNetwork(CollisionCategory);
var canvas = new Canvas(device, new CanvasBuffer());
canvas.State.ZOffset = -8;
for (int i = 0; i < pathfinderComponent.Pathfinder.SourceNodeNetwork.NodeCount; i++)
if (Thread > pathfinder.PathfindNodeNetworks.Count) return;
var pathfindNetwork = pathfinder.PathfindNodeNetworks[Thread];
Draw(device, pathfindNetwork.GetCollisionLayerNetwork(CollisionCategory), pathfinder.DefinitionNodeNetwork);
}
}

private void Draw(IDrawDevice device, IReadOnlyList<ICollisionLayerNode> network, IDefinitionNodeNetwork definitionNodeNetwork)
{
var canvas = new Canvas(device, new CanvasBuffer());
canvas.State.ZOffset = -8;
for (int i = 0; i < network.Count; i++)
{
var node = network[i];
canvas.State.ColorTint = ColorRgba.LightGrey;
var nodePosition = node.DefinitionNode.Position;
canvas.FillCircle(nodePosition.X, nodePosition.Y, NodeSize);
canvas.State.ColorTint = ColorRgba.VeryLightGrey;
if (node.DefinitionNode.Connections != null)
{
var node = network[i];
canvas.State.ColorTint = ColorRgba.LightGrey;
var nodePosition = node.DefinitionNode.Position;
canvas.FillCircle(nodePosition.X, nodePosition.Y, NodeSize);
canvas.State.ColorTint = ColorRgba.VeryLightGrey;
if (node.DefinitionNode.Connections != null)
canvas.State.ColorTint = new ColorRgba(199, 21, 133);
foreach (var connection in node.DefinitionNode.Connections)
{
var toNode = NodePointer.Dereference(connection.To, definitionNodeNetwork);
var vector = (toNode.Position - nodePosition) * 0.5f; //Times 0.5f so we can see the connections in both directions.
canvas.DrawDashLine(nodePosition.X, nodePosition.Y, nodePosition.X + vector.X, nodePosition.Y + vector.Y);
}
if (!float.IsNaN(node.Clearance))
{
canvas.State.ColorTint = new ColorRgba(199, 21, 133);
foreach (var connection in node.DefinitionNode.Connections)
{
var toNode = NodePointer.Dereference(connection.To, pathfinderComponent.Pathfinder.SourceNodeNetwork);
var vector = (toNode.Position - nodePosition) * 0.5f; //Times 0.5f so we can see the connections in both directions.
canvas.DrawDashLine(nodePosition.X, nodePosition.Y, nodePosition.X + vector.X, nodePosition.Y + vector.Y);
}
if (!float.IsNaN(node.Clearance))
{
canvas.State.ColorTint = ColorRgba.Black;
canvas.DrawText(node.Clearance.ToString(), nodePosition.X, nodePosition.Y, -1f, Alignment.Center);
}
canvas.State.ColorTint = ColorRgba.Black;
canvas.DrawText(node.Clearance.ToString(), nodePosition.X, nodePosition.Y, -1f, Alignment.Center);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void ICmpRenderer.Draw(IDrawDevice device)
canvas.State.ZOffset = -8;
switch (pathProvider.Path)
{
case CompletedPath completedPath:
case Path completedPath:
for (var index = 0; index < completedPath.NodePath.Length; index++)
{
if (index == 0) canvas.State.ColorTint = ColorRgba.Green;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
using Pathfindax.Algorithms;
using Pathfindax.Grid;
using Pathfindax.Grid;
using Pathfindax.PathfindEngine;

namespace Duality.Plugins.Pathfindax.Components
{
/// <summary>
/// Base class for duality pathfinders
/// </summary>
public abstract class PathfinderComponentBase<TSourceNodeNetwork> : Component, IPathfinderComponent<TSourceNodeNetwork>, ICmpInitializable
where TSourceNodeNetwork : class, IDefinitionNodeNetwork
public abstract class PathfinderComponentBase<TDefinitionNodeNetwork> : Component, ICmpInitializable
where TDefinitionNodeNetwork : class, IDefinitionNodeNetwork
{
/// <inheritdoc />
public IPathFindAlgorithm PathFindAlgorithm => Pathfinder.PathFindAlgorithm;
/// <inheritdoc />
public IPathfinder<TSourceNodeNetwork> Pathfinder { get; protected set; }
/// <summary>
///
/// </summary>
public IPathfinder Pathfinder { get; protected set; }

/// <inheritdoc />
/// <summary>
///
/// </summary>
public string PathfinderId { get; set; }

/// <summary>
///
/// </summary>
public int AmountOfThreads { get; set; } = 1;

/// <summary>
///
/// </summary>
/// <returns></returns>
protected TSourceNodeNetwork GetSourceNodeNetwork()
protected TDefinitionNodeNetwork GetSourceNodeNetwork()
{
var sourceProvider = GameObj.GetComponent<IDefinitionNodeNetworkProvider<TSourceNodeNetwork>>();
var sourceProvider = GameObj.GetComponent<IDefinitionNodeNetworkProvider<TDefinitionNodeNetwork>>();
if (sourceProvider == null)
{
Log.Game.WriteError($"{GetType()}: Could not find a component that implements {typeof(IDefinitionNodeNetworkProvider<TSourceNodeNetwork>)}.");
Log.Game.WriteError($"{GetType()}: Could not find a component that implements {typeof(IDefinitionNodeNetworkProvider<TDefinitionNodeNetwork>)}.");
return null;
}
var sourceNodeNetwork = sourceProvider.GenerateGrid2D();
if (sourceNodeNetwork == null)
{
Log.Game.WriteError($"{GetType()}: Found a component that implements {typeof(IDefinitionNodeNetworkProvider<TSourceNodeNetwork>)} but it could not generate a nodenetwork.");
Log.Game.WriteError($"{GetType()}: Found a component that implements {typeof(IDefinitionNodeNetworkProvider<TDefinitionNodeNetwork>)} but it could not generate a nodenetwork.");
return null;
}
return sourceNodeNetwork;
Expand All @@ -49,20 +55,5 @@ void ICmpInitializable.OnShutdown(ShutdownContext context)
{
Pathfinder?.Stop();
}

/// <inheritdoc />
public void ProcessPaths()
{
Pathfinder.ProcessCompletedPaths();
}

/// <summary>
/// Requests a new path
/// </summary>
/// <param name="pathRequest"></param>
public void RequestPath(PathRequest pathRequest)
{
Pathfinder.RequestPath(pathRequest);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@
<ItemGroup>
<Compile Include="Components\FlowFieldPathfinderComponent.cs" />
<Compile Include="Components\AstarPathfinderComponent.cs" />
<Compile Include="Components\Interfaces\IPathfinderComponent.cs" />
<Compile Include="Components\Interfaces\IPathProvider.cs" />
<Compile Include="Components\PathVisualizer.cs" />
<Compile Include="Components\PathfinderComponentBase.cs" />
<Compile Include="Components\NodeNetworkVisualizer.cs" />
<Compile Include="PathfindaxDualityCorePlugin.cs" />
<Compile Include="PathfindEngine\PathfinderProxy.cs" />
<Compile Include="PathfindEngine\PathfinderProxyBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 0fa3976

Please sign in to comment.