A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
KartObjectManager.cc
1#include "KartObjectManager.hh"
2
3#include "game/kart/KartCollide.hh"
4#include "game/kart/KartParamFileManager.hh"
5#include "game/system/RaceConfig.hh"
6
7#include <abstract/g3d/ResFile.hh>
8
9namespace Kart {
10
12void KartObjectManager::init() {
13 for (size_t i = 0; i < m_count; ++i) {
14 m_objects[i]->initImpl();
15 m_objects[i]->prepare();
16 }
17}
18
20void KartObjectManager::calc() {
21 for (size_t i = 0; i < m_count; ++i) {
22 KartObject *object = m_objects[i];
23 object->collide()->setTangentOff(EGG::Vector3f::zero);
24 object->collide()->setMovement(EGG::Vector3f::zero);
25 }
26
27 for (size_t i = 0; i < m_count; ++i) {
28 KartObject *object = m_objects[i];
29 object->calcSub();
30 object->calc();
31 }
32}
33
35KartObjectManager *KartObjectManager::CreateInstance() {
36 ASSERT(!s_instance);
37 s_instance = new KartObjectManager;
38 return s_instance;
39}
40
42void KartObjectManager::DestroyInstance() {
43 ASSERT(s_instance);
44 auto *instance = s_instance;
45 s_instance = nullptr;
46 delete instance;
47}
48
50KartObjectManager::KartObjectManager() {
51 const auto &raceScenario = System::RaceConfig::Instance()->raceScenario();
52 m_count = raceScenario.playerCount;
53 m_objects = new KartObject *[m_count];
54 KartParamFileManager::CreateInstance();
55
56 loadScaleAnimations();
57
58 for (size_t i = 0; i < m_count; ++i) {
59 const auto &player = raceScenario.players[i];
60 KartObject *object = KartObject::Create(player.character, player.vehicle, i);
61 object->createModel();
62 m_objects[i] = object;
63 }
64}
65
67KartObjectManager::~KartObjectManager() {
68 if (s_instance) {
69 s_instance = nullptr;
70 WARN("KartObjectManager instance not explicitly handled!");
71 }
72
73 KartParamFileManager::DestroyInstance();
74
75 for (size_t i = 0; i < m_count; ++i) {
76 delete m_objects[i];
77 }
78
79 delete[] m_objects;
80
81 delete s_pressScaleUpAnmChr;
82
83 // If the proxy list is not cleared when we're done with the KartObjectManager, the list's
84 // destructor calls delete on all of the links remaining in the list. Since the heaps are
85 // gone by that point, this results in a segmentation fault. So, we clear the links here.
86 KartObjectProxy::proxyList().clear();
87}
88
90void KartObjectManager::loadScaleAnimations() {
91 auto *resMgr = System::ResourceManager::Instance();
92 const void *file = resMgr->getFile("driver.brres", nullptr, System::ArchiveId::Core);
93 ASSERT(file);
94
95 // Copy construct onto the heap
96 auto resAnmChr = Abstract::g3d::ResFile(file).resAnmChr("press_scale_up");
97 s_pressScaleUpAnmChr = new Abstract::g3d::ResAnmChr(resAnmChr);
98}
99
100Abstract::g3d::ResAnmChr *KartObjectManager::s_pressScaleUpAnmChr = nullptr;
101
102KartObjectManager *KartObjectManager::s_instance = nullptr;
103
104} // namespace Kart
Represents the CHR0 file format, which pertains to model movement animations.
Definition ResAnmChr.hh:48
Represents a binary resource file which contains object models, textures, and animations.
Definition ResFile.hh:15
ResAnmChr resAnmChr(const char *pName) const
Retrieves the AnmChr section from the binary file.
Definition ResFile.hh:32
void prepare()
Sets the initial position and rotation of the kart based off the current track.
Pertains to kart-related functionality.