Skip to content

Commit

Permalink
feat(pathfinding): calculate flow field on separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
EuleMitKeule committed Nov 8, 2022
1 parent bab0aba commit 278ef7a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
97 changes: 56 additions & 41 deletions WorkingTitle/Assets/WorkingTitle.Unity/Map/PathfindingComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Sirenix.OdinInspector;
using UnityEngine;
using WorkingTitle.Lib.Pathfinding;
Expand All @@ -21,7 +24,11 @@ public class PathfindingComponent : SerializedMonoBehaviour

MapComponent MapComponent { get; set; }
public EntityComponent PlayerEntityComponent { get; private set; }


bool HasTargetPositionChanged { get; set; }

Coroutine UpdateFlowFieldCoroutine { get; set; }

void Awake()
{
MapComponent = GetComponent<MapComponent>();
Expand All @@ -31,12 +38,48 @@ void Start()
{
PlayerEntityComponent =
GetComponentInChildren<PlayerComponent>()
.GetComponent<EntityComponent>();
.GetComponent<EntityComponent>();

PlayerEntityComponent.CellPositionChanged += OnPlayerCellPositionChanged;

var obstaclePositions = MapComponent
.ObstacleTilemaps
.GetTilePositions()
.ToPositive(MapComponent.Bounds)
.ToList();

UpdateTarget();
UpdateDirections();
FlowField = CalcFlowField(obstaclePositions);

UpdateFlowFieldCoroutine = StartCoroutine(UpdateFlowField());
}

IEnumerator UpdateFlowField()
{
while (true)
{
if (HasTargetPositionChanged)
{
var obstaclePositions = MapComponent
.ObstacleTilemaps
.GetTilePositions()
.ToPositive(MapComponent.Bounds)
.ToList();

var thread = new Thread(() =>
{
UpdateTarget();
var flowField = CalcFlowField(obstaclePositions);
FlowField = flowField;
});

thread.Start();

HasTargetPositionChanged = false;
}

yield return new WaitForSeconds(0.1f);
}
}

public PathfindingCell GetCell(Vector2Int position)
Expand All @@ -48,9 +91,9 @@ public PathfindingCell GetCell(Vector2Int position)
}

if (FlowField.GridSize.x <= position.x ||
FlowField.GridSize.y <= position.y ||
position.x < 0 ||
position.y < 0)
FlowField.GridSize.y <= position.y ||
position.x < 0 ||
position.y < 0)
{

return null;
Expand All @@ -61,8 +104,7 @@ public PathfindingCell GetCell(Vector2Int position)

void OnPlayerCellPositionChanged(object sender, Vector2Int position)
{
UpdateTarget();
UpdateDirections();
HasTargetPositionChanged = true;
}

void UpdateTarget()
Expand All @@ -73,42 +115,15 @@ void UpdateTarget()
TargetPositiveCellPosition = PlayerEntityComponent.PositiveCellPosition;
}

void UpdateDirections()
FlowField CalcFlowField(List<Vector2Int> obstaclePositions)
{
if (!PlayerEntityComponent) return;
if (!PlayerEntityComponent) return null;

var obstaclePositions = MapComponent
.ObstacleTilemaps
.GetTilePositions()
.ToPositive(MapComponent.Bounds)
.ToList();
var flowField = new FlowField(TargetPositiveCellPosition, obstaclePositions, MapComponent.GridSize);
flowField.CalcCosts();
flowField.CalcDirections();

FlowField = new FlowField(TargetPositiveCellPosition, obstaclePositions, MapComponent.GridSize);
FlowField.CalcCosts();
FlowField.CalcDirections();
return flowField;
}

#if UNITY_EDITOR
// void OnDrawGizmos()
// {
// if (!FlowField?.IsDirectionsCalculated ?? true) return;
//
// var bounds = MapComponent.Bounds;
// var walkablePositions = MapComponent
// .WalkableTilemaps
// .GetTilePositions()
// .ToList();
//
// foreach (var walkablePosition in walkablePositions)
// {
// var positivePosition = walkablePosition.ToPositive(bounds);
// var direction = FlowField.Cells[positivePosition.x][positivePosition.y].Direction;
// var worldPosition = MapComponent.ToWorld(walkablePosition);
//
// Gizmos.color = Color.red;
// Gizmos.DrawRay(worldPosition, direction * 0.3f);
// }
// }
#endif
}
}
8 changes: 4 additions & 4 deletions WorkingTitle/ProjectSettings/PackageManagerSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ MonoBehaviour:
m_Script: {fileID: 13964, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_EditorClassIdentifier:
m_EnablePreReleasePackages: 0
m_EnablePreReleasePackages: 1
m_EnablePackageDependencies: 0
m_AdvancedSettingsExpanded: 1
m_ScopedRegistriesSettingsExpanded: 1
m_SeeAllPackageVersions: 0
oneTimeWarningShown: 0
oneTimeWarningShown: 1
m_Registries:
- m_Id: main
m_Name:
Expand All @@ -30,6 +30,6 @@ MonoBehaviour:
m_RegistryInfoDraft:
m_Modified: 0
m_ErrorMessage:
m_UserModificationsInstanceId: -834
m_OriginalInstanceId: -836
m_UserModificationsInstanceId: -826
m_OriginalInstanceId: -828
m_LoadAssets: 0

0 comments on commit 278ef7a

Please sign in to comment.