Spawn different objects withing level
This commit is contained in:
parent
7434cb66e8
commit
4a4b80bef3
@ -4,12 +4,11 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include "../coders/json.h"
|
#include "../coders/json.h"
|
||||||
|
|
||||||
class Serializable
|
class Serializable {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
virtual ~Serializable() { }
|
virtual ~Serializable() { }
|
||||||
virtual std::unique_ptr<dynamic::Map> serialize() const = 0;
|
virtual std::unique_ptr<dynamic::Map> serialize() const = 0;
|
||||||
virtual void deserialize(dynamic::Map* src) = 0;
|
virtual void deserialize(dynamic::Map* src) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif /* SERIALIZABLE_H */
|
||||||
@ -6,6 +6,7 @@
|
|||||||
#include "ChunksController.h"
|
#include "ChunksController.h"
|
||||||
|
|
||||||
#include "scripting/scripting.h"
|
#include "scripting/scripting.h"
|
||||||
|
#include "../objects/Object.h"
|
||||||
|
|
||||||
LevelController::LevelController(EngineSettings& settings, Level* level)
|
LevelController::LevelController(EngineSettings& settings, Level* level)
|
||||||
: settings(settings), level(level) {
|
: settings(settings), level(level) {
|
||||||
@ -24,6 +25,19 @@ void LevelController::update(float delta, bool input, bool pause) {
|
|||||||
level->update();
|
level->update();
|
||||||
chunks->update(settings.chunks.loadSpeed);
|
chunks->update(settings.chunks.loadSpeed);
|
||||||
blocks->update(delta);
|
blocks->update(delta);
|
||||||
|
|
||||||
|
// erease null pointers
|
||||||
|
level->objects.remove_if([](Object* obj) { return obj == nullptr; });
|
||||||
|
|
||||||
|
// update all objects that needed
|
||||||
|
for(Object *obj : level->objects)
|
||||||
|
{
|
||||||
|
if(obj) {
|
||||||
|
if(obj->shouldUpdate) {
|
||||||
|
obj->update(delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelController::onWorldSave() {
|
void LevelController::onWorldSave() {
|
||||||
|
|||||||
@ -202,7 +202,7 @@ void PlayerController::resetKeyboard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::updateControls(float delta){
|
void PlayerController::updateControls(float delta){
|
||||||
player->update(level, input, delta);
|
player->updateInput(level, input, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::updateInteraction(){
|
void PlayerController::updateInteraction(){
|
||||||
|
|||||||
30
src/objects/Object.h
Normal file
30
src/objects/Object.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef OBJECT_H
|
||||||
|
#define OBJECT_H
|
||||||
|
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Level;
|
||||||
|
|
||||||
|
class Object {
|
||||||
|
private:
|
||||||
|
Level* level;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int64_t objectUID;
|
||||||
|
bool shouldUpdate = true;
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual ~Object() { destroyed(); }
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void spawned() { }
|
||||||
|
virtual void update(float delta) { }
|
||||||
|
virtual void destroyed() { }
|
||||||
|
|
||||||
|
public:
|
||||||
|
Level* getLevel() { return level; }
|
||||||
|
void setLevel(Level* lvl) { level = lvl; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* OBJECT_H */
|
||||||
@ -33,7 +33,7 @@ Player::Player(glm::vec3 position, float speed) :
|
|||||||
Player::~Player() {
|
Player::~Player() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::update(
|
void Player::updateInput(
|
||||||
Level* level,
|
Level* level,
|
||||||
PlayerInput& input,
|
PlayerInput& input,
|
||||||
float delta) {
|
float delta) {
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include "../voxels/voxel.h"
|
#include "../voxels/voxel.h"
|
||||||
#include "../settings.h"
|
#include "../settings.h"
|
||||||
#include "../interfaces/Serializable.h"
|
#include "../interfaces/Serializable.h"
|
||||||
|
#include "../objects/Object.h"
|
||||||
|
|
||||||
class Camera;
|
class Camera;
|
||||||
class Hitbox;
|
class Hitbox;
|
||||||
@ -32,7 +33,7 @@ struct PlayerInput {
|
|||||||
bool flight;
|
bool flight;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Player : Serializable {
|
class Player : Object, Serializable {
|
||||||
float speed;
|
float speed;
|
||||||
int chosenSlot;
|
int chosenSlot;
|
||||||
glm::vec3 spawnpoint {};
|
glm::vec3 spawnpoint {};
|
||||||
@ -52,7 +53,7 @@ public:
|
|||||||
~Player();
|
~Player();
|
||||||
|
|
||||||
void teleport(glm::vec3 position);
|
void teleport(glm::vec3 position);
|
||||||
void update(Level* level, PlayerInput& input, float delta);
|
void updateInput(Level* level, PlayerInput& input, float delta);
|
||||||
|
|
||||||
void attemptToFindSpawnpoint(Level* level);
|
void attemptToFindSpawnpoint(Level* level);
|
||||||
|
|
||||||
|
|||||||
@ -8,17 +8,24 @@
|
|||||||
#include "../voxels/ChunksStorage.h"
|
#include "../voxels/ChunksStorage.h"
|
||||||
#include "../physics/Hitbox.h"
|
#include "../physics/Hitbox.h"
|
||||||
#include "../physics/PhysicsSolver.h"
|
#include "../physics/PhysicsSolver.h"
|
||||||
|
#include "../objects/Object.h"
|
||||||
#include "../objects/Player.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),
|
: world(world),
|
||||||
content(content),
|
content(content),
|
||||||
player(player),
|
|
||||||
chunksStorage(new ChunksStorage(this)),
|
chunksStorage(new ChunksStorage(this)),
|
||||||
events(new LevelEvents()) ,
|
events(new LevelEvents()) ,
|
||||||
settings(settings) {
|
settings(settings) {
|
||||||
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
|
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+
|
uint matrixSize = (settings.chunks.loadDistance+
|
||||||
settings.chunks.padding) * 2;
|
settings.chunks.padding) * 2;
|
||||||
chunks = new Chunks(matrixSize, matrixSize, 0, 0,
|
chunks = new Chunks(matrixSize, matrixSize, 0, 0,
|
||||||
@ -53,3 +60,19 @@ void Level::update() {
|
|||||||
World* Level::getWorld() {
|
World* Level::getWorld() {
|
||||||
return world.get();
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -5,10 +5,12 @@
|
|||||||
|
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
#include "../settings.h"
|
#include "../settings.h"
|
||||||
|
#include <list>
|
||||||
|
|
||||||
class Content;
|
class Content;
|
||||||
class World;
|
class World;
|
||||||
class Player;
|
class Player;
|
||||||
|
class Object;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
class LevelEvents;
|
class LevelEvents;
|
||||||
class Lighting;
|
class Lighting;
|
||||||
@ -19,6 +21,7 @@ class Level {
|
|||||||
public:
|
public:
|
||||||
std::unique_ptr<World> world;
|
std::unique_ptr<World> world;
|
||||||
const Content* const content;
|
const Content* const content;
|
||||||
|
std::list<Object*> objects;
|
||||||
Player* player;
|
Player* player;
|
||||||
Chunks* chunks;
|
Chunks* chunks;
|
||||||
ChunksStorage* chunksStorage;
|
ChunksStorage* chunksStorage;
|
||||||
@ -31,13 +34,15 @@ public:
|
|||||||
|
|
||||||
Level(World* world,
|
Level(World* world,
|
||||||
const Content* content,
|
const Content* content,
|
||||||
Player* player,
|
|
||||||
EngineSettings& settings);
|
EngineSettings& settings);
|
||||||
~Level();
|
~Level();
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
World* getWorld();
|
World* getWorld();
|
||||||
|
|
||||||
|
template<class T, typename... Args>
|
||||||
|
T* spawnObjectOfClass(Args&&... args);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* WORLD_LEVEL_H_ */
|
#endif /* WORLD_LEVEL_H_ */
|
||||||
|
|||||||
@ -62,9 +62,6 @@ void World::write(Level* level) {
|
|||||||
wfile->writePlayer(level->player);
|
wfile->writePlayer(level->player);
|
||||||
}
|
}
|
||||||
|
|
||||||
const float DEF_PLAYER_Y = 100.0f;
|
|
||||||
const float DEF_PLAYER_SPEED = 4.0f;
|
|
||||||
|
|
||||||
Level* World::create(std::string name,
|
Level* World::create(std::string name,
|
||||||
fs::path directory,
|
fs::path directory,
|
||||||
uint64_t seed,
|
uint64_t seed,
|
||||||
@ -72,8 +69,7 @@ Level* World::create(std::string name,
|
|||||||
const Content* content,
|
const Content* content,
|
||||||
const std::vector<ContentPack>& packs) {
|
const std::vector<ContentPack>& packs) {
|
||||||
World* world = new World(name, directory, seed, settings, content, 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, settings);
|
||||||
return new Level(world, content, player, settings);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ContentLUT* World::checkIndices(const fs::path& directory,
|
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");
|
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, settings);
|
||||||
Level* level = new Level(world.get(), content, player, settings);
|
wfile->readPlayer(level->player);
|
||||||
wfile->readPlayer(player);
|
|
||||||
|
|
||||||
world.release();
|
world.release();
|
||||||
return level;
|
return level;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user