metaforce/Runtime/World/CScriptActorKeyframe.cpp

166 lines
5.3 KiB
C++
Raw Normal View History

2016-11-20 21:53:15 +00:00
#include "CScriptActorKeyframe.hpp"
2018-05-14 21:44:09 +00:00
#include "CStateManager.hpp"
#include "World/CScriptActor.hpp"
#include "World/CScriptPlatform.hpp"
2018-05-17 00:04:07 +00:00
#include "World/CPatterned.hpp"
2017-01-15 03:07:01 +00:00
#include "TCastTo.hpp"
2016-11-20 21:53:15 +00:00
namespace urde
{
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)
2016-11-20 21:53:15 +00:00
{
2018-05-14 21:44:09 +00:00
x44_24_looping = looping;
2018-11-11 03:27:54 +00:00
x44_25_isPassive = isPassive;
2018-05-17 00:04:07 +00:00
x44_26_fadeOut = fadeOut;
x44_27_timedLoop = fadeOut;
2018-05-15 02:59:52 +00:00
x44_28_playing = false;
2018-05-14 21:44:09 +00:00
x44_29_ = false;
2016-11-20 21:53:15 +00:00
}
2017-01-15 03:07:01 +00:00
void CScriptActorKeyframe::Accept(IVisitor& visitor)
{
visitor.Visit(this);
}
2018-05-14 21:44:09 +00:00
void CScriptActorKeyframe::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr)
{
if (msg == EScriptObjectMessage::Action)
{
if (GetActive())
{
2018-11-11 03:27:54 +00:00
if (!x44_25_isPassive)
2018-05-14 21:44:09 +00:00
{
for (const SConnection& conn : x20_conns)
{
if (conn.x0_state != EScriptObjectState::Play || conn.x4_msg != EScriptObjectMessage::Play)
continue;
auto search = mgr.GetIdListForScript(conn.x8_objId);
for (auto it = search.first; it != search.second; ++it)
UpdateEntity(it->second, mgr);
}
}
2018-05-15 02:59:52 +00:00
x44_28_playing = true;
2018-05-14 21:44:09 +00:00
SendScriptMsgs(EScriptObjectState::Play, mgr, EScriptObjectMessage::None);
}
}
else if (msg == EScriptObjectMessage::InitializedInArea)
{
if (x34_animationId == -1)
x34_animationId = 0;
}
CEntity::AcceptScriptMsg(msg, uid, mgr);
}
2016-11-20 21:53:15 +00:00
void CScriptActorKeyframe::Think(float dt, CStateManager& mgr)
{
2018-11-11 03:27:54 +00:00
if (x44_25_isPassive || !x44_24_looping || !x44_27_timedLoop || !x44_28_playing || x40_lifetime <= 0.f)
{
2018-05-14 21:44:09 +00:00
CEntity::Think(dt, mgr);
return;
}
2018-05-14 21:44:09 +00:00
x40_lifetime -= dt;
if (x40_lifetime <= 0.f)
{
CEntity::Think(dt, mgr);
return;
}
2018-05-15 02:59:52 +00:00
x44_28_playing = false;
2018-05-14 21:44:09 +00:00
for (const SConnection& conn : x20_conns)
{
if (conn.x0_state != EScriptObjectState::Play || conn.x4_msg!= EScriptObjectMessage::Play)
continue;
CEntity* ent = mgr.ObjectById(mgr.GetIdForScript(conn.x8_objId));
if (TCastToPtr<CScriptActor> act = ent)
{
if (act->HasModelData() && act->GetModelData()->HasAnimData())
{
CAnimData* animData = act->ModelData()->AnimationData();
if (animData->IsAdditiveAnimation(x34_animationId))
animData->DelAdditiveAnimation(x34_animationId);
if (animData->GetDefaultAnimation() == x34_animationId)
animData->EnableLooping(false);
}
}
// TODO: Re-enable this
#if 0
2018-05-17 00:04:07 +00:00
else if (TCastToPtr<CPatterned> ai = ent)
2018-05-14 21:44:09 +00:00
{
2018-05-17 00:04:07 +00:00
CAnimData* animData = ai->ModelData()->AnimationData();
2018-05-14 21:44:09 +00:00
if (animData->IsAdditiveAnimation(x34_animationId))
2018-05-17 00:04:07 +00:00
{
2018-05-14 21:44:09 +00:00
animData->DelAdditiveAnimation(x34_animationId);
2018-05-17 00:04:07 +00:00
}
else if (ai->GetBodyController()->GetCurrentStateId() == pas::EAnimationState::Scripted &&
animData->GetDefaultAnimation() == x34_animationId)
{
ai->BodyController()->GetCommandMgr().DeliverCmd(CBodyStateCmd(EBodyStateCmd::ExitState));
}
2018-05-14 21:44:09 +00:00
}
#endif
2018-05-14 21:44:09 +00:00
}
CEntity::Think(dt, mgr);
2016-11-20 21:53:15 +00:00
}
2018-05-14 21:44:09 +00:00
void CScriptActorKeyframe::UpdateEntity(TUniqueId uid, CStateManager& mgr)
{
CEntity* ent = mgr.ObjectById(uid);
CActor* act = nullptr;
if (TCastToPtr<CScriptActor> tmp = ent)
act = tmp;
else if (TCastToPtr<CScriptPlatform> tmp = ent)
act = tmp;
if (act)
{
if (!act->GetActive())
mgr.SendScriptMsg(act, GetUniqueId(), EScriptObjectMessage::Activate);
act->SetDrawFlags({0, 0, 3, zeus::CColor::skWhite});
if (act->HasModelData() && act->GetModelData()->HasAnimData())
{
CAnimData* animData = act->ModelData()->AnimationData();
if (animData->IsAdditiveAnimation(x34_animationId))
{
2018-05-17 00:04:07 +00:00
animData->AddAdditiveAnimation(x34_animationId, 1.f, x44_24_looping, x44_26_fadeOut);
2018-05-14 21:44:09 +00:00
}
else
{
animData->SetAnimation(CAnimPlaybackParms(x34_animationId, -1, 1.f, true), false);
act->ModelData()->EnableLooping(x44_24_looping);
animData->MultiplyPlaybackRate(x3c_playbackRate);
}
}
}
// TODO: Re-enable this
#if 0
2018-05-17 00:04:07 +00:00
else if (TCastToPtr<CPatterned> ai = ent)
{
CAnimData* animData = ai->ModelData()->AnimationData();
if (animData->IsAdditiveAnimation(x34_animationId))
{
animData->AddAdditiveAnimation(x34_animationId, 1.f, x44_24_looping, x44_26_fadeOut);
}
else
{
ai->BodyController()->GetCommandMgr().DeliverCmd(
CBCScriptedCmd(x34_animationId, x44_24_looping, x44_27_timedLoop, x38_initialLifetime));
}
}
#endif
2018-05-14 21:44:09 +00:00
}
2016-11-20 21:53:15 +00:00
}