add entity events: on_attacked, on_used

This commit is contained in:
MihailRis 2024-07-16 14:04:05 +03:00
parent a36ffaacd9
commit 50050dbe40
6 changed files with 44 additions and 0 deletions

View File

@ -111,3 +111,7 @@ function on_update()
body:set_vel(dir)
end
end
function on_attacked(attacker, pid)
body:set_vel({0, 10, 0})
end

View File

@ -449,6 +449,20 @@ void PlayerController::processRightClick(Block* def, Block* target) {
}
}
void PlayerController::updateEntityInteraction(entityid_t eid, bool lclick, bool rclick) {
auto entityOpt = level->entities->get(eid);
if (!entityOpt.has_value()) {
return;
}
auto entity = entityOpt.value();
if (lclick) {
scripting::on_attacked(entity, player.get(), player->getEntity());
}
if (rclick) {
scripting::on_entity_used(entity, player.get());
}
}
void PlayerController::updateInteraction() {
auto indices = level->content->getIndices();
auto chunks = level->chunks.get();
@ -468,6 +482,9 @@ void PlayerController::updateInteraction() {
if (rclick && item->rt.funcsset.on_use) {
scripting::on_item_use(player.get(), item);
}
if (selection.entity) {
updateEntityInteraction(selection.entity, lclick, rclick);
}
return;
}

View File

@ -55,6 +55,7 @@ class PlayerController {
void updateKeyboard();
void resetKeyboard();
void updatePlayer(float delta);
void updateEntityInteraction(entityid_t eid, bool lclick, bool rclick);
void updateInteraction();
float stepsTimer = 0.0f;

View File

@ -348,6 +348,8 @@ void scripting::on_entity_spawn(
funcsset.on_save = lua::hasfield(L, "on_save");
funcsset.on_aim_on = lua::hasfield(L, "on_aim_on");
funcsset.on_aim_off = lua::hasfield(L, "on_aim_off");
funcsset.on_attacked = lua::hasfield(L, "on_attacked");
funcsset.on_used = lua::hasfield(L, "on_used");
lua::pop(L, 2);
component->env = compenv;
@ -439,6 +441,22 @@ void scripting::on_aim_off(const Entity& entity, Player* player) {
});
}
void scripting::on_attacked(const Entity& entity, Player* player, entityid_t attacker) {
process_entity_callback(entity, "on_attacked",
&entity_funcs_set::on_attacked, [player, attacker](auto L) {
lua::pushinteger(L, attacker);
lua::pushinteger(L, player->getId());
return 2;
});
}
void scripting::on_entity_used(const Entity& entity, Player* player) {
process_entity_callback(entity, "on_used",
&entity_funcs_set::on_used, [player](auto L) {
return lua::pushinteger(L, player->getId());
});
}
void scripting::on_entities_update() {
auto L = lua::get_main_thread();
lua::get_from(L, STDCOMP, "update", true);

View File

@ -95,6 +95,8 @@ namespace scripting {
void on_sensor_exit(const Entity& entity, size_t index, entityid_t oid);
void on_aim_on(const Entity& entity, Player* player);
void on_aim_off(const Entity& entity, Player* player);
void on_attacked(const Entity& entity, Player* player, entityid_t attacker);
void on_entity_used(const Entity& entity, Player* player);
/// @brief Called on UI view show
void on_ui_open(

View File

@ -24,6 +24,8 @@ struct entity_funcs_set {
bool on_save;
bool on_aim_on;
bool on_aim_off;
bool on_attacked;
bool on_used;
};
struct EntityDef;