metaforce/Runtime/World/CScriptBallTrigger.cpp

96 lines
3.3 KiB
C++
Raw Permalink Normal View History

#include "Runtime/World/CScriptBallTrigger.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/GameGlobalObjects.hpp"
#include "Runtime/World/CMorphBall.hpp"
#include "Runtime/World/CPlayer.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2021-04-10 01:42:06 -07:00
namespace metaforce {
2018-12-07 21:30:43 -08:00
static zeus::CAABox calculate_ball_aabox() {
const float extent = 0.33f * g_tweakPlayer->GetPlayerBallHalfExtent();
2018-12-07 21:30:43 -08:00
return {-extent, extent};
}
CScriptBallTrigger::CScriptBallTrigger(TUniqueId uid, std::string_view name, const CEntityInfo& info,
2018-12-07 21:30:43 -08:00
const zeus::CVector3f& pos, const zeus::CVector3f& scale, bool active, float f1,
float f2, float f3, const zeus::CVector3f& vec, bool b2)
: CScriptTrigger(uid, name, info, pos, calculate_ball_aabox(), CDamageInfo(CWeaponMode::Power(), 0.f, 0.f, 0.f),
zeus::skZero3f, ETriggerFlags::DetectMorphedPlayer, active, false, false)
2018-12-07 21:30:43 -08:00
, x150_force(f1)
, x154_minAngle(f2)
, x158_maxDistance(f3)
, x168_25_stopPlayer(b2) {
if (vec.canBeNormalized()) {
2018-12-07 21:30:43 -08:00
x15c_forceAngle = vec.normalized();
}
}
2018-12-07 21:30:43 -08:00
void CScriptBallTrigger::Accept(IVisitor& visitor) { visitor.Visit(this); }
2018-12-07 21:30:43 -08:00
void CScriptBallTrigger::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
if (msg == EScriptObjectMessage::Deactivate && GetActive()) {
mgr.GetPlayer().GetMorphBall()->SetBallBoostState(CMorphBall::EBallBoostState::BoostAvailable);
x168_24_canApplyForce = false;
}
2018-12-07 21:30:43 -08:00
CScriptTrigger::AcceptScriptMsg(msg, uid, mgr);
}
2018-12-07 21:30:43 -08:00
void CScriptBallTrigger::Think(float dt, CStateManager& mgr) {
if (!GetActive()) {
2018-12-07 21:30:43 -08:00
return;
}
2018-12-07 21:30:43 -08:00
CScriptTrigger::Think(dt, mgr);
CPlayer& player = mgr.GetPlayer();
2018-12-07 21:30:43 -08:00
if (player.GetMorphballTransitionState() != CPlayer::EPlayerMorphBallState::Morphed) {
x168_24_canApplyForce = false;
return;
}
2018-12-07 21:30:43 -08:00
const float ballRadius = player.GetMorphBall()->GetBallRadius();
const zeus::CVector3f radiusPosDif =
GetTranslation() - (player.GetTranslation() + zeus::CVector3f{0.f, 0.f, ballRadius});
2018-12-07 21:30:43 -08:00
const float distance = radiusPosDif.magnitude();
2018-12-07 21:30:43 -08:00
if (!x168_24_canApplyForce) {
if (distance < ballRadius) {
2018-12-07 21:30:43 -08:00
x168_24_canApplyForce = true;
} else {
const zeus::CVector3f offset = radiusPosDif.normalized();
2018-12-07 21:30:43 -08:00
if (std::cos(zeus::degToRad(x154_minAngle)) < (-offset).dot(x15c_forceAngle) && distance < x158_maxDistance) {
const float force = zeus::min((1.f / dt * distance), x150_force * (x158_maxDistance / (distance * distance)));
player.ApplyForceWR(force * (player.GetMass() * offset), zeus::CAxisAngle());
2018-12-07 21:30:43 -08:00
}
}
2018-12-07 21:30:43 -08:00
}
2018-12-07 21:30:43 -08:00
if (x148_28_playerTriggerProc) {
const zeus::CVector3f offset = GetTranslation() - zeus::CVector3f(0.f, 0.f, ballRadius);
if (x168_25_stopPlayer) {
2018-12-07 21:30:43 -08:00
player.Stop();
}
2018-12-07 21:30:43 -08:00
player.MoveToWR(offset, dt);
}
}
2018-12-07 21:30:43 -08:00
void CScriptBallTrigger::InhabitantAdded(CActor& act, CStateManager& /*mgr*/) {
if (const TCastToPtr<CPlayer> player = act) {
2018-12-07 21:30:43 -08:00
player->GetMorphBall()->SetBallBoostState(CMorphBall::EBallBoostState::BoostDisabled);
}
}
2018-12-07 21:30:43 -08:00
void CScriptBallTrigger::InhabitantExited(CActor& act, CStateManager&) {
if (const TCastToPtr<CPlayer> player = act) {
2018-12-07 21:30:43 -08:00
player->GetMorphBall()->SetBallBoostState(CMorphBall::EBallBoostState::BoostAvailable);
x168_24_canApplyForce = false;
}
}
2021-04-10 01:42:06 -07:00
} // namespace metaforce