Начал ИД вроде

This commit is contained in:
ThomasCharester committed 2026-02-06 22:52:36 +03:00
1 parent cd56b1969c
commit f557c58c9d
35 files changed
+683 -1484

No files matched your search

+5 -5
View File
@@ -54,7 +54,7 @@ namespace Code.Example.Race
private void RacersLoadCompleted() =>
_eventBus.RaiseEvent<IRaceGameInitializedHandle>(x => x.RaceGameReady());
public List<Racer.AI.Racer> PlacedRacers { get; } = new();
public List<Racer.Racer> PlacedRacers { get; } = new();
public void PlaceRacers()
{
@@ -62,7 +62,7 @@ namespace Code.Example.Race
{
PlacedRacers.Add(UnityEngine.Object.Instantiate(
_racerPrefabs[_randomizedTypes[i]],
_raceTrackData.trackData.RacerPoints[i].transform).GetComponent<Racer.AI.Racer>());
_raceTrackData.trackData.RacerPoints[i].transform).GetComponent<Racer.Racer>());
PlacedRacers[^1].SetData(_racerPrefabs[_randomizedTypes[i]].Data);
PlacedRacers[^1].Id = i;
_scores.SubscribeRacer(PlacedRacers[^1]);
@@ -74,10 +74,10 @@ namespace Code.Example.Race
}
}
private Dictionary<RacerType, Racer.AI.Racer> _racerPrefabs = new();
private Dictionary<RacerType, Racer.Racer> _racerPrefabs = new();
private Loading _raceGameLoading;
public void RacerLoaded(RacerType type, Racer.AI.Racer racer)
public void RacerLoaded(RacerType type, Racer.Racer racer)
{
if (racer)
_racerPrefabs.Add(type, racer);
@@ -86,7 +86,7 @@ namespace Code.Example.Race
}
// private Dictionary<RacerType, Racer.AI.Racer> _spawnedRacers = new();
public void RacerCreated(RacerType type, Racer.AI.Racer racer)
public void RacerCreated(RacerType type, Racer.Racer racer)
{
// _spawnedRacers.Add(type, racer);
// _raceGameLoading.DoProgress();
+7 -7
View File
@@ -8,7 +8,7 @@ namespace Code.Example.Race
{
public class RaceScores : IRacerPassedCheckpointHandler, IDisposable
{
private Dictionary<Racer.AI.Racer, int> _scores = new();
private Dictionary<Racer.Racer, int> _scores = new();
private EventBus _eventBus;
public RaceScores(EventBus eventBus)
@@ -16,28 +16,28 @@ namespace Code.Example.Race
_eventBus = eventBus;
_eventBus.Subscribe(this);
}
public void SubscribeRacer(Racer.AI.Racer racer)
public void SubscribeRacer(Racer.Racer racer)
{
_scores.Add(racer, 0);
}
public void ChangeScore(Racer.AI.Racer racer, int additionalScore)
public void ChangeScore(Racer.Racer racer, int additionalScore)
{
_scores[racer] += additionalScore;
_eventBus.RaiseEvent<IScoreChangedHandle>(x=> x.ScoreChanged(racer,additionalScore, GetScores()));
}
public void UnsubscribeRacer(Racer.AI.Racer racer)
public void UnsubscribeRacer(Racer.Racer racer)
{
_scores.Remove(racer);
}
public List<KeyValuePair<Racer.AI.Racer, int>> GetScores()
public List<KeyValuePair<Racer.Racer, int>> GetScores()
{
return _scores.OrderByDescending(x => x.Value).ToList();
}
public void OnRacerPassedCheckpoint(Racer.AI.Racer racer) => ChangeScore(racer,1);
public void OnRacerPassedCheckpoint(Racer.Racer racer) => ChangeScore(racer,1);
public void Dispose()
{
@@ -47,6 +47,6 @@ namespace Code.Example.Race
public interface IScoreChangedHandle : IGlobalSubscriber
{
void ScoreChanged(Racer.AI.Racer racer, int additionalScore, List<KeyValuePair<Racer.AI.Racer, int>> scores);
void ScoreChanged(Racer.Racer racer, int additionalScore, List<KeyValuePair<Racer.Racer, int>> scores);
}
}
@@ -0,0 +1,98 @@
using System.Collections.Generic;
using Code.Example.Race.Racer.AI.ID;
using UnityEngine;
namespace Code.Example.Race.Racer.AI
{
public class AIRacerID : Racer
{
public override void SetData(RacerData data)
{
Data = data;
_velocity = Vector3.zero;
_steering = new Steering();
}
public override void Initialize(List<Transform> waypoints)
{
Waypoints = waypoints;
}
public override void NextWaypoint()
{
if(CurrentWaypointIndex >= Waypoints.Count) CurrentWaypointIndex = 0;
CurrentWaypointIndex++;
}
public override void StartRacer()
{
_running = true;
}
public override void StopRacer()
{
_running = false;
}
[SerializeField] private AgentBehaviour agent;
private float _orientation;
private float _rotation;
private Vector3 _velocity;
private Steering _steering;
private bool _running;
private float NormalizeAngle(float angle)
{
switch (angle)
{
case > 360.0f:
{
while (angle > 360.0f) angle -= 360.0f;
break;
}
case < 0.0f:
{
while (angle < 0.0f) angle += 360.0f;
break;
}
}
return angle;
}
public virtual void Update()
{
if (!_running) return;
var displacement = _velocity * Time.deltaTime;
_orientation += _rotation * Time.deltaTime;
_orientation = NormalizeAngle(_orientation);
transform.Translate(displacement, Space.World);
transform.Rotate(Vector3.up, _orientation);
}
public virtual void LateUpdate()
{
if (!_running) return;
_steering = agent.GetSteering(Waypoints[CurrentWaypointIndex]);
_velocity += _steering.linear * Time.deltaTime;
_rotation += _steering.angular * Time.deltaTime;
if (_velocity.magnitude > Data.speed)
{
_velocity = _velocity.normalized * Data.speed;
}
if (_steering.angular == 0.0f)
{
_rotation = 0.0f;
}
if (_steering.linear.sqrMagnitude == 0.0f)
{
_velocity = Vector3.zero;
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fdcd01ab3c5147e4bfa3619d60fec5fc
timeCreated: 1770394482
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4f9bd0b568944e1f9b11f60f0fb79590
timeCreated: 1770394574
@@ -0,0 +1,58 @@
using UnityEngine;
namespace Code.Example.Race.Racer.AI.ID
{
public class Agent : MonoBehaviour
{
public float maxSpeed;
public float maxAccel;
public float orientation;
public float rotation;
public Vector3 velocity;
protected Steering steering;
private void Start()
{
velocity = Vector3.zero;
steering = new Steering();
}
public void SetSteering(Steering steering)
{
this.steering = steering;
}
public virtual void Update ()
{
Vector3 displacement = velocity * Time.deltaTime;
orientation += rotation * Time.deltaTime;
// необходимо ограничить значение переменной orientation
// диапазоном (0-360)
if (orientation < 0.0f)
orientation += 360.0f;
else if (orientation > 360.0f)
orientation -= 360.0f;
transform.Translate(displacement, Space.World);
transform.rotation = new Quaternion();
transform.Rotate(Vector3.up, orientation);
}
public virtual void LateUpdate()
{
velocity += steering.linear * Time.deltaTime;
rotation += steering.angular * Time.deltaTime;
if (velocity.magnitude > maxSpeed)
{
velocity = velocity.normalized * maxSpeed; // Исправлено: корректное нормирование вектора
}
if (steering.angular == 0.0f)
{
rotation = 0.0f;
}
if (steering.linear.sqrMagnitude == 0.0f)
{
velocity = Vector3.zero;
}
// Удалена строка: steering = new Steering();
// (создание нового объекта каждый кадр приведет к утечке памяти и сбросу данных)
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6d51ddde70f34264954a75923b43dcee
timeCreated: 1770394764
@@ -0,0 +1,11 @@
using UnityEngine;
namespace Code.Example.Race.Racer.AI.ID
{
public abstract class AgentBehaviour : MonoBehaviour
{
[SerializeField] protected AIRacerID agent;
public abstract Steering GetSteering(Transform target);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5af0fc1b61bd44d9a53fd01f93fe6029
timeCreated: 1770394682
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 70ca406222fb4e8e848ccf43db320cc9
timeCreated: 1770395746
@@ -0,0 +1,17 @@
using UnityEngine;
namespace Code.Example.Race.Racer.AI.ID.Behaviours
{
public class Flee : AgentBehaviour
{
public override Steering GetSteering(Transform target)
{
Steering steering = new Steering();
steering.linear = transform.position - target.position;
steering.linear.Normalize();
steering.linear *= agent.Data.maxAcceleration;
return steering;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 437d2ae2cd65457a82abc34df278228d
timeCreated: 1770395798
@@ -0,0 +1,17 @@
using UnityEngine;
namespace Code.Example.Race.Racer.AI.ID.Behaviours
{
public class Seek : AgentBehaviour
{
public override Steering GetSteering(Transform target)
{
Steering steering = new Steering();
steering.linear = target.position - transform.position;
steering.linear.Normalize();
steering.linear *= agent.Data.maxAcceleration;
return steering;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa69eb79c9ec48b682606dac0c0d4f2d
timeCreated: 1770395751
@@ -0,0 +1,16 @@
using UnityEngine;
namespace Code.Example.Race.Racer.AI.ID
{
public struct Steering
{
public float angular;
public Vector3 linear;
public Steering(float angular, Vector3 linear)
{
this.angular = angular;
this.linear = linear;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0395b595869e49b5b177fe54db531823
timeCreated: 1770394582
@@ -31,19 +31,10 @@ namespace Code.Example.Race.Racer.Player
public ControlMode control;
// public float maxAcceleration = 30.0f;
// public float brakeAcceleration = 50.0f;
//
// public float turnSensitivity = 1.0f;
// public float maxSteerAngle = 30.0f;
public Vector3 _centerOfMass;
public List<Wheel> wheels;
// float moveInput;
// float steerInput;
private Rigidbody carRb;
[SerializeField] private PlayerCarInput _playerCarInput;
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using UnityEngine;
namespace Code.Example.Race.Racer.AI
namespace Code.Example.Race.Racer.Player
{
public class PlayerRacer : Racer
{
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using UnityEngine;
namespace Code.Example.Race.Racer.AI
namespace Code.Example.Race.Racer
{
public abstract class Racer : MonoBehaviour
{
@@ -62,7 +62,7 @@ namespace Code.Example.Race.Racer
Addressables.LoadAssetAsync<GameObject>(_racerConfigs[racerType].prefabReference));
await _racerPrefabsHandle[racerType].ToUniTask();
var racer = _racerPrefabsHandle[racerType].Result.GetComponent<AI.Racer>();
var racer = _racerPrefabsHandle[racerType].Result.GetComponent<Racer>();
racer.SetData(_racerConfigs[racerType].racerData);
_eventBus.RaiseEvent<IRacerLoadedHandle>(x => x.RacerLoaded(racerType, racer));
@@ -90,7 +90,7 @@ namespace Code.Example.Race.Racer
public interface IRacerLoadedHandle : IGlobalSubscriber
{
public void RacerLoaded(RacerType type, AI.Racer racer);
public void RacerCreated(RacerType type, AI.Racer racer);
public void RacerLoaded(RacerType type, Racer racer);
public void RacerCreated(RacerType type, Racer racer);
}
}
+2 -2
View File
@@ -10,7 +10,7 @@ namespace Code.Example.Race.Track
[Inject] private EventBus _eventBus;
private void OnTriggerEnter(Collider other)
{
var racer = other.GetComponent<Racer.AI.Racer>();
var racer = other.GetComponent<Racer.Racer>();
racer.NextWaypoint();
_eventBus.RaiseEvent<IRacerPassedCheckpointHandler>(h => h.OnRacerPassedCheckpoint(racer));
@@ -19,6 +19,6 @@ namespace Code.Example.Race.Track
public interface IRacerPassedCheckpointHandler : IGlobalSubscriber
{
void OnRacerPassedCheckpoint(Racer.AI.Racer racer);
void OnRacerPassedCheckpoint(Racer.Racer racer);
}
}
@@ -34,11 +34,11 @@ namespace Code.Example.Race.UI
// image.DOColor(hiddenColor, 1f).onComplete += () => callback?.Invoke();
}
public void OnRacerPassedCheckpoint(Racer.AI.Racer racer)
public void OnRacerPassedCheckpoint(Racer.Racer racer)
{
}
public void ScoreChanged(Racer.AI.Racer racer, int additionalScore, List<KeyValuePair<Racer.AI.Racer,int>> scores)
public void ScoreChanged(Racer.Racer racer, int additionalScore, List<KeyValuePair<Racer.Racer,int>> scores)
{
// if (racer.Data.type == RacerType.Player)
string output = "";