metaforce/Runtime/World/CScriptActorRotate.cpp

148 lines
5.1 KiB
C++
Raw Permalink Normal View History

#include "Runtime/World/CScriptActorRotate.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/World/CScriptPlatform.hpp"
#include "Runtime/World/CScriptSpiderBallWaypoint.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2021-04-10 08:42:06 +00:00
namespace metaforce {
2017-11-13 06:19:18 +00:00
CScriptActorRotate::CScriptActorRotate(TUniqueId uid, std::string_view name, const CEntityInfo& info,
const zeus::CVector3f& rotation, float maxTime, bool updateActors,
bool updateOnCreation, bool active)
: CEntity(uid, info, active, name)
, x34_rotation(rotation)
, x40_maxTime(maxTime)
, x58_26_updateActors(updateActors)
2018-12-08 05:30:43 +00:00
, x58_27_updateOnCreation(updateOnCreation) {}
void CScriptActorRotate::Accept(IVisitor& visitor) { visitor.Visit(this); }
void CScriptActorRotate::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
if (msg == EScriptObjectMessage::Activate) {
CEntity::AcceptScriptMsg(msg, uid, mgr);
return;
}
2017-01-15 03:07:01 +00:00
2018-12-08 05:30:43 +00:00
if (msg == EScriptObjectMessage::Action || msg == EScriptObjectMessage::Next ||
(msg == EScriptObjectMessage::Registered && x58_27_updateOnCreation)) {
2018-12-08 05:30:43 +00:00
UpdateActors(msg == EScriptObjectMessage::Next, mgr);
}
2018-12-08 05:30:43 +00:00
CEntity::AcceptScriptMsg(msg, uid, mgr);
2017-01-15 03:07:01 +00:00
}
2018-12-08 05:30:43 +00:00
void CScriptActorRotate::Think(float dt, CStateManager& mgr) {
if (x58_24_updateRotation && GetActive()) {
x44_currentTime += dt;
if (x44_currentTime >= x40_maxTime) {
x58_24_updateRotation = false;
x44_currentTime = x40_maxTime;
2017-08-19 22:46:24 +00:00
}
const float timeOffset = x44_currentTime / x40_maxTime;
2017-08-19 22:46:24 +00:00
2018-12-08 05:30:43 +00:00
for (const auto& actorPair : x48_actors) {
if (const TCastToPtr<CActor> act = mgr.ObjectById(actorPair.first)) {
2021-04-19 03:11:18 +00:00
const zeus::CTransform xf = zeus::CTransform::RotateX(zeus::degToRad(timeOffset * x34_rotation.x())) *
zeus::CTransform::RotateY(zeus::degToRad(timeOffset * x34_rotation.y())) *
2021-04-19 03:11:18 +00:00
zeus::CTransform::RotateZ(zeus::degToRad(timeOffset * x34_rotation.z()));
2018-12-08 05:30:43 +00:00
zeus::CTransform localRot = actorPair.second * xf;
2021-04-19 03:11:18 +00:00
localRot.origin += act->GetTranslation();
2018-12-08 05:30:43 +00:00
act->SetTransform(localRot);
if (const TCastToPtr<CScriptPlatform> plat = mgr.ObjectById(actorPair.first)) {
2018-12-08 05:30:43 +00:00
UpdatePlatformRiders(*plat.GetPtr(), xf, mgr);
}
2018-12-08 05:30:43 +00:00
}
}
2021-04-19 03:11:18 +00:00
if (!x58_24_updateRotation) {
if (x58_25_updateSpiderBallWaypoints) {
2018-12-08 05:30:43 +00:00
UpdateSpiderBallWaypoints(mgr);
}
if (x58_26_updateActors) {
2018-12-08 05:30:43 +00:00
UpdateActors(false, mgr);
}
2017-08-19 22:46:24 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-08-19 22:46:24 +00:00
}
2018-12-08 05:30:43 +00:00
void CScriptActorRotate::UpdatePlatformRiders(CScriptPlatform& plat, const zeus::CTransform& xf, CStateManager& mgr) {
UpdatePlatformRiders(plat.GetStaticSlaves(), plat, xf, mgr);
UpdatePlatformRiders(plat.GetDynamicSlaves(), plat, xf, mgr);
2017-08-19 22:46:24 +00:00
}
void CScriptActorRotate::UpdatePlatformRiders(std::vector<SRiders>& riders, CScriptPlatform& plat,
2018-12-08 05:30:43 +00:00
const zeus::CTransform& xf, CStateManager& mgr) {
for (SRiders& rider : riders) {
if (const TCastToPtr<CActor> act = mgr.ObjectById(rider.x0_uid)) {
2018-12-08 05:30:43 +00:00
zeus::CTransform& riderXf = rider.x8_transform;
act->SetTransform(xf * rider.x8_transform);
2018-12-08 05:30:43 +00:00
act->SetTranslation(act->GetTranslation() + plat.GetTranslation());
2021-04-19 03:11:18 +00:00
if (!x58_24_updateRotation) {
2018-12-08 05:30:43 +00:00
riderXf = {act->GetTransform().basis, act->GetTranslation() - plat.GetTranslation()};
if (TCastToConstPtr<CScriptSpiderBallWaypoint>(act.GetPtr())) {
2021-04-19 03:11:18 +00:00
x58_25_updateSpiderBallWaypoints = true;
}
2018-12-08 05:30:43 +00:00
}
if (const TCastToPtr<CScriptPlatform> plat2 = mgr.ObjectById(rider.x0_uid)) {
2018-12-08 05:30:43 +00:00
UpdatePlatformRiders(*plat2.GetPtr(), xf, mgr);
}
2017-08-19 22:46:24 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-08-19 22:46:24 +00:00
}
2018-12-08 05:30:43 +00:00
void CScriptActorRotate::UpdateActors(bool next, CStateManager& mgr) {
if (x58_24_updateRotation) {
2018-12-08 05:30:43 +00:00
return;
}
2017-08-19 22:46:24 +00:00
2018-12-08 05:30:43 +00:00
x48_actors.clear();
2017-08-19 22:46:24 +00:00
2018-12-08 05:30:43 +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) {
if (const TCastToConstPtr<CActor> act = mgr.ObjectById(it->second)) {
x48_actors.insert_or_assign(it->second, act->GetTransform().getRotation());
2018-12-08 05:30:43 +00:00
}
2017-08-19 22:46:24 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-08-19 22:46:24 +00:00
2018-12-08 05:30:43 +00:00
SendScriptMsgs(EScriptObjectState::Play, mgr, EScriptObjectMessage::None);
if (!x48_actors.empty()) {
x58_24_updateRotation = true;
x44_currentTime = (next ? x40_maxTime : 0.f);
}
}
2017-08-19 22:46:24 +00:00
2018-12-08 05:30:43 +00:00
void CScriptActorRotate::UpdateSpiderBallWaypoints(CStateManager& mgr) {
rstl::reserved_vector<TUniqueId, 1024> waypointIds;
CObjectList& objectList = mgr.GetAllObjectList();
2021-04-19 03:11:18 +00:00
for (CEntity* ent : objectList) {
if (const TCastToPtr<CScriptSpiderBallWaypoint> wp = ent) {
2018-12-08 05:30:43 +00:00
waypointIds.push_back(wp->GetUniqueId());
2021-04-19 03:11:18 +00:00
wp->ClearWaypoints();
}
2018-12-08 05:30:43 +00:00
}
for (const TUniqueId& uid : waypointIds) {
auto* wp = static_cast<CScriptSpiderBallWaypoint*>(mgr.ObjectById(uid));
2018-12-08 05:30:43 +00:00
if (wp) {
2021-04-19 03:11:18 +00:00
wp->BuildWaypointListAndBounds(mgr);
wp->SetNotInSortedLists(false);
2017-08-19 22:46:24 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-08-19 22:46:24 +00:00
2021-04-19 03:11:18 +00:00
x58_25_updateSpiderBallWaypoints = false;
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce