Начал ИД вроде
This commit is contained in:
1 parent
cd56b1969c
commit
f557c58c9d
35 files changed
+683
-1484
No files matched your search
@@ -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
-1
@@ -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
|
||||
{
|
||||
File renamed without changes.
+1
-1
@@ -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
|
||||
{
|
||||
File renamed without changes.
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user