Развитие гоночек

This commit is contained in:
ThomasCharester committed 2026-02-06 10:30:50 +03:00
1 parent ec60c47652
commit b54083521f
532 files changed
+16091 -28231

No files matched your search

@@ -4,7 +4,7 @@ using UnityEngine.AI;
namespace Code.Example.Race.Racer.AI
{
public class AIDebugRacer : MonoBehaviour
public class AIDebugRacer : Racer
{
[SerializeField] private NavMeshAgent navMeshAgent;
[SerializeField] private List<Transform> waypoints;
@@ -15,11 +15,31 @@ namespace Code.Example.Race.Racer.AI
_currentWaypointIndex++;
}
public void NextWaypoint()
public override void NextWaypoint()
{
if(_currentWaypointIndex >= waypoints.Count) _currentWaypointIndex = 0;
navMeshAgent.destination = waypoints[_currentWaypointIndex++].position;
}
public override void StartRacer()
{
}
public override void StopRacer()
{
}
public override void SetData(RacerData data)
{
}
public override void Initialize(List<Transform> waypoints)
{
}
}
}
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace Code.Example.Race.Racer.AI
{
public class AIRacer : Racer
{
[SerializeField] private NavMeshAgent navMeshAgent;
public override void SetData(RacerData data)
{
Data = data;
navMeshAgent.angularSpeed = data.angularSpeed;
navMeshAgent.speed = data.speed;
}
public override void Initialize(List<Transform> waypoints)
{
navMeshAgent.isStopped = true;
Waypoints = waypoints;
navMeshAgent.destination = Waypoints[CurrentWaypointIndex].position;
CurrentWaypointIndex++;
}
public override void StartRacer() => navMeshAgent.isStopped = false;
public override void StopRacer() => navMeshAgent.isStopped = true;
public override void NextWaypoint()
{
if(CurrentWaypointIndex >= Waypoints.Count) CurrentWaypointIndex = 0;
navMeshAgent.destination = Waypoints[CurrentWaypointIndex++].position;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 05f30660967e40209aa88719fa498c78
timeCreated: 1769694590
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using UnityEngine;
namespace Code.Example.Race.Racer.AI
{
public class PlayerRacer : Racer
{
public override void SetData(RacerData data)
{
Data = data;
}
public override void Initialize(List<Transform> waypoints)
{
}
public override void NextWaypoint()
{
}
public override void StartRacer()
{
}
public override void StopRacer()
{
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6d3497d41c87415eab67cfc865262b79
timeCreated: 1769619757
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using UnityEngine;
namespace Code.Example.Race.Racer.AI
{
public abstract class Racer : MonoBehaviour
{
protected List<Transform> Waypoints;
protected int CurrentWaypointIndex;
public RacerData Data { get; protected set; }
public int Id { get; set; }
public abstract void SetData(RacerData data);
public abstract void Initialize(List<Transform> waypoints);
public abstract void NextWaypoint();
public abstract void StartRacer();
public abstract void StopRacer();
[SerializeField] protected Rigidbody carRigidbody;
public Rigidbody CarRigidbody => GetComponent<Rigidbody>();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7f87ed38713d4b779df9e46c83b03fb7
timeCreated: 1769619306
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2c5dfd92289c4aa38bb9a8bafc07d204
timeCreated: 1769699718
@@ -0,0 +1,32 @@
using UnityEngine;
using UnityEngine.AI;
namespace Code.Example.Race.Racer.Car
{
public class CarWheelRotator : MonoBehaviour
{
[SerializeField] private NavMeshAgent agent;
[SerializeField] private Transform[] wheels;
[SerializeField] private bool rotateDebug;
private void Update()
{
Vector3 wheelRotation;
foreach (var wheel in wheels)
{
if (rotateDebug)
{
UnityEngine.Debug.DrawRay(
wheel.position,
Vector3.Project(agent.velocity, agent.gameObject.transform.right),
Color.yellow, 900f);
UnityEngine.Debug.Log(Vector3.Project(agent.velocity, agent.gameObject.transform.right).magnitude);
}
wheelRotation = wheel.localRotation.eulerAngles;
wheelRotation.y = Vector3.Project(agent.velocity, agent.gameObject.transform.right).magnitude * 10f;
wheel.localRotation = Quaternion.Euler(wheelRotation);
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3634956b4d55431b8772a8612e2e3a4d
timeCreated: 1769699734
@@ -0,0 +1,23 @@
using DG.Tweening;
using UnityEngine;
namespace Code.Example.Race.Racer.Car
{
public class CarWheelRotatorPlayer : MonoBehaviour
{
[SerializeField] private Transform[] wheels;
[SerializeField] private bool rotateDebug;
public void RotateWheels(float amount)
{
// UnityEngine.Debug.Log("Rotating Wheels: " + amount);
foreach (var wheel in wheels)
{
var wheelRotation = wheel.localRotation.eulerAngles;
wheelRotation.y = amount;
wheel.DOLocalRotate(wheelRotation, 1f);
// wheel.localRotation = Quaternion.Euler(wheelRotation);
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c90fde90552d48f592b955cbf1ec5c77
timeCreated: 1769705011
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7d9a0a4882314e2f91bb356ea0f82538
timeCreated: 1769705255
@@ -0,0 +1,55 @@
using Code.Example.Race.Racer.AI;
using Code.Example.Race.Racer.Car;
using UnityEngine;
namespace Code.Example.Race.Racer.Player
{
public class PlayerCarController : MonoBehaviour
{
[SerializeField] private Rigidbody _rigidbody;
[SerializeField] private PlayerCarInput _playerCarInput;
[SerializeField] private PlayerRacer _playerRacer;
[SerializeField] private CarWheelRotatorPlayer _wheels;
private void Update()
{
if (_playerCarInput.MovementInput == Vector2.zero) return;
// var movementForward = transform.forward * (_playerCarInput.MovementInput.y * _playerRacer.Data.speed);
//
// _rigidbody.AddForce(movementForward * Time.fixedDeltaTime, ForceMode.Force);
var force = transform.forward * (_playerCarInput.MovementInput.y * _playerRacer.Data.speed);
_rigidbody.AddForce(force, ForceMode.Force);
// if (Mathf.Abs(_playerCarInput.MovementInput.y) < 0.1f && _rigidbody.linearVelocity.magnitude > 0.3f)
// {
// _rigidbody.AddForce(-_rigidbody.linearVelocity * (_playerRacer.Data.brakeForce * Time.fixedDeltaTime),
// ForceMode.Force);
// }
var steerInput = _playerCarInput.MovementInput.x;
if (_playerCarInput.MovementInput.y < 0)
steerInput = -_playerCarInput.MovementInput.x;
// var steer = steerInput * maxSteerAngle;
//
// if (frontLeftWheel) frontLeftWheel.localRotation = Quaternion.Euler(0, steer, 0);
// if (frontRightWheel) frontRightWheel.localRotation = Quaternion.Euler(0, steer, 0);
if (!(_rigidbody.linearVelocity.magnitude > 0.5f)) return;
// var turn = steerInput * _playerRacer.Data.turnSpeed * Time.fixedDeltaTime;
// _rigidbody.MoveRotation(_rigidbody.rotation * Quaternion.Euler(0, turn, 0));
if (!(_rigidbody.linearVelocity.magnitude is > 1f or < -1f)) return;
var rotateAmount = _playerCarInput.MovementInput.x * _playerRacer.Data.angularSpeed;
_wheels.RotateWheels(rotateAmount * 10 * Time.fixedDeltaTime);
var wheelRotation = transform.rotation.eulerAngles;
wheelRotation.y += rotateAmount;
transform.rotation = Quaternion.Euler(wheelRotation);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4b9e2e4a3ddc437ab250f609ea7fc33e
timeCreated: 1769705285
@@ -0,0 +1,166 @@
using UnityEngine;
using System;
using System.Collections.Generic;
namespace Code.Example.Race.Racer.Player
{
public class PlayerCarControllerRB : MonoBehaviour
{
public enum ControlMode
{
Keyboard,
Buttons
};
public enum Axel
{
Front,
Rear
}
[Serializable]
public struct Wheel
{
public GameObject wheelModel;
public WheelCollider wheelCollider;
public GameObject wheelEffectObj;
public ParticleSystem smokeParticle;
public Axel axel;
}
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;
// private CarLights carLights;
void Start()
{
carRb = GetComponent<Rigidbody>();
carRb.centerOfMass = _centerOfMass;
carLights = GetComponent<CarLights>();
}
void Update()
{
GetInputs();
AnimateWheels();
WheelEffects();
}
void LateUpdate()
{
Move();
Steer();
Brake();
}
public void MoveInput(float input)
{
moveInput = input;
}
public void SteerInput(float input)
{
steerInput = input;
}
void GetInputs()
{
if (control == ControlMode.Keyboard)
{
moveInput = Input.GetAxis("Vertical");
steerInput = Input.GetAxis("Horizontal");
}
}
void Move()
{
foreach (var wheel in wheels)
{
wheel.wheelCollider.motorTorque = moveInput * 600 * maxAcceleration * Time.deltaTime;
}
}
void Steer()
{
foreach (var wheel in wheels)
{
if (wheel.axel == Axel.Front)
{
var _steerAngle = steerInput * turnSensitivity * maxSteerAngle;
wheel.wheelCollider.steerAngle = Mathf.Lerp(wheel.wheelCollider.steerAngle, _steerAngle, 0.6f);
}
}
}
void Brake()
{
if (Input.GetKey(KeyCode.Space) || moveInput == 0)
{
foreach (var wheel in wheels)
{
wheel.wheelCollider.brakeTorque = 300 * brakeAcceleration * Time.deltaTime;
}
carLights.isBackLightOn = true;
carLights.OperateBackLights();
}
else
{
foreach (var wheel in wheels)
{
wheel.wheelCollider.brakeTorque = 0;
}
carLights.isBackLightOn = false;
carLights.OperateBackLights();
}
}
void AnimateWheels()
{
foreach (var wheel in wheels)
{
Quaternion rot;
Vector3 pos;
wheel.wheelCollider.GetWorldPose(out pos, out rot);
wheel.wheelModel.transform.position = pos;
wheel.wheelModel.transform.rotation = rot;
}
}
void WheelEffects()
{
foreach (var wheel in wheels)
{
//var dirtParticleMainSettings = wheel.smokeParticle.main;
if (Input.GetKey(KeyCode.Space) && wheel.axel == Axel.Rear && wheel.wheelCollider.isGrounded == true &&
carRb.linearVelocity.magnitude >= 10.0f)
{
wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = true;
wheel.smokeParticle.Emit(1);
}
else
{
wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = false;
}
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5f093c913ff54cb99cd22d7e473931e7
timeCreated: 1770299509
@@ -0,0 +1,32 @@
using Code.Infrastructure.Input;
using Reflex.Attributes;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Code.Example.Race.Racer.Player
{
public class PlayerCarInput : MonoBehaviour
{
private IInputService _inputService;
public Vector2 MovementInput { get; private set; }
[Inject]
public void Construct(IInputService inputService)
{
_inputService = inputService;
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
_inputService.BindActionCancel((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
}
private void MovementPerformed(InputAction.CallbackContext context) => MovementInput = context.ReadValue<Vector2>();
private void MovementEnded(InputAction.CallbackContext context) => MovementInput = Vector2.zero;
private void OnDestroy()
{
if (_inputService == null) return;
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
_inputService.UnbindActionCanceled((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e52315b65c57499981981e2507aaf7bc
timeCreated: 1769705872
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Code.Infrastructure.Configs;
using Code.Infrastructure.EventBus;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using EventBus = Code.Infrastructure.EventBus.EventBus;
namespace Code.Example.Race.Racer
{
public interface IRacerFactory
{
RacerSettingsConfig GetConfig(RacerType racerType);
UniTaskVoid LoadRacer(RacerType racerType);
void ReleaseRacer(RacerType racerType);
}
public class RacerFactory : IDisposable, IRacerFactory
{
private Dictionary<RacerType, RacerSettingsConfig> _racerConfigs;
private Dictionary<RacerType, AsyncOperationHandle<GameObject>> _racerPrefabsHandle = new();
private string _racerConfigsLabel;
private AsyncOperationHandle<IList<RacerSettingsConfig>> _racerConfigsHandle;
private EventBus _eventBus;
public RacerFactory(IConfigLocator configLocator, EventBus eventBus)
{
_eventBus = eventBus;
_racerConfigsLabel = configLocator.TryGetManyConfigAssetReferences<RacerSettingsConfig>();
LoadConfigs().Forget();
}
private async UniTaskVoid LoadConfigs()
{
_racerConfigsHandle = Addressables.LoadAssetsAsync<RacerSettingsConfig>(_racerConfigsLabel);
await _racerConfigsHandle.ToUniTask();
_racerConfigs = _racerConfigsHandle.Result.ToDictionary(x => x.racerData.type);
_eventBus.RaiseEvent<IRacerFactoryInitializedHandle>(x => x.RacerFactoryInitialized());
}
public RacerSettingsConfig GetConfig(RacerType racerType) => _racerConfigs[racerType];
public async UniTaskVoid LoadRacer(RacerType racerType)
{
if (_racerPrefabsHandle.ContainsKey(racerType))
{
_eventBus.RaiseEvent<IRacerLoadedHandle>(x => x.RacerLoaded(racerType, null));
return;
}
_racerPrefabsHandle.Add(racerType,
Addressables.LoadAssetAsync<GameObject>(_racerConfigs[racerType].prefabReference));
await _racerPrefabsHandle[racerType].ToUniTask();
var racer = _racerPrefabsHandle[racerType].Result.GetComponent<AI.Racer>();
racer.SetData(_racerConfigs[racerType].racerData);
_eventBus.RaiseEvent<IRacerLoadedHandle>(x => x.RacerLoaded(racerType, racer));
}
public void ReleaseRacer(RacerType racerType)
{
_racerPrefabsHandle[racerType].Release();
_racerPrefabsHandle.Remove(racerType);
}
public void Dispose()
{
foreach (var prefab in _racerPrefabsHandle)
prefab.Value.Release();
_racerConfigsHandle.Release();
}
}
public interface IRacerFactoryInitializedHandle : IGlobalSubscriber
{
public void RacerFactoryInitialized();
}
public interface IRacerLoadedHandle : IGlobalSubscriber
{
public void RacerLoaded(RacerType type, AI.Racer racer);
public void RacerCreated(RacerType type, AI.Racer racer);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5ab404f94a89460eae107b9d453fa64d
timeCreated: 1769620039
@@ -0,0 +1,27 @@
using System;
using Code.Infrastructure.Generators;
using UnityEngine.AddressableAssets;
namespace Code.Example.Race.Racer
{
[Config("Configs/Gameplay/RacerSettings", true)]
public class RacerSettings
{
[ToConfig] public RacerData racerData;
[ToConfig] public AssetReference prefabReference;
}
[Serializable]
public struct RacerData
{
public string name;
public float speed;
public float angularSpeed;
public float maxAcceleration;
public float brakeAcceleration;
public float turnSensitivity;
public float maxSteerAngle;
public RacerType type;
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f25b32b493144bfc9ca0c5016741d012
timeCreated: 1769619229
@@ -0,0 +1,9 @@
namespace Code.Example.Race.Racer
{
public enum RacerType
{
Player,
AIGreen,
AIRed
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 488f864fcf6f4632b8d50b39038f5472
timeCreated: 1769619126
@@ -0,0 +1,7 @@
namespace Code.Example.Race.Racer
{
public class RacistSpawnListDebug
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a9506da417a249929493862133eef179
timeCreated: 1769685790