From 779ad773d54f94950735fc0f29f1768d9089961e Mon Sep 17 00:00:00 2001 From: Vsevolod Kremianskii Date: Mon, 14 Dec 2020 23:46:35 +0700 Subject: [PATCH] feat: Implement setFacingPoint script routine --- src/game/object/spatial.cpp | 8 ++++++-- src/game/object/spatial.h | 1 + src/game/script/routines.h | 1 + src/game/script/routines_kotor.cpp | 2 +- src/game/script/routines_objects.cpp | 11 +++++++++++ src/game/script/routines_tsl.cpp | 2 +- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/game/object/spatial.cpp b/src/game/object/spatial.cpp index 3728471f..c39d3f06 100644 --- a/src/game/object/spatial.cpp +++ b/src/game/object/spatial.cpp @@ -97,9 +97,13 @@ bool SpatialObject::contains(const glm::vec3 &point) const { } void SpatialObject::face(const SpatialObject &other) { - if (_id == other._id) return; + if (_id != other._id) { + face(other._position); + } +} - glm::vec2 dir(glm::normalize(other._position - _position)); +void SpatialObject::face(const glm::vec3 &point) { + glm::vec2 dir(glm::normalize(point - _position)); _facing = -glm::atan(dir.x, dir.y); updateTransform(); } diff --git a/src/game/object/spatial.h b/src/game/object/spatial.h index dc4f6b5e..5dfae0be 100644 --- a/src/game/object/spatial.h +++ b/src/game/object/spatial.h @@ -46,6 +46,7 @@ public: void update(float dt) override; void face(const SpatialObject &other); + void face(const glm::vec3 &point); void applyEffect(const std::shared_ptr &eff); diff --git a/src/game/script/routines.h b/src/game/script/routines.h index 516c6cdb..46299f49 100644 --- a/src/game/script/routines.h +++ b/src/game/script/routines.h @@ -176,6 +176,7 @@ private: script::Variable getTag(const VariablesList &args, script::ExecutionContext &ctx); script::Variable getWaypointByTag(const VariablesList &args, script::ExecutionContext &ctx); script::Variable setFacing(const VariablesList &args, script::ExecutionContext &ctx); + script::Variable setFacingPoint(const VariablesList &args, script::ExecutionContext &ctx); script::Variable setItemStackSize(const VariablesList &args, script::ExecutionContext &ctx); script::Variable setLocked(const VariablesList &args, script::ExecutionContext &ctx); script::Variable soundObjectPlay(const VariablesList &args, script::ExecutionContext &ctx); diff --git a/src/game/script/routines_kotor.cpp b/src/game/script/routines_kotor.cpp index 6b31ff4e..00a86ee1 100644 --- a/src/game/script/routines_kotor.cpp +++ b/src/game/script/routines_kotor.cpp @@ -181,7 +181,7 @@ void Routines::addKotorRoutines() { add("GetIsDead", Int, { Object }, &Routines::getIsDead); add("PrintVector", Void, { TVector, Int }, &Routines::printVector); add("Vector", TVector, { Float, Float, Float }, &Routines::vectorCreate); - add("SetFacingPoint", Void, { TVector }); + add("SetFacingPoint", Void, { TVector }, &Routines::setFacingPoint); add("AngleToVector", TVector, { Float }); add("VectorToAngle", Float, { TVector }); add("TouchAttackMelee", Int, { Object, Int }); diff --git a/src/game/script/routines_objects.cpp b/src/game/script/routines_objects.cpp index 6186cf9f..daea2004 100644 --- a/src/game/script/routines_objects.cpp +++ b/src/game/script/routines_objects.cpp @@ -342,6 +342,17 @@ Variable Routines::setFacing(const VariablesList &args, ExecutionContext &ctx) { return Variable(); } +Variable Routines::setFacingPoint(const VariablesList &args, ExecutionContext &ctx) { + auto caller = getCallerAsSpatial(ctx); + if (caller) { + glm::vec3 target(getVector(args, 0)); + caller->face(target); + } else { + warn("Routines: setFacingPoint: invalid caller"); + } + return Variable(); +} + } // namespace game } // namespace reone diff --git a/src/game/script/routines_tsl.cpp b/src/game/script/routines_tsl.cpp index a11f4d6e..e6c23330 100644 --- a/src/game/script/routines_tsl.cpp +++ b/src/game/script/routines_tsl.cpp @@ -181,7 +181,7 @@ void Routines::addTslRoutines() { add("GetIsDead", Int, { Object }, &Routines::getIsDead); add("PrintVector", Void, { TVector, Int }, &Routines::printVector); add("Vector", TVector, { Float, Float, Float }, &Routines::vectorCreate); - add("SetFacingPoint", Void, { TVector }); + add("SetFacingPoint", Void, { TVector }, &Routines::setFacingPoint); add("AngleToVector", TVector, { Float }); add("VectorToAngle", Float, { TVector }); add("TouchAttackMelee", Int, { Object, Int });