feat: cameras saving/loading

This commit is contained in:
MihailRis
2024-07-11 22:45:57 +03:00
parent 45c4da048c
commit a013e7eefd
8 changed files with 66 additions and 17 deletions
+2 -2
View File
@@ -31,7 +31,7 @@ Content::Content(
UptrsMap<std::string, ContentPackRuntime> packs, UptrsMap<std::string, ContentPackRuntime> packs,
UptrsMap<std::string, BlockMaterial> blockMaterials, UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::SkeletonConfig> skeletons, UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
const ResourceIndicesSet& resourceIndices ResourceIndicesSet resourceIndices
) : indices(std::move(indices)), ) : indices(std::move(indices)),
packs(std::move(packs)), packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)), blockMaterials(std::move(blockMaterials)),
@@ -42,7 +42,7 @@ Content::Content(
drawGroups(std::move(drawGroups)) drawGroups(std::move(drawGroups))
{ {
for (size_t i = 0; i < RESOURCE_TYPES_COUNT; i++) { for (size_t i = 0; i < RESOURCE_TYPES_COUNT; i++) {
this->resourceIndices[i] = resourceIndices[i]; this->resourceIndices[i] = std::move(resourceIndices[i]);
} }
} }
+13 -7
View File
@@ -111,16 +111,18 @@ public:
class ResourceIndices { class ResourceIndices {
std::vector<std::string> names; std::vector<std::string> names;
std::unordered_map<std::string, size_t> indices; std::unordered_map<std::string, size_t> indices;
std::vector<dynamic::Map_sptr> savedData; std::unique_ptr<std::vector<dynamic::Map_sptr>> savedData;
public: public:
ResourceIndices() {} ResourceIndices()
: savedData(std::make_unique<std::vector<dynamic::Map_sptr>>()){
}
static constexpr size_t MISSING = -1; static constexpr size_t MISSING = -1;
void add(std::string name, dynamic::Map_sptr map) { void add(std::string name, dynamic::Map_sptr map) {
indices[name] = names.size(); indices[name] = names.size();
names.push_back(name); names.push_back(name);
savedData.push_back(map); savedData->push_back(map);
} }
const std::string& getName(size_t index) const { const std::string& getName(size_t index) const {
@@ -136,11 +138,11 @@ public:
} }
dynamic::Map_sptr getSavedData(size_t index) const { dynamic::Map_sptr getSavedData(size_t index) const {
return savedData.at(index); return savedData->at(index);
} }
void saveData(size_t index, dynamic::Map_sptr map) { void saveData(size_t index, dynamic::Map_sptr map) const {
savedData.at(index) = map; savedData->at(index) = map;
} }
size_t size() const { size_t size() const {
@@ -186,7 +188,7 @@ public:
UptrsMap<std::string, ContentPackRuntime> packs, UptrsMap<std::string, ContentPackRuntime> packs,
UptrsMap<std::string, BlockMaterial> blockMaterials, UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::SkeletonConfig> skeletons, UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
const ResourceIndicesSet& resourceIndices ResourceIndicesSet resourceIndices
); );
~Content(); ~Content();
@@ -194,6 +196,10 @@ public:
return indices.get(); return indices.get();
} }
inline const ResourceIndices& getIndices(ResourceType type) const {
return resourceIndices[static_cast<size_t>(type)];
}
const rigging::SkeletonConfig* getRig(const std::string& id) const; const rigging::SkeletonConfig* getRig(const std::string& id) const;
const BlockMaterial* findBlockMaterial(const std::string& id) const; const BlockMaterial* findBlockMaterial(const std::string& id) const;
const ContentPackRuntime* getPackRuntime(const std::string& id) const; const ContentPackRuntime* getPackRuntime(const std::string& id) const;
+1 -1
View File
@@ -76,7 +76,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
std::move(packs), std::move(packs),
std::move(blockMaterials), std::move(blockMaterials),
std::move(skeletons), std::move(skeletons),
resourceIndices std::move(resourceIndices)
); );
// Now, it's time to resolve foreign keys // Now, it's time to resolve foreign keys
+2 -1
View File
@@ -500,6 +500,7 @@ void ContentLoader::load() {
void ContentLoader::loadResources(ResourceType type, dynamic::List* list) { void ContentLoader::loadResources(ResourceType type, dynamic::List* list) {
for (size_t i = 0; i < list->size(); i++) { for (size_t i = 0; i < list->size(); i++) {
builder.resourceIndices[static_cast<size_t>(type)].add(list->str(i), nullptr); builder.resourceIndices[static_cast<size_t>(type)].add(
pack->id+":"+list->str(i), nullptr);
} }
} }
+1
View File
@@ -59,6 +59,7 @@ void LevelController::saveWorld() {
level->getWorld()->wfile->createDirectories(); level->getWorld()->wfile->createDirectories();
logger.info() << "writing world"; logger.info() << "writing world";
scripting::on_world_save(); scripting::on_world_save();
level->onSave();
level->getWorld()->write(level.get()); level->getWorld()->write(level.get());
} }
+8 -6
View File
@@ -5,20 +5,22 @@
class Camera { class Camera {
void updateVectors(); void updateVectors();
float fov; float fov = 1.0f;
public: public:
glm::vec3 front; glm::vec3 front {};
glm::vec3 up; glm::vec3 up {};
glm::vec3 right; glm::vec3 right {};
glm::vec3 dir; glm::vec3 dir {};
glm::vec3 position; glm::vec3 position {};
float zoom = 1.0f; float zoom = 1.0f;
glm::mat4 rotation {1.0f}; glm::mat4 rotation {1.0f};
bool perspective = true; bool perspective = true;
bool flipped = false; bool flipped = false;
float aspect = 0.0f; float aspect = 0.0f;
Camera() {}
Camera(glm::vec3 position, float fov); Camera(glm::vec3 position, float fov);
void rotate(float x, float y, float z); void rotate(float x, float y, float z);
+35
View File
@@ -13,6 +13,8 @@
#include "../objects/Entities.hpp" #include "../objects/Entities.hpp"
#include "../items/Inventory.hpp" #include "../items/Inventory.hpp"
#include "../items/Inventories.hpp" #include "../items/Inventories.hpp"
#include "../window/Camera.hpp"
#include "../data/dynamic_util.hpp"
Level::Level( Level::Level(
std::unique_ptr<World> worldPtr, std::unique_ptr<World> worldPtr,
@@ -51,6 +53,23 @@ Level::Level(
inventories = std::make_unique<Inventories>(*this); inventories = std::make_unique<Inventories>(*this);
inventories->store(player->getInventory()); inventories->store(player->getInventory());
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) {
auto camera = std::make_shared<Camera>();
if (auto map = cameraIndices.getSavedData(i)) {
dynamic::get_vec(map, "pos", camera->position);
dynamic::get_vec(map, "front", camera->front);
dynamic::get_vec(map, "up", camera->up);
map->flag("perspective", camera->perspective);
map->flag("flipped", camera->flipped);
map->num("zoom", camera->zoom);
float fov = camera->getFov();
map->num("fov", fov);
camera->setFov(fov);
}
cameras.push_back(std::move(camera));
}
} }
Level::~Level(){ Level::~Level(){
@@ -73,3 +92,19 @@ void Level::loadMatrix(int32_t x, int32_t z, uint32_t radius) {
World* Level::getWorld() { World* Level::getWorld() {
return world.get(); return world.get();
} }
void Level::onSave() {
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) {
auto& camera = *cameras.at(i);
auto map = dynamic::create_map();
map->put("pos", dynamic::to_value(camera.position));
map->put("front", dynamic::to_value(camera.front));
map->put("up", dynamic::to_value(camera.up));
map->put("perspective", camera.perspective);
map->put("flipped", camera.flipped);
map->put("zoom", camera.zoom);
map->put("fov", camera.getFov());
cameraIndices.saveData(i, std::move(map));
}
}
+4
View File
@@ -19,6 +19,7 @@ class LevelEvents;
class Lighting; class Lighting;
class PhysicsSolver; class PhysicsSolver;
class ChunksStorage; class ChunksStorage;
class Camera;
struct EngineSettings; struct EngineSettings;
/// @brief A level, contains chunks and objects /// @brief A level, contains chunks and objects
@@ -35,6 +36,7 @@ public:
std::unique_ptr<Lighting> lighting; std::unique_ptr<Lighting> lighting;
std::unique_ptr<LevelEvents> events; std::unique_ptr<LevelEvents> events;
std::unique_ptr<Entities> entities; std::unique_ptr<Entities> entities;
std::vector<std::shared_ptr<Camera>> cameras; // move somewhere?
const EngineSettings& settings; const EngineSettings& settings;
@@ -71,6 +73,8 @@ public:
std::shared_ptr<T> object = std::dynamic_pointer_cast<T>(objects[id]); std::shared_ptr<T> object = std::dynamic_pointer_cast<T>(objects[id]);
return object; return object;
} }
void onSave();
}; };
#endif /* WORLD_LEVEL_HPP_ */ #endif /* WORLD_LEVEL_HPP_ */