scripting: on_interact (right click on block) callback

This commit is contained in:
MihailRis 2023-12-29 00:31:21 +03:00
parent 2b4e6c1a14
commit 2c75a0bee4
5 changed files with 18 additions and 0 deletions

View File

@ -266,6 +266,10 @@ void PlayerController::updateInteraction(){
blocksController->breakBlock(player, block, x, y, z);
}
if (rclick){
if (block->rt.funcsset.oninteract) {
scripting::on_block_interact(player, block, x, y, z);
return;
}
if (block->model != BlockModel::xsprite){
x = (iend.x)+(norm.x);
y = (iend.y)+(norm.y);

View File

@ -6,6 +6,7 @@
#include "../../voxels/Block.h"
#include "../../voxels/Chunks.h"
#include "../../voxels/voxel.h"
#include "../../lighting/Lighting.h"
int l_block_name(lua_State* L) {
int id = lua_tointeger(L, 1);
@ -39,6 +40,7 @@ int l_set_block(lua_State* L) {
int z = lua_tointeger(L, 3);
int id = lua_tointeger(L, 4);
scripting::level->chunks->set(x, y, z, id, 0);
scripting::level->lighting->onBlockSet(x,y,z, id);
return 0;
}

View File

@ -116,6 +116,15 @@ void scripting::on_block_broken(Player* player, const Block* block, int x, int y
call_func(L, 3, name);
}
void scripting::on_block_interact(Player* player, const Block* block, int x, int y, int z) {
std::string name = block->name+".oninteract";
lua_getglobal(L, name.c_str());
lua_pushinteger(L, x);
lua_pushinteger(L, y);
lua_pushinteger(L, z);
call_func(L, 3, name);
}
void scripting::load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset) {
std::string src = files::read_string(file);
std::cout << "loading script " << file.u8string() << std::endl;
@ -129,6 +138,7 @@ void scripting::load_block_script(std::string prefix, fs::path file, block_funcs
funcsset->randupdate=rename_global(L, "on_random_update", (prefix+".randupdate").c_str());
funcsset->onbroken=rename_global(L, "on_broken", (prefix+".broken").c_str());
funcsset->onplaced=rename_global(L, "on_placed", (prefix+".placed").c_str());
funcsset->oninteract=rename_global(L, "on_interact", (prefix+".oninteract").c_str());
}
void scripting::close() {

View File

@ -21,6 +21,7 @@ namespace scripting {
void random_update_block(const Block* block, int x, int y, int z);
void on_block_placed(Player* player, const Block* block, int x, int y, int z);
void on_block_broken(Player* player, const Block* block, int x, int y, int z);
void on_block_interact(Player* player, const Block* block, int x, int y, int z);
void load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset);
void close();
}

View File

@ -21,6 +21,7 @@ struct block_funcs_set {
bool update: 1;
bool onplaced: 1;
bool onbroken: 1;
bool oninteract: 1;
bool randupdate: 1;
};