minor refactor

This commit is contained in:
MihailRis 2024-03-22 19:40:47 +03:00
parent 81641be024
commit 9df82ec0de
5 changed files with 37 additions and 11 deletions

View File

@ -14,7 +14,7 @@ LevelController::LevelController(EngineSettings& settings, Level* level)
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());
scripting::on_world_load(this);
}
void LevelController::update(float delta, bool input, bool pause) {
@ -33,12 +33,9 @@ void LevelController::update(float delta, bool input, bool pause) {
if (!pause) {
// update all objects that needed
for(auto obj : level->objects)
{
if(obj) {
if(obj->shouldUpdate) {
obj->update(delta);
}
for (auto obj : level->objects) {
if (obj && obj->shouldUpdate) {
obj->update(delta);
}
}
blocks->update(delta);
@ -63,6 +60,10 @@ Player* LevelController::getPlayer() {
return player->getPlayer();
}
BlocksController* LevelController::getBlocksController() {
return blocks.get();
}
PlayerController* LevelController::getPlayerController() {
return player.get();
}

View File

@ -38,6 +38,7 @@ public:
Level* getLevel();
Player* getPlayer();
BlocksController* getBlocksController();
PlayerController* getPlayerController();
};

View File

@ -4,10 +4,13 @@
#include "../../../engine.h"
#include "../../../files/engine_paths.h"
#include "../../../frontend/menu/menu.h"
#include "../../../frontend/screens.h"
#include "../../../logic/LevelController.h"
#include "../../../window/Window.h"
#include "../scripting.h"
#include <vector>
#include <memory>
static int l_get_worlds_list(lua_State* L) {
auto paths = scripting::engine->getPaths();
@ -28,6 +31,20 @@ static int l_open_world(lua_State* L) {
return 0;
}
static int l_close_world(lua_State* L) {
if (scripting::controller == nullptr) {
luaL_error(L, "no world open");
}
bool save_world = lua_toboolean(L, 1);
if (save_world) {
scripting::controller->saveWorld();
}
// destroy LevelScreen and run quit callbacks
scripting::engine->setScreen(nullptr);
// create and go to menu screen
scripting::engine->setScreen(std::make_shared<MenuScreen>(scripting::engine));
}
static int l_delete_world(lua_State* L) {
auto name = lua_tostring(L, 1);
menus::delete_world(name, scripting::engine);
@ -42,6 +59,7 @@ static int l_quit(lua_State* L) {
const luaL_Reg corelib [] = {
{"get_worlds_list", lua_wrap_errors<l_get_worlds_list>},
{"open_world", lua_wrap_errors<l_open_world>},
{"close_world", lua_wrap_errors<l_close_world>},
{"delete_world", lua_wrap_errors<l_delete_world>},
{"quit", lua_wrap_errors<l_quit>},
{NULL, NULL}

View File

@ -13,6 +13,7 @@
#include "../../items/ItemDef.h"
#include "../../items/Inventory.h"
#include "../../logic/BlocksController.h"
#include "../../logic/LevelController.h"
#include "../../frontend/UiDocument.h"
#include "../../engine.h"
#include "lua/LuaState.h"
@ -103,11 +104,12 @@ void scripting::process_post_runnables() {
}
}
void scripting::on_world_load(Level* level, BlocksController* blocks) {
scripting::level = level;
void scripting::on_world_load(LevelController* controller) {
scripting::level = controller->getLevel();
scripting::content = level->content;
scripting::indices = level->content->getIndices();
scripting::blocks = blocks;
scripting::blocks = controller->getBlocksController();
scripting::controller = controller;
load_script("world.lua");
for (auto& pack : scripting::engine->getContentPacks()) {
@ -146,6 +148,8 @@ void scripting::on_world_quit() {
scripting::level = nullptr;
scripting::content = nullptr;
scripting::indices = nullptr;
scripting::blocks = nullptr;
scripting::controller = nullptr;
}
void scripting::on_blocks_tick(const Block* block, int tps) {

View File

@ -23,6 +23,7 @@ struct block_funcs_set;
struct item_funcs_set;
struct uidocscript;
class BlocksController;
class LevelController;
namespace scripting {
extern Engine* engine;
@ -30,6 +31,7 @@ namespace scripting {
extern const ContentIndices* indices;
extern Level* level;
extern BlocksController* blocks;
extern LevelController* controller;
/// @brief Lua environment wrapper for automatic deletion
class Environment {
@ -54,7 +56,7 @@ namespace scripting {
void process_post_runnables();
void on_world_load(Level* level, BlocksController* blocks);
void on_world_load(LevelController* controller);
void on_world_tick();
void on_world_save();
void on_world_quit();