Spawn different objects withing level
This commit is contained in:
+25
-2
@@ -8,17 +8,24 @@
|
||||
#include "../voxels/ChunksStorage.h"
|
||||
#include "../physics/Hitbox.h"
|
||||
#include "../physics/PhysicsSolver.h"
|
||||
#include "../objects/Object.h"
|
||||
#include "../objects/Player.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;
|
||||
|
||||
Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
: world(world),
|
||||
content(content),
|
||||
player(player),
|
||||
chunksStorage(new ChunksStorage(this)),
|
||||
events(new LevelEvents()) ,
|
||||
settings(settings) {
|
||||
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
|
||||
|
||||
player = spawnObjectOfClass<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
|
||||
|
||||
|
||||
uint matrixSize = (settings.chunks.loadDistance+
|
||||
settings.chunks.padding) * 2;
|
||||
chunks = new Chunks(matrixSize, matrixSize, 0, 0,
|
||||
@@ -53,3 +60,19 @@ void Level::update() {
|
||||
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)
|
||||
{
|
||||
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);
|
||||
objects.push_back(obj);
|
||||
obj->objectUID = std::rand();
|
||||
obj->setLevel(this);
|
||||
obj->spawned();
|
||||
return tObj;
|
||||
}
|
||||
|
||||
+6
-1
@@ -5,10 +5,12 @@
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../settings.h"
|
||||
#include <list>
|
||||
|
||||
class Content;
|
||||
class World;
|
||||
class Player;
|
||||
class Object;
|
||||
class Chunks;
|
||||
class LevelEvents;
|
||||
class Lighting;
|
||||
@@ -19,6 +21,7 @@ class Level {
|
||||
public:
|
||||
std::unique_ptr<World> world;
|
||||
const Content* const content;
|
||||
std::list<Object*> objects;
|
||||
Player* player;
|
||||
Chunks* chunks;
|
||||
ChunksStorage* chunksStorage;
|
||||
@@ -31,13 +34,15 @@ public:
|
||||
|
||||
Level(World* world,
|
||||
const Content* content,
|
||||
Player* player,
|
||||
EngineSettings& settings);
|
||||
~Level();
|
||||
|
||||
void update();
|
||||
|
||||
World* getWorld();
|
||||
|
||||
template<class T, typename... Args>
|
||||
T* spawnObjectOfClass(Args&&... args);
|
||||
};
|
||||
|
||||
#endif /* WORLD_LEVEL_H_ */
|
||||
|
||||
+3
-8
@@ -62,9 +62,6 @@ void World::write(Level* level) {
|
||||
wfile->writePlayer(level->player);
|
||||
}
|
||||
|
||||
const float DEF_PLAYER_Y = 100.0f;
|
||||
const float DEF_PLAYER_SPEED = 4.0f;
|
||||
|
||||
Level* World::create(std::string name,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
@@ -72,8 +69,7 @@ Level* World::create(std::string name,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
World* world = new World(name, directory, seed, settings, content, packs);
|
||||
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
|
||||
return new Level(world, content, player, settings);
|
||||
return new Level(world, content, settings);
|
||||
}
|
||||
|
||||
ContentLUT* World::checkIndices(const fs::path& directory,
|
||||
@@ -98,9 +94,8 @@ Level* World::load(fs::path directory,
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
|
||||
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
|
||||
Level* level = new Level(world.get(), content, player, settings);
|
||||
wfile->readPlayer(player);
|
||||
Level* level = new Level(world.get(), content, settings);
|
||||
wfile->readPlayer(level->player);
|
||||
|
||||
world.release();
|
||||
return level;
|
||||
|
||||
Reference in New Issue
Block a user