Merge pull request #150 from DanielProl1xy/spawn_objects
Spawning objects within the level
This commit is contained in:
commit
6ee63eb717
@ -559,11 +559,11 @@ bool WorldFiles::readWorldInfo(World* world) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldFiles::writePlayer(Player* player) {
|
void WorldFiles::writePlayer(std::shared_ptr<Player> player) {
|
||||||
files::write_json(getPlayerFile(), player->serialize().release());
|
files::write_json(getPlayerFile(), player->serialize().release());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WorldFiles::readPlayer(Player* player) {
|
bool WorldFiles::readPlayer(std::shared_ptr<Player> player) {
|
||||||
fs::path file = getPlayerFile();
|
fs::path file = getPlayerFile();
|
||||||
if (!fs::is_regular_file(file)) {
|
if (!fs::is_regular_file(file)) {
|
||||||
std::cerr << "warning: player.json does not exists" << std::endl;
|
std::cerr << "warning: player.json does not exists" << std::endl;
|
||||||
|
|||||||
@ -143,13 +143,13 @@ public:
|
|||||||
chunk_inventories_map fetchInventories(int x, int z);
|
chunk_inventories_map fetchInventories(int x, int z);
|
||||||
|
|
||||||
bool readWorldInfo(World* world);
|
bool readWorldInfo(World* world);
|
||||||
bool readPlayer(Player* player);
|
bool readPlayer(std::shared_ptr<Player> player);
|
||||||
|
|
||||||
void writeRegion(int x, int y,
|
void writeRegion(int x, int y,
|
||||||
WorldRegion* entry,
|
WorldRegion* entry,
|
||||||
fs::path file,
|
fs::path file,
|
||||||
int layer);
|
int layer);
|
||||||
void writePlayer(Player* player);
|
void writePlayer(std::shared_ptr<Player> player);
|
||||||
/* @param world world info to save (nullable) */
|
/* @param world world info to save (nullable) */
|
||||||
void write(const World* world, const Content* content);
|
void write(const World* world, const Content* content);
|
||||||
void writePacks(const World* world);
|
void writePacks(const World* world);
|
||||||
|
|||||||
@ -645,6 +645,6 @@ void Hud::setPause(bool pause) {
|
|||||||
menu->setVisible(pause);
|
menu->setVisible(pause);
|
||||||
}
|
}
|
||||||
|
|
||||||
Player* Hud::getPlayer() const {
|
std::shared_ptr<Player> Hud::getPlayer() const {
|
||||||
return frontend->getLevel()->player;
|
return frontend->getLevel()->player;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -118,7 +118,7 @@ public:
|
|||||||
void remove(HudElement& element);
|
void remove(HudElement& element);
|
||||||
void remove(std::shared_ptr<gui::UINode> node);
|
void remove(std::shared_ptr<gui::UINode> node);
|
||||||
|
|
||||||
Player* getPlayer() const;
|
std::shared_ptr<Player> getPlayer() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SRC_HUD_H_ */
|
#endif /* SRC_HUD_H_ */
|
||||||
|
|||||||
25
src/interfaces/Object.h
Normal file
25
src/interfaces/Object.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef OBJECT_H
|
||||||
|
#define OBJECT_H
|
||||||
|
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Level;
|
||||||
|
|
||||||
|
class Object {
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
uint64_t objectUID;
|
||||||
|
bool shouldUpdate = true;
|
||||||
|
|
||||||
|
public:
|
||||||
|
~Object() { destroyed(); }
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void spawned() { }
|
||||||
|
virtual void update(float delta) { }
|
||||||
|
virtual void destroyed() { }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* OBJECT_H */
|
||||||
@ -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 */
|
||||||
@ -13,7 +13,7 @@
|
|||||||
class ContentLUT;
|
class ContentLUT;
|
||||||
class ContentIndices;
|
class ContentIndices;
|
||||||
|
|
||||||
class Inventory : Serializable {
|
class Inventory : public Serializable {
|
||||||
int64_t id;
|
int64_t id;
|
||||||
std::vector<ItemStack> slots;
|
std::vector<ItemStack> slots;
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include "ChunksController.h"
|
#include "ChunksController.h"
|
||||||
|
|
||||||
#include "scripting/scripting.h"
|
#include "scripting/scripting.h"
|
||||||
|
#include "../interfaces/Object.h"
|
||||||
|
|
||||||
LevelController::LevelController(EngineSettings& settings, Level* level)
|
LevelController::LevelController(EngineSettings& settings, Level* level)
|
||||||
: settings(settings), level(level) {
|
: settings(settings), level(level) {
|
||||||
@ -23,7 +24,20 @@ void LevelController::update(float delta, bool input, bool pause) {
|
|||||||
player->update(delta, input, pause);
|
player->update(delta, input, pause);
|
||||||
level->update();
|
level->update();
|
||||||
chunks->update(settings.chunks.loadSpeed);
|
chunks->update(settings.chunks.loadSpeed);
|
||||||
|
|
||||||
|
// erease null pointers
|
||||||
|
level->objects.remove_if([](auto obj) { return obj == nullptr; });
|
||||||
|
|
||||||
if (!pause) {
|
if (!pause) {
|
||||||
|
// update all objects that needed
|
||||||
|
for(auto obj : level->objects)
|
||||||
|
{
|
||||||
|
if(obj) {
|
||||||
|
if(obj->shouldUpdate) {
|
||||||
|
obj->update(delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
blocks->update(delta);
|
blocks->update(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,7 @@ const float C_ZOOM = 0.1f;
|
|||||||
const float CROUCH_SHIFT_Y = -0.2f;
|
const float CROUCH_SHIFT_Y = -0.2f;
|
||||||
|
|
||||||
|
|
||||||
CameraControl::CameraControl(Player* player, const CameraSettings& settings)
|
CameraControl::CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings)
|
||||||
: player(player),
|
: player(player),
|
||||||
camera(player->camera),
|
camera(player->camera),
|
||||||
currentViewCamera(player->currentCamera),
|
currentViewCamera(player->currentCamera),
|
||||||
@ -202,13 +202,13 @@ 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(){
|
||||||
auto indices = level->content->getIndices();
|
auto indices = level->content->getIndices();
|
||||||
Chunks* chunks = level->chunks;
|
Chunks* chunks = level->chunks;
|
||||||
Player* player = level->player;
|
Player* player = level->player.get();
|
||||||
Lighting* lighting = level->lighting;
|
Lighting* lighting = level->lighting;
|
||||||
Camera* camera = player->camera.get();
|
Camera* camera = player->camera.get();
|
||||||
glm::vec3 end;
|
glm::vec3 end;
|
||||||
|
|||||||
@ -12,7 +12,7 @@ class Level;
|
|||||||
class BlocksController;
|
class BlocksController;
|
||||||
|
|
||||||
class CameraControl {
|
class CameraControl {
|
||||||
Player* player;
|
std::shared_ptr<Player> player;
|
||||||
std::shared_ptr<Camera> camera, currentViewCamera;
|
std::shared_ptr<Camera> camera, currentViewCamera;
|
||||||
const CameraSettings& settings;
|
const CameraSettings& settings;
|
||||||
glm::vec3 offset;
|
glm::vec3 offset;
|
||||||
@ -20,7 +20,7 @@ class CameraControl {
|
|||||||
float shakeTimer = 0.0f;
|
float shakeTimer = 0.0f;
|
||||||
glm::vec3 interpVel {0.0f};
|
glm::vec3 interpVel {0.0f};
|
||||||
public:
|
public:
|
||||||
CameraControl(Player* player, const CameraSettings& settings);
|
CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings);
|
||||||
void updateMouse(PlayerInput& input);
|
void updateMouse(PlayerInput& input);
|
||||||
void update(PlayerInput& input, float delta, Chunks* chunks);
|
void update(PlayerInput& input, float delta, Chunks* chunks);
|
||||||
void refresh();
|
void refresh();
|
||||||
@ -28,7 +28,7 @@ public:
|
|||||||
|
|
||||||
class PlayerController {
|
class PlayerController {
|
||||||
Level* level;
|
Level* level;
|
||||||
Player* player;
|
std::shared_ptr<Player> player;
|
||||||
PlayerInput input;
|
PlayerInput input;
|
||||||
CameraControl camControl;
|
CameraControl camControl;
|
||||||
BlocksController* blocksController;
|
BlocksController* blocksController;
|
||||||
|
|||||||
@ -33,7 +33,7 @@ Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv)
|
|||||||
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 "../interfaces/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 : public Object, public 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,19 +8,27 @@
|
|||||||
#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 "../interfaces/Object.h"
|
||||||
#include "../objects/Player.h"
|
#include "../objects/Player.h"
|
||||||
#include "../items/Inventory.h"
|
#include "../items/Inventory.h"
|
||||||
#include "../items/Inventories.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),
|
: 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)
|
||||||
{
|
{
|
||||||
|
objCounter = 0;
|
||||||
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 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+
|
uint matrixSize = (settings.chunks.loadDistance+
|
||||||
settings.chunks.padding) * 2;
|
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 = std::make_unique<Inventories>(*this);
|
||||||
|
inventories->store(player->getInventory());
|
||||||
}
|
}
|
||||||
|
|
||||||
Level::~Level(){
|
Level::~Level(){
|
||||||
delete chunks;
|
delete chunks;
|
||||||
delete events;
|
delete events;
|
||||||
delete physics;
|
delete physics;
|
||||||
delete player;
|
|
||||||
delete lighting;
|
delete lighting;
|
||||||
delete chunksStorage;
|
delete chunksStorage;
|
||||||
|
|
||||||
|
for(auto obj : objects)
|
||||||
|
{
|
||||||
|
obj.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::update() {
|
void Level::update() {
|
||||||
@ -58,3 +71,18 @@ void Level::update() {
|
|||||||
World* Level::getWorld() {
|
World* Level::getWorld() {
|
||||||
return world.get();
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -5,10 +5,14 @@
|
|||||||
|
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
#include "../settings.h"
|
#include "../settings.h"
|
||||||
|
#include <list>
|
||||||
|
#include <vector>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
class Content;
|
class Content;
|
||||||
class World;
|
class World;
|
||||||
class Player;
|
class Player;
|
||||||
|
class Object;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
class Inventory;
|
class Inventory;
|
||||||
class Inventories;
|
class Inventories;
|
||||||
@ -18,10 +22,13 @@ class PhysicsSolver;
|
|||||||
class ChunksStorage;
|
class ChunksStorage;
|
||||||
|
|
||||||
class Level {
|
class Level {
|
||||||
|
private:
|
||||||
|
int objCounter;
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<World> world;
|
std::unique_ptr<World> world;
|
||||||
const Content* const content;
|
const Content* const content;
|
||||||
Player* player;
|
std::list<std::shared_ptr<Object>> objects;
|
||||||
|
std::shared_ptr<Player> player;
|
||||||
Chunks* chunks;
|
Chunks* chunks;
|
||||||
ChunksStorage* chunksStorage;
|
ChunksStorage* chunksStorage;
|
||||||
std::unique_ptr<Inventories> inventories;
|
std::unique_ptr<Inventories> inventories;
|
||||||
@ -34,13 +41,18 @@ 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();
|
||||||
|
|
||||||
|
// 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_ */
|
#endif /* WORLD_LEVEL_H_ */
|
||||||
|
|||||||
@ -64,10 +64,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;
|
|
||||||
const int DEF_PLAYER_INVENTORY_SIZE = 40;
|
|
||||||
|
|
||||||
Level* World::create(std::string name,
|
Level* World::create(std::string name,
|
||||||
fs::path directory,
|
fs::path directory,
|
||||||
uint64_t seed,
|
uint64_t seed,
|
||||||
@ -75,12 +71,7 @@ Level* World::create(std::string name,
|
|||||||
const Content* content,
|
const Content* content,
|
||||||
const std::vector<ContentPack>& packs) {
|
const std::vector<ContentPack>& packs) {
|
||||||
auto world = new World(name, directory, seed, settings, content, packs);
|
auto world = new World(name, directory, seed, settings, content, packs);
|
||||||
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
|
auto level = new Level(world, content, settings);
|
||||||
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());
|
|
||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,13 +88,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");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
|
auto level = new Level(world.get(), content, settings);
|
||||||
auto player = new Player(
|
wfile->readPlayer(level->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());
|
|
||||||
world.release();
|
world.release();
|
||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,7 +26,7 @@ public:
|
|||||||
world_load_error(std::string message);
|
world_load_error(std::string message);
|
||||||
};
|
};
|
||||||
|
|
||||||
class World : Serializable {
|
class World : public Serializable {
|
||||||
std::string name;
|
std::string name;
|
||||||
uint64_t seed;
|
uint64_t seed;
|
||||||
EngineSettings& settings;
|
EngineSettings& settings;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user