refactor players

This commit is contained in:
MihailRis
2024-11-22 16:17:13 +03:00
parent 7f41f95013
commit bd2acd5766
13 changed files with 101 additions and 151 deletions
+6 -30
View File
@@ -2,8 +2,9 @@
#include <memory>
#include <vector>
#include <unordered_map>
#include "interfaces/Object.hpp"
#include "typedefs.hpp"
inline constexpr float DEF_PLAYER_Y = 100.0f;
inline constexpr float DEF_PLAYER_SPEED = 4.0f;
@@ -19,6 +20,7 @@ class Lighting;
class PhysicsSolver;
class ChunksStorage;
class Camera;
class Player;
struct EngineSettings;
/// @brief A level, contains chunks and objects
@@ -26,7 +28,7 @@ class Level {
std::unique_ptr<World> world;
public:
const Content* const content;
std::vector<std::shared_ptr<Object>> objects;
std::unordered_map<int64_t, std::unique_ptr<Player>> players;
std::unique_ptr<Chunks> chunks;
std::unique_ptr<ChunksStorage> chunksStorage;
std::unique_ptr<Inventories> inventories;
@@ -52,35 +54,9 @@ public:
const World* getWorld() const;
/// Spawns object of class T and returns pointer to it.
/// @param T class that derives the Object class
/// @param args pass arguments needed for T class constructor
template <class T, typename... Args>
std::shared_ptr<T> spawnObject(Args&&... args) {
static_assert(
std::is_base_of<Object, T>::value,
"T must be a derived of Object class"
);
std::shared_ptr<T> tObj = std::make_shared<T>(args...);
void addPlayer(std::unique_ptr<Player> player);
std::shared_ptr<Object> obj =
std::dynamic_pointer_cast<Object, T>(tObj);
obj->objectUID = objects.size();
objects.push_back(obj);
obj->spawned();
return tObj;
}
template <class T>
std::shared_ptr<T> getObject(uint64_t id) const {
static_assert(
std::is_base_of<Object, T>::value,
"T must be a derived of Object class"
);
if (id >= objects.size()) return nullptr;
std::shared_ptr<T> object = std::dynamic_pointer_cast<T>(objects[id]);
return object;
}
Player* getPlayer(int64_t id) const;
void onSave();