added hud.lua script

This commit is contained in:
MihailRis 2024-02-18 12:45:37 +03:00
parent 33d14be71c
commit dabf42162c
5 changed files with 76 additions and 10 deletions

View File

@ -21,6 +21,7 @@
#include "../objects/Player.h"
#include "../logic/ChunksController.h"
#include "../logic/LevelController.h"
#include "../logic/scripting/scripting.h"
#include "../logic/scripting/scripting_frontend.h"
#include "../voxels/Chunks.h"
#include "../voxels/Chunk.h"
@ -97,11 +98,21 @@ LevelScreen::LevelScreen(Engine* engine, Level* level)
animator.reset(new TextureAnimator());
animator->addAnimations(engine->getAssets()->getAnimations());
auto content = level->content;
for (auto& pack : content->getPacks()) {
const ContentPack& info = pack->getInfo();
fs::path scriptFile = info.folder/fs::path("scripts/hud.lua");
if (fs::is_regular_file(scriptFile)) {
scripting::load_hud_script(pack->getEnvironment()->getId(), info.id, scriptFile);
}
}
scripting::on_frontend_init(hud.get());
}
LevelScreen::~LevelScreen() {
std::cout << "-- writing world" << std::endl;
scripting::on_frontend_close();
controller->onWorldSave();
auto world = level->getWorld();
world->write(level.get());

View File

@ -223,7 +223,7 @@ void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
}
}
bool register_event(int env, const std::string& name, const std::string& id) {
bool scripting::register_event(int env, const std::string& name, const std::string& id) {
if (state->pushenv(env) == 0) {
state->pushglobals();
}

View File

@ -41,6 +41,8 @@ namespace scripting {
void initialize(Engine* engine);
extern bool register_event(int env, const std::string& name, const std::string& id);
std::unique_ptr<Environment> create_environment(int parent=0);
std::unique_ptr<Environment> create_pack_environment(const ContentPack& pack);
std::unique_ptr<Environment> create_doc_environment(int parent, const std::string& name);
@ -62,20 +64,27 @@ namespace scripting {
@return true if prevents default action */
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
/* Called on UI view show */
/** Called on UI view show */
void on_ui_open(UiDocument* layout, Inventory* inventory);
/* Called on UI view close*/
/** Called on UI view close*/
void on_ui_close(UiDocument* layout, Inventory* inventory);
/* Load script associated with a Block */
/** Load script associated with a Block */
void load_block_script(int env, std::string prefix, fs::path file, block_funcs_set& funcsset);
/* Load script associated with an Item */
/** Load script associated with an Item */
void load_item_script(int env, std::string prefix, fs::path file, item_funcs_set& funcsset);
/* Load package-specific world script */
void load_world_script(int env, std::string prefix, fs::path file);
/* Load script associated with an UiDocument */
void load_layout_script(int env, std::string prefix, fs::path file, uidocscript& script);
/* Finalize lua state. Using scripting after will lead to Lua panic */
/**
* Load package-specific world script
* @param env environment id
* @param packid content-pack id
* @param file script file path
*/
void load_world_script(int env, std::string packid, fs::path file);
/** Load script associated with an UiDocument */
void load_layout_script(int env, std::string prefix, fs::path file, uidocscript& script);
/** Finalize lua state. Using scripting after will lead to Lua panic */
void close();
}

View File

@ -4,6 +4,11 @@
#include "lua/libhud.h"
#include "lua/LuaState.h"
#include "../../files/files.h"
#include "../../engine.h"
#include <iostream>
namespace scripting {
extern lua::LuaState* state;
}
@ -13,4 +18,31 @@ Hud* scripting::hud = nullptr;
void scripting::on_frontend_init(Hud* hud) {
scripting::hud = hud;
scripting::state->openlib("hud", hudlib, 0);
for (auto& pack : scripting::engine->getContentPacks()) {
if (state->getglobal(pack.id+".hudopen")) {
state->callNoThrow(0);
}
}
}
void scripting::on_frontend_close() {
scripting::hud = nullptr;
for (auto& pack : scripting::engine->getContentPacks()) {
if (state->getglobal(pack.id+".hudclose")) {
state->callNoThrow(0);
}
}
}
void scripting::load_hud_script(int env, std::string packid, fs::path file) {
std::string src = files::read_string(file);
std::cout << "loading script " << file.u8string() << std::endl;
state->loadbuffer(env, src, file.u8string());
state->callNoThrow(0);
register_event(env, "init", packid+".init");
register_event(env, "on_hud_open", packid+".hudopen");
register_event(env, "on_hud_close", packid+".hudclose");
}

View File

@ -1,12 +1,26 @@
#ifndef LOGIC_SCRIPTING_SCRIPTING_FRONTEND_H_
#define LOGIC_SCRIPTING_SCRIPTING_FRONTEND_H_
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
class Hud;
namespace scripting {
extern Hud* hud;
void on_frontend_init(Hud* hud);
void on_frontend_close();
/**
* Load package-specific hud script
* @param env environment id
* @param packid content-pack id
* @param file script file path
*/
void load_hud_script(int env, std::string packid, fs::path file);
}
#endif // LOGIC_SCRIPTING_SCRIPTING_FRONTEND_H_