Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/scripting/StarEntityLuaBindings.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
commit6352e8e3196f78388b6c771073f9e03eaa612673 (patch)
treee23772f79a7fbc41bc9108951e9e136857484bf4 /source/game/scripting/StarEntityLuaBindings.cpp
parent6741a057e5639280d85d0f88ba26f000baa58f61 (diff)
everything everywhere
all at once
Diffstat (limited to 'source/game/scripting/StarEntityLuaBindings.cpp')
-rw-r--r--source/game/scripting/StarEntityLuaBindings.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/source/game/scripting/StarEntityLuaBindings.cpp b/source/game/scripting/StarEntityLuaBindings.cpp
new file mode 100644
index 0000000..00c501a
--- /dev/null
+++ b/source/game/scripting/StarEntityLuaBindings.cpp
@@ -0,0 +1,77 @@
+#include "StarEntityLuaBindings.hpp"
+#include "StarJsonExtra.hpp"
+#include "StarLuaGameConverters.hpp"
+#include "StarPlayer.hpp"
+#include "StarMonster.hpp"
+#include "StarNpc.hpp"
+#include "StarWorld.hpp"
+
+namespace Star {
+
+LuaCallbacks LuaBindings::makeEntityCallbacks(Entity const* entity) {
+ LuaCallbacks callbacks;
+
+ callbacks.registerCallbackWithSignature<EntityId>("id", bind(EntityCallbacks::id, entity));
+ callbacks.registerCallbackWithSignature<LuaTable, LuaEngine&>(
+ "damageTeam", bind(EntityCallbacks::damageTeam, entity, _1));
+ callbacks.registerCallbackWithSignature<bool, EntityId>(
+ "isValidTarget", bind(EntityCallbacks::isValidTarget, entity, _1));
+ callbacks.registerCallbackWithSignature<Vec2F, EntityId>(
+ "distanceToEntity", bind(EntityCallbacks::distanceToEntity, entity, _1));
+ callbacks.registerCallbackWithSignature<bool, EntityId>(
+ "entityInSight", bind(EntityCallbacks::entityInSight, entity, _1));
+
+ callbacks.registerCallback("position", [entity]() { return entity->position(); });
+ callbacks.registerCallback("entityType", [entity]() { return EntityTypeNames.getRight(entity->entityType()); });
+ callbacks.registerCallback("uniqueId", [entity]() { return entity->uniqueId(); });
+ callbacks.registerCallback("persistent", [entity]() { return entity->persistent(); });
+
+ return callbacks;
+}
+
+EntityId LuaBindings::EntityCallbacks::id(Entity const* entity) {
+ return entity->entityId();
+}
+
+LuaTable LuaBindings::EntityCallbacks::damageTeam(Entity const* entity, LuaEngine& engine) {
+ auto table = engine.createTable();
+ auto team = entity->getTeam();
+ table.set("type", TeamTypeNames.getRight(team.type));
+ table.set("team", team.team);
+ return table;
+}
+
+bool LuaBindings::EntityCallbacks::isValidTarget(Entity const* entity, EntityId entityId) {
+ auto target = entity->world()->entity(entityId);
+
+ if (!target || !entity->getTeam().canDamage(target->getTeam(), false))
+ return false;
+
+ if (auto monster = as<Monster>(target))
+ return monster->aggressive();
+
+ if (auto npc = as<Npc>(target)) {
+ if (auto attackerNpc = as<Npc>(entity))
+ return npc->aggressive() || attackerNpc->aggressive();
+ return true;
+ }
+
+ return is<Player>(target);
+}
+
+Vec2F LuaBindings::EntityCallbacks::distanceToEntity(Entity const* entity, EntityId entityId) {
+ Vec2F dist;
+ if (auto target = entity->world()->entity(entityId))
+ dist = entity->world()->geometry().diff(target->position(), entity->position());
+
+ return dist;
+}
+
+bool LuaBindings::EntityCallbacks::entityInSight(Entity const* entity, EntityId entityId) {
+ if (auto target = entity->world()->entity(entityId))
+ return !entity->world()->lineTileCollision(target->position(), entity->position());
+ else
+ return false;
+}
+
+}