1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#include <libultraship.h>
#include "World.h"
#include "Cup.h"
#include "tracks/Track.h"
#include "TrainCrossing.h"
#include <memory>
#include "objects/Object.h"
#include "port/Game.h"
#include "engine/sky/Sky.h"
extern "C" {
#include "objects.h"
#include "main.h"
#include "defines.h"
#include "audio/external.h"
#include "menus.h"
#include "code_800029B0.h"
}
#include "engine/cameras/GameCamera.h"
World* World::Instance;
std::unique_ptr<Track> mTrack;
Cup* CurrentCup;
World::World() {
Instance = this;
RaceManagerInstance = std::make_unique<RaceManager>(*this);
}
World::~World() {
CleanWorld();
}
void World::AddCup(Cup* cup) {
Cups.push_back(cup);
}
void World::SetCurrentTrack(std::unique_ptr<Track> track) {
if (mTrack == track) {
return;
}
mTrack = std::move(track);
}
TrainCrossing* World::AddCrossing(Vec3f position, u32 waypointMin, u32 waypointMax, f32 approachRadius,
f32 exitRadius) {
auto crossing = std::make_shared<TrainCrossing>(position, waypointMin, waypointMax, approachRadius, exitRadius);
Crossings.push_back(crossing);
return crossing.get();
}
u32 World::GetCupIndex() {
return this->CupIndex;
}
u32 World::NextCup() {
s32 hack = 1;
// Do not display battle mode on GP, TT, or VS
if (gModeSelection != BATTLE) {
hack = 2;
}
if (CupIndex < Cups.size() - hack) {
CupIndex++;
CurrentCup = Cups[CupIndex];
CurrentCup->CursorPosition = 0;
reset_cycle_flash_menu();
play_sound2(SOUND_MENU_CURSOR_MOVE);
return CupIndex;
}
return Cups.size() - hack;
}
u32 World::PreviousCup() {
if (CupIndex > 0) {
CupIndex--;
CurrentCup = Cups[CupIndex];
CurrentCup->CursorPosition = 0;
reset_cycle_flash_menu();
play_sound2(SOUND_MENU_CURSOR_MOVE);
return CupIndex;
}
return 0;
}
void World::SetCupIndex(size_t index) {
CupIndex = index;
}
void World::SetCurrentCup(Cup* cup) {
if (cup) {
CurrentCup = cup;
CurrentCup->CursorPosition = 0;
}
}
void World::TickCameras() {
for (size_t i = 0; i < 4; i++) {
ScreenContext* screen = &gScreenContexts[i];
if (NULL == screen->pendingCamera) { continue; }
if (screen->pendingCamera != screen->camera) {
screen->camera = screen->pendingCamera;
screen->pendingCamera = nullptr;
}
}
for (auto& camera : Cameras) {
if (camera->IsActive()) {
camera->Tick();
}
}
}
AActor* World::AddActor(std::unique_ptr<AActor> actor) {
Actors.push_back(std::move(actor));
Actors.back()->BeginPlay();
return Actors.back().get();
}
struct Actor* World::AddBaseActor() {
Actors.push_back(std::make_unique<AActor>());
AActor* actor = Actors.back().get();
// Skip C++ vtable to access variables in C
return reinterpret_cast<struct Actor*>(reinterpret_cast<char*>(actor) + sizeof(void*));
}
void World::ActorBeginPlay(Actor* actor) {
AActor* act = ConvertActorToAActor(actor);
act->BeginPlay();
}
/**
* Converts a C struct Actor* to its C++ AActor class
*/
AActor* World::ConvertActorToAActor(Actor* actor) {
// Move the ptr back so that it points at the vtable.
// Which is the initial item in the class, or in other words
// Point to the class.
return reinterpret_cast<AActor*>((char*) actor - sizeof(void*));
}
/**
* Converts a C++ AActor class to a C Actor* struct.
*/
Actor* World::ConvertAActorToActor(AActor* actor) {
// Move the ptr forward past the vtable.
// This allows C to access the class variables like a normal Actor* struct.
return reinterpret_cast<Actor*>((char*) actor + sizeof(void*));
}
AActor* World::GetActor(size_t index) {
return Actors[index].get();
}
void World::TickActors() {
// This only ticks modded actors
for (auto& actor : Actors) {
if (actor->IsMod()) {
actor->Tick();
}
}
}
StaticMeshActor* World::AddStaticMeshActor(const std::string& name, FVector pos, IRotator rot, FVector scale, const std::string& model, int32_t* collision) {
StaticMeshActors.push_back(std::make_unique<StaticMeshActor>(name, pos, rot, scale, model, collision));
auto* actor = StaticMeshActors.back().get();
return actor;
}
void World::DrawStaticMeshActors() {
for (const auto& actor : StaticMeshActors) {
actor->Draw();
}
}
// OObject* World::AddObject(OObject object) {
// Objects.push_back(std::make_unique<OObject>(object));
// // This is an example of how to get the C object.
// // However, nothing is being done with it, so it's been commented out.
// // if (object->_objectIndex != -1) {
// // Object* cObj = &gObjectList[object->_objectIndex];
// // }
// return Objects.back().get();
// }
OObject* World::AddObject(std::unique_ptr<OObject> object) {
Objects.push_back(std::move(object));
// This is an example of how to get the C object.
// However, nothing is being done with it, so it's been commented out.
// if (object->_objectIndex != -1) {
// Object* cObj = &gObjectList[object->_objectIndex];
// }
return Objects.back().get();
}
void World::TickObjects() {
for (auto& object : Objects) {
object->Tick();
}
}
// Some objects such as lakitu are ticked in process_game_tick.
// This is a fallback to support those objects. Probably don't use this.
void World::TickObjects60fps() {
for (auto& object : Objects) {
object->Tick60fps();
}
}
ParticleEmitter* World::AddEmitter(std::unique_ptr<ParticleEmitter> emitter) {
Emitters.push_back(std::move(emitter));
return Emitters.back().get();
}
void World::DrawObjects(s32 cameraId) {
for (auto& object : Objects) {
object->Draw(cameraId);
}
}
void World::TickParticles() {
for (const auto& emitter : Emitters) {
emitter->Tick();
}
}
void World::DrawParticles(s32 cameraId) {
for (const auto& emitter : Emitters) {
emitter->Draw(cameraId);
}
}
// Sets OObjects or AActors static member variables back to default values
void World::Reset() {
for (auto& object : Objects) {
object->Reset(); // Used for OPenguin
}
}
Object* World::GetObjectByIndex(size_t index) {
// if (index < this->Objects.size()) {
// Assuming GameActor::a is accessible, use reinterpret_cast if needed
// return reinterpret_cast<Object*>(&this->Objects[index]->o);
//}
return nullptr; // Or handle the error as needed
}
// Deletes all objects from the world
void World::CleanWorld(void) {
printf("[Game.cpp] Clean World\n");
World::Reset(); // Reset OObjects
for (size_t i = 0; i < ARRAY_COUNT(mPlayerBombKart); i++) {
mPlayerBombKart[i].state = PlayerBombKart::PlayerBombKartState::DISABLED;
mPlayerBombKart[i]._primAlpha = 0;
}
gEditor.ClearObjects();
Actors.clear();
StaticMeshActors.clear();
Objects.clear();
Emitters.clear();
Lakitus.clear();
Sky::Instance->GetSkyActors().clear();
}
|