add entity event: on_fall
This commit is contained in:
parent
3946119a67
commit
50c714692a
@ -1,11 +1,23 @@
|
||||
inair = true
|
||||
function on_grounded()
|
||||
|
||||
function on_grounded(force)
|
||||
entity.transform:set_rot(mat4.rotate({0, 1, 0}, math.random()*360))
|
||||
inair = false
|
||||
end
|
||||
|
||||
function on_update()
|
||||
if inair then
|
||||
entity.transform:set_rot(mat4.rotate(entity.transform:get_rot(), {0, 1, 0}, math.random()*32))
|
||||
end
|
||||
function on_fall()
|
||||
inair = true
|
||||
end
|
||||
|
||||
function on_update()
|
||||
local tsf = entity.transform
|
||||
local body = entity.rigidbody
|
||||
if inair then
|
||||
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, math.random()*12))
|
||||
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, math.random()*12))
|
||||
end
|
||||
local dir = vec3.sub({player.get_pos(hud.get_player())}, tsf:get_pos())
|
||||
vec3.normalize(dir, dir)
|
||||
vec3.mul(dir, time.delta()*50.0, dir)
|
||||
--body:set_vel(vec3.add(rigidbody:get_vel(), dir))
|
||||
end
|
||||
|
||||
@ -271,6 +271,7 @@ scriptenv scripting::on_entity_spawn(const EntityDef& def, entityid_t eid, entit
|
||||
|
||||
lua::pushenv(L, *entityenv);
|
||||
funcsset.on_grounded = lua::hasfield(L, "on_grounded");
|
||||
funcsset.on_fall = lua::hasfield(L, "on_fall");
|
||||
funcsset.on_despawn = lua::hasfield(L, "on_despawn");
|
||||
lua::pop(L, 2);
|
||||
return entityenv;
|
||||
@ -306,10 +307,20 @@ bool scripting::on_entity_despawn(const EntityDef& def, const Entity& entity) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool scripting::on_entity_grounded(const EntityDef& def, const Entity& entity) {
|
||||
bool scripting::on_entity_grounded(const Entity& entity, float force) {
|
||||
const auto& script = entity.getScripting();
|
||||
if (script.funcsset.on_grounded) {
|
||||
return process_entity_callback(script.env, "on_grounded", nullptr);
|
||||
return process_entity_callback(script.env, "on_grounded", [force](auto L){
|
||||
return lua::pushnumber(L, force);
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool scripting::on_entity_fall(const Entity& entity) {
|
||||
const auto& script = entity.getScripting();
|
||||
if (script.funcsset.on_fall) {
|
||||
return process_entity_callback(script.env, "on_fall", nullptr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -78,7 +78,8 @@ namespace scripting {
|
||||
|
||||
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_grounded(const EntityDef& def, const Entity& entity);
|
||||
bool on_entity_grounded(const Entity& entity, float force);
|
||||
bool on_entity_fall(const Entity& entity);
|
||||
void on_entities_update();
|
||||
|
||||
/// @brief Called on UI view show
|
||||
|
||||
@ -67,6 +67,7 @@ void Entities::updatePhysics(float delta){
|
||||
continue;
|
||||
}
|
||||
auto& hitbox = rigidbody.hitbox;
|
||||
auto prevVel = hitbox.velocity;
|
||||
bool grounded = hitbox.grounded;
|
||||
physics->step(
|
||||
level->chunks.get(),
|
||||
@ -77,11 +78,14 @@ void Entities::updatePhysics(float delta){
|
||||
1.0f,
|
||||
true
|
||||
);
|
||||
hitbox.linearDamping = hitbox.grounded * 12;
|
||||
hitbox.linearDamping = hitbox.grounded * 24;
|
||||
transform.pos = hitbox.position;
|
||||
//transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0));
|
||||
if (hitbox.grounded && !grounded) {
|
||||
scripting::on_entity_grounded(eid.def, *get(eid.uid));
|
||||
scripting::on_entity_grounded(*get(eid.uid), glm::length(prevVel-hitbox.velocity));
|
||||
}
|
||||
if (!hitbox.grounded && grounded) {
|
||||
scripting::on_entity_fall(*get(eid.uid));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ struct entity_funcs_set {
|
||||
bool init : 1;
|
||||
bool on_despawn : 1;
|
||||
bool on_grounded : 1;
|
||||
bool on_fall : 1;
|
||||
};
|
||||
|
||||
struct EntityDef;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user