Merge branch 'main' into main
This commit is contained in:
+31
-3
@@ -8,19 +8,27 @@
|
||||
#include "../voxels/ChunksStorage.h"
|
||||
#include "../physics/Hitbox.h"
|
||||
#include "../physics/PhysicsSolver.h"
|
||||
#include "../interfaces/Object.h"
|
||||
#include "../objects/Player.h"
|
||||
#include "../items/Inventory.h"
|
||||
#include "../items/Inventories.h"
|
||||
|
||||
Level::Level(World* world, const Content* content, Player* player, EngineSettings& settings)
|
||||
|
||||
const float DEF_PLAYER_Y = 100.0f;
|
||||
const float DEF_PLAYER_SPEED = 4.0f;
|
||||
const int DEF_PLAYER_INVENTORY_SIZE = 40;
|
||||
|
||||
Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
: world(world),
|
||||
content(content),
|
||||
player(player),
|
||||
chunksStorage(new ChunksStorage(this)),
|
||||
events(new LevelEvents()) ,
|
||||
settings(settings)
|
||||
{
|
||||
objCounter = 0;
|
||||
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
|
||||
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
|
||||
player = spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
|
||||
|
||||
uint matrixSize = (settings.chunks.loadDistance+
|
||||
settings.chunks.padding) * 2;
|
||||
@@ -33,15 +41,20 @@ Level::Level(World* world, const Content* content, Player* player, EngineSetting
|
||||
});
|
||||
|
||||
inventories = std::make_unique<Inventories>(*this);
|
||||
inventories->store(player->getInventory());
|
||||
}
|
||||
|
||||
Level::~Level(){
|
||||
delete chunks;
|
||||
delete events;
|
||||
delete physics;
|
||||
delete player;
|
||||
delete lighting;
|
||||
delete chunksStorage;
|
||||
|
||||
for(auto obj : objects)
|
||||
{
|
||||
obj.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void Level::update() {
|
||||
@@ -58,3 +71,18 @@ void Level::update() {
|
||||
World* Level::getWorld() {
|
||||
return world.get();
|
||||
}
|
||||
|
||||
|
||||
template<class T, typename... Args>
|
||||
std::shared_ptr<T> Level::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...);
|
||||
|
||||
std::shared_ptr<Object> obj = std::dynamic_pointer_cast<Object, T>(tObj);
|
||||
objects.push_back(obj);
|
||||
obj->objectUID = objCounter;
|
||||
obj->spawned();
|
||||
objCounter += 1;
|
||||
return tObj;
|
||||
}
|
||||
|
||||
+14
-2
@@ -5,10 +5,14 @@
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../settings.h"
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
class Content;
|
||||
class World;
|
||||
class Player;
|
||||
class Object;
|
||||
class Chunks;
|
||||
class Inventory;
|
||||
class Inventories;
|
||||
@@ -18,10 +22,13 @@ class PhysicsSolver;
|
||||
class ChunksStorage;
|
||||
|
||||
class Level {
|
||||
private:
|
||||
int objCounter;
|
||||
public:
|
||||
std::unique_ptr<World> world;
|
||||
const Content* const content;
|
||||
Player* player;
|
||||
std::list<std::shared_ptr<Object>> objects;
|
||||
std::shared_ptr<Player> player;
|
||||
Chunks* chunks;
|
||||
ChunksStorage* chunksStorage;
|
||||
std::unique_ptr<Inventories> inventories;
|
||||
@@ -34,13 +41,18 @@ public:
|
||||
|
||||
Level(World* world,
|
||||
const Content* content,
|
||||
Player* player,
|
||||
EngineSettings& settings);
|
||||
~Level();
|
||||
|
||||
void update();
|
||||
|
||||
World* getWorld();
|
||||
|
||||
// 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);
|
||||
};
|
||||
|
||||
#endif /* WORLD_LEVEL_H_ */
|
||||
|
||||
+4
-17
@@ -82,10 +82,6 @@ void World::write(Level* level) {
|
||||
wfile->writePlayer(level->player);
|
||||
}
|
||||
|
||||
const float DEF_PLAYER_Y = 100.0f;
|
||||
const float DEF_PLAYER_SPEED = 4.0f;
|
||||
const int DEF_PLAYER_INVENTORY_SIZE = 40;
|
||||
|
||||
Level* World::create(std::string name,
|
||||
std::string type,
|
||||
fs::path directory,
|
||||
@@ -94,12 +90,8 @@ Level* World::create(std::string name,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
auto world = new World(name, type, directory, seed, settings, content, packs);
|
||||
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
|
||||
auto player = new Player(
|
||||
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv
|
||||
);
|
||||
auto level = new Level(world, content, player, settings);
|
||||
level->inventories->store(player->getInventory());
|
||||
auto level = new Level(world, content, settings);
|
||||
|
||||
return level;
|
||||
}
|
||||
|
||||
@@ -116,13 +108,8 @@ Level* World::load(fs::path directory,
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
|
||||
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
|
||||
auto player = new Player(
|
||||
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv
|
||||
);
|
||||
auto level = new Level(world.get(), content, player, settings);
|
||||
wfile->readPlayer(player);
|
||||
level->inventories->store(player->getInventory());
|
||||
auto level = new Level(world.get(), content, settings);
|
||||
wfile->readPlayer(level->player);
|
||||
world.release();
|
||||
return level;
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ public:
|
||||
world_load_error(std::string message);
|
||||
};
|
||||
|
||||
class World : Serializable {
|
||||
class World : public Serializable {
|
||||
std::string name;
|
||||
std::string type;
|
||||
uint64_t seed;
|
||||
|
||||
Reference in New Issue
Block a user