summaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp72
1 files changed, 49 insertions, 23 deletions
diff --git a/src/game.cpp b/src/game.cpp
index a1ba28b..e1accc9 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -8,12 +8,14 @@
#include "game-settings.hpp"
#include "helpers.hpp"
#include "imgui.h"
+#include "math-helpers.hpp"
#include "player-input.hpp"
#include "vendor/glm_include.hpp"
#include "window.hpp"
#include <GLFW/glfw3.h>
#include <cstring>
+#include <glm/gtc/quaternion.hpp>
#include <iomanip>
const uint64_t consoleBufferCount = 30;
@@ -127,25 +129,36 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
}
assert(et != nullptr);
CompInstance c{};
+ InstPos baseInst{};
+ baseInst.pos = glm::vec3(0, 0, 0);
+ baseInst.rot = glm::quat(1, 0, 0, 0);
+ baseInst.scale = glm::vec3(1, 1, 1);
if (comp.entHandle != c.entHandle)
stream << PKE_FILE_INSTANCE_ENTITY_HANDLE << handleStr << std::endl;
stream << PKE_FILE_INSTANCE_ENTITY_TYPE_CODE << et->entityTypeCode << std::endl;
- if (comp.instPos.pos != c.instPos.pos)
+
+ btTransform trans;
+ comp.bt.defaultMotionState.getWorldTransform(trans);
+ InstPos inst;
+ BulletToGlm(trans.getOrigin(), inst.pos);
+ BulletToGlm(trans.getRotation(), inst.rot);
+ BulletToGlm(comp.bt.collisionShape->getLocalScaling(), inst.scale);
+ if (inst.pos != baseInst.pos)
stream << PKE_FILE_INSTANCE_POS_POS << "["
- << std::setw(10) << comp.instPos.pos[0] << ","
- << std::setw(10) << comp.instPos.pos[1] << ","
- << std::setw(10) << comp.instPos.pos[2] << "]" << std::endl;
- if (comp.instPos.rot != c.instPos.rot)
+ << std::setw(10) << inst.pos[0] << ","
+ << std::setw(10) << inst.pos[1] << ","
+ << std::setw(10) << inst.pos[2] << "]" << std::endl;
+ if (inst.rot != baseInst.rot)
stream << PKE_FILE_INSTANCE_POS_ROT << "["
- << std::setw(10) << comp.instPos.rot[0] << ","
- << std::setw(10) << comp.instPos.rot[1] << ","
- << std::setw(10) << comp.instPos.rot[2] << ","
- << std::setw(10) << comp.instPos.rot[3] << "]" << std::endl;
- if (comp.instPos.scale != c.instPos.scale)
+ << std::setw(10) << inst.rot[0] << ","
+ << std::setw(10) << inst.rot[1] << ","
+ << std::setw(10) << inst.rot[2] << ","
+ << std::setw(10) << inst.rot[3] << "]" << std::endl;
+ if (inst.scale != baseInst.scale)
stream << PKE_FILE_INSTANCE_POS_SCALE << "["
- << std::setw(10) << comp.instPos.scale[0] << ","
- << std::setw(10) << comp.instPos.scale[1] << ","
- << std::setw(10) << comp.instPos.scale[2] << "]" << std::endl;
+ << std::setw(10) << inst.scale[0] << ","
+ << std::setw(10) << inst.scale[1] << ","
+ << std::setw(10) << inst.scale[2] << "]" << std::endl;
}
void ParseEntityType(std::ifstream &stream) {
@@ -231,6 +244,10 @@ void ParseEntityType(std::ifstream &stream) {
void ParseInstance(std::ifstream &stream) {
CompInstance comp{};
+ InstPos instPos{};
+ instPos.pos = glm::vec3(0, 0, 0);
+ instPos.rot = glm::quat(1, 0, 0, 0);
+ instPos.scale = glm::vec3(1, 1, 1);
char entTypeCode[21];
memset(reinterpret_cast<void *>(entTypeCode), '\0', 21);
while (stream.getline(readLine, readLineLength)) {
@@ -246,8 +263,7 @@ void ParseInstance(std::ifstream &stream) {
}
const auto &et = GlobalEntityTypes[existingEntityTypeIndex];
auto entityHandle = ECS_CreateEntity();
- ECS_CreateInstance(entityHandle, et.entityHandle);
- ECS_UpdateInstance(entityHandle, comp.instPos, true);
+ ECS_CreateInstance(entityHandle, et.entityHandle, instPos);
break;
}
if (strstr(readLine, PKE_FILE_INSTANCE_ENTITY_HANDLE)) {
@@ -272,7 +288,7 @@ void ParseInstance(std::ifstream &stream) {
long index = 0;
do {
assert(index < 3);
- STR2NUM_ERROR result = str2num(comp.instPos.pos[index], startingChar, pEnd);
+ STR2NUM_ERROR result = str2num(instPos.pos[index], startingChar, pEnd);
assert(result == STR2NUM_ERROR::SUCCESS);
startingChar = pEnd + 1;
++index;
@@ -287,7 +303,7 @@ void ParseInstance(std::ifstream &stream) {
long index = 0;
do {
assert(index < 4);
- STR2NUM_ERROR result = str2num(comp.instPos.rot[index], startingChar, pEnd);
+ STR2NUM_ERROR result = str2num(instPos.rot[index], startingChar, pEnd);
assert(result == STR2NUM_ERROR::SUCCESS);
startingChar = pEnd + 1;
++index;
@@ -302,7 +318,7 @@ void ParseInstance(std::ifstream &stream) {
long index = 0;
do {
assert(index < 3);
- STR2NUM_ERROR result = str2num(comp.instPos.scale[index], startingChar, pEnd);
+ STR2NUM_ERROR result = str2num(instPos.scale[index], startingChar, pEnd);
assert(result == STR2NUM_ERROR::SUCCESS);
startingChar = pEnd + 1;
++index;
@@ -420,7 +436,11 @@ void Game_Tick(double delta) {
while (entityInstancesToCreate.Count() > 0) {
auto createInfo = entityInstancesToCreate.Pop();
EntityHandle newEntity = ECS_CreateEntity();
- ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle);
+ InstPos instPos;
+ instPos.pos = glm::vec3(0, 0, 0);
+ instPos.rot = glm::quat(1, 0, 0, 0);
+ instPos.scale = glm::vec3(1);
+ ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle, instPos);
}
PkeInput_Tick(delta);
@@ -810,14 +830,20 @@ void RecordImGui_CompInstPos(bool readonly, CompInstance *component) {
ImGui::Separator();
bool changed = false;
+ btTransform trans;
+ component->bt.defaultMotionState.getWorldTransform(trans);
+ InstPos instPos;
+ BulletToGlm(trans.getOrigin(), instPos.pos);
+ BulletToGlm(trans.getRotation(), instPos.rot);
+ BulletToGlm(component->bt.collisionShape->getLocalScaling(), instPos.scale);
changed = ImGui::InputScalar("Instance Index", ImGuiDataType_U64, &component->index, nullptr, nullptr, nullptr, ImGuiInputTextFlags_ReadOnly) || changed;
- changed = ImGui::InputScalarN("pos", ImGuiDataType_Float, &component->instPos.pos, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed;
- changed = ImGui::InputScalarN("rot", ImGuiDataType_Float, &component->instPos.rot, 4, nullptr, nullptr, nullptr, inputTextFlags) || changed;
- changed = ImGui::InputScalarN("scale", ImGuiDataType_Float, &component->instPos.scale, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed;
+ changed = ImGui::InputScalarN("pos", ImGuiDataType_Float, &instPos.pos, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed;
+ changed = ImGui::InputScalarN("rot", ImGuiDataType_Float, &instPos.rot, 4, nullptr, nullptr, nullptr, inputTextFlags) || changed;
+ changed = ImGui::InputScalarN("scale", ImGuiDataType_Float, &instPos.scale, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed;
if (changed) {
- ECS_UpdateInstance(component->entHandle, component->instPos, true);
+ ECS_UpdateInstance(component->entHandle, instPos, true);
}
ImGui::Spacing();