1#include "ObjectChoropu.hh"
3#include "game/field/ObjectDirector.hh"
4#include "game/field/RailManager.hh"
6#include "game/system/RaceManager.hh"
12 : ObjectCollidable(params), StateManager(this) {
13 constexpr f32 MAX_SPEED = 20.0f;
15 m_startFrameOffset =
static_cast<s16
>(params.setting(1));
16 m_idleDuration = params.setting(0);
17 m_isStationary = strcmp(getName(),
"choropu") != 0;
19 s16 railIdx = params.pathId();
21 auto *rail = RailManager::Instance()->rail(railIdx);
22 rail->checkSphereFull();
26 if (!m_isStationary) {
27 const auto &flowTable = ObjectDirector::Instance()->flowTable();
28 const auto *collisionSet =
29 flowTable.set(flowTable.slot(flowTable.getIdFromName(
"choropu_ground")));
32 s16 height = parse<s16>(collisionSet->params.cylinder.height);
33 size_t groundCount =
static_cast<size_t>(MAX_GROUND_LEN / EGG::Mathf::abs(height * 2)) + 1;
34 m_groundObjs = std::span<ObjectChoropuGround *>(
new ObjectChoropuGround *[groundCount],
37 for (
auto *&obj : m_groundObjs) {
38 obj =
new ObjectChoropuGround(m_pos, m_rot, m_scale);
40 obj->resize(RADIUS, MAX_SPEED);
43 m_groundHeight = m_groundObjs.front()->height();
46 m_objHoll =
new ObjectChoropuHoll(params);
51ObjectChoropu::~ObjectChoropu() {
52 delete[] m_groundObjs.data();
56void ObjectChoropu::init() {
61 m_objHoll->setScale(
EGG::Vector3f(1.0f, m_objHoll->scale().y, 1.0f));
63 m_groundLength = 0.0f;
66 m_transMat = m_transform;
68 if (m_mapObj->pathId() == -1) {
72 m_railInterpolator->init(0.0f, 0);
73 m_railInterpolator->setPerPointVelocities(
true);
75 m_railMat = RailOrthonormalBasis(*m_railInterpolator);
76 m_pos = m_railMat.base(3);
77 m_flags.setBit(eFlags::Position);
80 m_objHoll->disableCollision();
87void ObjectChoropu::calc() {
88 constexpr u32 START_DELAY = 300;
91 u32 t = System::RaceManager::Instance()->timer();
92 if (t < m_startFrameOffset + START_DELAY) {
96 if (!m_isStationary) {
97 if (m_mapObj->pathId() == -1) {
101 m_railMat = RailOrthonormalBasis(*m_railInterpolator);
104 if (m_nextStateId >= 0) {
105 m_currentStateId = m_nextStateId;
109 auto enterFunc = m_entries[m_entryIds[m_currentStateId]].onEnter;
110 (this->*enterFunc)();
115 auto calcFunc = m_entries[m_entryIds[m_currentStateId]].onCalc;
118 m_objHoll->setScale(
EGG::Vector3f(1.0f, m_objHoll->scale().y, 1.0f));
123 Kart::Reaction reactionOnKart, Kart::Reaction ,
125 return m_currentStateId == 1 ? Kart::Reaction::SmallBump : reactionOnKart;
128void ObjectChoropu::enterStateStub() {}
131void ObjectChoropu::enterDigging() {
132 if (m_isStationary) {
135 for (
auto *&obj : m_groundObjs) {
136 obj->enableCollision();
140 m_objHoll->disableCollision();
141 m_groundLength = 0.0f;
146void ObjectChoropu::enterPeeking() {
147 if (m_isStationary) {
148 m_pos = m_transMat.base(3);
149 m_flags.setBit(eFlags::Position, eFlags::Rotation);
154 m_pos = m_railMat.base(3);
155 m_flags.setBit(eFlags::Position, eFlags::Rotation);
161 const auto &curTanDir = m_railInterpolator->curTangentDir();
162 s16 curPointIdx = m_railInterpolator->curPointIdx();
164 SetRotTangentHorizontal(mat, m_railInterpolator->floorNrm(curPointIdx), curTanDir);
165 mat.
setBase(3, m_railInterpolator->curPos());
167 m_objHoll->setTransform(mat);
168 m_objHoll->setPos(mat.
base(3));
169 m_objHoll->enableCollision();
174void ObjectChoropu::enterJumping() {
177 m_pos = m_isStationary ? m_transMat.base(3) : m_railMat.base(3);
178 m_flags.setBit(eFlags::Position, eFlags::Rotation);
182void ObjectChoropu::calcStateStub() {}
185void ObjectChoropu::calcDigging() {
186 if (m_isStationary) {
187 if (m_currentFrame > m_idleDuration) {
194 m_pos = m_railInterpolator->curPos();
195 m_flags.setBit(eFlags::Position);
197 if (m_railInterpolator->calc() == RailInterpolator::Status::SegmentEnd) {
198 if (m_railInterpolator->curPoint().setting[1] == 1) {
204 bool skipGroundCalc =
false;
206 if (m_railInterpolator->nextPoint().setting[1] == 1) {
207 f32 invT = 1.0f - m_railInterpolator->segmentT();
208 if (invT * m_railInterpolator->getCurrSegmentLength() < 250.0f) {
209 skipGroundCalc =
true;
213 if (!skipGroundCalc) {
220void ObjectChoropu::calcPeeking() {
221 constexpr s16 PEEK_DURATION = 40;
222 constexpr s16 STATE_DURATION = 100;
224 if (!m_isStationary) {
225 m_groundLength = std::max(0.0f, m_groundLength - m_railInterpolator->speed());
230 if (m_currentFrame > STATE_DURATION) {
234 if (m_currentFrame > PEEK_DURATION) {
240void ObjectChoropu::calcJumping() {
241 constexpr f32 JUMP_LINEAR_COEFFICIENT = 65.0f;
242 constexpr f32 JUMP_QUADRATIC_COEFFICIENT = 2.7f;
244 if (!m_isStationary) {
245 m_groundLength = std::max(0.0f, m_groundLength - m_railInterpolator->speed());
250 f32 pos = JUMP_LINEAR_COEFFICIENT *
static_cast<f32
>(m_currentFrame) -
251 static_cast<f32
>(m_currentFrame) * 0.5f * JUMP_QUADRATIC_COEFFICIENT *
252 static_cast<f32
>(m_currentFrame);
258 m_pos.y = pos + (m_isStationary ? m_transMat.base(3).y : m_railInterpolator->curPos().y);
259 m_flags.setBit(eFlags::Position);
263void ObjectChoropu::calcGround() {
264 m_groundLength += m_railInterpolator->getCurrVel();
265 if (m_groundLength > MAX_GROUND_LEN) {
266 m_groundLength = MAX_GROUND_LEN - 1.0f;
273void ObjectChoropu::calcGroundObjs() {
275 std::min(
static_cast<size_t>(m_groundLength / m_groundHeight) + 1, m_groundObjs.size());
277 for (
auto *&obj : m_groundObjs) {
278 obj->enableCollision();
281 for (
size_t i = idx; i < m_groundObjs.size(); ++i) {
282 m_groundObjs[i]->disableCollision();
285 if (m_groundLength > RADIUS) {
286 f32 height = std::min(m_groundHeight, m_groundLength) - RADIUS;
287 m_groundObjs[0]->calcPosAndMat(height, calcInterpolatedPose(RADIUS + 0.5f * height));
290 for (
size_t i = 1; i < idx - 1; ++i) {
291 f32 height = 0.5f * m_groundHeight + m_groundHeight *
static_cast<f32
>(i);
292 m_groundObjs[i]->calcPosAndMat(m_groundHeight, calcInterpolatedPose(height));
295 f32 height = m_groundLength - m_groundHeight *
static_cast<f32
>(idx - 1);
297 calcInterpolatedPose(0.5f * height + m_groundHeight *
static_cast<f32
>(idx - 1));
298 m_groundObjs[idx - 1]->calcPosAndMat(height, mat);
305 m_railInterpolator->evalCubicBezierOnPath(t, curDir, curTanDir);
311const std::array<StateManagerEntry<ObjectChoropu>, 5> StateManager<ObjectChoropu>::STATE_ENTRIES = {
313 {0, &ObjectChoropu::enterDigging, &ObjectChoropu::calcDigging},
314 {1, &ObjectChoropu::enterPeeking, &ObjectChoropu::calcPeeking},
315 {2, &ObjectChoropu::enterStateStub, &ObjectChoropu::calcStateStub},
316 {3, &ObjectChoropu::enterJumping, &ObjectChoropu::calcJumping},
317 {4, &ObjectChoropu::enterStateStub, &ObjectChoropu::calcStateStub},
320StateManager<ObjectChoropu>::StateManager(ObjectChoropu *obj) {
321 constexpr size_t ENTRY_COUNT = 5;
324 m_entries = std::span{STATE_ENTRIES};
325 m_entryIds = std::span(
new u16[ENTRY_COUNT], ENTRY_COUNT);
328 for (
auto &
id : m_entryIds) {
332 for (
size_t i = 0; i < m_entryIds.size(); ++i) {
333 m_entryIds[STATE_ENTRIES[i].id] = i;
337StateManager<ObjectChoropu>::~StateManager() {
338 delete[] m_entryIds.data();
344 : ObjectCollidable(
"choropu_ground", pos, rot, scale) {
345 const auto &flowTable = ObjectDirector::Instance()->flowTable();
346 const auto *collisionSet =
347 flowTable.set(flowTable.slot(flowTable.getIdFromName(
"choropu_ground")));
348 ASSERT(collisionSet);
350 s16 height = parse<s16>(collisionSet->params.cylinder.height);
351 m_height = 2.0f * EGG::Mathf::abs(
static_cast<f32
>(height));
355ObjectChoropuGround::~ObjectChoropuGround() =
default;
358void ObjectChoropuGround::calcPosAndMat(f32 height,
const EGG::Matrix34f &mat) {
360 SetRotTangentHorizontal(matTemp, mat.
base(2), EGG::Vector3f::ey);
361 matTemp.
setBase(1, matTemp.
base(1) * (height / m_height));
363 m_flags.setBit(eFlags::Matrix);
364 m_transform = matTemp;
365 m_pos = matTemp.
base(3);
370 : ObjectCollidable(params) {}
373ObjectChoropuHoll::~ObjectChoropuHoll() =
default;
void setBase(size_t col, const Vector3f &base)
Sets one column of a matrix.
Vector3f base(size_t col) const
Get a particular column from a matrix.
The highest level abstraction for a kart.