summaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2024-01-10 19:24:12 -0500
committerJonathan Bradley <jcb@pikum.xyz>2024-01-10 19:24:12 -0500
commitf07294ca65143fac8b1b426d1854212403721226 (patch)
tree2edeb8f2c9beea1cbb065f69910d53957ebda0ce /src/game.cpp
parent294c85f91ac5b2ff9e4ad3d99588ed0d1a72e6b7 (diff)
checkpoint - handle breaking ECS changes - compiles
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp75
1 files changed, 20 insertions, 55 deletions
diff --git a/src/game.cpp b/src/game.cpp
index dc32b2a..97bc247 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -91,17 +91,7 @@ void SerializeCamera(std::ofstream &stream, const PkeCamera &cam) {
void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
char handleStr[19] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
snprintf(handleStr, 19, "0x%016lX", comp.entHandle.hash);
- EntityType *et = nullptr;
- // EntityTypeDetails *etd = nullptr;
- for (long i = 0; i < GlobalEntityTypes.Count(); ++i) {
- for (int64_t k = 0; k < GlobalEntityTypes[i].detailsCount; ++k) {
- if (GlobalEntityTypes[i].details[k].grBindsHandle == comp.grBindsHandle) {
- et = &GlobalEntityTypes[i];
- // etd = &GlobalEntityTypes[i].details[k];
- break;
- }
- }
- }
+ EntityType *et = EntityType_FindByEntityHandle(comp.entHandle);
assert(et != nullptr);
CompInstance c{};
InstPos baseInst{};
@@ -151,7 +141,7 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
}
}
-void ParseCamera(LevelHandle levelHandle, std::ifstream &stream) {
+void ParseCamera(PkeLevel *level, std::ifstream &stream) {
PkeCamera cam{};
while (stream.getline(readLine, readLineLength)) {
if (strcmp(readLine, PKE_FILE_OBJ_END) == 0) {
@@ -162,9 +152,7 @@ void ParseCamera(LevelHandle levelHandle, std::ifstream &stream) {
rCam.type = cam.type;
rCam.orientation = cam.orientation;
rCam.isPrimary = cam.isPrimary;
- if (levelHandle != LevelHandle_MAX) {
- PkeLevel_RegisterCamera(levelHandle, rCam.handle);
- }
+ PkeLevel_RegisterCamera(level->levelHandle, rCam.handle);
if (rCam.isPrimary == true) {
ActiveCamera = &rCam;
}
@@ -239,7 +227,7 @@ void ParseCamera(LevelHandle levelHandle, std::ifstream &stream) {
}
}
-void ParseInstance(EntityHandle parentEntHandle, std::ifstream &stream) {
+void ParseInstance(Entity_Base *parentEntity, std::ifstream &stream) {
CompInstance comp{};
InstPos instPos{};
instPos.posRot = btTransform{};
@@ -252,39 +240,20 @@ void ParseInstance(EntityHandle parentEntHandle, std::ifstream &stream) {
while (stream.getline(readLine, readLineLength)) {
if (strstr(PKE_FILE_OBJ_END, readLine)) {
if (entTypeCode[0] == '\0') {
- printf("[Game::ParseInstance] Failed to create instance from save file. No EntTypeCode present.\n");
+ fprintf(stdout, "[Game::ParseInstance] Failed to create instance from save file. No EntTypeCode present.\n");
break;
}
- int64_t existingEntityTypeIndex = EntityType_FindByTypeCode(entTypeCode);
- if (existingEntityTypeIndex == -1) {
- printf("[Game::ParseInstance] Failed to create instance from save file. Unknown EntityTypeCode: \"%s\"\n", entTypeCode);
+ auto *etPtr = EntityType_FindByTypeCode(entTypeCode);
+ if (etPtr == nullptr) {
+ fprintf(stdout, "[Game::ParseInstance] Failed to create instance from save file. Unknown EntityTypeCode: \"%s\"\n", entTypeCode);
break;
}
- const auto &et = GlobalEntityTypes[existingEntityTypeIndex];
- for (int64_t i = 0; i < et.detailsCount; ++i) {
- auto &etd = et.details[i];
- auto entHandle = ECS_CreateEntity(parentEntHandle);
- auto &compInst = ECS_CreateInstance(entHandle, etd.entityHandle);
-
- strncpy(compInst.collisionCallback.name, comp.collisionCallback.name, 16);
- PkePlugin_SetSignatureFunc(&compInst.collisionCallback);
-
- compInst.physicsLayer = comp.physicsLayer;
- compInst.physicsMask = comp.physicsMask;
- btVector3 localInertia(0, 0, 0);
- etd.bt.shape->calculateLocalInertia(instPos.mass, localInertia);
- compInst.bt.motionState = Pke_New<btDefaultMotionState>(MemBkt_Bullet);
- new (compInst.bt.motionState) btDefaultMotionState(instPos.posRot);
- compInst.bt.rigidBody = Pke_New<btRigidBody>(MemBkt_Bullet);
- new (compInst.bt.rigidBody) btRigidBody(instPos.mass, compInst.bt.motionState, etd.bt.shape, localInertia);
- compInst.bt.rigidBody->setLinearVelocity(btVector3(0,0,0));
- compInst.bt.rigidBody->setAngularVelocity(btVector3(0,0,0));
- compInst.bt.rigidBody->getCollisionShape()->setLocalScaling(instPos.scale);
- BtDynamicsWorld->addRigidBody(compInst.bt.rigidBody);
- auto *broadphaseProxy = compInst.bt.rigidBody->getBroadphaseProxy();
- broadphaseProxy->m_collisionFilterGroup = static_cast<PhysicsCollision_T>(comp.physicsLayer);
- broadphaseProxy->m_collisionFilterMask = static_cast<PhysicsCollision_T>(comp.physicsMask);
- compInst.bt.rigidBody->setUserPointer(reinterpret_cast<void *>(compInst.entHandle.hash));
+
+ if (etPtr->createInstanceCallback.func != nullptr) {
+ reinterpret_cast<void(*)()>(etPtr->createInstanceCallback.func)();
+ } else {
+ EntityType_CreateGenericInstance(etPtr, &comp);
+ fprintf(stdout ,"[Game::ParseInstance] No callback func to create instance.");
}
break;
}
@@ -421,26 +390,22 @@ void Game_SaveSceneFile(const char *sceneFilePath) {
f.close();
}
-void Game_LoadSceneFile(LevelHandle levelHandle, const char *sceneFilePath) {
+void Game_LoadSceneFile(PkeLevel *level, const char *sceneFilePath) {
std::ifstream f(sceneFilePath);
if (!f.is_open()) {
fprintf(stderr, "Failed to load requested scene file: %s\n", sceneFilePath);
return;
}
memset(readLine, '\0', readLineLength);
- EntityHandle parentEntHandle = EntityHandle_MAX;
- if (levelHandle != LevelHandle_MAX) {
- parentEntHandle = ECS_CreateEntity();
- PkeLevel_RegisterWrappingEntity(levelHandle, parentEntHandle);
- }
+ ECS_CreateEntity(level);
while (f.getline(readLine, readLineLength)) {
if (strcmp(PKE_FILE_OBJ_CAMERA, readLine) == 0) {
- ParseCamera(levelHandle, f);
+ ParseCamera(level, f);
continue;
}
if (strcmp(PKE_FILE_OBJ_INSTANCE, readLine) == 0) {
- ParseInstance(parentEntHandle, f);
+ ParseInstance(level, f);
continue;
}
}
@@ -498,7 +463,7 @@ void Game_Tick(double delta) {
}
if (pkeSettings.rt.nextLevel != LevelHandle_MAX) {
// TODO async this
- Game_LoadSceneFile(pkeSettings.rt.nextLevel, pkeSettings.rt.sceneName);
+ Game_LoadSceneFile(PkeLevel_Get(pkeSettings.rt.nextLevel), pkeSettings.rt.sceneName);
pkeSettings.rt.activeLevel = pkeSettings.rt.nextLevel;
pkeSettings.rt.nextLevel = LevelHandle_MAX;
}
@@ -537,10 +502,10 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) {
Physics_Init();
Game_Init();
ECS_Init();
+ EntityType_Init();
PkeProject_Load(pkeSettings.args.projectPath);
CreateWindow(windowProps);
PkeInput_Init();
- EntityType_Init();
if (pkeSettings.args.pluginPath != nullptr) {
PkePlugin_Load(pkeSettings.args.pluginPath);
}