refactor: player cameras are resources now

This commit is contained in:
MihailRis 2024-07-12 00:02:01 +03:00
parent 9623f35862
commit 9a18476437
4 changed files with 36 additions and 20 deletions

View File

@ -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<Camera>(position, glm::radians(90.0f))),
spCamera(std::make_shared<Camera>(position, glm::radians(90.0f))),
tpCamera(std::make_shared<Camera>(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() {

View File

@ -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);

View File

@ -28,6 +28,23 @@ Level::Level(
entities(std::make_unique<Entities>(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<Camera>();
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<Inventories>(*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<Camera>();
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<Camera> 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);
}

View File

@ -75,6 +75,8 @@ public:
}
void onSave();
std::shared_ptr<Camera> getCamera(const std::string& name);
};
#endif /* WORLD_LEVEL_HPP_ */