1#include "ObjectChoropu.hh"
3#include "game/field/ObjectDirector.hh"
4#include "game/field/RailManager.hh"
6#include "game/kart/Reaction.hh"
8#include "game/system/RaceManager.hh"
13ObjectChoropu::ObjectChoropu(
const System::MapdataGeoObj ¶ms)
15 constexpr f32 MAX_SPEED = 20.0f;
17 m_startFrameOffset =
static_cast<s16
>(params.setting(1));
18 m_idleDuration = params.setting(0);
19 m_isStationary = strcmp(getName(),
"choropu") != 0;
21 s16 railIdx = params.pathId();
23 auto *rail = RailManager::Instance()->rail(railIdx);
24 rail->checkSphereFull();
28 if (!m_isStationary) {
29 const auto &flowTable = ObjectDirector::Instance()->flowTable();
30 const auto *collisionSet =
31 flowTable.set(flowTable.slot(flowTable.getIdFromName(
"choropu_ground")));
34 s16 height = parse<s16>(collisionSet->params.cylinder.height);
35 size_t groundCount =
static_cast<size_t>(MAX_GROUND_LEN / EGG::Mathf::abs(height * 2)) + 1;
36 m_groundObjs = owning_span<ObjectChoropuGround *>(groundCount);
38 for (
auto *&obj : m_groundObjs) {
39 obj = EGG::egg_new<ObjectChoropuGround>(pos(), rot(), scale());
41 obj->resize(RADIUS, MAX_SPEED);
44 m_groundHeight = m_groundObjs.front()->height();
47 m_objHoll = EGG::egg_new<ObjectChoropuHoll>(params);
52ObjectChoropu::~ObjectChoropu() =
default;
55void ObjectChoropu::init() {
59 m_objHoll->setScale(EGG::Vector3f(1.0f, m_objHoll->scale().y, 1.0f));
61 m_groundLength = 0.0f;
64 m_transMat = transform();
66 if (m_mapObj->pathId() == -1) {
70 m_railInterpolator->init(0.0f, 0);
71 m_railInterpolator->setPerPointVelocities(
true);
73 m_railPos = m_railInterpolator->curPos();
77 m_objHoll->disableCollision();
84void ObjectChoropu::calc() {
85 constexpr u32 START_DELAY = 300;
88 u32 t = System::RaceManager::Instance()->timer();
89 if (t < m_startFrameOffset + START_DELAY) {
93 if (!m_isStationary) {
94 if (m_mapObj->pathId() == -1) {
100 m_railPos = m_railInterpolator->curPos();
103 StateManager::calc();
105 m_objHoll->setScale(EGG::Vector3f(1.0f, m_objHoll->scale().y, 1.0f));
109Kart::Reaction ObjectChoropu::onCollision(Kart::KartObject * ,
110 Kart::Reaction reactionOnKart, Kart::Reaction ,
112 return m_currentStateId == 1 ? Kart::Reaction::SmallBump : reactionOnKart;
115void ObjectChoropu::enterStateStub() {}
118void ObjectChoropu::enterDigging() {
119 if (m_isStationary) {
122 for (
auto *&obj : m_groundObjs) {
123 obj->enableCollision();
127 m_objHoll->disableCollision();
128 m_groundLength = 0.0f;
133void ObjectChoropu::enterPeeking() {
134 if (m_isStationary) {
135 setPos(m_transMat.base(3));
136 setRot(EGG::Vector3f(rot().x, rot().y, 0.0f));
141 setRot(EGG::Vector3f(rot().x, 0.0f, 0.0f));
144 const auto &curTanDir = m_railInterpolator->curTangentDir();
145 s16 curPointIdx = m_railInterpolator->curPointIdx();
147 SetRotTangentHorizontal(mat, m_railInterpolator->floorNrm(curPointIdx), curTanDir);
148 mat.setBase(3, m_railInterpolator->curPos());
150 m_objHoll->setTransform(mat);
151 m_objHoll->enableCollision();
156void ObjectChoropu::enterJumping() {
159 setPos(m_isStationary ? m_transMat.base(3) : m_railPos);
160 setRot(EGG::Vector3f(rot().x, rot().y, 0.0f));
163void ObjectChoropu::calcStateStub() {}
166void ObjectChoropu::calcDigging() {
167 if (m_isStationary) {
168 if (m_currentFrame > m_idleDuration) {
175 setPos(m_railInterpolator->curPos());
177 if (m_railInterpolator->calc() == RailInterpolator::Status::SegmentEnd) {
178 if (m_railInterpolator->curPoint().setting[1] == 1) {
184 bool skipGroundCalc =
false;
186 if (m_railInterpolator->nextPoint().setting[1] == 1) {
187 f32 invT = 1.0f - m_railInterpolator->segmentT();
188 if (invT * m_railInterpolator->getCurrSegmentLength() < 250.0f) {
189 skipGroundCalc =
true;
193 if (!skipGroundCalc) {
200void ObjectChoropu::calcPeeking() {
201 constexpr s16 PEEK_DURATION = 40;
202 constexpr s16 STATE_DURATION = 100;
204 if (!m_isStationary) {
205 m_groundLength = std::max(0.0f, m_groundLength - m_railInterpolator->speed());
210 if (m_currentFrame > STATE_DURATION) {
214 if (m_currentFrame > PEEK_DURATION) {
220void ObjectChoropu::calcJumping() {
221 constexpr f32 JUMP_LINEAR_COEFFICIENT = 65.0f;
222 constexpr f32 JUMP_QUADRATIC_COEFFICIENT = 2.7f;
224 if (!m_isStationary) {
225 m_groundLength = std::max(0.0f, m_groundLength - m_railInterpolator->speed());
230 f32 posY = JUMP_LINEAR_COEFFICIENT *
static_cast<f32
>(m_currentFrame) -
231 static_cast<f32
>(m_currentFrame) * 0.5f * JUMP_QUADRATIC_COEFFICIENT *
232 static_cast<f32
>(m_currentFrame);
238 posY += (m_isStationary ? m_transMat.base(3).y : m_railInterpolator->curPos().y);
239 setPos(EGG::Vector3f(pos().x, posY, pos().z));
243void ObjectChoropu::calcGround() {
244 m_groundLength += m_railInterpolator->getCurrVel();
245 if (m_groundLength > MAX_GROUND_LEN) {
246 m_groundLength = MAX_GROUND_LEN - 1.0f;
253void ObjectChoropu::calcGroundObjs() {
255 std::min(
static_cast<size_t>(m_groundLength / m_groundHeight) + 1, m_groundObjs.size());
257 for (
auto *&obj : m_groundObjs) {
258 obj->enableCollision();
261 for (
size_t i = idx; i < m_groundObjs.size(); ++i) {
262 m_groundObjs[i]->disableCollision();
265 if (m_groundLength > RADIUS) {
266 f32 height = std::min(m_groundHeight, m_groundLength) - RADIUS;
267 m_groundObjs[0]->calcPosAndMat(height, calcInterpolatedPose(RADIUS + 0.5f * height));
270 for (
size_t i = 1; i < idx - 1; ++i) {
271 f32 height = 0.5f * m_groundHeight + m_groundHeight *
static_cast<f32
>(i);
272 m_groundObjs[i]->calcPosAndMat(m_groundHeight, calcInterpolatedPose(height));
275 f32 height = m_groundLength - m_groundHeight *
static_cast<f32
>(idx - 1);
277 calcInterpolatedPose(0.5f * height + m_groundHeight *
static_cast<f32
>(idx - 1));
278 m_groundObjs[idx - 1]->calcPosAndMat(height, mat);
282EGG::Matrix34f ObjectChoropu::calcInterpolatedPose(f32 t)
const {
283 EGG::Vector3f curDir;
284 EGG::Vector3f curTanDir;
285 m_railInterpolator->evalCubicBezierOnPath(t, curDir, curTanDir);
286 EGG::Matrix34f mat = OrthonormalBasis(curTanDir);
287 mat.setBase(3, curDir);
292ObjectChoropuGround::ObjectChoropuGround(
const EGG::Vector3f &pos,
const EGG::Vector3f &rot,
293 const EGG::Vector3f &scale)
294 : ObjectCollidable(
"choropu_ground", pos, rot, scale) {
295 const auto &flowTable = ObjectDirector::Instance()->flowTable();
296 const auto *collisionSet =
297 flowTable.set(flowTable.slot(flowTable.getIdFromName(
"choropu_ground")));
298 ASSERT(collisionSet);
300 s16 height = parse<s16>(collisionSet->params.cylinder.height);
301 m_height = 2.0f * EGG::Mathf::abs(
static_cast<f32
>(height));
305ObjectChoropuGround::~ObjectChoropuGround() =
default;
308void ObjectChoropuGround::calcPosAndMat(f32 height,
const EGG::Matrix34f &mat) {
309 EGG::Matrix34f matTemp;
310 SetRotTangentHorizontal(matTemp, mat.base(2), EGG::Vector3f::ey);
311 matTemp.setBase(1, matTemp.base(1) * (height / m_height));
312 matTemp.setBase(3, mat.base(3));
313 setTransform(matTemp);
317ObjectChoropuHoll::ObjectChoropuHoll(
const System::MapdataGeoObj ¶ms)
318 : ObjectCollidable(params) {}
321ObjectChoropuHoll::~ObjectChoropuHoll() =
default;
Base class that represents different "states" for an object.