world.lua script
This commit is contained in:
parent
5d6c2b9b8e
commit
abc3e47feb
@ -276,6 +276,12 @@ void ContentLoader::load(ContentBuilder* builder) {
|
|||||||
fixPackIndices();
|
fixPackIndices();
|
||||||
|
|
||||||
auto folder = pack->folder;
|
auto folder = pack->folder;
|
||||||
|
|
||||||
|
fs::path scriptFile = folder/fs::path("scripts/world.lua");
|
||||||
|
if (fs::is_regular_file(scriptFile)) {
|
||||||
|
scripting::load_world_script(pack->id, scriptFile);
|
||||||
|
}
|
||||||
|
|
||||||
if (!fs::is_regular_file(pack->getContentFile()))
|
if (!fs::is_regular_file(pack->getContentFile()))
|
||||||
return;
|
return;
|
||||||
auto root = files::read_json(pack->getContentFile());
|
auto root = files::read_json(pack->getContentFile());
|
||||||
|
|||||||
@ -123,8 +123,8 @@ void Engine::mainloop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() {
|
Engine::~Engine() {
|
||||||
scripting::close();
|
|
||||||
screen = nullptr;
|
screen = nullptr;
|
||||||
|
scripting::close();
|
||||||
|
|
||||||
Audio::finalize();
|
Audio::finalize();
|
||||||
|
|
||||||
|
|||||||
@ -100,8 +100,10 @@ LevelScreen::LevelScreen(Engine* engine, Level* level)
|
|||||||
|
|
||||||
LevelScreen::~LevelScreen() {
|
LevelScreen::~LevelScreen() {
|
||||||
std::cout << "-- writing world" << std::endl;
|
std::cout << "-- writing world" << std::endl;
|
||||||
|
controller->onWorldSave();
|
||||||
auto world = level->getWorld();
|
auto world = level->getWorld();
|
||||||
world->write(level.get());
|
world->write(level.get());
|
||||||
|
controller->onWorldQuit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelScreen::updateHotkeys() {
|
void LevelScreen::updateHotkeys() {
|
||||||
|
|||||||
@ -17,7 +17,6 @@ LevelController::LevelController(EngineSettings& settings, Level* level)
|
|||||||
}
|
}
|
||||||
|
|
||||||
LevelController::~LevelController() {
|
LevelController::~LevelController() {
|
||||||
scripting::on_world_quit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelController::update(float delta, bool input, bool pause) {
|
void LevelController::update(float delta, bool input, bool pause) {
|
||||||
@ -26,3 +25,11 @@ void LevelController::update(float delta, bool input, bool pause) {
|
|||||||
chunks->update(settings.chunks.loadSpeed);
|
chunks->update(settings.chunks.loadSpeed);
|
||||||
blocks->update(delta);
|
blocks->update(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LevelController::onWorldSave() {
|
||||||
|
scripting::on_world_save();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LevelController::onWorldQuit() {
|
||||||
|
scripting::on_world_quit();
|
||||||
|
}
|
||||||
|
|||||||
@ -29,6 +29,9 @@ public:
|
|||||||
void update(float delta,
|
void update(float delta,
|
||||||
bool input,
|
bool input,
|
||||||
bool pause);
|
bool pause);
|
||||||
|
|
||||||
|
void onWorldSave();
|
||||||
|
void onWorldQuit();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LOGIC_LEVEL_CONTROLLER_H_
|
#endif // LOGIC_LEVEL_CONTROLLER_H_
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <lua.hpp>
|
#include <lua.hpp>
|
||||||
|
|
||||||
|
#include "../../content/ContentPack.h"
|
||||||
#include "../../files/engine_paths.h"
|
#include "../../files/engine_paths.h"
|
||||||
#include "../../files/files.h"
|
#include "../../files/files.h"
|
||||||
#include "../../util/timeutil.h"
|
#include "../../util/timeutil.h"
|
||||||
@ -105,9 +106,40 @@ void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
|||||||
scripting::content = level->content;
|
scripting::content = level->content;
|
||||||
scripting::blocks = blocks;
|
scripting::blocks = blocks;
|
||||||
load_script("world.lua");
|
load_script("world.lua");
|
||||||
|
|
||||||
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
|
std::string name = pack.id+".worldopen";
|
||||||
|
lua_getglobal(L, name.c_str());
|
||||||
|
if (lua_isnil(L, lua_gettop(L))) {
|
||||||
|
lua_pop(L, lua_gettop(L));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
call_func(L, 0, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void scripting::on_world_save() {
|
||||||
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
|
std::string name = pack.id+".worldsave";
|
||||||
|
lua_getglobal(L, name.c_str());
|
||||||
|
if (lua_isnil(L, lua_gettop(L))) {
|
||||||
|
lua_pop(L, lua_gettop(L));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
call_func(L, 0, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_world_quit() {
|
void scripting::on_world_quit() {
|
||||||
|
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||||
|
std::string name = pack.id+".worldquit";
|
||||||
|
lua_getglobal(L, name.c_str());
|
||||||
|
if (lua_isnil(L, lua_gettop(L))) {
|
||||||
|
lua_pop(L, lua_gettop(L));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
call_func(L, 0, name);
|
||||||
|
}
|
||||||
scripting::level = nullptr;
|
scripting::level = nullptr;
|
||||||
scripting::content = nullptr;
|
scripting::content = nullptr;
|
||||||
}
|
}
|
||||||
@ -211,6 +243,20 @@ void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_s
|
|||||||
funcsset->on_block_break_by=rename_global(L, "on_block_break_by", (prefix+".blockbreakby").c_str());
|
funcsset->on_block_break_by=rename_global(L, "on_block_break_by", (prefix+".blockbreakby").c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void scripting::load_world_script(std::string prefix, fs::path file) {
|
||||||
|
std::string src = files::read_string(file);
|
||||||
|
std::cout << "loading script " << file.u8string() << std::endl;
|
||||||
|
if (luaL_loadbuffer(L, src.c_str(), src.size(), file.string().c_str())) {
|
||||||
|
handleError(L);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
call_func(L, 0, "<script>");
|
||||||
|
rename_global(L, "init", (prefix+".init").c_str());
|
||||||
|
rename_global(L, "on_world_open", (prefix+".worldopen").c_str());
|
||||||
|
rename_global(L, "on_world_save", (prefix+".worldsave").c_str());
|
||||||
|
rename_global(L, "on_world_quit", (prefix+".worldquit").c_str());
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::close() {
|
void scripting::close() {
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,7 @@ namespace scripting {
|
|||||||
|
|
||||||
void initialize(Engine* engine);
|
void initialize(Engine* engine);
|
||||||
void on_world_load(Level* level, BlocksController* blocks);
|
void on_world_load(Level* level, BlocksController* blocks);
|
||||||
|
void on_world_save();
|
||||||
void on_world_quit();
|
void on_world_quit();
|
||||||
void on_blocks_tick(const Block* block, int tps);
|
void on_blocks_tick(const Block* block, int tps);
|
||||||
void update_block(const Block* block, int x, int y, int z);
|
void update_block(const Block* block, int x, int y, int z);
|
||||||
@ -32,5 +33,6 @@ namespace scripting {
|
|||||||
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||||
void load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset);
|
void load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset);
|
||||||
void load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset);
|
void load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset);
|
||||||
|
void load_world_script(std::string prefix, fs::path file);
|
||||||
void close();
|
void close();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user