76 lines
2.4 KiB
C++
76 lines
2.4 KiB
C++
#include "CharacterManager.h"
|
|
#include "Animator.h"
|
|
#include "Character.h"
|
|
#include "Rig.h"
|
|
|
|
CharacterManager::CharacterManager() = default;
|
|
CharacterManager::~CharacterManager() = default;
|
|
|
|
Rig* CharacterManager::LoadRig(const std::string& name, const std::string& path) {
|
|
auto existing = rigs.find(name);
|
|
if (existing != rigs.end())
|
|
return existing->second.get();
|
|
|
|
auto rig = std::make_unique<Rig>(path);
|
|
Rig* result = rig.get();
|
|
rigs.emplace(name, std::move(rig));
|
|
return result;
|
|
}
|
|
|
|
Animation* CharacterManager::LoadAnimation(const std::string& name, const std::string& path,
|
|
const std::string& rigName) {
|
|
auto existing = animations.find(name);
|
|
if (existing != animations.end())
|
|
return existing->second.get();
|
|
|
|
auto animation = std::make_unique<Animation>(path, GetRig(rigName));
|
|
Animation* result = animation.get();
|
|
animations.emplace(name, std::move(animation));
|
|
return result;
|
|
}
|
|
|
|
Character* CharacterManager::CreateCharacter(const std::string& name,
|
|
const std::string& rigName,
|
|
const std::string& animationName) {
|
|
Character* existing = GetCharacter(name);
|
|
if (existing)
|
|
return existing;
|
|
|
|
auto character = std::make_unique<Character>();
|
|
character->name = name;
|
|
if (!rigName.empty())
|
|
character->body->SetRig(GetRig(rigName));
|
|
if (!animationName.empty())
|
|
character->body->SetAnimation(GetAnimation(animationName));
|
|
|
|
Character* result = character.get();
|
|
characters.emplace(name, std::move(character));
|
|
characterOrder.push_back(result);
|
|
return result;
|
|
}
|
|
|
|
Rig* CharacterManager::GetRig(const std::string& name) const {
|
|
auto it = rigs.find(name);
|
|
return it != rigs.end() ? it->second.get() : nullptr;
|
|
}
|
|
|
|
Animation* CharacterManager::GetAnimation(const std::string& name) const {
|
|
auto it = animations.find(name);
|
|
return it != animations.end() ? it->second.get() : nullptr;
|
|
}
|
|
|
|
Character* CharacterManager::GetCharacter(const std::string& name) const {
|
|
auto it = characters.find(name);
|
|
return it != characters.end() ? it->second.get() : nullptr;
|
|
}
|
|
|
|
void CharacterManager::Think() {
|
|
for (Character* character : characterOrder)
|
|
character->Think();
|
|
}
|
|
|
|
void CharacterManager::Step(float dt) {
|
|
for (Character* character : characterOrder)
|
|
character->Step(dt);
|
|
}
|