Skip to content

Commit

Permalink
Additional fixes for player track
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernthedev committed Jul 9, 2024
1 parent e4f18e4 commit 3252228
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 47 deletions.
1 change: 1 addition & 0 deletions include/Animation/PlayerTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ DECLARE_CLASS_CODEGEN(TrackParenting, PlayerTrack, UnityEngine::MonoBehaviour,
uint64_t lastCheckedTime = 0;

public:
static PlayerTrack* Create(PlayerTrackObject object);
static void AssignTrack(Track* track, PlayerTrackObject object);
private:
static std::unordered_map<PlayerTrackObject, SafePtrUnity<PlayerTrack>> playerTracks;
Expand Down
110 changes: 65 additions & 45 deletions src/Animation/PlayerTrack.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Animation/PlayerTrack.h"
#include "Animation/AnimationHelper.h"
#include "AssociatedData.h"
#include "GlobalNamespace/PlayerTransforms.hpp"
#include "NELogger.h"
#include "UnityEngine/GameObject.hpp"
#include "UnityEngine/Resources.hpp"
#include "GlobalNamespace/PauseController.hpp"
Expand Down Expand Up @@ -38,6 +40,56 @@ void PlayerTrack::ctor() {
if (!pauseController) pauseController.emplace(nullptr);
}

PlayerTrack* PlayerTrack::Create(PlayerTrackObject object) {
auto playerTransforms = Resources::FindObjectsOfTypeAll<PlayerTransforms*>()->FirstOrDefault();
if (!playerTransforms) {
CJDLogger::Logger.fmtLog<Paper::LogLevel::ERR>("PlayerTransforms not found");
return nullptr;
}

UnityEngine::Transform* target;
switch (object) {
case PlayerTrackObject::Head:
target = playerTransforms->_headTransform;
break;
case PlayerTrackObject::LeftHand:
target = playerTransforms->_leftHandTransform;
break;
case PlayerTrackObject::RightHand:
target = playerTransforms->_rightHandTransform;
break;
case PlayerTrackObject::Root:
default:
target = playerTransforms->_originTransform->parent;
break;
}

auto noodleObject = GameObject::New_ctor("NoodlePlayerTrack " + std::to_string((int)object));
auto playerTrack = noodleObject->AddComponent<PlayerTrack*>();
playerTrack->trackObject = object;
auto origin = playerTrack->origin = noodleObject->transform;

// Transform hierarchy manipulation: PLAYER PARENT -> NOODLE -> PLAYER
origin->SetParent(target->parent, false);
target->SetParent(origin, true);

playerTrack->startLocalRot = playerTrack->origin->get_localRotation();
playerTrack->startPos = playerTrack->origin->get_localPosition();

playerTrack->pauseController = Object::FindObjectOfType<PauseController*>();

if (playerTrack->pauseController) {
std::function<void()> pause = [playerTrack]() mutable { playerTrack->OnDidPauseEvent(); };
std::function<void()> resume = [playerTrack]() mutable { playerTrack->OnDidResumeEvent(); };
didPauseEventAction = custom_types::MakeDelegate<Action*>(pause);
playerTrack->pauseController->add_didPauseEvent(didPauseEventAction);
didResumeEventAction = custom_types::MakeDelegate<Action*>(resume);
playerTrack->pauseController->add_didResumeEvent(didResumeEventAction);
}

return playerTrack;
}

void PlayerTrack::AssignTrack(Track* track, PlayerTrackObject object) {
auto& playerTrack = PlayerTrack::playerTracks[object];

Expand All @@ -49,54 +101,17 @@ void PlayerTrack::AssignTrack(Track* track, PlayerTrackObject object) {
}

// Init
bool firstTime = playerTrack.ptr() == nullptr;
if (!playerTrack) {
auto playerTransforms = Resources::FindObjectsOfTypeAll<PlayerTransforms*>()->FirstOrDefault();
if (!playerTransforms) {
CJDLogger::Logger.fmtLog<Paper::LogLevel::ERR>("PlayerTransforms not found");
return;
}

UnityEngine::Transform* target;
switch (object) {
case PlayerTrackObject::Head:
target = playerTransforms->_headTransform;
break;
case PlayerTrackObject::LeftHand:
target = playerTransforms->_leftHandTransform;
break;
case PlayerTrackObject::RightHand:
target = playerTransforms->_rightHandTransform;
break;
case PlayerTrackObject::Root:
default:
target = playerTransforms->_originTransform->parent;
break;
}

noodleObject = GameObject::New_ctor("NoodlePlayerTrack " + std::to_string((int)object));
playerTrack = noodleObject->AddComponent<PlayerTrack*>();
playerTrack->trackObject = object;
playerTrack->origin = noodleObject->transform;


// Transform hierarchy manipulation: PLAYER PARENT -> NOODLE -> PLAYER
playerTrack->origin->SetParent(target->parent, false);
target->SetParent(playerTrack->origin, true);

playerTrack->startLocalRot = playerTrack->origin->get_localRotation();
playerTrack->startPos = playerTrack->origin->get_localPosition();

playerTrack->pauseController = Object::FindObjectOfType<PauseController*>();
playerTrack = Create(object);
}

if (playerTrack->pauseController) {
std::function<void()> pause = [playerTrack]() mutable { playerTrack->OnDidPauseEvent(); };
std::function<void()> resume = [playerTrack]() mutable { playerTrack->OnDidResumeEvent(); };
didPauseEventAction = custom_types::MakeDelegate<Action*>(pause);
playerTrack->pauseController->add_didPauseEvent(didPauseEventAction);
didResumeEventAction = custom_types::MakeDelegate<Action*>(resume);
playerTrack->pauseController->add_didResumeEvent(didResumeEventAction);
}
if (!playerTrack) {
NELogger::Logger.error("Failed to initialize player track {} {}", track ? track->name : "", (int)object);
return;
}

if (firstTime && object == PlayerTrackObject::Root) {
auto* pauseMenuManager = playerTrack->pauseController
? playerTrack->pauseController->_pauseMenuManager.ptr()
: NECaches::GameplayCoreContainer->TryResolve<PauseMenuManager*>();
Expand Down Expand Up @@ -135,6 +150,11 @@ void PlayerTrack::OnDidPauseEvent() {
NELogger::Logger.debug("PlayerTrack::OnDidPauseEvent");
this->set_enabled(false);

if (this->trackObject != PlayerTrackObject::Root) {
origin->localPosition = startPos;
origin->localRotation = startLocalRot;
}

if (trackController) {
trackController->set_enabled(false);
}
Expand Down
2 changes: 0 additions & 2 deletions src/AssociatedData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ ParentTrackEventData::ParentTrackEventData(rapidjson::Value const& eventData, Be
}

PlayerTrackEventData::PlayerTrackEventData(Track* track, std::optional<std::string_view> targetOpt) : track(track) {
this->target = PlayerTrackObject::Root;

if (targetOpt) {
auto targetStr = *targetOpt;

Expand Down

0 comments on commit 3252228

Please sign in to comment.