diff --git a/CommunityRaces.cs b/CommunityRaces.cs index 37493d0..dd1afc9 100644 --- a/CommunityRaces.cs +++ b/CommunityRaces.cs @@ -25,7 +25,7 @@ public class CommunityRaces : Script private bool _isInRace; private int _countdown = -1; private uint _missionStart; - private uint _seconds; + private uint _seconds = 0; private int _totalLaps; private float _oldAngle; private readonly UIMenu _quitMenu; @@ -70,7 +70,7 @@ public CommunityRaces() _quitMenu.Visible = false; Game.FadeScreenOut(500); Wait(1000); - Game.Player.Character.Position = _currentRace.Trigger; + Game.Player.Character.Position = _currentRace.Trigger + new Vector3(4f, 0f, 0f); EndRace(); Game.FadeScreenIn(500); AddRacesBlips(); @@ -151,13 +151,14 @@ private int CalculatePlayerPositionInRace() private void StartRace(Race race) { _records.Clear(); - _replay = new Replay(0, new Record[0]); _ghost = null; var fileName = "scripts\\Races\\Replays\\" + race.FileName + ".json"; if (File.Exists(fileName)) using (StreamReader reader = new StreamReader(fileName)) using (JsonTextReader jsonReader = new JsonTextReader(reader)) _replay = new JsonSerializer().Deserialize(jsonReader); + else + _replay = new Replay(0, (uint)_vehicleHash, new Record[0]); Game.FadeScreenOut(500); Wait(500); @@ -220,7 +221,7 @@ private void StartRace(Race race) break; case "Ghost": if (_replay.Records.Length > 0) - _ghost = new Ghost(_replay.Records.ToList(), _vehicleHash); + _ghost = new Ghost(_replay); break; case "None": break; @@ -291,6 +292,7 @@ private void EndRace() _records.Clear(); _ghost?.Delete(); + _ghost = null; } public void OnTick(object sender, EventArgs e) @@ -414,7 +416,7 @@ public void OnTick(object sender, EventArgs e) if (_countdown <= 0) { new UIResText("TIME", new Point(Convert.ToInt32(res.Width) - safe.X - 180, Convert.ToInt32(res.Height) - safe.Y - (90 + (1 * interval))), 0.3f, Color.White).Draw(); - new UIResText(FormatTime((int)unchecked(_seconds - _missionStart)), new Point(Convert.ToInt32(res.Width) - safe.X - 20, Convert.ToInt32(res.Height) - safe.Y - (102 + (1 * interval))), 0.5f, Color.White, Font.ChaletLondon, UIResText.Alignment.Right).Draw(); + new UIResText(FormatTime(_seconds - _missionStart), new Point(Convert.ToInt32(res.Width) - safe.X - 20, Convert.ToInt32(res.Height) - safe.Y - (102 + (1 * interval))), 0.5f, Color.White, Font.ChaletLondon, UIResText.Alignment.Right).Draw(); new Sprite("timerbars", "all_black_bg", new Point(Convert.ToInt32(res.Width) - safe.X - 248, Convert.ToInt32(res.Height) - safe.Y - (100 + (1 * interval))), new Size(250, 37), 0f, Color.FromArgb(200, 255, 255, 255)).Draw(); string label, value; @@ -442,12 +444,13 @@ public void OnTick(object sender, EventArgs e) new Sprite("timerbars", "all_black_bg", new Point(Convert.ToInt32(res.Width) - safe.X - 248, Convert.ToInt32(res.Height) - safe.Y - (100 + (3 * interval))), new Size(250, 37), 0f, Color.FromArgb(200, 255, 255, 255)).Draw(); } - if (Environment.TickCount >= _lastRecord + 20) + if (Environment.TickCount >= _lastRecord + 100) { _lastRecord = Environment.TickCount; _records.Add(new Record(_currentVehicle.Position, _currentVehicle.Quaternion, _currentVehicle.Velocity, _currentVehicle.Speed)); - _ghost?.Update(); + _ghost?.NextRecord(); } + _ghost?.Update(); } for (int i = 0; i < _rivalCheckpointStatus.Count; i++) @@ -518,14 +521,14 @@ public void OnTick(object sender, EventArgs e) if (score < 0) score = 0; _passed = new MissionPassedScreen(_currentRace.Name, score, score > 50 ? score > 90 ? MissionPassedScreen.Medal.Gold : MissionPassedScreen.Medal.Silver : MissionPassedScreen.Medal.Bronze); - _passed.AddItem("Time Elapsed", FormatTime((int)unchecked(_seconds - _missionStart)), MissionPassedScreen.TickboxState.None); + _passed.AddItem("Time Elapsed", FormatTime(_seconds - _missionStart), MissionPassedScreen.TickboxState.None); _passed.AddItem("Position", position + "/" + peoplecount, position == 1 ? MissionPassedScreen.TickboxState.Tick : MissionPassedScreen.TickboxState.Empty); _passed.OnContinueHit += () => { Game.FadeScreenOut(1000); Wait(1000); Function.Call(Hash._STOP_SCREEN_EFFECT, "HeistCelebPass"); - Game.Player.Character.Position = _currentRace.Trigger; + Game.Player.Character.Position = _currentRace.Trigger + new Vector3(4f, 0f, 0f); Game.Player.CanControlCharacter = true; World.RenderingCamera = null; EndRace(); @@ -542,7 +545,7 @@ public void OnTick(object sender, EventArgs e) Directory.CreateDirectory("scripts\\Races\\Replays"); using (StreamWriter file = File.CreateText("scripts\\Races\\Replays\\" + _currentRace.FileName + ".json")) - new JsonSerializer().Serialize(file, new Replay((int)(_seconds - _missionStart), _records.ToArray())); + new JsonSerializer().Serialize(file, new Replay(_seconds - _missionStart, (uint)_vehicleHash, _records.ToArray())); } } } @@ -573,7 +576,7 @@ private void OnAbort(object sender, EventArgs e) _races.Clear(); } - public string FormatTime(int seconds) + public string FormatTime(uint seconds) { var minutes = Convert.ToInt32(Math.Floor(seconds / 60f)); var secs = seconds % 60; @@ -677,7 +680,7 @@ public void BuildMenu(Race race) var carItem = new UIMenuListItem("Vehicle", tmpList, 0); carItem.OnListChanged += (item, index) => { - Enum.TryParse(item.Items[index].ToString(), out _vehicleHash); + _vehicleHash = race.AvailableVehicles[index]; _previewVehicle?.Delete(); _previewVehicle = World.CreateVehicle(Helpers.RequestModel((int)_vehicleHash), race.Trigger); if (_previewVehicle == null) return; diff --git a/Replay.cs b/Replay.cs index a75d56d..dbe04e1 100644 --- a/Replay.cs +++ b/Replay.cs @@ -1,19 +1,19 @@ using GTA; using GTA.Math; -using GTA.Native; using System; -using System.Collections.Generic; namespace CommunityRaces { public class Replay { - public int Time; + public uint Time; + public uint Vehicle; public Record[] Records; - public Replay(int time, Record[] records) + public Replay(uint time, uint vehicle, Record[] records) { Time = time; + Vehicle = vehicle; Records = records; } } @@ -68,17 +68,17 @@ public class Ghost public Vehicle Vehicle; public Ped Ped; - private readonly List Records; + private readonly Record[] Records; private readonly Blip Blip; private int Index; private int StopTime; - public Ghost(List records, VehicleHash vehicle) + public Ghost(Replay replay) { - Records = records; + Records = replay.Records; Index = 0; var record = Records[Index]; - Vehicle = World.CreateVehicle(Helpers.RequestModel((int)vehicle), record.GetPosition(), record.GetRotation().Z); + Vehicle = World.CreateVehicle(Helpers.RequestModel((int)replay.Vehicle), record.GetPosition(), record.GetRotation().Z); Vehicle.Quaternion = record.GetRotation(); Vehicle.IsInvincible = true; Vehicle.Alpha = 100; @@ -92,18 +92,14 @@ public Ghost(List records, VehicleHash vehicle) public void Update() { - if (Records.Count > Index + 1) + if (Index < Records.Length) { - if (!Ped.IsInVehicle(Vehicle)) - Ped.SetIntoVehicle(Vehicle, VehicleSeat.Driver); - - Index++; var record = Records[Index]; if (record.S > 0.2f && Vehicle.IsInRangeOf(record.GetPosition(), 7.0f)) { Vehicle.Velocity = record.GetVelocity() + (record.GetPosition() - Vehicle.Position); - Vehicle.Quaternion = Quaternion.Slerp(Vehicle.Quaternion, record.GetRotation(), 0.5f); + Vehicle.Quaternion = Quaternion.Slerp(Vehicle.Quaternion, record.GetRotation(), 0.25f); StopTime = Environment.TickCount; } else if (Environment.TickCount - StopTime <= 1000) @@ -118,6 +114,19 @@ public void Update() Vehicle.Quaternion = record.GetRotation(); } } + else + Vehicle.HandbrakeOn = true; + } + + public void NextRecord() + { + if (Index < Records.Length) + { + Index++; + + if (!Ped.IsInVehicle(Vehicle)) + Ped.SetIntoVehicle(Vehicle, VehicleSeat.Driver); + } } public void Delete() @@ -125,7 +134,6 @@ public void Delete() Blip?.Remove(); Ped?.Delete(); Vehicle?.Delete(); - Records?.Clear(); } } }