metaforce/Runtime/World/CScriptActorKeyframe.cpp

132 lines
4.8 KiB
C++
Raw Normal View History

#include "Runtime/World/CScriptActorKeyframe.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/World/CPatterned.hpp"
#include "Runtime/World/CScriptActor.hpp"
#include "Runtime/World/CScriptPlatform.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2016-11-20 21:53:15 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2017-11-13 06:19:18 +00:00
CScriptActorKeyframe::CScriptActorKeyframe(TUniqueId uid, std::string_view name, const CEntityInfo& info, s32 animId,
2018-11-11 03:27:54 +00:00
bool looping, float lifetime, bool isPassive, u32 fadeOut, bool active,
2018-05-17 00:04:07 +00:00
float totalPlayback)
: CEntity(uid, info, active, name)
, x34_animationId(animId)
2018-05-14 21:44:09 +00:00
, x38_initialLifetime(lifetime)
, x3c_playbackRate(totalPlayback)
, x40_lifetime(lifetime)
, x44_24_looping(looping)
, x44_25_isPassive(isPassive)
, x44_26_fadeOut(fadeOut != 0u)
, x44_27_timedLoop(fadeOut != 0u) {}
2016-11-20 21:53:15 +00:00
2018-12-08 05:30:43 +00:00
void CScriptActorKeyframe::Accept(IVisitor& visitor) { visitor.Visit(this); }
2018-05-14 21:44:09 +00:00
2018-12-08 05:30:43 +00:00
void CScriptActorKeyframe::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
if (msg == EScriptObjectMessage::Action) {
if (GetActive()) {
if (!x44_25_isPassive) {
for (const SConnection& conn : x20_conns) {
if (conn.x0_state != EScriptObjectState::Play || conn.x4_msg != EScriptObjectMessage::Play) {
2018-12-08 05:30:43 +00:00
continue;
}
2018-05-14 21:44:09 +00:00
const auto search = mgr.GetIdListForScript(conn.x8_objId);
for (auto it = search.first; it != search.second; ++it) {
2018-12-08 05:30:43 +00:00
UpdateEntity(it->second, mgr);
}
2018-05-14 21:44:09 +00:00
}
2018-12-08 05:30:43 +00:00
}
x44_28_playing = true;
SendScriptMsgs(EScriptObjectState::Play, mgr, EScriptObjectMessage::None);
2018-05-14 21:44:09 +00:00
}
2018-12-08 05:30:43 +00:00
} else if (msg == EScriptObjectMessage::InitializedInArea) {
if (x34_animationId == -1)
x34_animationId = 0;
}
2018-05-14 21:44:09 +00:00
2018-12-08 05:30:43 +00:00
CEntity::AcceptScriptMsg(msg, uid, mgr);
2018-05-14 21:44:09 +00:00
}
2016-11-20 21:53:15 +00:00
2018-12-08 05:30:43 +00:00
void CScriptActorKeyframe::Think(float dt, CStateManager& mgr) {
if (x44_25_isPassive || !x44_24_looping || !x44_27_timedLoop || !x44_28_playing || x40_lifetime <= 0.f) {
CEntity::Think(dt, mgr);
return;
}
2018-05-14 21:44:09 +00:00
2018-12-08 05:30:43 +00:00
x40_lifetime -= dt;
if (x40_lifetime > 0.f) {
CEntity::Think(dt, mgr);
return;
}
2018-05-14 21:44:09 +00:00
2018-12-08 05:30:43 +00:00
x44_28_playing = false;
for (const SConnection& conn : x20_conns) {
if (conn.x0_state != EScriptObjectState::Play || conn.x4_msg != EScriptObjectMessage::Play) {
2018-12-08 05:30:43 +00:00
continue;
}
2018-12-08 05:30:43 +00:00
CEntity* ent = mgr.ObjectById(mgr.GetIdForScript(conn.x8_objId));
if (const TCastToPtr<CScriptActor> act = ent) {
2018-12-08 05:30:43 +00:00
if (act->HasModelData() && act->GetModelData()->HasAnimData()) {
CAnimData* animData = act->GetModelData()->GetAnimationData();
if (animData->IsAdditiveAnimation(x34_animationId)) {
2018-12-08 05:30:43 +00:00
animData->DelAdditiveAnimation(x34_animationId);
}
2018-05-14 21:44:09 +00:00
if (animData->GetDefaultAnimation() == x34_animationId) {
2018-12-08 05:30:43 +00:00
animData->EnableLooping(false);
}
2018-12-08 05:30:43 +00:00
}
} else if (const TCastToPtr<CPatterned> ai = ent) {
CAnimData* animData = ai->GetModelData()->GetAnimationData();
2018-12-08 05:30:43 +00:00
if (animData->IsAdditiveAnimation(x34_animationId)) {
animData->DelAdditiveAnimation(x34_animationId);
} else if (ai->GetBodyController()->GetCurrentStateId() == pas::EAnimationState::Scripted &&
animData->GetDefaultAnimation() == x34_animationId) {
ai->GetBodyController()->GetCommandMgr().DeliverCmd(CBodyStateCmd(EBodyStateCmd::ExitState));
2018-12-08 05:30:43 +00:00
}
2018-05-14 21:44:09 +00:00
}
2018-12-08 05:30:43 +00:00
}
2018-05-14 21:44:09 +00:00
2018-12-08 05:30:43 +00:00
CEntity::Think(dt, mgr);
2016-11-20 21:53:15 +00:00
}
2018-12-08 05:30:43 +00:00
void CScriptActorKeyframe::UpdateEntity(TUniqueId uid, CStateManager& mgr) {
CEntity* ent = mgr.ObjectById(uid);
CActor* act = nullptr;
if (const TCastToPtr<CScriptActor> actor = ent) {
act = actor.GetPtr();
} else if (const TCastToPtr<CScriptPlatform> platform = ent) {
act = platform.GetPtr();
}
2018-05-14 21:44:09 +00:00
2018-12-08 05:30:43 +00:00
if (act) {
if (!act->GetActive()) {
2018-12-08 05:30:43 +00:00
mgr.SendScriptMsg(act, GetUniqueId(), EScriptObjectMessage::Activate);
}
act->SetDrawFlags({0, 0, 3, zeus::skWhite});
2018-12-08 05:30:43 +00:00
if (act->HasModelData() && act->GetModelData()->HasAnimData()) {
CAnimData* animData = act->GetModelData()->GetAnimationData();
2018-12-08 05:30:43 +00:00
if (animData->IsAdditiveAnimation(x34_animationId)) {
animData->AddAdditiveAnimation(x34_animationId, 1.f, x44_24_looping, x44_26_fadeOut);
} else {
animData->SetAnimation(CAnimPlaybackParms(x34_animationId, -1, 1.f, true), false);
act->GetModelData()->EnableLooping(x44_24_looping);
2018-12-08 05:30:43 +00:00
animData->MultiplyPlaybackRate(x3c_playbackRate);
}
2018-05-14 21:44:09 +00:00
}
2018-12-08 05:30:43 +00:00
} else if (TCastToPtr<CPatterned> ai = ent) {
CAnimData* animData = ai->GetModelData()->GetAnimationData();
2018-12-08 05:30:43 +00:00
if (animData->IsAdditiveAnimation(x34_animationId)) {
animData->AddAdditiveAnimation(x34_animationId, 1.f, x44_24_looping, x44_26_fadeOut);
} else {
ai->GetBodyController()->GetCommandMgr().DeliverCmd(
2018-12-08 05:30:43 +00:00
CBCScriptedCmd(x34_animationId, x44_24_looping, x44_27_timedLoop, x38_initialLifetime));
2018-05-17 00:04:07 +00:00
}
2018-12-08 05:30:43 +00:00
}
2018-05-14 21:44:09 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce