forked from codecat/tm-editor-trails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrail.as
115 lines (101 loc) · 2.79 KB
/
Trail.as
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
class Trail
{
array<Sample> m_samples;
array<Event@> m_events;
TrailPlayer@ m_player; //TODO: Do we need this here? It's a circular reference!
vec3 m_lastPosition;
int m_lastGear = 1;
Trail()
{
m_samples.Reserve(1000);
m_events.Reserve(25);
@m_player = TrailPlayer(this);
}
#if TMNEXT
void Update(CSmScriptPlayer@ scriptPlayer)
#else
void Update(CTrackManiaScriptPlayer@ scriptPlayer)
#endif
{
UpdateSample(scriptPlayer);
UpdateEvents(scriptPlayer);
}
#if TMNEXT
void UpdateSample(CSmScriptPlayer@ scriptPlayer)
#else
void UpdateSample(CTrackManiaScriptPlayer@ scriptPlayer)
#endif
{
// There is no reason to save a sample if we're still in the same position as the last time
if (m_samples.Length > 0) {
//TODO: We should also check vehicle rotation when we do this
if ((m_lastPosition - scriptPlayer.Position).LengthSquared() < 0.1) {
return;
}
}
// Get velocity
#if TMNEXT
vec3 velocity = scriptPlayer.Velocity;
#else
// Maniaplanet doesn't have the Velocity property, so we must manually calculate it
vec3 velocity;
if (m_samples.Length > 0) {
velocity = (scriptPlayer.Position - m_lastPosition).Normalized() * scriptPlayer.Speed;
}
#endif
// Remember our last position
m_lastPosition = scriptPlayer.Position;
// Create a sample and store it in the trail
Sample newSample;
newSample.m_time = State::CurrentRaceTime / 1000.0;
newSample.m_position = scriptPlayer.Position;
newSample.m_dir = quat(scriptPlayer.AimDirection);
newSample.m_velocity = velocity;
m_samples.InsertLast(newSample);
}
#if TMNEXT
void UpdateEvents(CSmScriptPlayer@ scriptPlayer)
#else
void UpdateEvents(CTrackManiaScriptPlayer@ scriptPlayer)
#endif
{
// If our gear changed
int gear = scriptPlayer.EngineCurGear;
if (m_lastGear != gear) {
// Create a gear change event
auto newGearChangeEvent = Events::GearChange();
newGearChangeEvent.m_time = State::CurrentRaceTime / 1000.0;
newGearChangeEvent.m_position = scriptPlayer.Position;
newGearChangeEvent.m_prev = m_lastGear;
newGearChangeEvent.m_new = gear;
m_events.InsertLast(newGearChangeEvent);
// Remember our last gear
m_lastGear = scriptPlayer.EngineCurGear;
}
}
double GetStartTime()
{
if (m_samples.Length == 0) {
return 0;
}
return m_samples[0].m_time;
}
double GetEndTime()
{
// Subtract the last sample time with the first sample time to get the duration of the trail
if (m_samples.Length == 0) {
return 0;
}
return m_samples[m_samples.Length - 1].m_time;
}
double GetDuration()
{
// Subtract the last sample time with the first sample time to get the duration of the trail
if (m_samples.Length == 0) {
return 0;
}
double firstTime = m_samples[0].m_time;
double lastTime = m_samples[m_samples.Length - 1].m_time;
return lastTime - firstTime;
}
}