spawning objects
This commit is contained in:
+17
-14
@@ -8,7 +8,7 @@
|
||||
#include "../voxels/ChunksStorage.h"
|
||||
#include "../physics/Hitbox.h"
|
||||
#include "../physics/PhysicsSolver.h"
|
||||
#include "../objects/Object.h"
|
||||
#include "../interfaces/Object.h"
|
||||
#include "../objects/Player.h"
|
||||
#include "../items/Inventory.h"
|
||||
#include "../items/Inventories.h"
|
||||
@@ -25,11 +25,10 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
events(new LevelEvents()) ,
|
||||
settings(settings)
|
||||
{
|
||||
objCounter = 0;
|
||||
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
|
||||
|
||||
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
|
||||
player = spawnObjectOfClass<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
|
||||
|
||||
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;
|
||||
@@ -42,15 +41,20 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
});
|
||||
|
||||
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() {
|
||||
@@ -68,18 +72,17 @@ World* Level::getWorld() {
|
||||
return world.get();
|
||||
}
|
||||
|
||||
// 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>
|
||||
T* Level::spawnObjectOfClass(Args&&... 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");
|
||||
T* tObj = new T(args...);
|
||||
Object* obj = reinterpret_cast<Object*>(tObj);
|
||||
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 = std::rand();
|
||||
obj->setLevel(this);
|
||||
obj->objectUID = objCounter;
|
||||
obj->spawned();
|
||||
objCounter += 1;
|
||||
return tObj;
|
||||
}
|
||||
|
||||
+7
-3
@@ -6,6 +6,8 @@
|
||||
#include "../typedefs.h"
|
||||
#include "../settings.h"
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
class Content;
|
||||
class World;
|
||||
@@ -20,11 +22,13 @@ class PhysicsSolver;
|
||||
class ChunksStorage;
|
||||
|
||||
class Level {
|
||||
private:
|
||||
int objCounter;
|
||||
public:
|
||||
std::unique_ptr<World> world;
|
||||
const Content* const content;
|
||||
std::list<Object*> objects;
|
||||
Player* player;
|
||||
std::list<std::shared_ptr<Object>> objects;
|
||||
std::shared_ptr<Player> player;
|
||||
Chunks* chunks;
|
||||
ChunksStorage* chunksStorage;
|
||||
std::unique_ptr<Inventories> inventories;
|
||||
@@ -45,7 +49,7 @@ public:
|
||||
World* getWorld();
|
||||
|
||||
template<class T, typename... Args>
|
||||
T* spawnObjectOfClass(Args&&... args);
|
||||
std::shared_ptr<T> spawnObject(Args&&... args);
|
||||
};
|
||||
|
||||
#endif /* WORLD_LEVEL_H_ */
|
||||
|
||||
+3
-7
@@ -61,20 +61,17 @@ void World::write(Level* level) {
|
||||
}
|
||||
|
||||
wfile->write(this, content);
|
||||
wfile->writePlayer(level->player);
|
||||
wfile->writePlayer(level->player.get());
|
||||
}
|
||||
|
||||
|
||||
Level* World::create(std::string name,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
|
||||
auto world = new World(name, directory, seed, settings, content, packs);
|
||||
auto level = new Level(world, content, settings);
|
||||
level->inventories->store(level->player->getInventory());
|
||||
return level;
|
||||
}
|
||||
|
||||
@@ -91,9 +88,8 @@ Level* World::load(fs::path directory,
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
|
||||
Level* level = new Level(world.get(), content, settings);
|
||||
wfile->readPlayer(level->player);
|
||||
level->inventories->store(level->player->getInventory());
|
||||
auto level = new Level(world.get(), content, settings);
|
||||
wfile->readPlayer(level->player.get());
|
||||
world.release();
|
||||
return level;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user