SMG-Decomp
A decompilation of Super Mario Galaxy 1
Loading...
Searching...
No Matches
KeySwitch.cpp
1#include "Game/MapObj/KeySwitch.hpp"
2#include "JSystem/JMath/JMath.hpp"
3
4KeySwitch::~KeySwitch() {
5
6}
7
8KeySwitch::KeySwitch(const char *pName) : LiveActor(pName) {
9 mCameraInfo = 0;
10 mCurDemoFrame = -1;
11}
12
13void KeySwitch::init(const JMapInfoIter &rIter) {
14 MR::initDefaultPos(this, rIter);
15 initModelManagerWithAnm("KeySwitch", 0, false);
16 MR::connectToSceneMapObjDecoration(this);
17 initHitSensor(1);
18 TVec3f box;
19 box.x = 0.0f;
20 box.y = 105.0f;
21 box.z = 0.0f;
22 MR::addHitSensorMapObjSimple(this, "body", 8, 105.0f, box);
23 initBinder(105.0f, 105.0f, 0);
24 initEffectKeeper(0, 0, false);
25 initSound(4, false);
26 MR::initShadowVolumeCylinder(this, 90.0f);
27
28 if (MR::isValidInfo(rIter)) {
29 MR::initActorCamera(this, rIter, &mCameraInfo);
30 }
31
32 bool usesSwitch = false;
33
34 if (MR::isValidInfo(rIter)) {
35 bool uses = MR::useStageSwitchReadAppear(this, rIter);
36 if (uses) {
37 usesSwitch = true;
38 MR::syncStageSwitchAppear(this);
39 }
40
41 MR::needStageSwitchWriteA(this, rIter);
42 }
43
44 if (usesSwitch || !MR::isValidInfo(rIter)) {
45 initNerve(&NrvKeySwitch::KeySwitchNrvDemoStart::sInstance);
46
47 }
48 else {
49 initNerve(&NrvKeySwitch::KeySwitchNrvWait::sInstance);
50 }
51
52 if (usesSwitch || !MR::isValidInfo(rIter)) {
53 makeActorDead();
54 }
55 else {
56 makeActorAppeared();
57 }
58}
59
60void KeySwitch::initKeySwitchByOwner(const JMapInfoIter &rIter) {
62 MR::needStageSwitchWriteA(this, rIter);
63 MR::initActorCamera(this, rIter, &mCameraInfo);
64}
65
66void KeySwitch::appearKeySwitch(const TVec3f &rVec) {
67 MR::resetPosition(this, rVec);
68 MR::onCalcGravity(this);
69 MR::invalidateClipping(this);
70 appear();
71}
72
73void KeySwitch::exeDemoStart() {
74 if (!mCameraInfo) {
75 setNerve(&NrvKeySwitch::KeySwitchNrvAppear::sInstance);
76 return;
77 }
78
79 if (MR::tryStartDemoWithoutCinemaFrame(this, cDemoName)) {
80 MR::startActorCameraTargetSelf(this, mCameraInfo, -1);
81 mCurDemoFrame = 0;
82 setNerve(&NrvKeySwitch::KeySwitchNrvAppear::sInstance);
83 }
84}
85
86void KeySwitch::exeAppear() {
87 if (MR::isFirstStep(this)) {
88 MR::startBck(this, "Rotation", 0);
89 mVelocity.scale(-40.0f, mGravity);
90 MR::invalidateClipping(this);
91 MR::startSound(this, "SE_OJ_KEY_SWITCH_APPEAR", -1, -1);
92 }
93
94 MR::setBckRate(this, MR::calcNerveValue(this, 0xB4, 3.0f, 1.5f));
95 MR::addVelocityToGravity(this, 2.0f);
96
97 bool val = false;
98
99 if (MR::isBindedGround(this)) {
100 if (PSVECMag(mVelocity.toCVec()) >= 10.0f) {
101 s32 mag = PSVECMag(mVelocity.toCVec());
102 mag *= 2;
103 if (mag > 0x64) {
104 mag = 0x64;
105 }
106
107 MR::startSound(this, "SE_OJ_KEY_SWITCH_BOUND", mag, -1);
108 TVec3f neg;
109 neg.negateInline_2(mGravity);
110 MR::calcReboundVelocity(&mVelocity, neg, 0.60000002f, 0.69999999f);
111 }
112 else {
113 val = true;
114 }
115 }
116
117 if (!tryAvoid()) {
118 if (val) {
119 MR::validateClipping(this);
120 setNerve(&NrvKeySwitch::KeySwitchNrvWait::sInstance);
121 }
122 }
123}
124
125void KeySwitch::exeWait() {
126 if (MR::isFirstStep(this)) {
127 MR::tryStartBck(this, "Rotation", 0);
128 MR::setBckRate(this, 1.0f);
129 mVelocity.x = 0.0f;
130 mVelocity.y = 0.0f;
131 mVelocity.z = 0.0f;
132 MR::offCalcGravity(this);
133 }
134}
135
136void KeySwitch::appear() {
137 LiveActor::appear();
138}
139
140void KeySwitch::kill() {
141 MR::onSwitchA(this);
142 MR::startSystemSE("SE_SY_READ_RIDDLE_S", -1, -1);
143 MR::emitEffect(this, "Get");
144 LiveActor::kill();
145}
146
148 TVec3f front;
149 MR::calcFrontVec(&front, this);
150 TPos3f mtx;
151 MR::calcMtxFromGravityAndZAxis(&mtx, this, mGravity, front);
152 MR::setBaseTRMtx(this, mtx);
153}
154
155void KeySwitch::control() {
156 if (mCurDemoFrame != -1 && mCameraInfo) {
157 if (mCurDemoFrame >= 0x28) {
158 MR::endActorCamera(this, mCameraInfo, false, -1);
159 MR::endDemo(this, cDemoName);
160 mCameraInfo = 0;
161 mCurDemoFrame = -1;
162 }
163 else {
164 mCurDemoFrame = mCurDemoFrame + 1;
165 }
166 }
167}
168
169bool KeySwitch::receiveMsgPlayerAttack(u32 msg, HitSensor *a1, HitSensor *a2) {
170 return MR::isMsgStarPieceReflect(msg);
171}
172
173bool KeySwitch::receiveOtherMsg(u32 msg, HitSensor *a1, HitSensor *a2) {
174 if (MR::isMsgStarPieceReflect(msg)) {
175 return 1;
176 }
177
178 if (isNerve(&NrvKeySwitch::KeySwitchNrvAppear::sInstance) && MR::isLessEqualStep(this, 0x3C)) {
179 return 0;
180 }
181
182 if (MR::isMsgItemGet(msg)) {
183 MR::startSound(this, "SE_OJ_KEY_SWITCH_GET", -1, -1);
184 MR::tryRumblePadMiddle(this, 0);
185 kill();
186 return 1;
187 }
188
189 return 0;
190}
191
192bool KeySwitch::tryAvoid() {
193 LiveActor* sensorActor;
194 HitSensor* sensor = 0;
195
196 if (MR::isBindedGround(this)) {
197 sensor = MR::getGroundSensor(this);
198 }
199 else if (MR::isBindedWall(this)) {
200 sensor = MR::getWallSensor(this);
201 }
202
203 if (!sensor) {
204 return false;
205 }
206
207 if (sensor->mSensorType != 0x58) {
208 return false;
209 }
210
211 sensorActor = sensor->mActor;
212 TVec3f up;
213 MR::calcUpVec(&up, sensorActor);
214 TVec3f thing;
215 thing.subInline2(mPosition, sensorActor->mPosition);
216 TVec3f stack_8;
217 JMAVECScaleAdd(up.toCVec(), thing.toCVec(), stack_8.toVec(), -up.dot(thing));
218
219 if (MR::normalizeOrZero(&stack_8)) {
220 MR::calcFrontVec(&stack_8, sensorActor);
221 }
222
223 mVelocity.scale(10.0f, stack_8);
224 return true;
225}
226
227namespace NrvKeySwitch {
228 INIT_NERVE(KeySwitchNrvDemoStart);
229 INIT_NERVE(KeySwitchNrvAppear);
230 INIT_NERVE(KeySwitchNrvWait);
231
232 void KeySwitchNrvWait::execute(Spine *pSpine) const {
233 KeySwitch* key = reinterpret_cast<KeySwitch*>(pSpine->mExecutor);
234 key->exeWait();
235 }
236
237 void KeySwitchNrvAppear::execute(Spine *pSpine) const {
238 KeySwitch* key = reinterpret_cast<KeySwitch*>(pSpine->mExecutor);
239 key->exeAppear();
240 }
241
242 void KeySwitchNrvDemoStart::execute(Spine *pSpine) const {
243 KeySwitch* key = reinterpret_cast<KeySwitch*>(pSpine->mExecutor);
244 key->exeDemoStart();
245 }
246};
virtual void init(const JMapInfoIter &)
Intializes the NameObj and can set various settings and construct necessary classes.
Definition KeySwitch.cpp:13
virtual void calcAndSetBaseMtx()
Calculates and sets the base matrix of the actor.
The basis of a drawable actor that can contain states (see: Nerve)
Definition LiveActor.hpp:24
TVec3f mPosition
3D vector of the actor's position.
Definition LiveActor.hpp:95
TVec3f mVelocity
3D vector of the actor's velocity.
Definition LiveActor.hpp:98
TVec3f mGravity
3D vector of the actor's gravity.
Definition LiveActor.hpp:99
void initWithoutIter()
Initializes a NameObj without a JMapInfoIter instance.
Definition NameObj.cpp:41
Definition Spine.hpp:9