One step closer to multiplayer
This commit is contained in:
parent
9d9c9f5570
commit
0157213a56
@ -34,9 +34,10 @@
|
|||||||
#include "graphics/Skybox.h"
|
#include "graphics/Skybox.h"
|
||||||
#include "graphics/ChunksRenderer.h"
|
#include "graphics/ChunksRenderer.h"
|
||||||
|
|
||||||
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend)
|
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player)
|
||||||
: engine(engine),
|
: engine(engine),
|
||||||
level(frontend->getLevel())
|
level(frontend->getLevel()),
|
||||||
|
player(player)
|
||||||
{
|
{
|
||||||
frustumCulling = std::make_unique<Frustum>();
|
frustumCulling = std::make_unique<Frustum>();
|
||||||
lineBatch = std::make_unique<LineBatch>();
|
lineBatch = std::make_unique<LineBatch>();
|
||||||
@ -174,7 +175,6 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
|||||||
shader->uniform3f("u_cameraPos", camera->position);
|
shader->uniform3f("u_cameraPos", camera->position);
|
||||||
shader->uniform1i("u_cubemap", 1);
|
shader->uniform1i("u_cubemap", 1);
|
||||||
{
|
{
|
||||||
auto player = level->player;
|
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
||||||
ItemDef* item = indices->getItemDef(stack.getItemId());
|
ItemDef* item = indices->getItemDef(stack.getItemId());
|
||||||
@ -191,7 +191,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
|||||||
skybox->bind();
|
skybox->bind();
|
||||||
atlas->getTexture()->bind();
|
atlas->getTexture()->bind();
|
||||||
|
|
||||||
drawChunks(level->chunks, camera, shader);
|
drawChunks(level->chunks.get(), camera, shader);
|
||||||
|
|
||||||
// Selected block
|
// Selected block
|
||||||
if (PlayerController::selectedBlockId != -1 && hudVisible){
|
if (PlayerController::selectedBlockId != -1 && hudVisible){
|
||||||
@ -213,7 +213,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
|||||||
const glm::vec3 center = pos + hitbox.center();
|
const glm::vec3 center = pos + hitbox.center();
|
||||||
const glm::vec3 size = hitbox.size();
|
const glm::vec3 size = hitbox.size();
|
||||||
lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f));
|
lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f));
|
||||||
if (level->player->debug) {
|
if (player->debug) {
|
||||||
lineBatch->line(point, point+norm*0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f));
|
lineBatch->line(point, point+norm*0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -222,7 +222,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
|||||||
skybox->unbind();
|
skybox->unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hudVisible && level->player->debug) {
|
if (hudVisible && player->debug) {
|
||||||
GfxContext ctx = pctx.sub();
|
GfxContext ctx = pctx.sub();
|
||||||
ctx.depthTest(true);
|
ctx.depthTest(true);
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
|||||||
|
|
||||||
if (settings.debug.showChunkBorders){
|
if (settings.debug.showChunkBorders){
|
||||||
linesShader->uniformMatrix("u_projview", camera->getProjView());
|
linesShader->uniformMatrix("u_projview", camera->getProjView());
|
||||||
glm::vec3 coord = level->player->camera->position;
|
glm::vec3 coord = player->camera->position;
|
||||||
if (coord.x < 0) coord.x--;
|
if (coord.x < 0) coord.x--;
|
||||||
if (coord.z < 0) coord.z--;
|
if (coord.z < 0) coord.z--;
|
||||||
int cx = floordiv((int)coord.x, CHUNK_W);
|
int cx = floordiv((int)coord.x, CHUNK_W);
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
#include "../graphics/GfxContext.h"
|
#include "../graphics/GfxContext.h"
|
||||||
|
|
||||||
class Level;
|
class Level;
|
||||||
|
class Player;
|
||||||
class Camera;
|
class Camera;
|
||||||
class Batch3D;
|
class Batch3D;
|
||||||
class LineBatch;
|
class LineBatch;
|
||||||
@ -29,6 +30,7 @@ class Skybox;
|
|||||||
class WorldRenderer {
|
class WorldRenderer {
|
||||||
Engine* engine;
|
Engine* engine;
|
||||||
Level* level;
|
Level* level;
|
||||||
|
Player* player;
|
||||||
std::unique_ptr<Frustum> frustumCulling;
|
std::unique_ptr<Frustum> frustumCulling;
|
||||||
std::unique_ptr<LineBatch> lineBatch;
|
std::unique_ptr<LineBatch> lineBatch;
|
||||||
std::unique_ptr<ChunksRenderer> renderer;
|
std::unique_ptr<ChunksRenderer> renderer;
|
||||||
@ -37,7 +39,7 @@ class WorldRenderer {
|
|||||||
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
|
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
|
||||||
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
|
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
|
||||||
public:
|
public:
|
||||||
WorldRenderer(Engine* engine, LevelFrontend* frontend);
|
WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player);
|
||||||
~WorldRenderer();
|
~WorldRenderer();
|
||||||
|
|
||||||
void draw(const GfxContext& context, Camera* camera, bool hudVisible);
|
void draw(const GfxContext& context, Camera* camera, bool hudVisible);
|
||||||
|
|||||||
@ -84,14 +84,14 @@ void ChunksRenderer::threadLoop(int index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ChunksRenderer::process(std::shared_ptr<Chunk> chunk, BlocksRenderer& renderer) {
|
void ChunksRenderer::process(std::shared_ptr<Chunk> chunk, BlocksRenderer& renderer) {
|
||||||
renderer.build(chunk.get(), level->chunksStorage);
|
renderer.build(chunk.get(), level->chunksStorage.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Mesh> ChunksRenderer::render(std::shared_ptr<Chunk> chunk, bool important) {
|
std::shared_ptr<Mesh> ChunksRenderer::render(std::shared_ptr<Chunk> chunk, bool important) {
|
||||||
chunk->setModified(false);
|
chunk->setModified(false);
|
||||||
|
|
||||||
if (important) {
|
if (important) {
|
||||||
Mesh* mesh = renderer->render(chunk.get(), level->chunksStorage);
|
Mesh* mesh = renderer->render(chunk.get(), level->chunksStorage.get());
|
||||||
auto sptr = std::shared_ptr<Mesh>(mesh);
|
auto sptr = std::shared_ptr<Mesh>(mesh);
|
||||||
meshes[glm::ivec2(chunk->x, chunk->z)] = sptr;
|
meshes[glm::ivec2(chunk->x, chunk->z)] = sptr;
|
||||||
return sptr;
|
return sptr;
|
||||||
|
|||||||
@ -40,6 +40,7 @@
|
|||||||
#include "gui/UINode.h"
|
#include "gui/UINode.h"
|
||||||
#include "gui/GUI.h"
|
#include "gui/GUI.h"
|
||||||
#include "ContentGfxCache.h"
|
#include "ContentGfxCache.h"
|
||||||
|
#include "menu/menu.h"
|
||||||
#include "screens.h"
|
#include "screens.h"
|
||||||
#include "WorldRenderer.h"
|
#include "WorldRenderer.h"
|
||||||
#include "BlocksPreview.h"
|
#include "BlocksPreview.h"
|
||||||
@ -122,11 +123,10 @@ std::shared_ptr<UINode> Hud::createDebugPanel(Engine* engine) {
|
|||||||
L" visible: "+std::to_wstring(level->chunks->visible);
|
L" visible: "+std::to_wstring(level->chunks->visible);
|
||||||
}));
|
}));
|
||||||
panel->add(create_label([=](){
|
panel->add(create_label([=](){
|
||||||
auto player = level->player;
|
|
||||||
auto* indices = level->content->getIndices();
|
auto* indices = level->content->getIndices();
|
||||||
auto def = indices->getBlockDef(player->selectedVoxel.id);
|
auto def = indices->getBlockDef(player->selectedVoxel.id);
|
||||||
std::wstringstream stream;
|
std::wstringstream stream;
|
||||||
stream << std::hex << level->player->selectedVoxel.states;
|
stream << std::hex << player->selectedVoxel.states;
|
||||||
if (def) {
|
if (def) {
|
||||||
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
|
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
|
||||||
}
|
}
|
||||||
@ -151,19 +151,19 @@ std::shared_ptr<UINode> Hud::createDebugPanel(Engine* engine) {
|
|||||||
// Coord input
|
// Coord input
|
||||||
auto box = std::make_shared<TextBox>(L"");
|
auto box = std::make_shared<TextBox>(L"");
|
||||||
box->setTextSupplier([=]() {
|
box->setTextSupplier([=]() {
|
||||||
Hitbox* hitbox = level->player->hitbox.get();
|
Hitbox* hitbox = player->hitbox.get();
|
||||||
return util::to_wstring(hitbox->position[ax], 2);
|
return util::to_wstring(hitbox->position[ax], 2);
|
||||||
});
|
});
|
||||||
box->setTextConsumer([=](std::wstring text) {
|
box->setTextConsumer([=](std::wstring text) {
|
||||||
try {
|
try {
|
||||||
glm::vec3 position = level->player->hitbox->position;
|
glm::vec3 position = player->hitbox->position;
|
||||||
position[ax] = std::stoi(text);
|
position[ax] = std::stoi(text);
|
||||||
level->player->teleport(position);
|
player->teleport(position);
|
||||||
} catch (std::invalid_argument& _){
|
} catch (std::invalid_argument& _){
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
box->setOnEditStart([=](){
|
box->setOnEditStart([=](){
|
||||||
Hitbox* hitbox = level->player->hitbox.get();
|
Hitbox* hitbox = player->hitbox.get();
|
||||||
box->setText(std::to_wstring(int(hitbox->position[ax])));
|
box->setText(std::to_wstring(int(hitbox->position[ax])));
|
||||||
});
|
});
|
||||||
box->setSize(glm::vec2(230, 27));
|
box->setSize(glm::vec2(230, 27));
|
||||||
@ -212,7 +212,6 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
|
|||||||
auto level = frontend->getLevel();
|
auto level = frontend->getLevel();
|
||||||
auto content = level->content;
|
auto content = level->content;
|
||||||
auto indices = content->getIndices();
|
auto indices = content->getIndices();
|
||||||
auto player = level->player;
|
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
|
|
||||||
int itemsCount = indices->countItemDefs();
|
int itemsCount = indices->countItemDefs();
|
||||||
@ -239,8 +238,6 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<InventoryView> Hud::createHotbar() {
|
std::shared_ptr<InventoryView> Hud::createHotbar() {
|
||||||
auto level = frontend->getLevel();
|
|
||||||
auto player = level->player;
|
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
|
|
||||||
SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr);
|
SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr);
|
||||||
@ -254,10 +251,12 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
Hud::Hud(Engine* engine, LevelFrontend* frontend)
|
Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
|
||||||
: assets(engine->getAssets()),
|
: engine(engine),
|
||||||
|
assets(engine->getAssets()),
|
||||||
gui(engine->getGUI()),
|
gui(engine->getGUI()),
|
||||||
frontend(frontend)
|
frontend(frontend),
|
||||||
|
player(player)
|
||||||
{
|
{
|
||||||
auto menu = gui->getMenu();
|
auto menu = gui->getMenu();
|
||||||
|
|
||||||
@ -335,7 +334,6 @@ void Hud::cleanup() {
|
|||||||
|
|
||||||
void Hud::update(bool visible) {
|
void Hud::update(bool visible) {
|
||||||
auto level = frontend->getLevel();
|
auto level = frontend->getLevel();
|
||||||
auto player = level->player;
|
|
||||||
auto menu = gui->getMenu();
|
auto menu = gui->getMenu();
|
||||||
|
|
||||||
debugPanel->setVisible(player->debug && visible);
|
debugPanel->setVisible(player->debug && visible);
|
||||||
@ -419,8 +417,6 @@ void Hud::update(bool visible) {
|
|||||||
* Show inventory on the screen and turn on inventory mode blocking movement
|
* Show inventory on the screen and turn on inventory mode blocking movement
|
||||||
*/
|
*/
|
||||||
void Hud::openInventory() {
|
void Hud::openInventory() {
|
||||||
auto level = frontend->getLevel();
|
|
||||||
auto player = level->player;
|
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
|
|
||||||
inventoryOpen = true;
|
inventoryOpen = true;
|
||||||
@ -472,8 +468,6 @@ void Hud::openPermanent(UiDocument* doc) {
|
|||||||
|
|
||||||
auto invview = std::dynamic_pointer_cast<InventoryView>(root);
|
auto invview = std::dynamic_pointer_cast<InventoryView>(root);
|
||||||
if (invview) {
|
if (invview) {
|
||||||
auto level = frontend->getLevel();
|
|
||||||
auto player = level->player;
|
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
invview->bind(inventory, frontend, interaction.get());
|
invview->bind(inventory, frontend, interaction.get());
|
||||||
}
|
}
|
||||||
@ -548,9 +542,6 @@ void Hud::remove(std::shared_ptr<UINode> node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Hud::draw(const GfxContext& ctx){
|
void Hud::draw(const GfxContext& ctx){
|
||||||
auto level = frontend->getLevel();
|
|
||||||
auto player = level->player;
|
|
||||||
|
|
||||||
const Viewport& viewport = ctx.getViewport();
|
const Viewport& viewport = ctx.getViewport();
|
||||||
const uint width = viewport.getWidth();
|
const uint width = viewport.getWidth();
|
||||||
const uint height = viewport.getHeight();
|
const uint height = viewport.getHeight();
|
||||||
@ -568,7 +559,7 @@ void Hud::draw(const GfxContext& ctx){
|
|||||||
hotbarView->setSelected(player->getChosenSlot());
|
hotbarView->setSelected(player->getChosenSlot());
|
||||||
|
|
||||||
// Crosshair
|
// Crosshair
|
||||||
if (!pause && Events::_cursor_locked && !level->player->debug) {
|
if (!pause && Events::_cursor_locked && !player->debug) {
|
||||||
GfxContext chctx = ctx.sub();
|
GfxContext chctx = ctx.sub();
|
||||||
chctx.blendMode(blendmode::inversion);
|
chctx.blendMode(blendmode::inversion);
|
||||||
auto texture = assets->getTexture("gui/crosshair");
|
auto texture = assets->getTexture("gui/crosshair");
|
||||||
@ -583,7 +574,7 @@ void Hud::draw(const GfxContext& ctx){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delta-time visualizer
|
// Delta-time visualizer
|
||||||
if (level->player->debug) {
|
if (player->debug) {
|
||||||
batch->texture(nullptr);
|
batch->texture(nullptr);
|
||||||
const int dmwidth = 256;
|
const int dmwidth = 256;
|
||||||
const float dmscale = 4000.0f;
|
const float dmscale = 4000.0f;
|
||||||
@ -648,6 +639,7 @@ void Hud::setPause(bool pause) {
|
|||||||
|
|
||||||
auto menu = gui->getMenu();
|
auto menu = gui->getMenu();
|
||||||
if (pause) {
|
if (pause) {
|
||||||
|
menus::create_pause_panel(engine, frontend->getLevel());
|
||||||
menu->setPage("pause");
|
menu->setPage("pause");
|
||||||
} else {
|
} else {
|
||||||
menu->reset();
|
menu->reset();
|
||||||
@ -656,6 +648,6 @@ void Hud::setPause(bool pause) {
|
|||||||
menu->setVisible(pause);
|
menu->setVisible(pause);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Player> Hud::getPlayer() const {
|
Player* Hud::getPlayer() const {
|
||||||
return frontend->getLevel()->player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,6 +65,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
class Hud {
|
class Hud {
|
||||||
|
Engine* engine;
|
||||||
Assets* assets;
|
Assets* assets;
|
||||||
std::unique_ptr<Camera> uicamera;
|
std::unique_ptr<Camera> uicamera;
|
||||||
|
|
||||||
@ -84,6 +85,7 @@ class Hud {
|
|||||||
std::shared_ptr<SlotView> grabbedItemView;
|
std::shared_ptr<SlotView> grabbedItemView;
|
||||||
gui::GUI* gui;
|
gui::GUI* gui;
|
||||||
LevelFrontend* frontend;
|
LevelFrontend* frontend;
|
||||||
|
Player* player;
|
||||||
|
|
||||||
std::vector<HudElement> elements;
|
std::vector<HudElement> elements;
|
||||||
|
|
||||||
@ -98,7 +100,7 @@ class Hud {
|
|||||||
|
|
||||||
void cleanup();
|
void cleanup();
|
||||||
public:
|
public:
|
||||||
Hud(Engine* engine, LevelFrontend* frontend);
|
Hud(Engine* engine, LevelFrontend* frontend, Player* player);
|
||||||
~Hud();
|
~Hud();
|
||||||
|
|
||||||
void update(bool hudVisible);
|
void update(bool hudVisible);
|
||||||
@ -118,7 +120,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);
|
||||||
|
|
||||||
std::shared_ptr<Player> getPlayer() const;
|
Player* getPlayer() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SRC_HUD_H_ */
|
#endif /* SRC_HUD_H_ */
|
||||||
|
|||||||
@ -87,16 +87,14 @@ void MenuScreen::draw(float delta) {
|
|||||||
|
|
||||||
static bool backlight;
|
static bool backlight;
|
||||||
|
|
||||||
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine), level(level){
|
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
|
||||||
menus::create_pause_panel(engine, level);
|
|
||||||
|
|
||||||
auto& settings = engine->getSettings();
|
auto& settings = engine->getSettings();
|
||||||
auto assets = engine->getAssets();
|
auto assets = engine->getAssets();
|
||||||
|
|
||||||
controller = std::make_unique<LevelController>(settings, level);
|
controller = std::make_unique<LevelController>(settings, level);
|
||||||
frontend = std::make_unique<LevelFrontend>(level, assets);
|
frontend = std::make_unique<LevelFrontend>(level, assets);
|
||||||
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get());
|
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get(), controller->getPlayer());
|
||||||
hud = std::make_unique<Hud>(engine, frontend.get());
|
hud = std::make_unique<Hud>(engine, frontend.get(), controller->getPlayer());
|
||||||
|
|
||||||
backlight = settings.graphics.backlight;
|
backlight = settings.graphics.backlight;
|
||||||
|
|
||||||
@ -119,8 +117,8 @@ LevelScreen::~LevelScreen() {
|
|||||||
std::cout << "-- writing world" << std::endl;
|
std::cout << "-- writing world" << std::endl;
|
||||||
scripting::on_frontend_close();
|
scripting::on_frontend_close();
|
||||||
controller->onWorldSave();
|
controller->onWorldSave();
|
||||||
auto world = level->getWorld();
|
auto world = controller->getLevel()->getWorld();
|
||||||
world->write(level.get());
|
world->write(controller->getLevel());
|
||||||
controller->onWorldQuit();
|
controller->onWorldQuit();
|
||||||
engine->getPaths()->setWorldFolder(fs::path());
|
engine->getPaths()->setWorldFolder(fs::path());
|
||||||
}
|
}
|
||||||
@ -134,10 +132,10 @@ void LevelScreen::updateHotkeys() {
|
|||||||
hudVisible = !hudVisible;
|
hudVisible = !hudVisible;
|
||||||
}
|
}
|
||||||
if (Events::jpressed(keycode::F3)) {
|
if (Events::jpressed(keycode::F3)) {
|
||||||
level->player->debug = !level->player->debug;
|
controller->getPlayer()->debug = !controller->getPlayer()->debug;
|
||||||
}
|
}
|
||||||
if (Events::jpressed(keycode::F5)) {
|
if (Events::jpressed(keycode::F5)) {
|
||||||
level->chunks->saveAndClear();
|
controller->getLevel()->chunks->saveAndClear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,14 +151,14 @@ void LevelScreen::update(float delta) {
|
|||||||
|
|
||||||
// TODO: subscribe for setting change
|
// TODO: subscribe for setting change
|
||||||
EngineSettings& settings = engine->getSettings();
|
EngineSettings& settings = engine->getSettings();
|
||||||
level->player->camera->setFov(glm::radians(settings.camera.fov));
|
controller->getPlayer()->camera->setFov(glm::radians(settings.camera.fov));
|
||||||
if (settings.graphics.backlight != backlight) {
|
if (settings.graphics.backlight != backlight) {
|
||||||
level->chunks->saveAndClear();
|
controller->getLevel()->chunks->saveAndClear();
|
||||||
backlight = settings.graphics.backlight;
|
backlight = settings.graphics.backlight;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hud->isPause()) {
|
if (!hud->isPause()) {
|
||||||
level->world->updateTimers(delta);
|
controller->getLevel()->world->updateTimers(delta);
|
||||||
animator->update(delta);
|
animator->update(delta);
|
||||||
}
|
}
|
||||||
controller->update(delta, !inputLocked, hud->isPause());
|
controller->update(delta, !inputLocked, hud->isPause());
|
||||||
@ -168,7 +166,7 @@ void LevelScreen::update(float delta) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LevelScreen::draw(float delta) {
|
void LevelScreen::draw(float delta) {
|
||||||
auto camera = level->player->currentCamera;
|
auto camera = controller->getPlayer()->currentCamera;
|
||||||
|
|
||||||
Viewport viewport(Window::width, Window::height);
|
Viewport viewport(Window::width, Window::height);
|
||||||
GfxContext ctx(nullptr, viewport, batch.get());
|
GfxContext ctx(nullptr, viewport, batch.get());
|
||||||
@ -177,12 +175,8 @@ void LevelScreen::draw(float delta) {
|
|||||||
|
|
||||||
if (hudVisible) {
|
if (hudVisible) {
|
||||||
hud->draw(ctx);
|
hud->draw(ctx);
|
||||||
if (level->player->debug) {
|
if (controller->getPlayer()->debug) {
|
||||||
hud->drawDebug(1 / delta);
|
hud->drawDebug(1 / delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Level* LevelScreen::getLevel() const {
|
|
||||||
return level.get();
|
|
||||||
}
|
|
||||||
|
|||||||
@ -38,7 +38,6 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
class LevelScreen : public Screen {
|
class LevelScreen : public Screen {
|
||||||
std::unique_ptr<Level> level;
|
|
||||||
std::unique_ptr<LevelFrontend> frontend;
|
std::unique_ptr<LevelFrontend> frontend;
|
||||||
std::unique_ptr<Hud> hud;
|
std::unique_ptr<Hud> hud;
|
||||||
std::unique_ptr<WorldRenderer> worldRenderer;
|
std::unique_ptr<WorldRenderer> worldRenderer;
|
||||||
@ -53,8 +52,6 @@ public:
|
|||||||
|
|
||||||
void update(float delta) override;
|
void update(float delta) override;
|
||||||
void draw(float delta) override;
|
void draw(float delta) override;
|
||||||
|
|
||||||
Level* getLevel() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FRONTEND_SCREENS_H_
|
#endif // FRONTEND_SCREENS_H_
|
||||||
|
|||||||
@ -53,8 +53,8 @@ int Clock::getTickId() const {
|
|||||||
|
|
||||||
BlocksController::BlocksController(Level* level, uint padding)
|
BlocksController::BlocksController(Level* level, uint padding)
|
||||||
: level(level),
|
: level(level),
|
||||||
chunks(level->chunks),
|
chunks(level->chunks.get()),
|
||||||
lighting(level->lighting),
|
lighting(level->lighting.get()),
|
||||||
randTickClock(20, 3),
|
randTickClock(20, 3),
|
||||||
blocksTickClock(20, 1),
|
blocksTickClock(20, 1),
|
||||||
padding(padding) {
|
padding(padding) {
|
||||||
|
|||||||
@ -28,6 +28,7 @@ public:
|
|||||||
int getTickId() const;
|
int getTickId() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* BlocksController manages block updates and block data (aka inventories) */
|
||||||
class BlocksController {
|
class BlocksController {
|
||||||
Level* level;
|
Level* level;
|
||||||
Chunks* chunks;
|
Chunks* chunks;
|
||||||
|
|||||||
@ -24,8 +24,8 @@ const uint MIN_SURROUNDING = 9;
|
|||||||
|
|
||||||
ChunksController::ChunksController(Level* level, uint padding)
|
ChunksController::ChunksController(Level* level, uint padding)
|
||||||
: level(level),
|
: level(level),
|
||||||
chunks(level->chunks),
|
chunks(level->chunks.get()),
|
||||||
lighting(level->lighting),
|
lighting(level->lighting.get()),
|
||||||
padding(padding),
|
padding(padding),
|
||||||
generator(WorldGenerators::createGenerator(level->getWorld()->getGenerator(), level->content)) {
|
generator(WorldGenerators::createGenerator(level->getWorld()->getGenerator(), level->content)) {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,32 +1,32 @@
|
|||||||
#include "LevelController.h"
|
#include "LevelController.h"
|
||||||
#include "../world/Level.h"
|
#include "../world/Level.h"
|
||||||
|
#include "../physics/Hitbox.h"
|
||||||
#include "PlayerController.h"
|
|
||||||
#include "BlocksController.h"
|
|
||||||
#include "ChunksController.h"
|
|
||||||
|
|
||||||
#include "scripting/scripting.h"
|
#include "scripting/scripting.h"
|
||||||
#include "../interfaces/Object.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),
|
||||||
blocks = std::make_unique<BlocksController>(level, settings.chunks.padding);
|
blocks(std::make_unique<BlocksController>(level, settings.chunks.padding)),
|
||||||
chunks = std::make_unique<ChunksController>(level, settings.chunks.padding);
|
chunks(std::make_unique<ChunksController>(level, settings.chunks.padding)),
|
||||||
player = std::make_unique<PlayerController>(level, settings, blocks.get());
|
player(std::make_unique<PlayerController>(level, settings, blocks.get())) {
|
||||||
|
|
||||||
scripting::on_world_load(level, blocks.get());
|
scripting::on_world_load(level, blocks.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
LevelController::~LevelController() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void LevelController::update(float delta, bool input, bool pause) {
|
void LevelController::update(float delta, bool input, bool pause) {
|
||||||
player->update(delta, input, pause);
|
player->update(delta, input, pause);
|
||||||
level->update();
|
glm::vec3 position = player->getPlayer()->hitbox->position;
|
||||||
|
level->loadMatrix(position.x, position.z, settings.chunks.loadDistance + settings.chunks.padding * 2);
|
||||||
chunks->update(settings.chunks.loadSpeed);
|
chunks->update(settings.chunks.loadSpeed);
|
||||||
|
|
||||||
// erease null pointers
|
// erease null pointers
|
||||||
level->objects.remove_if([](auto obj) { return obj == nullptr; });
|
level->objects.erase(
|
||||||
|
std::remove_if(
|
||||||
|
level->objects.begin(), level->objects.end(),
|
||||||
|
[](auto obj) { return obj == nullptr; }),
|
||||||
|
level->objects.end()
|
||||||
|
);
|
||||||
|
|
||||||
if (!pause) {
|
if (!pause) {
|
||||||
// update all objects that needed
|
// update all objects that needed
|
||||||
@ -49,3 +49,11 @@ void LevelController::onWorldSave() {
|
|||||||
void LevelController::onWorldQuit() {
|
void LevelController::onWorldQuit() {
|
||||||
scripting::on_world_quit();
|
scripting::on_world_quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Level* LevelController::getLevel() {
|
||||||
|
return level.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Player* LevelController::getPlayer() {
|
||||||
|
return player->getPlayer();
|
||||||
|
}
|
||||||
|
|||||||
@ -4,22 +4,23 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include "../settings.h"
|
#include "../settings.h"
|
||||||
|
|
||||||
|
#include "PlayerController.h"
|
||||||
|
#include "BlocksController.h"
|
||||||
|
#include "ChunksController.h"
|
||||||
|
|
||||||
class Level;
|
class Level;
|
||||||
class BlocksController;
|
class Player;
|
||||||
class ChunksController;
|
|
||||||
class PlayerController;
|
|
||||||
|
|
||||||
/* LevelController manages other controllers */
|
/* LevelController manages other controllers */
|
||||||
class LevelController {
|
class LevelController {
|
||||||
EngineSettings& settings;
|
EngineSettings& settings;
|
||||||
Level* level;
|
std::unique_ptr<Level> level;
|
||||||
// Sub-controllers
|
// Sub-controllers
|
||||||
std::unique_ptr<BlocksController> blocks;
|
std::unique_ptr<BlocksController> blocks;
|
||||||
std::unique_ptr<ChunksController> chunks;
|
std::unique_ptr<ChunksController> chunks;
|
||||||
std::unique_ptr<PlayerController> player;
|
std::unique_ptr<PlayerController> player;
|
||||||
public:
|
public:
|
||||||
LevelController(EngineSettings& settings, Level* level);
|
LevelController(EngineSettings& settings, Level* level);
|
||||||
~LevelController();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@param delta time elapsed since the last update
|
@param delta time elapsed since the last update
|
||||||
@ -32,6 +33,9 @@ public:
|
|||||||
|
|
||||||
void onWorldSave();
|
void onWorldSave();
|
||||||
void onWorldQuit();
|
void onWorldQuit();
|
||||||
|
|
||||||
|
Level* getLevel();
|
||||||
|
Player* getPlayer();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LOGIC_LEVEL_CONTROLLER_H_
|
#endif // LOGIC_LEVEL_CONTROLLER_H_
|
||||||
|
|||||||
@ -142,8 +142,8 @@ PlayerController::PlayerController(
|
|||||||
const EngineSettings& settings,
|
const EngineSettings& settings,
|
||||||
BlocksController* blocksController)
|
BlocksController* blocksController)
|
||||||
: level(level),
|
: level(level),
|
||||||
player(level->player),
|
player(level->getObject<Player>(0)),
|
||||||
camControl(level->player, settings.camera),
|
camControl(player, settings.camera),
|
||||||
blocksController(blocksController) {
|
blocksController(blocksController) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,7 +186,7 @@ void PlayerController::updateCamera(float delta, bool movement) {
|
|||||||
if (movement) {
|
if (movement) {
|
||||||
camControl.updateMouse(input);
|
camControl.updateMouse(input);
|
||||||
}
|
}
|
||||||
camControl.update(input, delta, level->chunks);
|
camControl.update(input, delta, level->chunks.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::resetKeyboard() {
|
void PlayerController::resetKeyboard() {
|
||||||
@ -207,9 +207,8 @@ void PlayerController::updateControls(float 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.get();
|
||||||
Player* player = level->player.get();
|
Lighting* lighting = level->lighting.get();
|
||||||
Lighting* lighting = level->lighting;
|
|
||||||
Camera* camera = player->camera.get();
|
Camera* camera = player->camera.get();
|
||||||
glm::vec3 end;
|
glm::vec3 end;
|
||||||
glm::ivec3 iend;
|
glm::ivec3 iend;
|
||||||
@ -269,24 +268,24 @@ void PlayerController::updateInteraction(){
|
|||||||
|
|
||||||
if (lclick) {
|
if (lclick) {
|
||||||
if (!input.shift && item->rt.funcsset.on_block_break_by) {
|
if (!input.shift && item->rt.funcsset.on_block_break_by) {
|
||||||
if (scripting::on_item_break_block(player, item, x, y, z))
|
if (scripting::on_item_break_block(player.get(), item, x, y, z))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Block* target = indices->getBlockDef(vox->id);
|
Block* target = indices->getBlockDef(vox->id);
|
||||||
if (lclick && target->breakable){
|
if (lclick && target->breakable){
|
||||||
blocksController->breakBlock(player, target, x, y, z);
|
blocksController->breakBlock(player.get(), target, x, y, z);
|
||||||
}
|
}
|
||||||
if (rclick) {
|
if (rclick) {
|
||||||
if (!input.shift && item->rt.funcsset.on_use_on_block) {
|
if (!input.shift && item->rt.funcsset.on_use_on_block) {
|
||||||
if (scripting::on_item_use_on_block(player, item, x, y, z))
|
if (scripting::on_item_use_on_block(player.get(), item, x, y, z))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (def && rclick){
|
if (def && rclick){
|
||||||
if (!input.shift && target->rt.funcsset.oninteract) {
|
if (!input.shift && target->rt.funcsset.oninteract) {
|
||||||
if (scripting::on_block_interact(player, target, x, y, z))
|
if (scripting::on_block_interact(player.get(), target, x, y, z))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!target->replaceable){
|
if (!target->replaceable){
|
||||||
@ -310,7 +309,7 @@ void PlayerController::updateInteraction(){
|
|||||||
chunks->set(x, y, z, chosenBlock, states);
|
chunks->set(x, y, z, chosenBlock, states);
|
||||||
lighting->onBlockSet(x,y,z, chosenBlock);
|
lighting->onBlockSet(x,y,z, chosenBlock);
|
||||||
if (def->rt.funcsset.onplaced) {
|
if (def->rt.funcsset.onplaced) {
|
||||||
scripting::on_block_placed(player, def, x, y, z);
|
scripting::on_block_placed(player.get(), def, x, y, z);
|
||||||
}
|
}
|
||||||
blocksController->updateSides(x, y, z);
|
blocksController->updateSides(x, y, z);
|
||||||
}
|
}
|
||||||
@ -337,3 +336,7 @@ void PlayerController::updateInteraction(){
|
|||||||
selectedBlockStates = 0;
|
selectedBlockStates = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Player* PlayerController::getPlayer() {
|
||||||
|
return player.get();
|
||||||
|
}
|
||||||
|
|||||||
@ -49,6 +49,8 @@ public:
|
|||||||
const EngineSettings& settings,
|
const EngineSettings& settings,
|
||||||
BlocksController* blocksController);
|
BlocksController* blocksController);
|
||||||
void update(float delta, bool input, bool pause);
|
void update(float delta, bool input, bool pause);
|
||||||
|
|
||||||
|
Player* getPlayer();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* PLAYER_CONTROL_H_ */
|
#endif /* PLAYER_CONTROL_H_ */
|
||||||
|
|||||||
@ -11,9 +11,9 @@
|
|||||||
/* == player library ==*/
|
/* == player library ==*/
|
||||||
int l_player_get_pos(lua_State* L) {
|
int l_player_get_pos(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
if (playerid != 1)
|
auto player = scripting::level->getObject<Player>(playerid);
|
||||||
return 0;
|
if (!player) return 0;
|
||||||
glm::vec3 pos = scripting::level->player->hitbox->position;
|
glm::vec3 pos = player->hitbox->position;
|
||||||
lua_pushnumber(L, pos.x);
|
lua_pushnumber(L, pos.x);
|
||||||
lua_pushnumber(L, pos.y);
|
lua_pushnumber(L, pos.y);
|
||||||
lua_pushnumber(L, pos.z);
|
lua_pushnumber(L, pos.z);
|
||||||
@ -22,20 +22,19 @@ int l_player_get_pos(lua_State* L) {
|
|||||||
|
|
||||||
int l_player_set_pos(lua_State* L) {
|
int l_player_set_pos(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
if (playerid != 1)
|
|
||||||
return 0;
|
|
||||||
lua::luanumber x = lua_tonumber(L, 2);
|
lua::luanumber x = lua_tonumber(L, 2);
|
||||||
lua::luanumber y = lua_tonumber(L, 3);
|
lua::luanumber y = lua_tonumber(L, 3);
|
||||||
lua::luanumber z = lua_tonumber(L, 4);
|
lua::luanumber z = lua_tonumber(L, 4);
|
||||||
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
|
auto player = scripting::level->getObject<Player>(playerid);
|
||||||
|
if (player) player->hitbox->position = glm::vec3(x, y, z);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_player_get_vel(lua_State* L) {
|
int l_player_get_vel(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
if (playerid != 1)
|
auto player = scripting::level->getObject<Player>(playerid);
|
||||||
return 0;
|
if (!player) return 0;
|
||||||
glm::vec3 vel = scripting::level->player->hitbox->velocity;
|
glm::vec3 vel = player->hitbox->velocity;
|
||||||
lua_pushnumber(L, vel.x);
|
lua_pushnumber(L, vel.x);
|
||||||
lua_pushnumber(L, vel.y);
|
lua_pushnumber(L, vel.y);
|
||||||
lua_pushnumber(L, vel.z);
|
lua_pushnumber(L, vel.z);
|
||||||
@ -44,20 +43,19 @@ int l_player_get_vel(lua_State* L) {
|
|||||||
|
|
||||||
int l_player_set_vel(lua_State* L) {
|
int l_player_set_vel(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
if (playerid != 1)
|
|
||||||
return 0;
|
|
||||||
lua::luanumber x = lua_tonumber(L, 2);
|
lua::luanumber x = lua_tonumber(L, 2);
|
||||||
lua::luanumber y = lua_tonumber(L, 3);
|
lua::luanumber y = lua_tonumber(L, 3);
|
||||||
lua::luanumber z = lua_tonumber(L, 4);
|
lua::luanumber z = lua_tonumber(L, 4);
|
||||||
scripting::level->player->hitbox->velocity = glm::vec3(x, y, z);
|
auto player = scripting::level->getObject<Player>(playerid);
|
||||||
|
if (player) player->hitbox->velocity = glm::vec3(x, y, z);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_player_get_rot(lua_State* L) {
|
int l_player_get_rot(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
if (playerid != 1)
|
auto player = scripting::level->getObject<Player>(playerid);
|
||||||
return 0;
|
if (!player) return 0;
|
||||||
glm::vec2 rot = scripting::level->player->cam;
|
glm::vec2 rot = player->cam;
|
||||||
lua_pushnumber(L, rot.x);
|
lua_pushnumber(L, rot.x);
|
||||||
lua_pushnumber(L, rot.y);
|
lua_pushnumber(L, rot.y);
|
||||||
return 2;
|
return 2;
|
||||||
@ -65,11 +63,11 @@ int l_player_get_rot(lua_State* L) {
|
|||||||
|
|
||||||
int l_player_set_rot(lua_State* L) {
|
int l_player_set_rot(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
if (playerid != 1)
|
auto player = scripting::level->getObject<Player>(playerid);
|
||||||
return 0;
|
if (!player) return 0;
|
||||||
lua::luanumber x = lua_tonumber(L, 2);
|
lua::luanumber x = lua_tonumber(L, 2);
|
||||||
lua::luanumber y = lua_tonumber(L, 3);
|
lua::luanumber y = lua_tonumber(L, 3);
|
||||||
glm::vec2& cam = scripting::level->player->cam;
|
glm::vec2& cam = player->cam;
|
||||||
cam.x = x;
|
cam.x = x;
|
||||||
cam.y = y;
|
cam.y = y;
|
||||||
return 0;
|
return 0;
|
||||||
@ -77,9 +75,8 @@ int l_player_set_rot(lua_State* L) {
|
|||||||
|
|
||||||
int l_player_get_inv(lua_State* L) {
|
int l_player_get_inv(lua_State* L) {
|
||||||
int playerid = lua_tointeger(L, 1);
|
int playerid = lua_tointeger(L, 1);
|
||||||
if (playerid != 1)
|
auto player = scripting::level->getObject<Player>(playerid);
|
||||||
return 0;
|
if (!player) return 0;
|
||||||
auto player = scripting::level->player;
|
|
||||||
lua_pushinteger(L, player->getInventory()->getId());
|
lua_pushinteger(L, player->getInventory()->getId());
|
||||||
lua_pushinteger(L, player->getChosenSlot());
|
lua_pushinteger(L, player->getChosenSlot());
|
||||||
return 2;
|
return 2;
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include "../../files/files.h"
|
#include "../../files/files.h"
|
||||||
#include "../../util/timeutil.h"
|
#include "../../util/timeutil.h"
|
||||||
#include "../../world/Level.h"
|
#include "../../world/Level.h"
|
||||||
|
#include "../../objects/Player.h"
|
||||||
#include "../../voxels/Block.h"
|
#include "../../voxels/Block.h"
|
||||||
#include "../../items/ItemDef.h"
|
#include "../../items/ItemDef.h"
|
||||||
#include "../../items/Inventory.h"
|
#include "../../items/Inventory.h"
|
||||||
@ -160,7 +161,7 @@ void scripting::on_block_placed(Player* player, const Block* block, int x, int y
|
|||||||
std::string name = block->name+".placed";
|
std::string name = block->name+".placed";
|
||||||
if (state->getglobal(name)) {
|
if (state->getglobal(name)) {
|
||||||
state->pushivec3(x, y, z);
|
state->pushivec3(x, y, z);
|
||||||
state->pushinteger(1); // playerid placeholder
|
state->pushinteger(player->getId());
|
||||||
state->callNoThrow(4);
|
state->callNoThrow(4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,7 +170,7 @@ void scripting::on_block_broken(Player* player, const Block* block, int x, int y
|
|||||||
std::string name = block->name+".broken";
|
std::string name = block->name+".broken";
|
||||||
if (state->getglobal(name)) {
|
if (state->getglobal(name)) {
|
||||||
state->pushivec3(x, y, z);
|
state->pushivec3(x, y, z);
|
||||||
state->pushinteger(1); // playerid placeholder
|
state->pushinteger(player->getId());
|
||||||
state->callNoThrow(4);
|
state->callNoThrow(4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,7 +179,7 @@ bool scripting::on_block_interact(Player* player, const Block* block, int x, int
|
|||||||
std::string name = block->name+".interact";
|
std::string name = block->name+".interact";
|
||||||
if (state->getglobal(name)) {
|
if (state->getglobal(name)) {
|
||||||
state->pushivec3(x, y, z);
|
state->pushivec3(x, y, z);
|
||||||
state->pushinteger(1); // playerid placeholder
|
state->pushinteger(player->getId());
|
||||||
if (state->callNoThrow(4)) {
|
if (state->callNoThrow(4)) {
|
||||||
return state->toboolean(-1);
|
return state->toboolean(-1);
|
||||||
}
|
}
|
||||||
@ -190,7 +191,7 @@ bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x,
|
|||||||
std::string name = item->name+".useon";
|
std::string name = item->name+".useon";
|
||||||
if (state->getglobal(name)) {
|
if (state->getglobal(name)) {
|
||||||
state->pushivec3(x, y, z);
|
state->pushivec3(x, y, z);
|
||||||
state->pushinteger(1); // playerid placeholder
|
state->pushinteger(player->getId());
|
||||||
if (state->callNoThrow(4)) {
|
if (state->callNoThrow(4)) {
|
||||||
return state->toboolean(-1);
|
return state->toboolean(-1);
|
||||||
}
|
}
|
||||||
@ -202,7 +203,7 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
|||||||
std::string name = item->name+".blockbreakby";
|
std::string name = item->name+".blockbreakby";
|
||||||
if (state->getglobal(name)) {
|
if (state->getglobal(name)) {
|
||||||
state->pushivec3(x, y, z);
|
state->pushivec3(x, y, z);
|
||||||
state->pushinteger(1); // playerid placeholder
|
state->pushinteger(player->getId());
|
||||||
if (state->callNoThrow(4)) {
|
if (state->callNoThrow(4)) {
|
||||||
return state->toboolean(-1);
|
return state->toboolean(-1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,7 +78,7 @@ void Player::updateInput(
|
|||||||
float vel = std::max(glm::length(hitbox->velocity * 0.25f), 1.0f);
|
float vel = std::max(glm::length(hitbox->velocity * 0.25f), 1.0f);
|
||||||
int substeps = int(delta * vel * 1000);
|
int substeps = int(delta * vel * 1000);
|
||||||
substeps = std::min(100, std::max(1, substeps));
|
substeps = std::min(100, std::max(1, substeps));
|
||||||
level->physics->step(level->chunks, hitbox.get(),
|
level->physics->step(level->chunks.get(), hitbox.get(),
|
||||||
delta, substeps,
|
delta, substeps,
|
||||||
crouch, flight ? 0.0f : 1.0f,
|
crouch, flight ? 0.0f : 1.0f,
|
||||||
!noclip);
|
!noclip);
|
||||||
|
|||||||
@ -73,7 +73,7 @@ public:
|
|||||||
static void convert(dynamic::Map* data, const ContentLUT* lut);
|
static void convert(dynamic::Map* data, const ContentLUT* lut);
|
||||||
|
|
||||||
inline int getId() const {
|
inline int getId() const {
|
||||||
return 1;
|
return objectUID;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -17,8 +17,8 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
Chunks::Chunks(int w, int d,
|
Chunks::Chunks(uint32_t w, uint32_t d,
|
||||||
int ox, int oz,
|
int32_t ox, int32_t oz,
|
||||||
WorldFiles* wfile,
|
WorldFiles* wfile,
|
||||||
LevelEvents* events,
|
LevelEvents* events,
|
||||||
const Content* content)
|
const Content* content)
|
||||||
@ -32,7 +32,7 @@ Chunks::Chunks(int w, int d,
|
|||||||
chunksCount = 0;
|
chunksCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
voxel* Chunks::get(int x, int y, int z){
|
voxel* Chunks::get(int32_t x, int32_t y, int32_t z) {
|
||||||
x -= ox * CHUNK_W;
|
x -= ox * CHUNK_W;
|
||||||
z -= oz * CHUNK_D;
|
z -= oz * CHUNK_D;
|
||||||
int cx = floordiv(x, CHUNK_W);
|
int cx = floordiv(x, CHUNK_W);
|
||||||
@ -71,28 +71,28 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z){
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::isSolidBlock(int x, int y, int z) {
|
bool Chunks::isSolidBlock(int32_t x, int32_t y, int32_t z) {
|
||||||
voxel* v = get(x, y, z);
|
voxel* v = get(x, y, z);
|
||||||
if (v == nullptr)
|
if (v == nullptr)
|
||||||
return false;
|
return false;
|
||||||
return contentIds->getBlockDef(v->id)->rt.solid;
|
return contentIds->getBlockDef(v->id)->rt.solid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::isReplaceableBlock(int x, int y, int z) {
|
bool Chunks::isReplaceableBlock(int32_t x, int32_t y, int32_t z) {
|
||||||
voxel* v = get(x, y, z);
|
voxel* v = get(x, y, z);
|
||||||
if (v == nullptr)
|
if (v == nullptr)
|
||||||
return false;
|
return false;
|
||||||
return contentIds->getBlockDef(v->id)->replaceable;
|
return contentIds->getBlockDef(v->id)->replaceable;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::isObstacleBlock(int x, int y, int z) {
|
bool Chunks::isObstacleBlock(int32_t x, int32_t y, int32_t z) {
|
||||||
voxel* v = get(x, y, z);
|
voxel* v = get(x, y, z);
|
||||||
if (v == nullptr)
|
if (v == nullptr)
|
||||||
return false;
|
return false;
|
||||||
return contentIds->getBlockDef(v->id)->obstacle;
|
return contentIds->getBlockDef(v->id)->obstacle;
|
||||||
}
|
}
|
||||||
|
|
||||||
ubyte Chunks::getLight(int x, int y, int z, int channel){
|
ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel){
|
||||||
x -= ox * CHUNK_W;
|
x -= ox * CHUNK_W;
|
||||||
z -= oz * CHUNK_D;
|
z -= oz * CHUNK_D;
|
||||||
int cx = floordiv(x, CHUNK_W);
|
int cx = floordiv(x, CHUNK_W);
|
||||||
@ -106,10 +106,10 @@ ubyte Chunks::getLight(int x, int y, int z, int channel){
|
|||||||
int lx = x - cx * CHUNK_W;
|
int lx = x - cx * CHUNK_W;
|
||||||
int ly = y - cy * CHUNK_H;
|
int ly = y - cy * CHUNK_H;
|
||||||
int lz = z - cz * CHUNK_D;
|
int lz = z - cz * CHUNK_D;
|
||||||
return chunk->lightmap.get(lx,ly,lz, channel);
|
return chunk->lightmap.get(lx, ly, lz, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
light_t Chunks::getLight(int x, int y, int z){
|
light_t Chunks::getLight(int32_t x, int32_t y, int32_t z){
|
||||||
x -= ox * CHUNK_W;
|
x -= ox * CHUNK_W;
|
||||||
z -= oz * CHUNK_D;
|
z -= oz * CHUNK_D;
|
||||||
int cx = floordiv(x, CHUNK_W);
|
int cx = floordiv(x, CHUNK_W);
|
||||||
@ -126,7 +126,7 @@ light_t Chunks::getLight(int x, int y, int z){
|
|||||||
return chunk->lightmap.get(lx,ly,lz);
|
return chunk->lightmap.get(lx,ly,lz);
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk* Chunks::getChunkByVoxel(int x, int y, int z){
|
Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z){
|
||||||
if (y < 0 || y >= CHUNK_H)
|
if (y < 0 || y >= CHUNK_H)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
x -= ox * CHUNK_W;
|
x -= ox * CHUNK_W;
|
||||||
@ -146,7 +146,7 @@ Chunk* Chunks::getChunk(int x, int z){
|
|||||||
return chunks[z * w + x].get();
|
return chunks[z * w + x].get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::set(int x, int y, int z, int id, uint8_t states){
|
void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states){
|
||||||
if (y < 0 || y >= CHUNK_H)
|
if (y < 0 || y >= CHUNK_H)
|
||||||
return;
|
return;
|
||||||
x -= ox * CHUNK_W;
|
x -= ox * CHUNK_W;
|
||||||
@ -396,7 +396,7 @@ glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDis
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Chunks::setCenter(int x, int z) {
|
void Chunks::setCenter(int32_t x, int32_t z) {
|
||||||
int cx = floordiv(x, CHUNK_W);
|
int cx = floordiv(x, CHUNK_W);
|
||||||
int cz = floordiv(z, CHUNK_D);
|
int cz = floordiv(z, CHUNK_D);
|
||||||
cx -= ox + w / 2;
|
cx -= ox + w / 2;
|
||||||
@ -406,7 +406,7 @@ void Chunks::setCenter(int x, int z) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::translate(int dx, int dz){
|
void Chunks::translate(int32_t dx, int32_t dz) {
|
||||||
for (uint i = 0; i < volume; i++){
|
for (uint i = 0; i < volume; i++){
|
||||||
chunksSecond[i] = nullptr;
|
chunksSecond[i] = nullptr;
|
||||||
}
|
}
|
||||||
@ -433,7 +433,7 @@ void Chunks::translate(int dx, int dz){
|
|||||||
oz += dz;
|
oz += dz;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::resize(int newW, int newD) {
|
void Chunks::resize(uint32_t newW, uint32_t newD) {
|
||||||
if (newW < w) {
|
if (newW < w) {
|
||||||
int delta = w - newW;
|
int delta = w - newW;
|
||||||
translate(delta / 2, 0);
|
translate(delta / 2, 0);
|
||||||
@ -461,7 +461,7 @@ void Chunks::resize(int newW, int newD) {
|
|||||||
chunksSecond = std::move(newChunksSecond);
|
chunksSecond = std::move(newChunksSecond);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::_setOffset(int x, int z){
|
void Chunks::_setOffset(int32_t x, int32_t z) {
|
||||||
ox = x;
|
ox = x;
|
||||||
oz = z;
|
oz = z;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,23 +26,23 @@ public:
|
|||||||
size_t volume;
|
size_t volume;
|
||||||
size_t chunksCount;
|
size_t chunksCount;
|
||||||
size_t visible;
|
size_t visible;
|
||||||
int w,d;
|
uint32_t w, d;
|
||||||
int ox,oz;
|
int32_t ox, oz;
|
||||||
WorldFiles* worldFiles;
|
WorldFiles* worldFiles;
|
||||||
LevelEvents* events;
|
LevelEvents* events;
|
||||||
|
|
||||||
Chunks(int w, int d, int ox, int oz,
|
Chunks(uint32_t w, uint32_t d, int32_t ox, int32_t oz,
|
||||||
WorldFiles* worldFiles, LevelEvents* events, const Content* content);
|
WorldFiles* worldFiles, LevelEvents* events, const Content* content);
|
||||||
~Chunks() = default;
|
~Chunks() = default;
|
||||||
|
|
||||||
bool putChunk(std::shared_ptr<Chunk> chunk);
|
bool putChunk(std::shared_ptr<Chunk> chunk);
|
||||||
|
|
||||||
Chunk* getChunk(int x, int z);
|
Chunk* getChunk(int32_t x, int32_t z);
|
||||||
Chunk* getChunkByVoxel(int x, int y, int z);
|
Chunk* getChunkByVoxel(int32_t x, int32_t y, int32_t z);
|
||||||
voxel* get(int x, int y, int z);
|
voxel* get(int32_t x, int32_t y, int32_t z);
|
||||||
light_t getLight(int x, int y, int z);
|
light_t getLight(int32_t x, int32_t y, int32_t z);
|
||||||
ubyte getLight(int x, int y, int z, int channel);
|
ubyte getLight(int32_t x, int32_t y, int32_t z, int channel);
|
||||||
void set(int x, int y, int z, int id, uint8_t states);
|
void set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states);
|
||||||
|
|
||||||
voxel* rayCast(glm::vec3 start,
|
voxel* rayCast(glm::vec3 start,
|
||||||
glm::vec3 dir,
|
glm::vec3 dir,
|
||||||
@ -54,16 +54,16 @@ public:
|
|||||||
glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist);
|
glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist);
|
||||||
|
|
||||||
const AABB* isObstacleAt(float x, float y, float z);
|
const AABB* isObstacleAt(float x, float y, float z);
|
||||||
bool isSolidBlock(int x, int y, int z);
|
bool isSolidBlock(int32_t x, int32_t y, int32_t z);
|
||||||
bool isReplaceableBlock(int x, int y, int z);
|
bool isReplaceableBlock(int32_t x, int32_t y, int32_t z);
|
||||||
bool isObstacleBlock(int x, int y, int z);
|
bool isObstacleBlock(int32_t x, int32_t y, int32_t z);
|
||||||
|
|
||||||
// does not move chunks inside
|
// does not move chunks inside
|
||||||
void _setOffset(int x, int z);
|
void _setOffset(int32_t x, int32_t z);
|
||||||
|
|
||||||
void setCenter(int x, int z);
|
void setCenter(int32_t x, int32_t z);
|
||||||
void translate(int x, int z);
|
void translate(int32_t x, int32_t z);
|
||||||
void resize(int newW, int newD);
|
void resize(uint32_t newW, uint32_t newD);
|
||||||
|
|
||||||
void saveAndClear();
|
void saveAndClear();
|
||||||
};
|
};
|
||||||
|
|||||||
@ -8,7 +8,6 @@
|
|||||||
#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"
|
||||||
@ -21,20 +20,18 @@ const int DEF_PLAYER_INVENTORY_SIZE = 40;
|
|||||||
Level::Level(World* world, const Content* content, EngineSettings& settings)
|
Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||||
: world(world),
|
: world(world),
|
||||||
content(content),
|
content(content),
|
||||||
chunksStorage(new ChunksStorage(this)),
|
chunksStorage(std::make_unique<ChunksStorage>(this)),
|
||||||
events(new LevelEvents()) ,
|
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
|
||||||
|
events(std::make_unique<LevelEvents>()),
|
||||||
settings(settings)
|
settings(settings)
|
||||||
{
|
{
|
||||||
objCounter = 0;
|
|
||||||
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
|
|
||||||
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
|
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
|
||||||
player = spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
|
auto 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;
|
chunks = std::make_unique<Chunks>(matrixSize, matrixSize, 0, 0,
|
||||||
chunks = new Chunks(matrixSize, matrixSize, 0, 0,
|
world->wfile.get(), events.get(), content);
|
||||||
world->wfile.get(), events, content);
|
lighting = std::make_unique<Lighting>(content, chunks.get());
|
||||||
lighting = new Lighting(content, chunks);
|
|
||||||
|
|
||||||
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
|
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
|
||||||
this->chunksStorage->remove(chunk->x, chunk->z);
|
this->chunksStorage->remove(chunk->x, chunk->z);
|
||||||
@ -45,26 +42,16 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Level::~Level(){
|
Level::~Level(){
|
||||||
delete chunks;
|
for(auto obj : objects) {
|
||||||
delete events;
|
|
||||||
delete physics;
|
|
||||||
delete lighting;
|
|
||||||
delete chunksStorage;
|
|
||||||
|
|
||||||
for(auto obj : objects)
|
|
||||||
{
|
|
||||||
obj.reset();
|
obj.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::update() {
|
void Level::loadMatrix(int32_t x, int32_t z, uint32_t radius) {
|
||||||
glm::vec3 position = player->hitbox->position;
|
chunks->setCenter(x, z);
|
||||||
chunks->setCenter(position.x, position.z);
|
radius = std::min(radius, settings.chunks.loadDistance + settings.chunks.padding * 2);
|
||||||
|
if (chunks->w != radius) {
|
||||||
int matrixSize = (settings.chunks.loadDistance+
|
chunks->resize(radius, radius);
|
||||||
settings.chunks.padding) * 2;
|
|
||||||
if (chunks->w != matrixSize) {
|
|
||||||
chunks->resize(matrixSize, matrixSize);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,17 +59,3 @@ 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;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -3,16 +3,13 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "../typedefs.h"
|
|
||||||
#include "../settings.h"
|
#include "../settings.h"
|
||||||
#include <list>
|
#include "../interfaces/Object.h"
|
||||||
#include <vector>
|
#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;
|
||||||
@ -21,21 +18,19 @@ class Lighting;
|
|||||||
class PhysicsSolver;
|
class PhysicsSolver;
|
||||||
class ChunksStorage;
|
class ChunksStorage;
|
||||||
|
|
||||||
|
/* A level, contains chunks and objects */
|
||||||
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;
|
||||||
std::list<std::shared_ptr<Object>> objects;
|
std::vector<std::shared_ptr<Object>> objects;
|
||||||
std::shared_ptr<Player> player;
|
std::unique_ptr<Chunks> chunks;
|
||||||
Chunks* chunks;
|
std::unique_ptr<ChunksStorage> chunksStorage;
|
||||||
ChunksStorage* chunksStorage;
|
|
||||||
std::unique_ptr<Inventories> inventories;
|
std::unique_ptr<Inventories> inventories;
|
||||||
|
|
||||||
PhysicsSolver* physics;
|
std::unique_ptr<PhysicsSolver> physics;
|
||||||
Lighting* lighting;
|
std::unique_ptr<Lighting> lighting;
|
||||||
LevelEvents* events;
|
std::unique_ptr<LevelEvents> events;
|
||||||
|
|
||||||
const EngineSettings& settings;
|
const EngineSettings& settings;
|
||||||
|
|
||||||
@ -44,7 +39,7 @@ public:
|
|||||||
EngineSettings& settings);
|
EngineSettings& settings);
|
||||||
~Level();
|
~Level();
|
||||||
|
|
||||||
void update();
|
void loadMatrix(int32_t x, int32_t z, uint32_t radius);
|
||||||
|
|
||||||
World* getWorld();
|
World* getWorld();
|
||||||
|
|
||||||
@ -52,7 +47,24 @@ public:
|
|||||||
// @param T class that derives the Object class
|
// @param T class that derives the Object class
|
||||||
// @param args pass arguments needed for T class constructor
|
// @param args pass arguments needed for T class constructor
|
||||||
template<class T, typename... Args>
|
template<class T, typename... Args>
|
||||||
std::shared_ptr<T> spawnObject(Args&&... args);
|
std::shared_ptr<T> 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);
|
||||||
|
obj->objectUID = objects.size();
|
||||||
|
objects.push_back(obj);
|
||||||
|
obj->spawned();
|
||||||
|
return tObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
std::shared_ptr<T> getObject(uint64_t id) {
|
||||||
|
static_assert(std::is_base_of<Object, T>::value, "T must be a derived of Object class");
|
||||||
|
if (id >= objects.size()) return nullptr;
|
||||||
|
std::shared_ptr<T> object = std::dynamic_pointer_cast<T>(objects[id]);
|
||||||
|
return object;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* WORLD_LEVEL_H_ */
|
#endif /* WORLD_LEVEL_H_ */
|
||||||
|
|||||||
@ -50,7 +50,7 @@ void World::updateTimers(float delta) {
|
|||||||
void World::write(Level* level) {
|
void World::write(Level* level) {
|
||||||
const Content* content = level->content;
|
const Content* content = level->content;
|
||||||
|
|
||||||
Chunks* chunks = level->chunks;
|
Chunks* chunks = level->chunks.get();
|
||||||
|
|
||||||
for (size_t i = 0; i < chunks->volume; i++) {
|
for (size_t i = 0; i < chunks->volume; i++) {
|
||||||
auto chunk = chunks->chunks[i];
|
auto chunk = chunks->chunks[i];
|
||||||
@ -64,7 +64,11 @@ void World::write(Level* level) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wfile->write(this, content);
|
wfile->write(this, content);
|
||||||
wfile->writePlayer(level->player);
|
for (auto object : level->objects) {
|
||||||
|
if (std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object)) {
|
||||||
|
wfile->writePlayer(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Level* World::create(std::string name,
|
Level* World::create(std::string name,
|
||||||
@ -77,9 +81,6 @@ Level* World::create(std::string name,
|
|||||||
) {
|
) {
|
||||||
auto world = new World(name, generator, directory, seed, settings, content, packs);
|
auto world = new World(name, generator, directory, seed, settings, content, packs);
|
||||||
auto level = new Level(world, content, settings);
|
auto level = new Level(world, content, settings);
|
||||||
auto inventory = level->player->getInventory();
|
|
||||||
inventory->setId(world->getNextInventoryId());
|
|
||||||
level->inventories->store(inventory);
|
|
||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,9 +98,13 @@ Level* World::load(fs::path directory,
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto level = new Level(world.get(), content, settings);
|
auto level = new Level(world.get(), content, settings);
|
||||||
wfile->readPlayer(level->player);
|
for (auto object : level->objects) {
|
||||||
level->inventories->store(level->player->getInventory());
|
if (std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object)) {
|
||||||
world.release();
|
wfile->readPlayer(player);
|
||||||
|
level->inventories->store(player->getInventory());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(void)world.release();
|
||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,6 +27,7 @@ public:
|
|||||||
world_load_error(std::string message);
|
world_load_error(std::string message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* World - holds all world data except the level (chunks and objects) */
|
||||||
class World : Serializable {
|
class World : Serializable {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string generator;
|
std::string generator;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user