One step closer to multiplayer

This commit is contained in:
InfiniteCoder 2024-03-02 22:59:31 +03:00 committed by MihailRis
parent e0daa4d1b9
commit 91a4b16796
26 changed files with 213 additions and 219 deletions

View File

@ -261,4 +261,4 @@ EnginePaths* Engine::getPaths() {
std::shared_ptr<Screen> Engine::getScreen() {
return screen;
}
}

View File

@ -34,9 +34,10 @@
#include "graphics/Skybox.h"
#include "graphics/ChunksRenderer.h"
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend)
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player)
: engine(engine),
level(frontend->getLevel())
level(frontend->getLevel()),
player(player)
{
frustumCulling = std::make_unique<Frustum>();
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->uniform1i("u_cubemap", 1);
{
auto player = level->player;
auto inventory = player->getInventory();
ItemStack& stack = inventory->getSlot(player->getChosenSlot());
ItemDef* item = indices->getItemDef(stack.getItemId());
@ -191,7 +191,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
skybox->bind();
atlas->getTexture()->bind();
drawChunks(level->chunks, camera, shader);
drawChunks(level->chunks.get(), camera, shader);
// Selected block
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 size = hitbox.size();
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));
}
}
@ -222,7 +222,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
skybox->unbind();
}
if (hudVisible && level->player->debug) {
if (hudVisible && player->debug) {
GfxContext ctx = pctx.sub();
ctx.depthTest(true);
@ -230,7 +230,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
if (settings.debug.showChunkBorders){
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.z < 0) coord.z--;
int cx = floordiv((int)coord.x, CHUNK_W);

View File

@ -14,6 +14,7 @@
#include "../graphics/GfxContext.h"
class Level;
class Player;
class Camera;
class Batch3D;
class LineBatch;
@ -29,6 +30,7 @@ class Skybox;
class WorldRenderer {
Engine* engine;
Level* level;
Player* player;
std::unique_ptr<Frustum> frustumCulling;
std::unique_ptr<LineBatch> lineBatch;
std::unique_ptr<ChunksRenderer> renderer;
@ -37,7 +39,7 @@ class WorldRenderer {
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
public:
WorldRenderer(Engine* engine, LevelFrontend* frontend);
WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player);
~WorldRenderer();
void draw(const GfxContext& context, Camera* camera, bool hudVisible);

View File

@ -84,14 +84,14 @@ void ChunksRenderer::threadLoop(int index) {
}
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) {
chunk->setModified(false);
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);
meshes[glm::ivec2(chunk->x, chunk->z)] = sptr;
return sptr;

View File

@ -40,6 +40,7 @@
#include "gui/UINode.h"
#include "gui/GUI.h"
#include "ContentGfxCache.h"
#include "menu/menu.h"
#include "screens.h"
#include "WorldRenderer.h"
#include "BlocksPreview.h"
@ -122,11 +123,10 @@ std::shared_ptr<UINode> Hud::createDebugPanel(Engine* engine) {
L" visible: "+std::to_wstring(level->chunks->visible);
}));
panel->add(create_label([=](){
auto player = level->player;
auto* indices = level->content->getIndices();
auto def = indices->getBlockDef(player->selectedVoxel.id);
std::wstringstream stream;
stream << std::hex << level->player->selectedVoxel.states;
stream << std::hex << player->selectedVoxel.states;
if (def) {
stream << L" (" << util::str2wstr_utf8(def->name) << L")";
}
@ -151,19 +151,19 @@ std::shared_ptr<UINode> Hud::createDebugPanel(Engine* engine) {
// Coord input
auto box = std::make_shared<TextBox>(L"");
box->setTextSupplier([=]() {
Hitbox* hitbox = level->player->hitbox.get();
Hitbox* hitbox = player->hitbox.get();
return util::to_wstring(hitbox->position[ax], 2);
});
box->setTextConsumer([=](std::wstring text) {
try {
glm::vec3 position = level->player->hitbox->position;
glm::vec3 position = player->hitbox->position;
position[ax] = std::stoi(text);
level->player->teleport(position);
player->teleport(position);
} catch (std::invalid_argument& _){
}
});
box->setOnEditStart([=](){
Hitbox* hitbox = level->player->hitbox.get();
Hitbox* hitbox = player->hitbox.get();
box->setText(std::to_wstring(int(hitbox->position[ax])));
});
box->setSize(glm::vec2(230, 27));
@ -212,7 +212,6 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
auto level = frontend->getLevel();
auto content = level->content;
auto indices = content->getIndices();
auto player = level->player;
auto inventory = player->getInventory();
int itemsCount = indices->countItemDefs();
@ -239,8 +238,6 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
}
std::shared_ptr<InventoryView> Hud::createHotbar() {
auto level = frontend->getLevel();
auto player = level->player;
auto inventory = player->getInventory();
SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr);
@ -254,10 +251,12 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
return view;
}
Hud::Hud(Engine* engine, LevelFrontend* frontend)
: assets(engine->getAssets()),
Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
: engine(engine),
assets(engine->getAssets()),
gui(engine->getGUI()),
frontend(frontend)
frontend(frontend),
player(player)
{
auto menu = gui->getMenu();
@ -335,7 +334,6 @@ void Hud::cleanup() {
void Hud::update(bool visible) {
auto level = frontend->getLevel();
auto player = level->player;
auto menu = gui->getMenu();
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
*/
void Hud::openInventory() {
auto level = frontend->getLevel();
auto player = level->player;
auto inventory = player->getInventory();
inventoryOpen = true;
@ -472,8 +468,6 @@ void Hud::openPermanent(UiDocument* doc) {
auto invview = std::dynamic_pointer_cast<InventoryView>(root);
if (invview) {
auto level = frontend->getLevel();
auto player = level->player;
auto inventory = player->getInventory();
invview->bind(inventory, frontend, interaction.get());
}
@ -548,9 +542,6 @@ void Hud::remove(std::shared_ptr<UINode> node) {
}
void Hud::draw(const GfxContext& ctx){
auto level = frontend->getLevel();
auto player = level->player;
const Viewport& viewport = ctx.getViewport();
const uint width = viewport.getWidth();
const uint height = viewport.getHeight();
@ -568,7 +559,7 @@ void Hud::draw(const GfxContext& ctx){
hotbarView->setSelected(player->getChosenSlot());
// Crosshair
if (!pause && Events::_cursor_locked && !level->player->debug) {
if (!pause && Events::_cursor_locked && !player->debug) {
GfxContext chctx = ctx.sub();
chctx.blendMode(blendmode::inversion);
auto texture = assets->getTexture("gui/crosshair");
@ -583,7 +574,7 @@ void Hud::draw(const GfxContext& ctx){
}
// Delta-time visualizer
if (level->player->debug) {
if (player->debug) {
batch->texture(nullptr);
const int dmwidth = 256;
const float dmscale = 4000.0f;
@ -648,6 +639,7 @@ void Hud::setPause(bool pause) {
auto menu = gui->getMenu();
if (pause) {
menus::create_pause_panel(engine, frontend->getLevel());
menu->setPage("pause");
} else {
menu->reset();
@ -656,6 +648,6 @@ void Hud::setPause(bool pause) {
menu->setVisible(pause);
}
std::shared_ptr<Player> Hud::getPlayer() const {
return frontend->getLevel()->player;
Player* Hud::getPlayer() const {
return player;
}

View File

@ -65,6 +65,7 @@ public:
};
class Hud {
Engine* engine;
Assets* assets;
std::unique_ptr<Camera> uicamera;
@ -84,6 +85,7 @@ class Hud {
std::shared_ptr<SlotView> grabbedItemView;
gui::GUI* gui;
LevelFrontend* frontend;
Player* player;
std::vector<HudElement> elements;
@ -98,7 +100,7 @@ class Hud {
void cleanup();
public:
Hud(Engine* engine, LevelFrontend* frontend);
Hud(Engine* engine, LevelFrontend* frontend, Player* player);
~Hud();
void update(bool hudVisible);
@ -118,7 +120,7 @@ public:
void remove(HudElement& element);
void remove(std::shared_ptr<gui::UINode> node);
std::shared_ptr<Player> getPlayer() const;
Player* getPlayer() const;
};
#endif /* SRC_HUD_H_ */

View File

@ -19,4 +19,4 @@ namespace menus {
void refresh_menus(Engine* engine);
}
#endif // FRONTEND_MENU_MENU_H_
#endif // FRONTEND_MENU_MENU_H_

View File

@ -89,17 +89,15 @@ void MenuScreen::draw(float delta) {
static bool backlight;
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine), level(level){
menus::create_pause_panel(engine, level);
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
auto& settings = engine->getSettings();
auto assets = engine->getAssets();
controller = std::make_unique<LevelController>(settings, level);
frontend = std::make_unique<LevelFrontend>(level, assets);
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get());
hud = std::make_unique<Hud>(engine, frontend.get());
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get(), controller->getPlayer());
hud = std::make_unique<Hud>(engine, frontend.get(), controller->getPlayer());
backlight = settings.graphics.backlight;
animator = std::make_unique<TextureAnimator>();
@ -121,8 +119,8 @@ LevelScreen::~LevelScreen() {
std::cout << "-- writing world" << std::endl;
scripting::on_frontend_close();
controller->onWorldSave();
auto world = level->getWorld();
world->write(level.get());
auto world = controller->getLevel()->getWorld();
world->write(controller->getLevel());
controller->onWorldQuit();
engine->getPaths()->setWorldFolder(fs::path());
}
@ -136,10 +134,10 @@ void LevelScreen::updateHotkeys() {
hudVisible = !hudVisible;
}
if (Events::jpressed(keycode::F3)) {
level->player->debug = !level->player->debug;
controller->getPlayer()->debug = !controller->getPlayer()->debug;
}
if (Events::jpressed(keycode::F5)) {
level->chunks->saveAndClear();
controller->getLevel()->chunks->saveAndClear();
}
}
@ -163,14 +161,14 @@ void LevelScreen::update(float delta) {
// TODO: subscribe for setting change
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) {
level->chunks->saveAndClear();
controller->getLevel()->chunks->saveAndClear();
backlight = settings.graphics.backlight;
}
if (!hud->isPause()) {
level->world->updateTimers(delta);
controller->getLevel()->world->updateTimers(delta);
animator->update(delta);
}
controller->update(delta, !inputLocked, hud->isPause());
@ -178,7 +176,7 @@ void LevelScreen::update(float delta) {
}
void LevelScreen::draw(float delta) {
auto camera = level->player->currentCamera;
auto camera = controller->getPlayer()->currentCamera;
Viewport viewport(Window::width, Window::height);
GfxContext ctx(nullptr, viewport, batch.get());
@ -187,12 +185,8 @@ void LevelScreen::draw(float delta) {
if (hudVisible) {
hud->draw(ctx);
if (level->player->debug) {
if (controller->getPlayer()->debug) {
hud->drawDebug(1 / delta);
}
}
}
Level* LevelScreen::getLevel() const {
return level.get();
}

View File

@ -38,7 +38,6 @@ public:
};
class LevelScreen : public Screen {
std::unique_ptr<Level> level;
std::unique_ptr<LevelFrontend> frontend;
std::unique_ptr<Hud> hud;
std::unique_ptr<WorldRenderer> worldRenderer;
@ -53,8 +52,6 @@ public:
void update(float delta) override;
void draw(float delta) override;
Level* getLevel() const;
};
#endif // FRONTEND_SCREENS_H_

View File

@ -53,8 +53,8 @@ int Clock::getTickId() const {
BlocksController::BlocksController(Level* level, uint padding)
: level(level),
chunks(level->chunks),
lighting(level->lighting),
chunks(level->chunks.get()),
lighting(level->lighting.get()),
randTickClock(20, 3),
blocksTickClock(20, 1),
padding(padding) {

View File

@ -28,6 +28,7 @@ public:
int getTickId() const;
};
/* BlocksController manages block updates and block data (aka inventories) */
class BlocksController {
Level* level;
Chunks* chunks;

View File

@ -24,8 +24,8 @@ const uint MIN_SURROUNDING = 9;
ChunksController::ChunksController(Level* level, uint padding)
: level(level),
chunks(level->chunks),
lighting(level->lighting),
chunks(level->chunks.get()),
lighting(level->lighting.get()),
padding(padding),
generator(WorldGenerators::createGenerator(level->getWorld()->getGenerator(), level->content)) {
}
@ -131,4 +131,4 @@ void ChunksController::createChunk(int x, int z) {
}
chunk->setLoaded(true);
chunk->setReady(true);
}
}

View File

@ -1,32 +1,32 @@
#include "LevelController.h"
#include "../world/Level.h"
#include "PlayerController.h"
#include "BlocksController.h"
#include "ChunksController.h"
#include "../physics/Hitbox.h"
#include "scripting/scripting.h"
#include "../interfaces/Object.h"
LevelController::LevelController(EngineSettings& settings, Level* level)
: settings(settings), level(level) {
blocks = std::make_unique<BlocksController>(level, settings.chunks.padding);
chunks = std::make_unique<ChunksController>(level, settings.chunks.padding);
player = std::make_unique<PlayerController>(level, settings, blocks.get());
: settings(settings), level(level),
blocks(std::make_unique<BlocksController>(level, settings.chunks.padding)),
chunks(std::make_unique<ChunksController>(level, settings.chunks.padding)),
player(std::make_unique<PlayerController>(level, settings, blocks.get())) {
scripting::on_world_load(level, blocks.get());
}
LevelController::~LevelController() {
}
void LevelController::update(float delta, bool input, bool 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);
// 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) {
// update all objects that needed
@ -49,3 +49,11 @@ void LevelController::onWorldSave() {
void LevelController::onWorldQuit() {
scripting::on_world_quit();
}
Level* LevelController::getLevel() {
return level.get();
}
Player* LevelController::getPlayer() {
return player->getPlayer();
}

View File

@ -4,22 +4,23 @@
#include <memory>
#include "../settings.h"
#include "PlayerController.h"
#include "BlocksController.h"
#include "ChunksController.h"
class Level;
class BlocksController;
class ChunksController;
class PlayerController;
class Player;
/* LevelController manages other controllers */
class LevelController {
EngineSettings& settings;
Level* level;
std::unique_ptr<Level> level;
// Sub-controllers
std::unique_ptr<BlocksController> blocks;
std::unique_ptr<ChunksController> chunks;
std::unique_ptr<PlayerController> player;
public:
LevelController(EngineSettings& settings, Level* level);
~LevelController();
/*
@param delta time elapsed since the last update
@ -32,6 +33,9 @@ public:
void onWorldSave();
void onWorldQuit();
Level* getLevel();
Player* getPlayer();
};
#endif // LOGIC_LEVEL_CONTROLLER_H_

View File

@ -142,8 +142,8 @@ PlayerController::PlayerController(
const EngineSettings& settings,
BlocksController* blocksController)
: level(level),
player(level->player),
camControl(level->player, settings.camera),
player(level->getObject<Player>(0)),
camControl(player, settings.camera),
blocksController(blocksController) {
}
@ -186,7 +186,7 @@ void PlayerController::updateCamera(float delta, bool movement) {
if (movement) {
camControl.updateMouse(input);
}
camControl.update(input, delta, level->chunks);
camControl.update(input, delta, level->chunks.get());
}
void PlayerController::resetKeyboard() {
@ -207,9 +207,8 @@ void PlayerController::updateControls(float delta){
void PlayerController::updateInteraction(){
auto indices = level->content->getIndices();
Chunks* chunks = level->chunks;
Player* player = level->player.get();
Lighting* lighting = level->lighting;
Chunks* chunks = level->chunks.get();
Lighting* lighting = level->lighting.get();
Camera* camera = player->camera.get();
glm::vec3 end;
glm::ivec3 iend;
@ -269,24 +268,24 @@ void PlayerController::updateInteraction(){
if (lclick) {
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;
}
}
Block* target = indices->getBlockDef(vox->id);
if (lclick && target->breakable){
blocksController->breakBlock(player, target, x, y, z);
blocksController->breakBlock(player.get(), target, x, y, z);
}
if (rclick) {
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;
}
}
if (def && rclick){
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;
}
if (!target->replaceable){
@ -310,7 +309,7 @@ void PlayerController::updateInteraction(){
chunks->set(x, y, z, chosenBlock, states);
lighting->onBlockSet(x,y,z, chosenBlock);
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);
}
@ -337,3 +336,7 @@ void PlayerController::updateInteraction(){
selectedBlockStates = 0;
}
}
Player* PlayerController::getPlayer() {
return player.get();
}

View File

@ -49,6 +49,8 @@ public:
const EngineSettings& settings,
BlocksController* blocksController);
void update(float delta, bool input, bool pause);
Player* getPlayer();
};
#endif /* PLAYER_CONTROL_H_ */

View File

@ -11,9 +11,9 @@
/* == player library ==*/
int l_player_get_pos(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
glm::vec3 pos = scripting::level->player->hitbox->position;
auto player = scripting::level->getObject<Player>(playerid);
if (!player) return 0;
glm::vec3 pos = player->hitbox->position;
lua_pushnumber(L, pos.x);
lua_pushnumber(L, pos.y);
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 playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
lua::luanumber x = lua_tonumber(L, 2);
lua::luanumber y = lua_tonumber(L, 3);
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;
}
int l_player_get_vel(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
glm::vec3 vel = scripting::level->player->hitbox->velocity;
auto player = scripting::level->getObject<Player>(playerid);
if (!player) return 0;
glm::vec3 vel = player->hitbox->velocity;
lua_pushnumber(L, vel.x);
lua_pushnumber(L, vel.y);
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 playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
lua::luanumber x = lua_tonumber(L, 2);
lua::luanumber y = lua_tonumber(L, 3);
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;
}
int l_player_get_rot(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
glm::vec2 rot = scripting::level->player->cam;
auto player = scripting::level->getObject<Player>(playerid);
if (!player) return 0;
glm::vec2 rot = player->cam;
lua_pushnumber(L, rot.x);
lua_pushnumber(L, rot.y);
return 2;
@ -65,11 +63,11 @@ int l_player_get_rot(lua_State* L) {
int l_player_set_rot(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
auto player = scripting::level->getObject<Player>(playerid);
if (!player) return 0;
lua::luanumber x = lua_tonumber(L, 2);
lua::luanumber y = lua_tonumber(L, 3);
glm::vec2& cam = scripting::level->player->cam;
glm::vec2& cam = player->cam;
cam.x = x;
cam.y = y;
return 0;
@ -77,9 +75,8 @@ int l_player_set_rot(lua_State* L) {
int l_player_get_inv(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
auto player = scripting::level->player;
auto player = scripting::level->getObject<Player>(playerid);
if (!player) return 0;
lua_pushinteger(L, player->getInventory()->getId());
lua_pushinteger(L, player->getChosenSlot());
return 2;

View File

@ -8,6 +8,7 @@
#include "../../files/files.h"
#include "../../util/timeutil.h"
#include "../../world/Level.h"
#include "../../objects/Player.h"
#include "../../voxels/Block.h"
#include "../../items/ItemDef.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";
if (state->getglobal(name)) {
state->pushivec3(x, y, z);
state->pushinteger(1); // playerid placeholder
state->pushinteger(player->getId());
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";
if (state->getglobal(name)) {
state->pushivec3(x, y, z);
state->pushinteger(1); // playerid placeholder
state->pushinteger(player->getId());
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";
if (state->getglobal(name)) {
state->pushivec3(x, y, z);
state->pushinteger(1); // playerid placeholder
state->pushinteger(player->getId());
if (state->callNoThrow(4)) {
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";
if (state->getglobal(name)) {
state->pushivec3(x, y, z);
state->pushinteger(1); // playerid placeholder
state->pushinteger(player->getId());
if (state->callNoThrow(4)) {
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";
if (state->getglobal(name)) {
state->pushivec3(x, y, z);
state->pushinteger(1); // playerid placeholder
state->pushinteger(player->getId());
if (state->callNoThrow(4)) {
return state->toboolean(-1);
}

View File

@ -78,7 +78,7 @@ void Player::updateInput(
float vel = std::max(glm::length(hitbox->velocity * 0.25f), 1.0f);
int substeps = int(delta * vel * 1000);
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,
crouch, flight ? 0.0f : 1.0f,
!noclip);

View File

@ -73,7 +73,7 @@ public:
static void convert(dynamic::Map* data, const ContentLUT* lut);
inline int getId() const {
return 1;
return objectUID;
}
};

View File

@ -17,8 +17,8 @@
#include <limits.h>
#include <vector>
Chunks::Chunks(int w, int d,
int ox, int oz,
Chunks::Chunks(uint32_t w, uint32_t d,
int32_t ox, int32_t oz,
WorldFiles* wfile,
LevelEvents* events,
const Content* content)
@ -32,7 +32,7 @@ Chunks::Chunks(int w, int d,
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;
z -= oz * CHUNK_D;
int cx = floordiv(x, CHUNK_W);
@ -71,28 +71,28 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z){
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);
if (v == nullptr)
return false;
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);
if (v == nullptr)
return false;
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);
if (v == nullptr)
return false;
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;
z -= oz * CHUNK_D;
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 ly = y - cy * CHUNK_H;
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;
z -= oz * CHUNK_D;
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);
}
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)
return nullptr;
x -= ox * CHUNK_W;
@ -146,7 +146,7 @@ Chunk* Chunks::getChunk(int x, int z){
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)
return;
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 cz = floordiv(z, CHUNK_D);
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++){
chunksSecond[i] = nullptr;
}
@ -433,7 +433,7 @@ void Chunks::translate(int dx, int dz){
oz += dz;
}
void Chunks::resize(int newW, int newD) {
void Chunks::resize(uint32_t newW, uint32_t newD) {
if (newW < w) {
int delta = w - newW;
translate(delta / 2, 0);
@ -461,7 +461,7 @@ void Chunks::resize(int newW, int newD) {
chunksSecond = std::move(newChunksSecond);
}
void Chunks::_setOffset(int x, int z){
void Chunks::_setOffset(int32_t x, int32_t z) {
ox = x;
oz = z;
}

View File

@ -26,23 +26,23 @@ public:
size_t volume;
size_t chunksCount;
size_t visible;
int w,d;
int ox,oz;
uint32_t w, d;
int32_t ox, oz;
WorldFiles* worldFiles;
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);
~Chunks() = default;
bool putChunk(std::shared_ptr<Chunk> chunk);
Chunk* getChunk(int x, int z);
Chunk* getChunkByVoxel(int x, int y, int z);
voxel* get(int x, int y, int z);
light_t getLight(int x, int y, int z);
ubyte getLight(int x, int y, int z, int channel);
void set(int x, int y, int z, int id, uint8_t states);
Chunk* getChunk(int32_t x, int32_t z);
Chunk* getChunkByVoxel(int32_t x, int32_t y, int32_t z);
voxel* get(int32_t x, int32_t y, int32_t z);
light_t getLight(int32_t x, int32_t y, int32_t z);
ubyte getLight(int32_t x, int32_t y, int32_t z, int channel);
void set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states);
voxel* rayCast(glm::vec3 start,
glm::vec3 dir,
@ -54,16 +54,16 @@ public:
glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist);
const AABB* isObstacleAt(float x, float y, float z);
bool isSolidBlock(int x, int y, int z);
bool isReplaceableBlock(int x, int y, int z);
bool isObstacleBlock(int x, int y, int z);
bool isSolidBlock(int32_t x, int32_t y, int32_t z);
bool isReplaceableBlock(int32_t x, int32_t y, int32_t z);
bool isObstacleBlock(int32_t x, int32_t y, int32_t z);
// 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 translate(int x, int z);
void resize(int newW, int newD);
void setCenter(int32_t x, int32_t z);
void translate(int32_t x, int32_t z);
void resize(uint32_t newW, uint32_t newD);
void saveAndClear();
};

View File

@ -8,7 +8,6 @@
#include "../voxels/ChunksStorage.h"
#include "../physics/Hitbox.h"
#include "../physics/PhysicsSolver.h"
#include "../interfaces/Object.h"
#include "../objects/Player.h"
#include "../items/Inventory.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)
: world(world),
content(content),
chunksStorage(new ChunksStorage(this)),
events(new LevelEvents()) ,
chunksStorage(std::make_unique<ChunksStorage>(this)),
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
events(std::make_unique<LevelEvents>()),
settings(settings)
{
objCounter = 0;
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
player = 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+
settings.chunks.padding) * 2;
chunks = new Chunks(matrixSize, matrixSize, 0, 0,
world->wfile.get(), events, content);
lighting = new Lighting(content, chunks);
uint matrixSize = (settings.chunks.loadDistance + settings.chunks.padding) * 2;
chunks = std::make_unique<Chunks>(matrixSize, matrixSize, 0, 0,
world->wfile.get(), events.get(), content);
lighting = std::make_unique<Lighting>(content, chunks.get());
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
this->chunksStorage->remove(chunk->x, chunk->z);
@ -45,26 +42,16 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
}
Level::~Level(){
delete chunks;
delete events;
delete physics;
delete lighting;
delete chunksStorage;
for(auto obj : objects)
{
for(auto obj : objects) {
obj.reset();
}
}
void Level::update() {
glm::vec3 position = player->hitbox->position;
chunks->setCenter(position.x, position.z);
int matrixSize = (settings.chunks.loadDistance+
settings.chunks.padding) * 2;
if (chunks->w != matrixSize) {
chunks->resize(matrixSize, matrixSize);
void Level::loadMatrix(int32_t x, int32_t z, uint32_t radius) {
chunks->setCenter(x, z);
radius = std::min(radius, settings.chunks.loadDistance + settings.chunks.padding * 2);
if (chunks->w != radius) {
chunks->resize(radius, radius);
}
}
@ -72,17 +59,3 @@ World* Level::getWorld() {
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;
}

View File

@ -3,16 +3,13 @@
#include <memory>
#include "../typedefs.h"
#include "../settings.h"
#include <list>
#include "../interfaces/Object.h"
#include <vector>
#include <chrono>
class Content;
class World;
class Player;
class Object;
class Chunks;
class Inventory;
class Inventories;
@ -21,21 +18,19 @@ class Lighting;
class PhysicsSolver;
class ChunksStorage;
/* A level, contains chunks and objects */
class Level {
private:
int objCounter;
public:
std::unique_ptr<World> world;
const Content* const content;
std::list<std::shared_ptr<Object>> objects;
std::shared_ptr<Player> player;
Chunks* chunks;
ChunksStorage* chunksStorage;
std::vector<std::shared_ptr<Object>> objects;
std::unique_ptr<Chunks> chunks;
std::unique_ptr<ChunksStorage> chunksStorage;
std::unique_ptr<Inventories> inventories;
PhysicsSolver* physics;
Lighting* lighting;
LevelEvents* events;
std::unique_ptr<PhysicsSolver> physics;
std::unique_ptr<Lighting> lighting;
std::unique_ptr<LevelEvents> events;
const EngineSettings& settings;
@ -44,7 +39,7 @@ public:
EngineSettings& settings);
~Level();
void update();
void loadMatrix(int32_t x, int32_t z, uint32_t radius);
World* getWorld();
@ -52,7 +47,24 @@ public:
// @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);
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_ */

View File

@ -50,7 +50,7 @@ void World::updateTimers(float delta) {
void World::write(Level* level) {
const Content* content = level->content;
Chunks* chunks = level->chunks;
Chunks* chunks = level->chunks.get();
for (size_t i = 0; i < chunks->volume; i++) {
auto chunk = chunks->chunks[i];
@ -64,7 +64,11 @@ void World::write(Level* level) {
}
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,
@ -77,9 +81,6 @@ Level* World::create(std::string name,
) {
auto world = new World(name, generator, directory, seed, settings, content, packs);
auto level = new Level(world, content, settings);
auto inventory = level->player->getInventory();
inventory->setId(world->getNextInventoryId());
level->inventories->store(inventory);
return level;
}
@ -97,9 +98,13 @@ Level* World::load(fs::path directory,
}
auto level = new Level(world.get(), content, settings);
wfile->readPlayer(level->player);
level->inventories->store(level->player->getInventory());
world.release();
for (auto object : level->objects) {
if (std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object)) {
wfile->readPlayer(player);
level->inventories->store(player->getInventory());
}
}
(void)world.release();
return level;
}
@ -193,4 +198,4 @@ std::unique_ptr<dynamic::Map> World::serialize() const {
root->put("next-inventory-id", nextInventoryId);
return root;
}
}

View File

@ -27,6 +27,7 @@ public:
world_load_error(std::string message);
};
/* World - holds all world data except the level (chunks and objects) */
class World : Serializable {
std::string name;
std::string generator;
@ -157,4 +158,4 @@ public:
void deserialize(dynamic::Map *src) override;
};
#endif /* WORLD_WORLD_H_ */
#endif /* WORLD_WORLD_H_ */