This commit is contained in:
MrTorex committed 2026-02-17 17:28:32 +03:00
1 parent fd3a472348
commit 8302032c9f
10 files changed
+2191 -2

No files matched your search

+7 -1
View File
@@ -19,7 +19,13 @@ namespace Code.Base
Steering = new Steering();
}
public void SetSteering(Steering steering) => Steering = steering;
public void SetSteering(Steering steering) => SetSteering(steering, 1.0f);
public void SetSteering(Steering steering, float weight)
{
Steering.Linear += weight * steering.Linear;
Steering.Angular += weight * steering.Angular;
}
public virtual void Update()
{
+2 -1
View File
@@ -4,6 +4,7 @@ namespace Code.Base
{
public class AgentBehaviour : MonoBehaviour
{
public float weight = 1.0f;
public GameObject target;
protected Agent Agent;
@@ -14,7 +15,7 @@ namespace Code.Base
public virtual void Update()
{
Agent.SetSteering(GetSteering());
Agent.SetSteering(GetSteering(), weight);
}
public virtual Steering GetSteering()
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 98ee692bd20e47c196523165094ad7d1
timeCreated: 1771337764
+220
View File
@@ -0,0 +1,220 @@
using Code.Base;
using Code.Lab1;
using System.Collections.Generic;
using System.Linq;
using Code.Lab3;
using UnityEngine;
namespace Code.Lab4
{
public class BlendedPathFollower : Seek
{
public Path path;
public float pathOffset = 1.0f;
[Range(0, 1)]
public float pathFollowingWeight = 0.7f;
[Range(0, 1)]
public float optimalPathWeight = 0.3f;
private float _currentParam;
private List<Vector3> _waypoints;
private int _currentTargetIndex = -1;
private List<int> _visitedIndices;
private bool _reachedLastPoint;
public override void Awake()
{
base.Awake();
target = new GameObject("BlendedPathFollower_Target");
_visitedIndices = new List<int>();
InitializeWaypoints();
}
private void InitializeWaypoints()
{
if (path == null || path.nodes == null || path.nodes.Count == 0) return;
_waypoints = new List<Vector3>();
foreach (var node in path.nodes.Where(node => node != null))
{
_waypoints.Add(node.transform.position);
}
FindClosestStartPoint();
}
private void FindClosestStartPoint()
{
var minDistance = float.MaxValue;
var closestIndex = 0;
for (var i = 0; i < _waypoints.Count; i++)
{
var distance = Vector3.Distance(transform.position, _waypoints[i]);
if (distance < minDistance)
{
minDistance = distance;
closestIndex = i;
}
}
_currentTargetIndex = closestIndex;
_currentParam = 0;
_visitedIndices.Clear();
_reachedLastPoint = false;
}
public override Steering GetSteering()
{
if (_waypoints == null || _waypoints.Count == 0 || _reachedLastPoint)
{
return new Steering();
}
var pathSteering = GetPathFollowingSteering();
var optimalSteering = GetOptimalPathSteering();
var blendedSteering = new Steering
{
Linear = (pathFollowingWeight * pathSteering.Linear) + (optimalPathWeight * optimalSteering.Linear),
Angular = (pathFollowingWeight * pathSteering.Angular) + (optimalPathWeight * optimalSteering.Angular)
};
if (blendedSteering.Linear.magnitude > Agent.maxAcceleration)
{
blendedSteering.Linear = blendedSteering.Linear.normalized * Agent.maxAcceleration;
}
return blendedSteering;
}
private Steering GetPathFollowingSteering()
{
if (!path) return new Steering();
_currentParam = path.GetParam(transform.position, _currentParam);
var targetParam = _currentParam + pathOffset;
target.transform.position = path.GetPosition(targetParam);
return base.GetSteering();
}
private Steering GetOptimalPathSteering()
{
if (_waypoints == null || _waypoints.Count == 0) return new Steering();
var distanceToTarget = Vector3.Distance(transform.position, _waypoints[_currentTargetIndex]);
if (distanceToTarget < 1f)
{
if (!_visitedIndices.Contains(_currentTargetIndex))
{
_visitedIndices.Add(_currentTargetIndex);
}
if (_visitedIndices.Count >= _waypoints.Count)
{
_reachedLastPoint = true;
return new Steering();
}
FindNextTarget();
}
var tempTarget = target.transform.position;
target.transform.position = _waypoints[_currentTargetIndex];
var steering = base.GetSteering();
target.transform.position = tempTarget;
return steering;
}
private void FindNextTarget()
{
var shortestDistance = float.MaxValue;
var bestTargetIndex = -1;
for (var i = 0; i < _waypoints.Count; i++)
{
if (_visitedIndices.Contains(i))
continue;
var distance = Vector3.Distance(transform.position, _waypoints[i]);
if (distance < shortestDistance)
{
shortestDistance = distance;
bestTargetIndex = i;
}
}
if (bestTargetIndex != -1)
{
_currentTargetIndex = bestTargetIndex;
}
else
{
_reachedLastPoint = true;
}
}
public void ResetPathFollowing()
{
_visitedIndices.Clear();
FindClosestStartPoint();
_reachedLastPoint = false;
}
public void SetWeights(float pathWeight, float optimalWeight)
{
pathFollowingWeight = Mathf.Clamp01(pathWeight);
optimalPathWeight = Mathf.Clamp01(optimalWeight);
float total = pathFollowingWeight + optimalPathWeight;
if (total > 1f)
{
pathFollowingWeight /= total;
optimalPathWeight /= total;
}
}
private void OnDrawGizmosSelected()
{
if (_waypoints == null) return;
for (var i = 0; i < _waypoints.Count; i++)
{
if (_reachedLastPoint)
{
Gizmos.color = Color.gray;
}
else if (i == _currentTargetIndex)
{
Gizmos.color = Color.green;
}
else if (_visitedIndices != null && _visitedIndices.Contains(i))
{
Gizmos.color = Color.blue;
}
else
{
Gizmos.color = Color.yellow;
}
Gizmos.DrawSphere(_waypoints[i], 0.3f);
}
if (path != null)
{
Gizmos.color = Color.magenta;
var segments = path.GetSegments();
foreach (var segment in segments)
{
Gizmos.DrawLine(segment.A, segment.B);
}
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cb64189ea5454c1eaa62be8a1d5c94dc
timeCreated: 1771337777
+29
View File
@@ -0,0 +1,29 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace Code.UI
{
public class Lab4Controller : MonoBehaviour
{
[SerializeField] private Button toMenuButton;
[SerializeField] private Button resetButton;
private void Awake()
{
if (toMenuButton != null)
toMenuButton.onClick.AddListener(LoadMainMenu);
if (resetButton != null)
resetButton.onClick.AddListener(Reload);
}
private static void LoadMainMenu() => SceneManager.LoadScene("MainMenu");
private static void Reload() => SceneManager.LoadScene("Lab4");
private void OnDestroy()
{
if (toMenuButton != null) toMenuButton.onClick.RemoveListener(LoadMainMenu);
if (resetButton != null) resetButton.onClick.RemoveListener(Reload);
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e201593e802a43c8b05f1464d9323b1e
timeCreated: 1771338422
File diff suppressed because it is too large. Load diff
+7
View File
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ee8e25040bf8dca46a66cc5bde646eb9
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -23,6 +23,9 @@ EditorBuildSettings:
- enabled: 1
path: Assets/Resources/Scenes/Lab3Dop.unity
guid: 730c0cda26d6600288a3972891f96dc1
- enabled: 1
path: Assets/Resources/Scenes/Lab4.unity
guid: ee8e25040bf8dca46a66cc5bde646eb9
m_configObjects:
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b, type: 3}
m_UseUCBPForAssetBundles: 0