minor refactor
This commit is contained in:
@@ -14,7 +14,7 @@ LevelController::LevelController(EngineSettings& settings, Level* level)
|
|||||||
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(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelController::update(float delta, bool input, bool pause) {
|
void LevelController::update(float delta, bool input, bool pause) {
|
||||||
@@ -33,12 +33,9 @@ void LevelController::update(float delta, bool input, bool pause) {
|
|||||||
|
|
||||||
if (!pause) {
|
if (!pause) {
|
||||||
// update all objects that needed
|
// update all objects that needed
|
||||||
for(auto obj : level->objects)
|
for (auto obj : level->objects) {
|
||||||
{
|
if (obj && obj->shouldUpdate) {
|
||||||
if(obj) {
|
obj->update(delta);
|
||||||
if(obj->shouldUpdate) {
|
|
||||||
obj->update(delta);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
blocks->update(delta);
|
blocks->update(delta);
|
||||||
@@ -63,6 +60,10 @@ Player* LevelController::getPlayer() {
|
|||||||
return player->getPlayer();
|
return player->getPlayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BlocksController* LevelController::getBlocksController() {
|
||||||
|
return blocks.get();
|
||||||
|
}
|
||||||
|
|
||||||
PlayerController* LevelController::getPlayerController() {
|
PlayerController* LevelController::getPlayerController() {
|
||||||
return player.get();
|
return player.get();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ public:
|
|||||||
Level* getLevel();
|
Level* getLevel();
|
||||||
Player* getPlayer();
|
Player* getPlayer();
|
||||||
|
|
||||||
|
BlocksController* getBlocksController();
|
||||||
PlayerController* getPlayerController();
|
PlayerController* getPlayerController();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,13 @@
|
|||||||
#include "../../../engine.h"
|
#include "../../../engine.h"
|
||||||
#include "../../../files/engine_paths.h"
|
#include "../../../files/engine_paths.h"
|
||||||
#include "../../../frontend/menu/menu.h"
|
#include "../../../frontend/menu/menu.h"
|
||||||
|
#include "../../../frontend/screens.h"
|
||||||
|
#include "../../../logic/LevelController.h"
|
||||||
#include "../../../window/Window.h"
|
#include "../../../window/Window.h"
|
||||||
#include "../scripting.h"
|
#include "../scripting.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
static int l_get_worlds_list(lua_State* L) {
|
static int l_get_worlds_list(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
auto paths = scripting::engine->getPaths();
|
||||||
@@ -28,6 +31,20 @@ static int l_open_world(lua_State* L) {
|
|||||||
return 0;
|
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) {
|
static int l_delete_world(lua_State* L) {
|
||||||
auto name = lua_tostring(L, 1);
|
auto name = lua_tostring(L, 1);
|
||||||
menus::delete_world(name, scripting::engine);
|
menus::delete_world(name, scripting::engine);
|
||||||
@@ -42,6 +59,7 @@ static int l_quit(lua_State* L) {
|
|||||||
const luaL_Reg corelib [] = {
|
const luaL_Reg corelib [] = {
|
||||||
{"get_worlds_list", lua_wrap_errors<l_get_worlds_list>},
|
{"get_worlds_list", lua_wrap_errors<l_get_worlds_list>},
|
||||||
{"open_world", lua_wrap_errors<l_open_world>},
|
{"open_world", lua_wrap_errors<l_open_world>},
|
||||||
|
{"close_world", lua_wrap_errors<l_close_world>},
|
||||||
{"delete_world", lua_wrap_errors<l_delete_world>},
|
{"delete_world", lua_wrap_errors<l_delete_world>},
|
||||||
{"quit", lua_wrap_errors<l_quit>},
|
{"quit", lua_wrap_errors<l_quit>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include "../../items/ItemDef.h"
|
#include "../../items/ItemDef.h"
|
||||||
#include "../../items/Inventory.h"
|
#include "../../items/Inventory.h"
|
||||||
#include "../../logic/BlocksController.h"
|
#include "../../logic/BlocksController.h"
|
||||||
|
#include "../../logic/LevelController.h"
|
||||||
#include "../../frontend/UiDocument.h"
|
#include "../../frontend/UiDocument.h"
|
||||||
#include "../../engine.h"
|
#include "../../engine.h"
|
||||||
#include "lua/LuaState.h"
|
#include "lua/LuaState.h"
|
||||||
@@ -103,11 +104,12 @@ void scripting::process_post_runnables() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
void scripting::on_world_load(LevelController* controller) {
|
||||||
scripting::level = level;
|
scripting::level = controller->getLevel();
|
||||||
scripting::content = level->content;
|
scripting::content = level->content;
|
||||||
scripting::indices = level->content->getIndices();
|
scripting::indices = level->content->getIndices();
|
||||||
scripting::blocks = blocks;
|
scripting::blocks = controller->getBlocksController();
|
||||||
|
scripting::controller = controller;
|
||||||
load_script("world.lua");
|
load_script("world.lua");
|
||||||
|
|
||||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
@@ -146,6 +148,8 @@ void scripting::on_world_quit() {
|
|||||||
scripting::level = nullptr;
|
scripting::level = nullptr;
|
||||||
scripting::content = nullptr;
|
scripting::content = nullptr;
|
||||||
scripting::indices = nullptr;
|
scripting::indices = nullptr;
|
||||||
|
scripting::blocks = nullptr;
|
||||||
|
scripting::controller = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_blocks_tick(const Block* block, int tps) {
|
void scripting::on_blocks_tick(const Block* block, int tps) {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ struct block_funcs_set;
|
|||||||
struct item_funcs_set;
|
struct item_funcs_set;
|
||||||
struct uidocscript;
|
struct uidocscript;
|
||||||
class BlocksController;
|
class BlocksController;
|
||||||
|
class LevelController;
|
||||||
|
|
||||||
namespace scripting {
|
namespace scripting {
|
||||||
extern Engine* engine;
|
extern Engine* engine;
|
||||||
@@ -30,6 +31,7 @@ namespace scripting {
|
|||||||
extern const ContentIndices* indices;
|
extern const ContentIndices* indices;
|
||||||
extern Level* level;
|
extern Level* level;
|
||||||
extern BlocksController* blocks;
|
extern BlocksController* blocks;
|
||||||
|
extern LevelController* controller;
|
||||||
|
|
||||||
/// @brief Lua environment wrapper for automatic deletion
|
/// @brief Lua environment wrapper for automatic deletion
|
||||||
class Environment {
|
class Environment {
|
||||||
@@ -54,7 +56,7 @@ namespace scripting {
|
|||||||
|
|
||||||
void process_post_runnables();
|
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_tick();
|
||||||
void on_world_save();
|
void on_world_save();
|
||||||
void on_world_quit();
|
void on_world_quit();
|
||||||
|
|||||||
Reference in New Issue
Block a user