diff --git a/WorkingTitle/Assets/WorkingTitle.Unity/Map/PathfindingComponent.cs b/WorkingTitle/Assets/WorkingTitle.Unity/Map/PathfindingComponent.cs index 9d9a235..ccc9e1a 100644 --- a/WorkingTitle/Assets/WorkingTitle.Unity/Map/PathfindingComponent.cs +++ b/WorkingTitle/Assets/WorkingTitle.Unity/Map/PathfindingComponent.cs @@ -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; @@ -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(); @@ -31,12 +38,48 @@ void Start() { PlayerEntityComponent = GetComponentInChildren() - .GetComponent(); + .GetComponent(); 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) @@ -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; @@ -61,8 +104,7 @@ public PathfindingCell GetCell(Vector2Int position) void OnPlayerCellPositionChanged(object sender, Vector2Int position) { - UpdateTarget(); - UpdateDirections(); + HasTargetPositionChanged = true; } void UpdateTarget() @@ -73,42 +115,15 @@ void UpdateTarget() TargetPositiveCellPosition = PlayerEntityComponent.PositiveCellPosition; } - void UpdateDirections() + FlowField CalcFlowField(List 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 } } \ No newline at end of file diff --git a/WorkingTitle/ProjectSettings/PackageManagerSettings.asset b/WorkingTitle/ProjectSettings/PackageManagerSettings.asset index 785714b..b3fa0e3 100644 --- a/WorkingTitle/ProjectSettings/PackageManagerSettings.asset +++ b/WorkingTitle/ProjectSettings/PackageManagerSettings.asset @@ -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: @@ -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