| 1 | #include "Library/Controller/SpinInputAnalyzer.h" |
|---|---|
| 2 | |
| 3 | #include "Library/Math/MathUtil.h" |
| 4 | |
| 5 | namespace al { |
| 6 | |
| 7 | SpinInputAnalyzer::SpinInputAnalyzer() { |
| 8 | mChangeHistory.allocBufferAssert(size: 128, heap: nullptr); |
| 9 | mChangeHistory.fill(v: 0.0f); |
| 10 | |
| 11 | mInputHistory.allocBufferAssert(size: 128, heap: nullptr); |
| 12 | mInputHistory.fill(v: sead::Vector2f::zero); |
| 13 | } |
| 14 | |
| 15 | void SpinInputAnalyzer::update(const sead::Vector2f& controllerInput) { |
| 16 | s32 prevIndex = (mCurrentIndex - 1 + 128) % 128; |
| 17 | |
| 18 | mInputHistory[mCurrentIndex].set(controllerInput); |
| 19 | mChangeHistory[mCurrentIndex] = |
| 20 | mInputHistory[prevIndex].cross(t: mInputHistory[mCurrentIndex]) * 0.5f; |
| 21 | |
| 22 | sead::Vector2f currentInput = mInputHistory[mCurrentIndex]; |
| 23 | tryNormalizeOrZero(vec: ¤tInput); |
| 24 | |
| 25 | f32 sumOfChanges = 0.0f; |
| 26 | for (s32 i = 0; (u32)i < 70 && i < mNumUsableEntries; i++) { |
| 27 | s32 indexMinusI = (mCurrentIndex + (128 - i)) % 128; |
| 28 | |
| 29 | sead::Vector2f iPrevInput = mInputHistory[indexMinusI]; |
| 30 | tryNormalizeOrZero(vec: &iPrevInput); |
| 31 | if ((u32)i >= 30) { |
| 32 | if (sead::Mathf::abs(x: sumOfChanges) >= 5.0f && iPrevInput.dot(t: currentInput) > 0.7f) { |
| 33 | f32 spinDirection = sign(x: sumOfChanges); |
| 34 | mNumUsableEntries = 0; |
| 35 | mSpinDirection = (s32)spinDirection; |
| 36 | mSpinTimer = 31; // actually, it ends up being 30 due to subtracting one below |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | sumOfChanges += mChangeHistory[indexMinusI]; |
| 42 | } |
| 43 | |
| 44 | if (mSpinTimer >= 1) { |
| 45 | mSpinTimer--; |
| 46 | if (mSpinTimer == 0) |
| 47 | mSpinDirection = 0; |
| 48 | } |
| 49 | |
| 50 | mCurrentIndex = (mCurrentIndex + 1) % 128; |
| 51 | if (mNumUsableEntries <= 127) |
| 52 | mNumUsableEntries++; |
| 53 | } |
| 54 | |
| 55 | } // namespace al |
| 56 |