add entity events: on_spawn, on_despawn

This commit is contained in:
MihailRis
2024-06-28 12:16:31 +03:00
parent e1eb652ef4
commit eb2be5e8b6
13 changed files with 116 additions and 22 deletions
+26
View File
@@ -13,6 +13,8 @@
#include "../../logic/BlocksController.hpp"
#include "../../logic/LevelController.hpp"
#include "../../objects/Player.hpp"
#include "../../objects/EntityDef.hpp"
#include "../../objects/Entities.hpp"
#include "../../util/stringutil.hpp"
#include "../../util/timeutil.hpp"
#include "../../util/timeutil.hpp"
@@ -225,6 +227,22 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
});
}
bool scripting::on_entity_spawn(const EntityDef& def, entityid_t eid) {
std::string name = def.name + ".spawn";
return lua::emit_event(lua::get_main_thread(), name, [eid] (auto L) {
lua::pushinteger(L, eid);
return 1;
});
}
bool scripting::on_entity_despawn(const EntityDef& def, entityid_t eid) {
std::string name = def.name + ".despawn";
return lua::emit_event(lua::get_main_thread(), name, [eid] (auto L) {
lua::pushinteger(L, eid);
return 1;
});
}
void scripting::on_ui_open(
UiDocument* layout,
std::vector<dynamic::Value> args
@@ -308,6 +326,14 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi
funcsset.on_block_break_by = register_event(env, "on_block_break_by", prefix+".blockbreakby");
}
void scripting::load_entity_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, entity_funcs_set& funcsset) {
int env = *senv;
load_script(env, "entity", file);
funcsset.init = register_event(env, "init", prefix+".init");
funcsset.on_spawn = register_event(env, "on_spawn", prefix+".spawn");
funcsset.on_despawn = register_event(env, "on_despawn", prefix+".despawn");
}
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {
int env = *senv;
load_script(env, "world", file);
+23 -2
View File
@@ -27,9 +27,12 @@ class Inventory;
class UiDocument;
struct block_funcs_set;
struct item_funcs_set;
struct entity_funcs_set;
struct uidocscript;
class BlocksController;
class LevelController;
class Entity;
struct EntityDef;
namespace scripting {
extern Engine* engine;
@@ -73,6 +76,10 @@ namespace scripting {
/// @return true if prevents default action
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
bool on_entity_spawn(const EntityDef& def, entityid_t eid);
bool on_entity_despawn(const EntityDef& def, entityid_t eid);
/// @brief Called on UI view show
void on_ui_open(
UiDocument* layout,
@@ -89,14 +96,28 @@ namespace scripting {
/// @param prefix pack id
/// @param file item script file
/// @param funcsset block callbacks set
void load_block_script(const scriptenv& env, const std::string& prefix, const fs::path& file, block_funcs_set& funcsset);
void load_block_script(
const scriptenv& env,
const std::string& prefix,
const fs::path& file,
block_funcs_set& funcsset);
/// @brief Load script associated with an Item
/// @param env environment
/// @param prefix pack id
/// @param file item script file
/// @param funcsset item callbacks set
void load_item_script(const scriptenv& env, const std::string& prefix, const fs::path& file, item_funcs_set& funcsset);
void load_item_script(
const scriptenv& env,
const std::string& prefix,
const fs::path& file,
item_funcs_set& funcsset);
void load_entity_script(
const scriptenv& env,
const std::string& prefix,
const fs::path& file,
entity_funcs_set& funcsset);
/// @brief Load package-specific world script
/// @param env environment