From 9a1847643765394b369e3861b8becef5eb924a5e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 12 Jul 2024 00:02:01 +0300 Subject: [PATCH] refactor: player cameras are resources now --- src/objects/Player.cpp | 9 ++++++--- src/window/Camera.hpp | 4 +++- src/world/Level.cpp | 41 +++++++++++++++++++++++++---------------- src/world/Level.hpp | 2 ++ 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 4c9e5067..ac01964e 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -29,11 +29,14 @@ Player::Player(Level* level, glm::vec3 position, float speed, position(position), inventory(std::move(inv)), eid(eid), - camera(std::make_shared(position, glm::radians(90.0f))), - spCamera(std::make_shared(position, glm::radians(90.0f))), - tpCamera(std::make_shared(position, glm::radians(90.0f))), + camera(level->getCamera("base:first-person")), + spCamera(level->getCamera("base:third-person-front")), + tpCamera(level->getCamera("base:third-person-back")), currentCamera(camera) { + camera->setFov(glm::radians(90.0f)); + spCamera->setFov(glm::radians(90.0f)); + tpCamera->setFov(glm::radians(90.0f)); } Player::~Player() { diff --git a/src/window/Camera.hpp b/src/window/Camera.hpp index 9f7d4bbe..d4ce0049 100644 --- a/src/window/Camera.hpp +++ b/src/window/Camera.hpp @@ -20,7 +20,9 @@ public: bool flipped = false; float aspect = 0.0f; - Camera() {} + Camera() { + updateVectors(); + } Camera(glm::vec3 position, float fov); void rotate(float x, float y, float z); diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 89820fd9..9cee7b08 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -28,6 +28,23 @@ Level::Level( entities(std::make_unique(this)), settings(settings) { + auto& cameraIndices = content->getIndices(ResourceType::CAMERA); + std::cout << cameraIndices.size() << std::endl; + for (size_t i = 0; i < cameraIndices.size(); i++) { + auto camera = std::make_shared(); + if (auto map = cameraIndices.getSavedData(i)) { + dynamic::get_vec(map, "pos", camera->position); + dynamic::get_mat(map, "rot", camera->rotation); + 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)); + } + if (world->nextEntityId) { entities->setNextID(world->nextEntityId); } @@ -53,22 +70,6 @@ Level::Level( inventories = std::make_unique(*this); inventories->store(player->getInventory()); - - auto& cameraIndices = content->getIndices(ResourceType::CAMERA); - for (size_t i = 0; i < cameraIndices.size(); i++) { - auto camera = std::make_shared(); - if (auto map = cameraIndices.getSavedData(i)) { - dynamic::get_vec(map, "pos", camera->position); - dynamic::get_mat(map, "rot", camera->rotation); - 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(){ @@ -106,3 +107,11 @@ void Level::onSave() { cameraIndices.saveData(i, std::move(map)); } } + +std::shared_ptr Level::getCamera(const std::string& name) { + size_t index = content->getIndices(ResourceType::CAMERA).indexOf(name); + if (index == ResourceIndices::MISSING) { + return nullptr; + } + return cameras.at(index); +} diff --git a/src/world/Level.hpp b/src/world/Level.hpp index 00c45d9c..cb2c0710 100644 --- a/src/world/Level.hpp +++ b/src/world/Level.hpp @@ -75,6 +75,8 @@ public: } void onSave(); + + std::shared_ptr getCamera(const std::string& name); }; #endif /* WORLD_LEVEL_HPP_ */