add on_update event
This commit is contained in:
parent
bb1c0b4b44
commit
982c8b132d
@ -1,9 +1,11 @@
|
|||||||
function on_despawn()
|
inair = true
|
||||||
print("despawn")
|
|
||||||
end
|
|
||||||
|
|
||||||
function on_grounded()
|
function on_grounded()
|
||||||
entity.transform:set_rot(mat4.rotate({0, 1, 0}, math.random()*360))
|
entity.transform:set_rot(mat4.rotate({0, 1, 0}, math.random()*360))
|
||||||
print(stdcomp)
|
inair = false
|
||||||
entity:despawn()
|
end
|
||||||
|
|
||||||
|
function on_update()
|
||||||
|
if inair then
|
||||||
|
entity.transform:set_rot(mat4.rotate(entity.transform:get_rot(), {0, 1, 0}, math.random()*32))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -40,6 +40,21 @@ return {
|
|||||||
return entity
|
return entity
|
||||||
end,
|
end,
|
||||||
remove_Entity = function(eid)
|
remove_Entity = function(eid)
|
||||||
entities[eid] = nil;
|
local entity = entities[eid]
|
||||||
|
if entity then
|
||||||
|
entity.env = nil
|
||||||
|
entities[eid] = nil;
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
update = function()
|
||||||
|
for id,entity in pairs(entities) do
|
||||||
|
local callback = entity.env.on_update
|
||||||
|
if callback then
|
||||||
|
local result, err = pcall(callback)
|
||||||
|
if err then
|
||||||
|
print(err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,6 +50,7 @@ void LevelController::update(float delta, bool input, bool pause) {
|
|||||||
}
|
}
|
||||||
blocks->update(delta);
|
blocks->update(delta);
|
||||||
level->entities->updatePhysics(delta);
|
level->entities->updatePhysics(delta);
|
||||||
|
level->entities->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -115,7 +115,8 @@ static scriptenv create_entity_environment(const scriptenv& parent, int entityId
|
|||||||
lua::pushvalue(L, -2);
|
lua::pushvalue(L, -2);
|
||||||
lua::setfield(L, "entity");
|
lua::setfield(L, "entity");
|
||||||
|
|
||||||
lua::pop(L, 2);
|
lua::setfield(L, "env");
|
||||||
|
lua::pop(L);
|
||||||
|
|
||||||
return std::shared_ptr<int>(new int(id), [=](int* id) {
|
return std::shared_ptr<int>(new int(id), [=](int* id) {
|
||||||
lua::removeEnvironment(L, *id);
|
lua::removeEnvironment(L, *id);
|
||||||
@ -307,12 +308,18 @@ bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) {
|
|||||||
|
|
||||||
bool scripting::on_entity_grounded(const EntityDef& def, const Entity& entity) {
|
bool scripting::on_entity_grounded(const EntityDef& def, const Entity& entity) {
|
||||||
const auto& script = entity.getScripting();
|
const auto& script = entity.getScripting();
|
||||||
if (script.funcsset.on_despawn) {
|
if (script.funcsset.on_grounded) {
|
||||||
return process_entity_callback(script.env, "on_grounded", nullptr);
|
return process_entity_callback(script.env, "on_grounded", nullptr);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void scripting::on_entities_update() {
|
||||||
|
auto L = lua::get_main_thread();
|
||||||
|
lua::get_from(L, STDCOMP, "update", true);
|
||||||
|
lua::call_nothrow(L, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_ui_open(
|
void scripting::on_ui_open(
|
||||||
UiDocument* layout,
|
UiDocument* layout,
|
||||||
std::vector<dynamic::Value> args
|
std::vector<dynamic::Value> args
|
||||||
|
|||||||
@ -79,6 +79,7 @@ namespace scripting {
|
|||||||
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&);
|
||||||
bool on_entity_despawn(const EntityDef& def, const Entity& entity);
|
bool on_entity_despawn(const EntityDef& def, const Entity& entity);
|
||||||
bool on_entity_grounded(const EntityDef& def, const Entity& entity);
|
bool on_entity_grounded(const EntityDef& def, const Entity& entity);
|
||||||
|
void on_entities_update();
|
||||||
|
|
||||||
/// @brief Called on UI view show
|
/// @brief Called on UI view show
|
||||||
void on_ui_open(
|
void on_ui_open(
|
||||||
|
|||||||
@ -86,6 +86,10 @@ void Entities::updatePhysics(float delta){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entities::update() {
|
||||||
|
scripting::on_entities_update();
|
||||||
|
}
|
||||||
|
|
||||||
void Entities::renderDebug(LineBatch& batch) {
|
void Entities::renderDebug(LineBatch& batch) {
|
||||||
batch.lineWidth(1.0f);
|
batch.lineWidth(1.0f);
|
||||||
auto view = registry.view<Transform, Rigidbody>();
|
auto view = registry.view<Transform, Rigidbody>();
|
||||||
|
|||||||
@ -103,6 +103,7 @@ public:
|
|||||||
|
|
||||||
void clean();
|
void clean();
|
||||||
void updatePhysics(float delta);
|
void updatePhysics(float delta);
|
||||||
|
void update();
|
||||||
|
|
||||||
void renderDebug(LineBatch& batch);
|
void renderDebug(LineBatch& batch);
|
||||||
void render(Assets* assets, ModelBatch& batch, Frustum& frustum);
|
void render(Assets* assets, ModelBatch& batch, Frustum& frustum);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user