forked from oldnapalm/CommunityRaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplay.cs
139 lines (124 loc) · 3.91 KB
/
Replay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using GTA;
using GTA.Math;
using System;
namespace CommunityRaces
{
public class Replay
{
public uint Time;
public uint Vehicle;
public Record[] Records;
public Replay(uint time, uint vehicle, Record[] records)
{
Time = time;
Vehicle = vehicle;
Records = records;
}
}
public class Record
{
public float PX;
public float PY;
public float PZ;
public float RX;
public float RY;
public float RZ;
public float RW;
public float VX;
public float VY;
public float VZ;
public float S;
public Record(Vector3 position, Quaternion rotation, Vector3 velocity, float speed)
{
PX = position.X;
PY = position.Y;
PZ = position.Z;
RX = rotation.X;
RY = rotation.Y;
RZ = rotation.Z;
RW = rotation.W;
VX = velocity.X;
VY = velocity.Y;
VZ = velocity.Z;
S = speed;
}
public Vector3 GetPosition()
{
return new Vector3(PX, PY, PZ);
}
public Quaternion GetRotation()
{
return new Quaternion(RX, RY, RZ, RW);
}
public Vector3 GetVelocity()
{
return new Vector3(VX, VY, VZ);
}
}
public class Ghost
{
public Vehicle Vehicle;
public Ped Ped;
private readonly Record[] Records;
private readonly Blip Blip;
private int Index;
private int StopTime;
public Ghost(Replay replay)
{
Records = replay.Records;
Index = 0;
var record = Records[Index];
Vehicle = World.CreateVehicle(Helpers.RequestModel((int)replay.Vehicle), record.GetPosition(), record.GetRotation().Z);
Vehicle.Quaternion = record.GetRotation();
Vehicle.IsInvincible = true;
Vehicle.Alpha = 100;
Ped = World.CreateRandomPed(record.GetPosition());
Ped.IsInvincible = true;
Ped.Alpha = 100;
Ped.SetIntoVehicle(Vehicle, VehicleSeat.Driver);
Blip = Vehicle.AddBlip();
Blip.Sprite = BlipSprite.Ghost;
}
public void Update()
{
if (Index < Records.Length)
{
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.25f);
StopTime = Environment.TickCount;
}
else if (Environment.TickCount - StopTime <= 1000)
{
Vector3 posTarget = Helpers.LinearVectorLerp(Vehicle.Position, record.GetPosition() + (record.GetPosition() - Vehicle.Position), Environment.TickCount - StopTime, 1000);
Vehicle.PositionNoOffset = posTarget;
Vehicle.Quaternion = Quaternion.Slerp(Vehicle.Quaternion, record.GetRotation(), 0.5f);
}
else
{
Vehicle.Position = record.GetPosition();
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()
{
Blip?.Remove();
Ped?.Delete();
Vehicle?.Delete();
}
}
}