forked from Ares-Developers/YRpp
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Facing.h
139 lines (114 loc) · 2.67 KB
/
Facing.h
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
#pragma once
#include <Dir.h>
#include <Timer.h>
#include <algorithm>
class FacingClass
{
public:
explicit FacingClass() noexcept { }
explicit FacingClass(const noinit_t&) noexcept { }
explicit FacingClass(int rate) noexcept
{
SetROT(rate);
}
explicit FacingClass(const DirStruct& facing) noexcept
{
DesiredFacing = facing;
}
explicit FacingClass(DirType dir) noexcept
{
DesiredFacing.SetDir(dir);
}
explicit FacingClass(const FacingClass& another) noexcept
: DesiredFacing { another.DesiredFacing }
, StartFacing { another.StartFacing }
, RotationTimer { another.RotationTimer }
, ROT { another.ROT }
{ }
FacingClass& operator=(const FacingClass& another)
{
if (this != &another)
{
DesiredFacing = another.DesiredFacing;
StartFacing = another.StartFacing;
RotationTimer = another.RotationTimer;
ROT = another.ROT;
}
return *this;
}
bool SetDesired(const DirStruct& facing) //JMP_THIS(0x4C9220)
{
if (DesiredFacing == facing)
return false;
StartFacing = Current();
DesiredFacing = facing;
if (static_cast<short>(ROT.Raw) > 0)
RotationTimer.Start(NumSteps());
return true;
}
bool SetCurrent(const DirStruct& facing)
{
bool ret = Current() != facing;
if (ret)
{
DesiredFacing = facing;
StartFacing = facing;
}
RotationTimer.Start(0);
return ret;
}
DirStruct Desired() const
{
return DesiredFacing;
}
DirStruct Current() const //0x4C93D0
{
if (IsRotating())
{
const short diff = Difference().Raw;
const short num_steps = static_cast<short>(NumSteps());
if (num_steps > 0)
{
const int steps_left = RotationTimer.GetTimeLeft();
return DirStruct { DesiredFacing.Raw - steps_left * diff / num_steps };
}
}
return DesiredFacing;
}
bool IsRotating() const // JMP_THIS(0x4C9480)
{
return static_cast<short>(ROT.Raw) > 0 && RotationTimer.GetTimeLeft();
}
bool IsRotatingCCW() const // JMP_THIS(0x4C94F0) counter-clockwise
{
if (!IsRotating())
return false;
return static_cast<short>(Difference().Raw) < 0;
}
bool IsRotatingCW() const // JMP_THIS(0x4C94B0) clockwise
{
if (!IsRotating())
return false;
return static_cast<short>(Difference().Raw) > 0;
}
DirStruct Difference() const
{
return DirStruct { static_cast<short>(DesiredFacing.Raw) - static_cast<short>(StartFacing.Raw) };
}
void SetROT(int rate)
{
if (rate > 127)
rate = 127;
ROT.SetDir(static_cast<DirType>(rate));
}
private:
int NumSteps() const
{
return std::abs(static_cast<short>(Difference().Raw)) / ROT.Raw;
}
public:
DirStruct DesiredFacing;
DirStruct StartFacing; // The starting direction from which to calcuate the rotation.
CDTimerClass RotationTimer;
DirStruct ROT;
};