[Orxonox-commit 7019] r11638 - in code/branches/ScriptableController_HS17/src/orxonox: infos scriptablecontroller worldentities
kohlia at orxonox.net
kohlia at orxonox.net
Mon Dec 4 16:17:59 CET 2017
Author: kohlia
Date: 2017-12-04 16:17:59 +0100 (Mon, 04 Dec 2017)
New Revision: 11638
Modified:
code/branches/ScriptableController_HS17/src/orxonox/infos/GametypeInfo.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc
Log:
Position/velocity setting works now, relative script paths not, added test script
Modified: code/branches/ScriptableController_HS17/src/orxonox/infos/GametypeInfo.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/infos/GametypeInfo.cc 2017-12-04 15:17:43 UTC (rev 11637)
+++ code/branches/ScriptableController_HS17/src/orxonox/infos/GametypeInfo.cc 2017-12-04 15:17:59 UTC (rev 11638)
@@ -317,7 +317,8 @@
if(player->isHumanPlayer() && player->isLocalPlayer())
{
this->getLevel()->getScriptableController()->setPlayer(player);
- this->getLevel()->getScriptableController()->runScript(this->getLevel()->getScript());
+ // TODO Fix for relative paths
+ this->getLevel()->getScriptableController()->runScript(this->getLevel()->getFilename() + "/" + this->getLevel()->getScript());
}
}
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc 2017-12-04 15:17:43 UTC (rev 11637)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc 2017-12-04 15:17:59 UTC (rev 11638)
@@ -63,6 +63,11 @@
this->worldEntities_[id] = entity;
}
+void ScriptableController::registerMobileEntity(std::string id, MobileEntity *entity)
+{
+ this->mobileEntities_[id] = entity;
+}
+
void ScriptableController::registerPawn(std::string id, Pawn *pawn)
{
this->worldEntities_[id] = pawn;
@@ -123,6 +128,16 @@
return entity != this->worldEntities_.end() ? entity->second : nullptr;
}
+MobileEntity *ScriptableController::getMobileEntityByID(std::string id) const
+{
+ if(id == "player" || id == "Player" || id == "PLAYER")
+ return this->player_->getControllableEntity();
+
+ auto entity = this->mobileEntities_.find(id);
+ return entity != this->mobileEntities_.end() ? entity->second : nullptr;
+
+}
+
Pawn *ScriptableController::getPawnByID(std::string id) const
{
auto pawn = this->pawns_.find(id);
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h 2017-12-04 15:17:43 UTC (rev 11637)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h 2017-12-04 15:17:59 UTC (rev 11638)
@@ -58,6 +58,16 @@
void registerWorldEntity(std::string id, WorldEntity *entity);
/**
+ * @brief Register a MobileEntity to the ScriptableController
+ * @param id The ID of the MobileEntity
+ * @param entity The MobileEntity
+ *
+ * The ScriptableController needs a list of all MobileEntity's so it can
+ * convert an ID to a MobileEntity.
+ */
+ void registerMobileEntity(std::string id, MobileEntity *entity);
+
+ /**
* @brief Register a Pawn to the ScriptableController
* @param id The ID of the Pawn
* @param pawn The Pawn
@@ -100,6 +110,13 @@
WorldEntity *getWorldEntityByID(std::string id) const;
/**
+ * @brief Convert an ID to a MobileEntity pointer
+ * @param id The ID of the MobileEntity
+ * @return A pointer to the MobileEntity, nullptr if it's not found
+ */
+ MobileEntity *getMobileEntityByID(std::string id) const;
+
+ /**
* @brief Convert an ID to a Pawt pointer
* @param id The ID of the Pawn
* @return A pointer to the Pawn, nullptr if it's not found
@@ -110,6 +127,7 @@
std::list<std::unique_ptr<ScriptableControllerAPI> > apis_;
PlayerInfo *player_;
std::map<std::string, WorldEntity*> worldEntities_;
+ std::map<std::string, MobileEntity*> mobileEntities_;
std::map<std::string, Pawn*> pawns_;
std::map<Pawn*, std::string> pawnsReverse_;
std::map<std::string, ControllableEntity*> controllabelEntities_;
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc 2017-12-04 15:17:43 UTC (rev 11637)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc 2017-12-04 15:17:59 UTC (rev 11638)
@@ -25,6 +25,11 @@
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled");
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit");
+ LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setPosition)>::registerFunction<&ScriptableControllerAPI::setPosition>(this, lua, "setPosition");
+ LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setOrientation)>::registerFunction<&ScriptableControllerAPI::setOrientation>(this, lua, "setOrientation");
+ LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setVelocity)>::registerFunction<&ScriptableControllerAPI::setVelocity>(this, lua, "setVelocity");
+ LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setAngularVelocity)>::registerFunction<&ScriptableControllerAPI::setAngularVelocity>(this, lua, "setAngularVelocity");
+
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn");
this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false);
@@ -97,6 +102,82 @@
this->pawnsToKill_.push_back(id);
}
+void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z)
+{
+ WorldEntity *entity = this->controller_->getWorldEntityByID(id);
+ if(entity == nullptr)
+ {
+ orxout(user_warning) << "Trying to set position of an unknown object" << std::endl;
+ return;
+ }
+
+ const Vector3 &old = entity->getPosition();
+
+ x = std::isnan(x) ? old.x : x;
+ y = std::isnan(y) ? old.y : y;
+ z = std::isnan(z) ? old.z : z;
+
+ entity->setPosition(x, y, z);
+}
+
+void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle)
+{
+ WorldEntity *entity = this->controller_->getWorldEntityByID(id);
+ if(entity == nullptr)
+ {
+ orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl;
+ return;
+ }
+
+ Vector3 old_axis;
+ Degree old_angle;
+
+ entity->getOrientation().ToAngleAxis(old_angle, old_axis);
+
+ x = std::isnan(x) ? old_axis.x : x;
+ y = std::isnan(y) ? old_axis.y : y;
+ z = std::isnan(z) ? old_axis.z : z;
+ angle = std::isnan(x) ? old_angle.valueDegrees() : angle;
+
+ entity->setOrientation(Vector3(x, y, z), Degree(angle));
+}
+
+void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z)
+{
+ MobileEntity *entity = this->controller_->getMobileEntityByID(id);
+ if(entity == nullptr)
+ {
+ orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl;
+ return;
+ }
+
+ const Vector3 &old = entity->getVelocity();
+
+ x = std::isnan(x) ? old.x : x;
+ y = std::isnan(y) ? old.y : y;
+ z = std::isnan(z) ? old.z : z;
+
+ entity->setVelocity(x, y, z);
+}
+
+void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z)
+{
+ MobileEntity *entity = this->controller_->getMobileEntityByID(id);
+ if(entity == nullptr)
+ {
+ orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl;
+ return;
+ }
+
+ const Vector3 &old = entity->getAngularVelocity();
+
+ x = std::isnan(x) ? old.x : x;
+ y = std::isnan(y) ? old.y : y;
+ z = std::isnan(z) ? old.z : z;
+
+ entity->setAngularVelocity(x, y, z);
+}
+
void ScriptableControllerAPI::pawnKilled(std::string id)
{
for(auto callback : this->pawnDestroyedHandlers_[id])
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h 2017-12-04 15:17:43 UTC (rev 11637)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h 2017-12-04 15:17:59 UTC (rev 11638)
@@ -38,7 +38,7 @@
*/
~ScriptableControllerAPI();
- // --- API ----------------------------------
+// ### API ####################################################################
/**
* @brief Print a message
@@ -138,9 +138,53 @@
*/
void killPawn(std::string id);
- // ------------------------------------------
+ /**
+ * @brief Set the position of an object
+ * @param id The ID of the object
+ * @param x The position on the x-axis
+ * @param y The position on the y-axis
+ * @param z The position on the z-axis
+ */
+ void setPosition(std::string id, double x, double y, double z);
/**
+ * @brief Set the orientation of an object
+ * @param id The ID of the object
+ * @param x The x component of the axis vector
+ * @param y The y component of the axis vector
+ * @param z The z component of the axis vector
+ * @param angle The angle around the axis
+ *
+ * To set the orientation, you have to specify the direction that the
+ * object should be facing with the vector (x, y, z) and the rotation
+ * of the object around this axis with 'angle', which has to be given
+ * in degrees, NOT radian. The vector does not have to be normalized.
+ */
+ void setOrientation(std::string id, double x, double y, double z, double angle);
+
+ /**
+ * @brief Set the velocity of an object
+ * @param id The ID of the object
+ * @param x The velocity in x-direction
+ * @param y The velocity in y-direction
+ * @param z The velocity in z-direction
+ *
+ * The velocity is in units per second.
+ */
+ void setVelocity(std::string id, double x, double y, double z);
+
+ /**
+ * @brief Set the angular velocity of an object
+ * @param id The ID of the object
+ * @param x The rotation velocity around the x-axis
+ * @param y The rotation velocity around the y-axis
+ * @param z The rotation velocity around the z-axis
+ */
+ void setAngularVelocity(std::string id, double x, double y, double z);
+
+// ### API END ################################################################
+
+ /**
* @brief Called by ScriptableController when a pawn is killed
* @param id The dead pawn
*
Modified: code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc 2017-12-04 15:17:43 UTC (rev 11637)
+++ code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc 2017-12-04 15:17:59 UTC (rev 11638)
@@ -73,6 +73,9 @@
else
this->setAngularVelocity(rotationAxis.normalisedCopy() * rotationRate.valueRadians());
}
+
+ if(!this->id_.empty() && this->getLevel() != nullptr)
+ this->getLevel()->getScriptableController()->registerMobileEntity(this->id_, this);
}
void MobileEntity::tick(float dt)
More information about the Orxonox-commit
mailing list