add entity event: on_grounded

This commit is contained in:
MihailRis 2024-06-28 12:47:45 +03:00
parent eb2be5e8b6
commit e0e5faa4a8
7 changed files with 24 additions and 6 deletions

View File

@ -5,3 +5,7 @@ end
function on_despawn(eid)
print("despawn", eid)
end
function on_grounded(eid)
Transform.set_rot(eid, mat4.rotate({0, 1, 0}, math.random()*360))
end

View File

@ -9,6 +9,8 @@ function on_hud_open()
local eid = entity.spawn("base:drop", ppos)
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
Rigidbody.set_vel(eid, vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)))
Transform.set_rot(eid, mat4.rotate({0, 1, 0}, math.random() * 360))
Transform.set_rot(eid,
mat4.rotate(mat4.rotate(mat4.rotate({0, 1, 0}, math.random() * 360),
{1, 0, 0}, math.random() * 360), {0, 0, 1}, math.random() * 360))
end)
end

View File

@ -243,6 +243,14 @@ bool scripting::on_entity_despawn(const EntityDef& def, entityid_t eid) {
});
}
bool scripting::on_entity_grounded(const EntityDef& def, entityid_t eid) {
std::string name = def.name + ".grounded";
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
@ -332,6 +340,7 @@ void scripting::load_entity_script(const scriptenv& senv, const std::string& pre
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");
funcsset.on_grounded = register_event(env, "on_grounded", prefix+".grounded");
}
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {

View File

@ -77,8 +77,8 @@ namespace scripting {
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);
bool on_entity_grounded(const EntityDef& def, entityid_t eid);
/// @brief Called on UI view show
void on_ui_open(

View File

@ -61,13 +61,14 @@ void Entities::clean() {
}
void Entities::updatePhysics(float delta){
auto view = registry.view<Transform, Rigidbody>();
auto view = registry.view<EntityId, Transform, Rigidbody>();
auto physics = level->physics.get();
for (auto [entity, transform, rigidbody] : view.each()) {
for (auto [entity, eid, transform, rigidbody] : view.each()) {
if (!rigidbody.enabled) {
continue;
}
auto& hitbox = rigidbody.hitbox;
bool grounded = hitbox.grounded;
physics->step(
level->chunks.get(),
&hitbox,
@ -80,8 +81,8 @@ void Entities::updatePhysics(float delta){
hitbox.linearDamping = hitbox.grounded * 12;
transform.pos = hitbox.position;
//transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0));
if (hitbox.grounded) {
//hitbox.velocity.y = 10;
if (hitbox.grounded && !grounded) {
scripting::on_entity_grounded(eid.def, eid.uid);
}
}
}

View File

@ -85,6 +85,7 @@ class Entities {
entityid_t nextID = 1;
public:
Entities(Level* level);
void clean();
void updatePhysics(float delta);

View File

@ -10,6 +10,7 @@ struct entity_funcs_set {
bool init : 1;
bool on_spawn : 1;
bool on_despawn : 1;
bool on_grounded : 1;
};
struct EntityDef {