From cb510840ae2cce8bc048e54aeb6f2d83d2f34f3f Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Fri, 5 Jun 2015 19:34:46 -0400 Subject: [PATCH] Work on scripting, namely improving interaction with library objects --- src/physics/physics.cpp | 6 +- src/scriptengine/script_track.cpp | 40 +++---------- src/tracks/track_object.cpp | 31 ++++++++-- src/tracks/track_object.hpp | 2 +- src/tracks/track_object_manager.cpp | 93 +++++++---------------------- src/tracks/track_object_manager.hpp | 5 +- 6 files changed, 57 insertions(+), 120 deletions(-) diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 1488de877..10e21d5e8 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -196,14 +196,12 @@ void Physics::update(float dt) std::string lib_id; std::string* lib_id_ptr = NULL; if (library != NULL) - { lib_id = library->getID(); - lib_id_ptr = &lib_id; - } + lib_id_ptr = &lib_id; if (scripting_function.size() > 0) { - script_engine->runFunction("void " + scripting_function + "(int, const string, const string)", + script_engine->runFunction("void " + scripting_function + "(int, const string, const string)", [&](asIScriptContext* ctx) { ctx->SetArgDWord(0, kartId); ctx->SetArgObject(1, lib_id_ptr); diff --git a/src/scriptengine/script_track.cpp b/src/scriptengine/script_track.cpp index 8f69ec709..a2c475167 100644 --- a/src/scriptengine/script_track.cpp +++ b/src/scriptengine/script_track.cpp @@ -57,33 +57,9 @@ namespace Scripting * Get a track object by ID. * @return An object of type @ref Scripting_TrackObject */ - TrackObject* getTrackObject(std::string* objID) + TrackObject* getTrackObject(std::string* libraryInstance, std::string* objID) { - return World::getWorld()->getTrack()->getTrackObjectManager()->getTrackObject(*objID); - } - - /** Hide/disable a track object */ - void disableTrackObject(std::string* objID) - { - World::getWorld()->getTrack()->getTrackObjectManager()->disable(*objID); - } - - /** Show/enable a track objects */ - void enableTrackObject(std::string* objID) - { - World::getWorld()->getTrack()->getTrackObjectManager()->enable(*objID); - } - - /** Disables an action trigger of specified ID */ - void disableTrigger(std::string* triggerID) - { - World::getWorld()->getTrack()->getTrackObjectManager()->disable(*triggerID); - } - - /** Enables an action trigger of specified ID */ - void enableTrigger(std::string* triggerID) - { - World::getWorld()->getTrack()->getTrackObjectManager()->enable(*triggerID); + return World::getWorld()->getTrack()->getTrackObjectManager()->getTrackObject(*libraryInstance, *objID); } /** Creates a trigger at the specified location */ @@ -250,18 +226,18 @@ namespace Scripting r = engine->RegisterObjectType("SoundEmitter", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); r = engine->RegisterObjectType("Animator", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterGlobalFunction("void disableTrackObject(const string &in)", asFUNCTION(disableTrackObject), asCALL_CDECL); assert(r >= 0); - r = engine->RegisterGlobalFunction("void enableTrackObject(const string &in)", asFUNCTION(enableTrackObject), asCALL_CDECL); assert(r >= 0); - r = engine->RegisterGlobalFunction("void enableTrigger(const string &in)", asFUNCTION(enableTrigger), asCALL_CDECL); assert(r >= 0); - r = engine->RegisterGlobalFunction("void disableTrigger(const string &in)", asFUNCTION(disableTrigger), asCALL_CDECL); assert(r >= 0); + //r = engine->RegisterGlobalFunction("void disableTrackObject(const string &in)", asFUNCTION(disableTrackObject), asCALL_CDECL); assert(r >= 0); + //r = engine->RegisterGlobalFunction("void enableTrackObject(const string &in)", asFUNCTION(enableTrackObject), asCALL_CDECL); assert(r >= 0); + //r = engine->RegisterGlobalFunction("void enableTrigger(const string &in)", asFUNCTION(enableTrigger), asCALL_CDECL); assert(r >= 0); + //r = engine->RegisterGlobalFunction("void disableTrigger(const string &in)", asFUNCTION(disableTrigger), asCALL_CDECL); assert(r >= 0); r = engine->RegisterGlobalFunction("void createTrigger(const string &in, const Vec3 &in, float distance)", asFUNCTION(createTrigger), asCALL_CDECL); assert(r >= 0); - r = engine->RegisterGlobalFunction("TrackObject@ getTrackObject(const string &in)", asFUNCTION(getTrackObject), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("TrackObject@ getTrackObject(const string &in, const string &in)", asFUNCTION(getTrackObject), asCALL_CDECL); assert(r >= 0); r = engine->RegisterGlobalFunction("void exitRace()", asFUNCTION(exitRace), asCALL_CDECL); assert(r >= 0); r = engine->RegisterGlobalFunction("void pauseRace()", asFUNCTION(pauseRace), asCALL_CDECL); assert(r >= 0); // TrackObject - r = engine->RegisterObjectMethod("TrackObject", "void setEnable(bool status)", asMETHOD(TrackObject, setEnable), asCALL_THISCALL); assert(r >= 0); + r = engine->RegisterObjectMethod("TrackObject", "void setEnabled(bool status)", asMETHOD(TrackObject, setEnabled), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("TrackObject", "SoundEmitter@ getSoundEmitter()", asMETHOD(TrackObject, getSoundEmitter), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("TrackObject", "PhysicalObject@ getPhysics()", asMETHOD(TrackObject, getPhysics), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("TrackObject", "Mesh@ getMesh()", asMETHOD(TrackObject, getMesh), asCALL_THISCALL); assert(r >= 0); diff --git a/src/tracks/track_object.cpp b/src/tracks/track_object.cpp index c4e8ba9b2..86af10c46 100644 --- a/src/tracks/track_object.cpp +++ b/src/tracks/track_object.cpp @@ -284,9 +284,9 @@ TrackObject::~TrackObject() */ void TrackObject::reset() { - if (m_presentation ) m_presentation->reset(); - if (m_animator ) m_animator->reset(); - if(m_physical_object) m_physical_object->reset(); + if (m_presentation ) m_presentation->reset(); + if (m_animator ) m_animator->reset(); + if (m_physical_object) m_physical_object->reset(); } // reset // ---------------------------------------------------------------------------- @@ -294,10 +294,29 @@ void TrackObject::reset() * disabled objects will not be displayed anymore. * \param mode Enable (true) or disable (false) this object. */ -void TrackObject::setEnable(bool mode) +void TrackObject::setEnabled(bool enabled) { - m_enabled = mode; - if (m_presentation != NULL) m_presentation->setEnable(m_enabled); + if (m_enabled == enabled) + return; + + m_enabled = enabled; + + if (m_presentation != NULL) + m_presentation->setEnable(m_enabled); + + if (enabled) + reset(); // TODO: not sure why there is a reset here + + if (getType() == "mesh") + { + if (m_physical_object != NULL) + { + if (enabled) + m_physical_object->addBody(); + else + m_physical_object->removeBody(); + } + } } // setEnable // ---------------------------------------------------------------------------- diff --git a/src/tracks/track_object.hpp b/src/tracks/track_object.hpp index 7031e9b7c..1dc6c3fe5 100644 --- a/src/tracks/track_object.hpp +++ b/src/tracks/track_object.hpp @@ -207,7 +207,7 @@ public: */ PhysicalObject* getPhysics() { return m_physical_object; } /** Hide or show the object */ - void setEnable(bool mode); + void setEnabled(bool mode); /* @} */ /* @} */ /* @} */ diff --git a/src/tracks/track_object_manager.cpp b/src/tracks/track_object_manager.cpp index f90531a5d..f75d8697b 100644 --- a/src/tracks/track_object_manager.cpp +++ b/src/tracks/track_object_manager.cpp @@ -81,95 +81,42 @@ void TrackObjectManager::reset() for (TrackObject* curr : m_all_objects) { curr->reset(); - if (!curr->isEnabled()) - { - //PhysicalObjects may need to be added - if (curr->getType() == "mesh") - { - if (curr->getPhysicalObject() != NULL) - curr->getPhysicalObject()->addBody(); - } - } - curr->setEnable(true); + curr->setEnabled(true); } } // reset - -// ---------------------------------------------------------------------------- -/** disables all track objects with a particular ID - * \param name Name or ID for disabling - */ -void TrackObjectManager::disable(std::string name) -{ - for (TrackObject* curr : m_all_objects) - { - if (curr->getName() == (name) || curr->getID() == (name)) - { - - curr->setEnable(false); - if (curr->getType() == "mesh") - { - if (curr->getPhysicalObject()!=NULL) - curr->getPhysicalObject()->removeBody(); - } - } - } -} -// ---------------------------------------------------------------------------- -/** enables all track objects with a particular ID - * \param name Name or ID for enabling - */ -void TrackObjectManager::enable(std::string name) -{ - for (TrackObject* curr : m_all_objects) - { - if (curr->getName() == (name) || curr->getID() == (name)) - { - curr->reset(); - curr->setEnable(true); - if (curr->getType() == "mesh") - { - if (curr->getPhysicalObject() != NULL) - curr->getPhysicalObject()->addBody(); - } - } - } -} - -// ---------------------------------------------------------------------------- -/** returns activation status for all track objects - * with a particular ID - * \param name Name or ID of track object - */ -bool TrackObjectManager::getStatus(std::string name) -{ - for (TrackObject* curr : m_all_objects){ - if (curr->getName() == (name)||curr->getID()==(name)) - { - - return curr->isEnabled(); - - } - } - //object not found - return false; -} // ---------------------------------------------------------------------------- /** returns a reference to the track object * with a particular ID * \param name Name or ID of track object */ -TrackObject* TrackObjectManager::getTrackObject(std::string name) +TrackObject* TrackObjectManager::getTrackObject(const std::string& libraryInstance, + const std::string& name) { for (TrackObject* curr : m_all_objects) { - if (curr->getName() == (name) || curr->getID() == (name)) + //if (curr->getParentLibrary() != NULL) + // Log::info("TrackObjectManager", "Found %s::%s", curr->getParentLibrary()->getID().c_str(), curr->getID().c_str()); + //else + // Log::info("TrackObjectManager", "Found ::%s", curr->getID().c_str()); + + if (curr->getParentLibrary() == NULL) { + if (libraryInstance.size() > 0) + continue; + } + else + { + if (libraryInstance != curr->getParentLibrary()->getID()) + continue; + } + if (curr->getID() == name) + { return curr; - } } //object not found + Log::warn("TrackObjectManager", "Object not found : %s::%s", libraryInstance.c_str(), name.c_str()); return NULL; } /** Handles an explosion, i.e. it makes sure that all physical objects are diff --git a/src/tracks/track_object_manager.hpp b/src/tracks/track_object_manager.hpp index ca0224f4b..24cc17557 100644 --- a/src/tracks/track_object_manager.hpp +++ b/src/tracks/track_object_manager.hpp @@ -62,9 +62,6 @@ public: void update(float dt); void handleExplosion(const Vec3 &pos, const PhysicalObject *mp, bool secondary_hits=true); - void disable(std::string name); - void enable (std::string name); - bool getStatus(std::string name); void castRay(const btVector3 &from, const btVector3 &to, btVector3 *hit_point, const Material **material, btVector3 *normal = NULL, @@ -77,7 +74,7 @@ public: void removeObject(TrackObject* who); - TrackObject* getTrackObject(std::string name); + TrackObject* getTrackObject(const std::string& libraryInstance, const std::string& name); PtrVector& getObjects() { return m_all_objects; } const PtrVector& getObjects() const { return m_all_objects; }