Platformer example started

This commit is contained in:
ThomasCharester committed 2026-02-22 13:59:04 +03:00
1 parent efcfaae647
commit 9f026b76d6
325 files changed
+23540 -270

No files matched your search

@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
namespace Code.Example.Race
{
public class PlayerPrefsRacePoopshitter
{
private List<RaceData> _races = new();
public PlayerPrefsRacePoopshitter()
{
GetRaces();
}
public List<RaceData> GetRaces()
{
var path = Path.Combine(Application.persistentDataPath,"raceData.json");
using (StreamReader reader = new(path))
{
var json = reader.ReadToEnd();
_races = JsonConvert.DeserializeObject<List<RaceData>>(json);
}
// for (int i = 0; i < _raceCount; i++)
// races.Add(new(
// PlayerPrefs.GetFloat($"Race{i}"),
// PlayerPrefs.GetInt($"Place{i}")));
return _races;
}
public void AddRace(RaceData race)
{
// PlayerPrefs.SetFloat($"Race{_raceCount}", race.raceTimeSeconds);
// PlayerPrefs.SetInt($"Place{_raceCount}", race.place);
// PlayerPrefs.SetInt("RaceCount", ++_raceCount);
_races.Add(race);
var json = JsonConvert.SerializeObject(_races, Formatting.Indented);
var path = Path.Combine(Application.persistentDataPath,"raceData.json");
using (StreamWriter writer = new(path))
{
writer.Write(json);
}
}
}
[Serializable]
public struct RaceData
{
public float raceTimeSeconds;
public int place;
public RaceData(float raceTimeSeconds, int place)
{
this.raceTimeSeconds = raceTimeSeconds;
this.place = place;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e984eddc1697449487c9a790345dfb0b
timeCreated: 1770653240
+6 -1
View File
@@ -4,7 +4,8 @@ using System.Linq;
using Code.Example.Race.Racer;
using Code.Example.Race.Track;
using Code.Infrastructure.EventBus;
using Code.Infrastructure.Loadings;
using Code.Infrastructure.Loadings;
using UnityEngine;
namespace Code.Example.Race
{
@@ -28,8 +29,12 @@ namespace Code.Example.Race
RandomizeRacers(raceTrackData.trackData.RacerPoints.Count);
}
private float _timeStart;
public float RaceTime => Time.time - _timeStart;
public void StartRacers()
{
_timeStart = Time.time;
foreach (var racer in PlacedRacers)
{
racer.StartRacer();
@@ -8,5 +8,29 @@ namespace Code.Example.Race.Racer.AI
{
SteeringInput = steeringLinear;
}
protected override void Brake()
{
if (CarRb.linearVelocity.magnitude > 10f && (SteeringInput.y > CarRb.linearVelocity.y || SteeringInput.x > CarRb.linearVelocity.x))
{
foreach (var wheel in wheels)
{
wheel.wheelCollider.brakeTorque = 100 * racer.Data.brakeAcceleration * Time.fixedDeltaTime;
}
// carLights.isBackLightOn = true;
// carLights.OperateBackLights();
}
else
{
foreach (var wheel in wheels)
{
wheel.wheelCollider.brakeTorque = 0;
}
// carLights.isBackLightOn = false;
// carLights.OperateBackLights();
}
}
}
}
@@ -11,6 +11,7 @@ namespace Code.Example.Race.Racer.AI
{
[Inject] private EventBus _eventBus;
[SerializeField] private CarControllerRB carController;
public override void SetData(RacerData data)
{
Data = data;
@@ -10,7 +10,7 @@ namespace Code.Example.Race.Racer.AI.ID.Behaviours
steering.linear = transform.position - target.position;
steering.linear.Normalize();
steering.linear *= agent.Data.maxAcceleration;
// steering.linear *= agent.Data.maxAcceleration;
return steering;
}
}
@@ -0,0 +1,42 @@
using UnityEngine;
using System.Collections;
namespace Code.Example.Race.Racer.AI.ID.Behaviours
{
public class Pursue : Seek
{
public float maxPrediction;
private GameObject _targetAux;
private GameObject _target;
public void Awake()
{
_targetAux = _target;
_target = new GameObject();
}
void OnDestroy()
{
Destroy(_targetAux);
}
public override Steering GetSteering(Transform targetTransform)
{
var direction = _targetAux.transform.position - transform.position;
var distance = direction.magnitude;
var speed = agent.CarRigidbody.linearVelocity.magnitude;
float prediction;
if (speed <= distance / maxPrediction)
prediction = maxPrediction;
else
prediction = distance / speed;
_target.transform.position = _targetAux.transform.position;
// _target.transform.position += targetAgent.velocity * prediction;
return base.GetSteering(_target.transform);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c3a071d2cab443528dd16bdc044bab65
timeCreated: 1770998212
@@ -11,8 +11,7 @@ namespace Code.Example.Race.Racer.AI.ID.Behaviours
steering.linear = target.position - transform.position;
UnityEngine.Debug.DrawRay(transform.position, steering.linear, Color.yellow, 5f);
// steering.linear *= agent.Data.maxAcceleration;
steering.linear.Normalize();
UnityEngine.Debug.Log(steering.linear);
steering.linear.Normalize();
return steering;
}
}
@@ -34,7 +34,7 @@ namespace Code.Example.Race.Racer
public List<Wheel> wheels;
protected Rigidbody CarRb;
protected Rigidbody CarRb;
protected Vector2 SteeringInput = Vector2.zero;
public abstract void SetSteering(Vector2 steeringLinear);
@@ -16,9 +16,9 @@ namespace Code.Example.Race.Racer.Player
{
_inputService = inputService;
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
_inputService.BindActionCancel((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
_inputService.BindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakePerformed);
_inputService.BindActionCancel((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
_inputService.BindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
}
private void MovementPerformed(InputAction.CallbackContext context) => MovementInput = context.ReadValue<Vector2>();
@@ -33,9 +33,9 @@ namespace Code.Example.Race.Racer.Player
{
if (_inputService == null) return;
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
_inputService.UnbindActionCanceled((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
_inputService.UnbindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakePerformed);
_inputService.UnbindActionCanceled((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
_inputService.UnbindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using Code.Infrastructure.UI;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Code.Example.Race.UI
{
public class PlayerScores : UIPanel
{
[SerializeField] private Image image;
[SerializeField] private TMP_Text scoreText;
[SerializeField] private Color shownColor;
[SerializeField] private Color hiddenColor;
public void SetText(List<RaceData> scores)
{
scoreText.text = "";
foreach (var score in scores)
{
var t = TimeSpan.FromSeconds(score.raceTimeSeconds);
scoreText.text += $@"{score.place} место за {t:mm\:ss\:fff}\n";
}
}
public override void Open(Action callback = null)
{
image.DOColor(shownColor, 1f);
scoreText.DOColor(shownColor, 1f).onComplete = () => callback?.Invoke();
}
public override void Close(Action callback = null)
{
image.DOColor(hiddenColor, 1f);
scoreText.DOColor(hiddenColor, 1f).onComplete = () => callback?.Invoke();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 250fd47281f3493e86590f89511bba17
timeCreated: 1770654299
+2 -2
View File
@@ -17,13 +17,13 @@ namespace Code.Example.Race.UI
public void SetText(string text) => winText.text = text;
public override void Open(Action callback = null)
{
image.DOColor(shownColor, 1f).onComplete = () => callback?.Invoke();
image.DOColor(shownColor, 1f);
winText.DOColor(shownColor, 1f).onComplete = () => callback?.Invoke();
}
public override void Close(Action callback = null)
{
image.DOColor(hiddenColor, 1f).onComplete = () => callback?.Invoke();
image.DOColor(hiddenColor, 1f);
winText.DOColor(hiddenColor, 1f).onComplete = () => callback?.Invoke();
}
}