metaforce/Runtime/MP1/World/CFlickerBat.cpp

253 lines
9.0 KiB
C++
Raw Permalink Normal View History

#include "Runtime/MP1/World/CFlickerBat.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/Camera/CCameraManager.hpp"
#include "Runtime/Camera/CGameCamera.hpp"
#include "Runtime/Collision/CGameCollision.hpp"
#include "Runtime/World/CPlayer.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2018-10-11 22:57:05 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce::MP1 {
2018-10-11 22:57:05 +00:00
CFlickerBat::CFlickerBat(TUniqueId uid, std::string_view name, CPatterned::EFlavorType flavor, const CEntityInfo& info,
const zeus::CTransform& xf, CModelData&& mData, const CPatternedInfo& pInfo,
EColliderType colType, bool startsHidden, const CActorParameters& actParms,
bool enableLineOfSight)
2018-12-08 05:30:43 +00:00
: CPatterned(ECharacter::FlickerBat, uid, name, flavor, info, xf, std::move(mData), pInfo, EMovementType::Flyer,
colType, EBodyType::Pitchable, actParms, EKnockBackVariant::Small)
, x574_state(EFlickerBatState(startsHidden))
, x580_27_enableLOSCheck(enableLineOfSight) {
SetupPlayerCollision(startsHidden);
2018-12-08 05:30:43 +00:00
x3d8_xDamageThreshold = 0.f;
x402_27_noXrayModel = false;
2018-10-11 22:57:05 +00:00
}
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
void CFlickerBat::Accept(IVisitor& visitor) { visitor.Visit(this); }
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
void CFlickerBat::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
CPatterned::AcceptScriptMsg(msg, uid, mgr);
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
if (msg == EScriptObjectMessage::Registered) {
RemoveMaterial(EMaterialTypes::Solid, mgr);
2018-12-17 05:40:27 +00:00
mgr.GetActiveFlickerBats().push_back(GetUniqueId());
2018-12-08 05:30:43 +00:00
x450_bodyController->Activate(mgr);
x450_bodyController->BodyStateInfo().SetMaximumPitch(zeus::degToRad(60.f));
} else if (msg == EScriptObjectMessage::Deleted) {
2018-12-17 05:40:27 +00:00
mgr.GetActiveFlickerBats().remove(GetUniqueId());
2018-12-08 05:30:43 +00:00
}
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
void CFlickerBat::Think(float dt, CStateManager& mgr) {
if (!GetActive())
return;
x402_29_drawParticles = mgr.GetPlayerState()->GetActiveVisor(mgr) != CPlayerState::EPlayerVisor::XRay;
2019-02-08 07:56:54 +00:00
if (GetFlickerBatState() == EFlickerBatState::FadeIn || GetFlickerBatState() == EFlickerBatState::FadeOut) {
x578_fadeRemTime -= dt;
if (x578_fadeRemTime <= 0.f) {
if (GetFlickerBatState() == EFlickerBatState::FadeIn)
SetFlickerBatState(EFlickerBatState::Visible, mgr);
2018-12-08 05:30:43 +00:00
else
2019-02-08 07:56:54 +00:00
SetFlickerBatState(EFlickerBatState::Hidden, mgr);
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
}
2018-11-14 23:00:08 +00:00
2018-12-17 05:40:27 +00:00
bool inXray = mgr.GetPlayerState()->GetCurrentVisor() == CPlayerState::EPlayerVisor::XRay;
if (inXray != x580_24_wasInXray) {
if (inXray) {
2019-02-08 07:56:54 +00:00
if (GetFlickerBatState() == EFlickerBatState::Hidden) {
2018-12-17 05:40:27 +00:00
AddMaterial(EMaterialTypes::Target, EMaterialTypes::Orbit, mgr);
SetMuted(false);
}
CreateShadow(false);
} else {
2019-02-08 07:56:54 +00:00
if (GetFlickerBatState() == EFlickerBatState::Hidden) {
2018-12-17 05:40:27 +00:00
RemoveMaterial(EMaterialTypes::Target, EMaterialTypes::Orbit, mgr);
SetMuted(true);
}
CreateShadow(true);
}
x580_24_wasInXray = inXray;
}
float alpha = 0.f;
if (!x580_24_wasInXray) {
2019-02-08 07:56:54 +00:00
if (GetFlickerBatState() == EFlickerBatState::Visible)
2018-12-17 05:40:27 +00:00
alpha = 1.f;
2019-02-08 07:56:54 +00:00
else if (GetFlickerBatState() == EFlickerBatState::FadeIn || GetFlickerBatState() == EFlickerBatState::FadeOut) {
alpha = x578_fadeRemTime * x57c_ooFadeDur;
if (GetFlickerBatState() == EFlickerBatState::FadeIn)
2018-12-17 05:40:27 +00:00
alpha = 1.f - alpha;
}
} else
alpha = 1.f;
x42c_color.a() = alpha;
x94_simpleShadow->SetUserAlpha(alpha);
xe7_31_targetable = (mgr.GetPlayerState()->GetCurrentVisor() == CPlayerState::EPlayerVisor::XRay
? true
: x574_state == EFlickerBatState::Visible || x574_state == EFlickerBatState::FadeIn);
2018-12-08 05:30:43 +00:00
CPatterned::Think(dt, mgr);
2018-11-14 23:00:08 +00:00
}
void CFlickerBat::Render(CStateManager& mgr) {
2019-02-08 07:56:54 +00:00
if (!x580_24_wasInXray && x580_26_inLOS &&
(GetFlickerBatState() == EFlickerBatState::FadeIn || GetFlickerBatState() == EFlickerBatState::FadeOut)) {
2018-12-17 05:40:27 +00:00
float strength = 0.f;
2019-02-08 07:56:54 +00:00
if (GetFlickerBatState() == EFlickerBatState::FadeIn) {
strength = 4.f * (x578_fadeRemTime - .75f);
} else if (GetFlickerBatState() == EFlickerBatState::FadeOut) {
strength = 4.f * x578_fadeRemTime;
2018-12-17 05:40:27 +00:00
}
if (strength > 0.f && strength < 1.f)
mgr.DrawSpaceWarp(GetTranslation(), 0.3f * std::sin(M_PIF * strength));
}
2018-11-14 23:00:08 +00:00
2019-02-08 07:56:54 +00:00
if (x580_26_inLOS) {
2018-12-17 05:40:27 +00:00
mgr.SetupFogForAreaNonCurrent(GetAreaIdAlways());
CPatterned::Render(mgr);
mgr.SetupFogForArea(GetAreaIdAlways());
} else
CPatterned::Render(mgr);
}
void CFlickerBat::Touch(CActor& act, CStateManager& mgr) {
if (TCastToPtr<CPlayer> pl = act) {
if (x420_curDamageRemTime <= 0.f) {
mgr.ApplyDamage(GetUniqueId(), pl->GetUniqueId(), GetUniqueId(), GetContactDamage(),
CMaterialFilter::MakeIncludeExclude({EMaterialTypes::Solid}, {}), {});
x420_curDamageRemTime = x424_damageWaitTime;
}
}
CPatterned::Touch(act, mgr);
}
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
void CFlickerBat::DoUserAnimEvent(CStateManager& mgr, const CInt32POINode& node, EUserEventType type, float dt) {
2018-12-17 05:40:27 +00:00
if (type == EUserEventType::FadeIn)
ToggleVisible(mgr);
else
CPatterned::DoUserAnimEvent(mgr, node, type, dt);
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
void CFlickerBat::Death(CStateManager& mgr, const zeus::CVector3f& direction, EScriptObjectState state) {
2019-02-08 07:56:54 +00:00
SetFlickerBatState(EFlickerBatState::Visible, mgr);
SetMuted(false);
2018-12-08 05:30:43 +00:00
CPatterned::Death(mgr, direction, state);
2018-11-14 23:00:08 +00:00
}
2019-04-07 05:14:48 +00:00
bool CFlickerBat::CanBeShot(const CStateManager& mgr, int) {
2019-02-08 07:56:54 +00:00
return (GetFlickerBatState() == EFlickerBatState::Visible || GetFlickerBatState() == EFlickerBatState::FadeIn ||
2018-12-08 05:30:43 +00:00
mgr.GetPlayerState()->GetCurrentVisor() == CPlayerState::EPlayerVisor::XRay);
2018-11-14 23:00:08 +00:00
}
void CFlickerBat::Patrol(CStateManager& mgr, EStateMsg state, float dt) {
2018-12-17 05:40:27 +00:00
CPatterned::Patrol(mgr, state, dt);
x450_bodyController->GetCommandMgr().DeliverFaceVector((x2e0_destPos - GetTranslation()).normalized());
}
2018-11-14 23:00:08 +00:00
2018-12-17 05:40:27 +00:00
void CFlickerBat::Attack(CStateManager&, EStateMsg msg, float) {
if (msg == EStateMsg::Update) {
x450_bodyController->GetCommandMgr().DeliverCmd(
CBCLocomotionCmd((x2e0_destPos - GetTranslation()).normalized(), {}, 1.f));
2018-12-17 05:40:27 +00:00
}
}
2018-11-14 23:00:08 +00:00
2018-12-17 05:40:27 +00:00
void CFlickerBat::Shuffle(CStateManager& mgr, EStateMsg msg, float) {
if (msg == EStateMsg::Activate) {
CRandom16* rnd = mgr.GetActiveRandom();
SetDestPos(GetTranslation() +
zeus::CVector3f(100.f * rnd->Float() - 50.f, 100.f * rnd->Float() - 50.f, 100.f * rnd->Float() - 50.f));
} else if (msg == EStateMsg::Update) {
ApproachDest(mgr);
}
}
2018-11-14 23:00:08 +00:00
2018-12-17 05:40:27 +00:00
void CFlickerBat::Taunt(CStateManager& mgr, EStateMsg msg, float) {
if (msg == EStateMsg::Activate) {
NotifyNeighbors(mgr);
x400_24_hitByPlayerProjectile = false;
}
}
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
bool CFlickerBat::InPosition(CStateManager& mgr, float arg) {
return GetTransform().frontVector().dot(mgr.GetPlayer().GetAimPosition(mgr, 0.f) - GetTranslation()) > 0.f;
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
bool CFlickerBat::HearShot(CStateManager&, float) {
if (x580_25_heardShot) {
x580_25_heardShot = false;
return true;
}
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
return false;
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
void CFlickerBat::SetFlickerBatState(EFlickerBatState state, CStateManager& mgr) {
if (state == x574_state)
return;
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
FlickerBatStateChanged(state, mgr);
x574_state = state;
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
void CFlickerBat::FlickerBatStateChanged(EFlickerBatState state, CStateManager& mgr) {
2019-02-08 07:56:54 +00:00
if (state == EFlickerBatState::Visible) {
2018-12-08 05:30:43 +00:00
if (mgr.GetPlayerState()->GetCurrentVisor() != CPlayerState::EPlayerVisor::XRay)
CreateShadow(true);
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
AddMaterial(EMaterialTypes::Target, mgr);
2019-02-08 07:56:54 +00:00
} else if (state == EFlickerBatState::Hidden) {
2018-12-08 05:30:43 +00:00
SetMuted(true);
RemoveMaterial(EMaterialTypes::Target, mgr);
2019-02-08 07:56:54 +00:00
} else if (state == EFlickerBatState::FadeIn) {
2018-12-08 05:30:43 +00:00
if (mgr.GetPlayerState()->GetCurrentVisor() != CPlayerState::EPlayerVisor::XRay) {
CreateShadow(true);
SetMuted(false);
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
CheckStaticIntersection(mgr);
SetupPlayerCollision(true);
2019-02-08 07:56:54 +00:00
} else if (state == EFlickerBatState::FadeOut) {
2018-12-08 05:30:43 +00:00
if (mgr.GetPlayerState()->GetCurrentVisor() != CPlayerState::EPlayerVisor::XRay)
CreateShadow(true);
CheckStaticIntersection(mgr);
SetupPlayerCollision(false);
}
2018-11-14 23:00:08 +00:00
}
2018-12-08 05:30:43 +00:00
void CFlickerBat::CheckStaticIntersection(CStateManager& mgr) {
2019-02-08 07:56:54 +00:00
if (!x580_27_enableLOSCheck) {
x580_26_inLOS = false;
2018-12-08 05:30:43 +00:00
return;
}
2018-11-14 23:00:08 +00:00
2018-12-08 05:30:43 +00:00
zeus::CVector3f camPos = mgr.GetCameraManager()->GetCurrentCamera(mgr)->GetTranslation();
zeus::CVector3f diff = GetBoundingBox().center() - camPos;
float mag = diff.magnitude();
diff *= zeus::CVector3f(1.f / mag);
2019-02-08 07:56:54 +00:00
x580_26_inLOS = CGameCollision::RayStaticIntersectionBool(mgr, camPos, diff, mag,
2021-06-07 19:29:18 +00:00
CMaterialFilter::MakeExclude({EMaterialTypes::SeeThrough}));
2018-11-14 23:00:08 +00:00
}
2018-12-17 05:40:27 +00:00
void CFlickerBat::NotifyNeighbors(CStateManager& mgr) {
for (TUniqueId uid : mgr.GetActiveFlickerBats()) {
if (CFlickerBat* flick = CPatterned::CastTo<CFlickerBat>(mgr.ObjectById(uid)))
if ((GetTranslation() - flick->GetTranslation()).magnitude() < 100.f)
flick->SetHeardShot(true);
}
}
void CFlickerBat::ToggleVisible(CStateManager& mgr) {
2019-02-08 07:56:54 +00:00
if (GetFlickerBatState() == EFlickerBatState::Visible || GetFlickerBatState() == EFlickerBatState::FadeIn)
SetFlickerBatState(EFlickerBatState::FadeOut, mgr);
2018-12-17 05:40:27 +00:00
else
2019-02-08 07:56:54 +00:00
SetFlickerBatState(EFlickerBatState::FadeIn, mgr);
2018-12-17 05:40:27 +00:00
2019-02-08 07:56:54 +00:00
x578_fadeRemTime = 1.f;
x57c_ooFadeDur = 1.f / x578_fadeRemTime;
2018-12-17 05:40:27 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce::MP1