add component ARGS variable

This commit is contained in:
MihailRis 2024-07-01 00:17:58 +03:00
parent da6b014d62
commit 1b41a75cf4
6 changed files with 24 additions and 7 deletions

View File

@ -1,5 +1,5 @@
{
"hitbox": [0.2, 0.2, 0.2],
"hitbox": [0.2, 0.125, 0.2],
"triggers": [
[-0.2, -0.2, -0.2, 0.2, 0.2, 0.2]
]

View File

@ -31,7 +31,11 @@ static int l_spawn(lua::State* L) {
auto defname = lua::tostring(L, 1);
auto& def = content->entities.require(defname);
auto pos = lua::tovec3(L, 2);
level->entities->spawn(def, pos);
dynamic::Value args = dynamic::NONE;
if (lua::gettop(L) > 2) {
args = lua::tovalue(L, 3);
}
level->entities->spawn(def, pos, args);
return 1;
}

View File

@ -256,7 +256,12 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
});
}
scriptenv scripting::on_entity_spawn(const EntityDef& def, entityid_t eid, entity_funcs_set& funcsset) {
scriptenv scripting::on_entity_spawn(
const EntityDef& def,
entityid_t eid,
entity_funcs_set& funcsset,
dynamic::Value args
) {
auto L = lua::get_main_thread();
lua::requireglobal(L, STDCOMP);
if (lua::getfield(L, "new_Entity")) {
@ -266,6 +271,8 @@ scriptenv scripting::on_entity_spawn(const EntityDef& def, entityid_t eid, entit
auto entityenv = create_entity_environment(get_root_environment(), -1);
lua::get_from(L, lua::CHUNKS_TABLE, def.scriptName, true);
lua::pushenv(L, *entityenv);
lua::pushvalue(L, args);
lua::setfield(L, "ARGS");
lua::setfenv(L);
lua::call_nothrow(L, 0, 0);

View File

@ -76,7 +76,12 @@ namespace scripting {
/// @return true if prevents default action
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
scriptenv on_entity_spawn(const EntityDef& def, entityid_t eid, entity_funcs_set&);
scriptenv on_entity_spawn(
const EntityDef& def,
entityid_t eid,
entity_funcs_set&,
dynamic::Value args
);
bool on_entity_despawn(const EntityDef& def, const Entity& entity);
bool on_entity_grounded(const Entity& entity, float force);
bool on_entity_fall(const Entity& entity);

View File

@ -30,7 +30,7 @@ void Entity::destroy() {
Entities::Entities(Level* level) : level(level) {
}
entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos, dynamic::Value args) {
auto entity = registry.create();
glm::vec3 size(1);
auto id = nextID++;
@ -55,7 +55,7 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
}
auto& scripting = registry.emplace<Scripting>(entity, entity_funcs_set {}, nullptr);
entities[id] = entity;
scripting.env = scripting::on_entity_spawn(def, id, scripting.funcsset);
scripting.env = scripting::on_entity_spawn(def, id, scripting.funcsset, std::move(args));
return id;
}

View File

@ -3,6 +3,7 @@
#include "../typedefs.hpp"
#include "../physics/Hitbox.hpp"
#include "../data/dynamic.hpp"
#include <vector>
#include <optional>
@ -131,7 +132,7 @@ public:
void renderDebug(LineBatch& batch, const Frustum& frustum);
void render(Assets* assets, ModelBatch& batch, const Frustum& frustum);
entityid_t spawn(EntityDef& def, glm::vec3 pos);
entityid_t spawn(EntityDef& def, glm::vec3 pos, dynamic::Value args=dynamic::NONE);
std::optional<Entity> get(entityid_t id) {
const auto& found = entities.find(id);