NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
d_rail.cpp
1#include <game/bases/d_rail.hpp>
2#include <game/bases/d_s_stage.hpp>
3
5 dCdFile_c *cdFile = dCd_c::m_instance->getFileP(dScStage_c::m_instance->mCurrFile);
6 sRailInfoData *curr = cdFile->mpRails;
7 for (u32 i = 0; i < cdFile->mRailCount; i++) {
8 if (curr->mID == id) {
9 return curr;
10 }
11 curr++;
12 }
13 return nullptr;
14}
15
16bool dRail_c::set(u8 id, mVec3_c *pos, mVec3_c *speed, float *speedF, u16 startIndex, u8 reverse) {
17 mCount = 0;
18 mpNodes = nullptr;
19 sRailInfoData *railInfo = getRailInfoP(id);
20 if (railInfo == nullptr) {
21 return false;
22 }
23
24 dCdFile_c *cdFile = dCd_c::m_instance->getFileP(dScStage_c::m_instance->mCurrFile);
25 sRailNodeData *railNode = &cdFile->mpRailNodes[railInfo->mNodeIdx];
26
27 // Check for invalid start index
28 if (railInfo->mCount <= startIndex) {
29 startIndex = 0;
30 }
31
32 float dummy = 0.0f; // [For .sdata2 data order]
33 mCount = railInfo->mCount;
34 mpPos = pos;
35 mpPos->x = railNode[startIndex].mX;
36 mpPos->y = -railNode[startIndex].mY;
37 mpSpeed = speed;
38 mpSpeedF = speedF;
39 mpNodes = railNode;
40 mIdxCurr = startIndex;
41 mIdxNext = startIndex;
42 mDelayTimer = railNode->mDelay;
43 mReverse = reverse;
44 mFlags = railInfo->mFlags;
45 mNodeFlags = railNode[startIndex].mFlags;
46
49 calcSpeed();
50 return true;
51}
52
54 bool reachedEnd = false;
55 if (mReverse) {
56 mIdxNext--;
57 if (mIdxNext < 0) {
58 if (mFlags & RAIL_FLAG_LOOP) {
59 mIdxNext = mCount - 1;
60 } else if (mBounce) {
61 mReverse ^= 1;
62 mIdxNext = 1;
63 } else {
64 mIdxNext = 0;
65 reachedEnd = true;
66 }
67 }
68 } else {
69 mIdxNext++;
70 if (mIdxNext >= mCount) {
71 if (mFlags & RAIL_FLAG_LOOP) {
72 mIdxNext = 0;
73 } else if (mBounce) {
74 mReverse ^= 1;
75 mIdxNext = mCount - 2;
76 } else {
77 mIdxNext = mCount - 1;
78 reachedEnd = true;
79 }
80 }
81 }
82 return reachedEnd;
83}
84
86 if (mpNodes == nullptr) {
87 return;
88 }
93}
94
96 float speed = mpNodes[mIdxCurr].mSpeed;
97 float accel = mpNodes[mIdxCurr].mAccel;
98 float prevSpeed = *mpSpeedF;
99 float newSpeed;
100 if (accel == 0.0f) {
101 newSpeed = speed;
102 } else {
103 if (prevSpeed < speed) {
104 newSpeed = prevSpeed + accel;
105 if (newSpeed > speed) {
106 newSpeed = speed;
107 }
108 } else {
109 newSpeed = prevSpeed;
110 if (prevSpeed > speed) {
111 newSpeed = prevSpeed - accel;
112 if (newSpeed < speed) {
113 newSpeed = speed;
114 }
115 }
116 }
117 }
118
119 *mpSpeedF = newSpeed;
120
121 if (mAngleCalcTimer > 0){
123 } else {
124 mAngleCalcTimer = 8;
126 }
127
128 mpSpeed->x = *mpSpeedF * nw4r::math::CosIdx(mAngle);
129 mpSpeed->y = *mpSpeedF * nw4r::math::SinIdx(mAngle);
130 mpSpeed->z = 0.0f;
131}
132
134 mNodeFlags = 0;
135 if (mpNodes == nullptr) {
136 return false;
137 }
138 if (mFlags & RAIL_FLAG_DISABLED) {
139 return false;
140 }
141 if (mDelayTimer != 0) {
142 mDelayTimer--;
143 return false;
144 }
145 mFlags &= ~RAIL_FLAG_DELAYING;
146
147 *mpPos += *mpSpeed;
148
149 bool res = false;
150 if (checkArrive()) {
151 mNodeFlags = mpNodes[mIdxNext].mFlags;
152 if (mNodeFlags & NODE_FLAG_1) {
153 mFlags |= RAIL_FLAG_DISABLED | RAIL_FLAG_DELAYING;
154 } else {
156 if (calcNextPoint()) {
157 mFlags |= RAIL_FLAG_DISABLED | RAIL_FLAG_DELAYING;
158 } else {
160 mDelayTimer = mpNodes[mIdxCurr].mDelay;
161 mFlags |= RAIL_FLAG_DELAYING;
162 }
163 }
164 res = true;
165 }
166 calcSpeed();
167 return res;
168}
169
171 float dx = mTargetPos.x - mpPos->x;
172 float dy = mTargetPos.y - mpPos->y;
173 float squareDist = dx * dx + dy * dy;
174 float squareSpeed = *mpSpeedF * *mpSpeedF;
175 return squareDist < squareSpeed;
176}
Course data file holder. A course data file contains the actual course elements - areas,...
Definition d_cd.hpp:11
void calcTargetPos()
Calculates the target position based on the next node.
Definition d_rail.cpp:85
bool set(u8 id, mVec3_c *pos, mVec3_c *speed, float *speedF, u16 startIndex, u8 reverse)
Initializes the rail with the specified ID and parameters.
Definition d_rail.cpp:16
u16 mFlags
The rail flags.
Definition d_rail.hpp:55
float * mpSpeedF
Reference to the forward speed of the object on the rail.
Definition d_rail.hpp:49
mVec3_c * mpSpeed
Reference to the speed vector of the object on the rail.
Definition d_rail.hpp:48
bool calcNextPoint()
Advances the node index to the next target point.
Definition d_rail.cpp:53
mVec3_c * mpPos
Reference to the position of the object on the rail.
Definition d_rail.hpp:47
short mIdxCurr
The current node index.
Definition d_rail.hpp:51
short mIdxNext
The next node index.
Definition d_rail.hpp:52
u16 mAngle
The current angle of movement.
Definition d_rail.hpp:54
bool mBounce
Whether to reverse direction at the ends of the rail (only applicable if not looping).
Definition d_rail.hpp:59
virtual void calcSpeed()
Calculates and applies the new speed based on the current and next node.
Definition d_rail.cpp:95
bool execute()
Executes the rail movement logic. Returns whether a target node has been reached.
Definition d_rail.cpp:133
u16 mCount
The number of nodes in the rail.
Definition d_rail.hpp:50
mVec2_c mTargetPos
The position of the next node.
Definition d_rail.hpp:46
u8 mReverse
Whether to go through the rail in reverse.
Definition d_rail.hpp:57
sRailNodeData * mpNodes
The array of rail nodes.
Definition d_rail.hpp:45
u16 mNodeFlags
The current node flags.
Definition d_rail.hpp:56
static sRailInfoData * getRailInfoP(u8 id)
Gets the rail data for the specified ID.
Definition d_rail.cpp:4
int mAngleCalcTimer
Cooldown timer for angle recalculation.
Definition d_rail.hpp:58
u16 mDelayTimer
By how many frames to delay before starting movement from a node.
Definition d_rail.hpp:53
bool checkArrive()
Checks whether the rail has arrived at the next target node.
Definition d_rail.cpp:170
A three-dimensional floating point vector.
Definition m_vec.hpp:100
s16 atan2s(float sin, float cos)
Converts a sine and a cosine to an angle in units.
Definition c_math.cpp:167