From 403d61e75422156aff72248d36c24bc3af49881b Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Wed, 17 Jun 2015 19:33:53 -0400 Subject: [PATCH] Allow creating initially hidden objects, that can later be shown through scripting --- src/physics/physical_object.cpp | 14 +++++- src/physics/physical_object.hpp | 2 + src/tracks/track_object.cpp | 75 +++++++++++++++++------------ src/tracks/track_object.hpp | 4 ++ src/tracks/track_object_manager.cpp | 2 +- 5 files changed, 64 insertions(+), 33 deletions(-) diff --git a/src/physics/physical_object.cpp b/src/physics/physical_object.cpp index 181167cfb..c8d0e2b4b 100644 --- a/src/physics/physical_object.cpp +++ b/src/physics/physical_object.cpp @@ -144,6 +144,7 @@ PhysicalObject::PhysicalObject(bool is_dynamic, m_reset_height = settings.m_reset_height; m_on_kart_collision = settings.m_on_kart_collision; m_on_item_collision = settings.m_on_item_collision; + m_body_added = false; m_init_pos.setIdentity(); Vec3 radHpr(m_init_hpr); @@ -483,6 +484,7 @@ void PhysicalObject::init() } World::getWorld()->getPhysics()->addBody(m_body); + m_body_added = true; if(m_triangle_mesh) m_triangle_mesh->setBody(m_body); } // init @@ -611,14 +613,22 @@ void PhysicalObject::setInteraction(std::string interaction){ /** Remove body from physics dynamic world interaction type for object*/ void PhysicalObject::removeBody() { - World::getWorld()->getPhysics()->removeBody(m_body); + if (m_body_added) + { + World::getWorld()->getPhysics()->removeBody(m_body); + m_body_added = false; + } } // Remove body // ---------------------------------------------------------------------------- /** Add body to physics dynamic world */ void PhysicalObject::addBody() { - World::getWorld()->getPhysics()->addBody(m_body); + if (!m_body_added) + { + m_body_added = true; + World::getWorld()->getPhysics()->addBody(m_body); + } } // Add body // ---------------------------------------------------------------------------- diff --git a/src/physics/physical_object.hpp b/src/physics/physical_object.hpp index a06339029..02d2580ba 100644 --- a/src/physics/physical_object.hpp +++ b/src/physics/physical_object.hpp @@ -113,6 +113,8 @@ private: /** The mass of this object. */ float m_mass; + bool m_body_added; + /** The pointer that is stored in the bullet rigid body back to * this object. */ UserPointer m_user_pointer; diff --git a/src/tracks/track_object.cpp b/src/tracks/track_object.cpp index b9783da6a..08c014dda 100644 --- a/src/tracks/track_object.cpp +++ b/src/tracks/track_object.cpp @@ -133,6 +133,26 @@ void TrackObject::init(const XMLNode &xml_node, scene::ISceneNode* parent, m_type = type; + m_initially_visible = true; + std::string condition; + xml_node.get("if", &condition); + if (condition == "false") + { + m_initially_visible = false; + } + else if (condition.size() > 0) + { + unsigned char result = -1; + Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + std::function null_callback; + script_engine->runFunction("bool " + condition + "()", null_callback, + [&](asIScriptContext* ctx) { result = ctx->GetReturnByte(); }); + + if (result == 0) + m_initially_visible = false; + } + if (!m_initially_visible) + setEnabled(false); if (xml_node.getName() == "particle-emitter") { @@ -180,7 +200,7 @@ void TrackObject::init(const XMLNode &xml_node, scene::ISceneNode* parent, else { scene::ISceneNode *glownode = NULL; - + bool is_movable = false; if (lod_instance) { m_type = "lod"; @@ -222,11 +242,7 @@ void TrackObject::init(const XMLNode &xml_node, scene::ISceneNode* parent, node->setPosition(absTransform.getTranslation()); node->setRotation(absTransform.getRotationDegrees()); node->setScale(absTransform.getScale()); - - if (parent_library != NULL) - { - parent_library->addMovableChild(this); - } + is_movable = true; } glownode = node; @@ -243,6 +259,11 @@ void TrackObject::init(const XMLNode &xml_node, scene::ISceneNode* parent, this); } + if (is_movable && parent_library != NULL) + { + parent_library->addMovableChild(this); + } + video::SColor glow; if (xml_node.get("glow", &glow) && glownode) { @@ -273,26 +294,10 @@ void TrackObject::init(const XMLNode &xml_node, scene::ISceneNode* parent, reset(); - // some static meshes are conditional - std::string condition; - xml_node.get("if", &condition); - if (condition == "false") - { - // TODO: doesn't work (in all cases), probably it's a bit too early, children are not loaded yet + if (!m_initially_visible) + setEnabled(false); + if (parent_library != NULL && !parent_library->isEnabled()) setEnabled(false); - } - else if (condition.size() > 0) - { - unsigned char result = -1; - Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); - std::function null_callback; - script_engine->runFunction("bool " + condition + "()", null_callback, - [&](asIScriptContext* ctx) { result = ctx->GetReturnByte(); }); - - // TODO: doesn't work (in all cases), probably it's a bit too early, children are not loaded yet - if (result == 0) - setEnabled(false); - } } // TrackObject // ---------------------------------------------------------------------------- @@ -324,16 +329,16 @@ void TrackObject::reset() */ void TrackObject::setEnabled(bool enabled) { - if (m_enabled == enabled) - return; - + //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 (enabled) + // reset(); if (getType() == "mesh") { @@ -353,6 +358,14 @@ void TrackObject::setEnabled(bool enabled) } // setEnable // ---------------------------------------------------------------------------- + +void TrackObject::resetEnabled() +{ + setEnabled(m_initially_visible); +} + +// ---------------------------------------------------------------------------- + void TrackObject::update(float dt) { if (m_presentation) m_presentation->update(dt); @@ -464,5 +477,7 @@ const core::vector3df& TrackObject::getScale() const void TrackObject::addMovableChild(TrackObject* child) { + if (!m_enabled) + child->setEnabled(false); m_movable_children.push_back(child); } diff --git a/src/tracks/track_object.hpp b/src/tracks/track_object.hpp index fd21c7aa5..a463e01c2 100644 --- a/src/tracks/track_object.hpp +++ b/src/tracks/track_object.hpp @@ -94,6 +94,8 @@ protected: std::vector m_movable_children; + bool m_initially_visible = true; + void init(const XMLNode &xml_node, scene::ISceneNode* parent, ModelDefinitionLoader& model_def_loader, TrackObject* parent_library); @@ -213,6 +215,8 @@ public: /* @} */ /* @} */ /* @} */ + + void resetEnabled(); // ------------------------------------------------------------------------ ThreeDAnimation* getAnimator() { return m_animator; } // ------------------------------------------------------------------------ diff --git a/src/tracks/track_object_manager.cpp b/src/tracks/track_object_manager.cpp index f75d8697b..bbb4a445b 100644 --- a/src/tracks/track_object_manager.cpp +++ b/src/tracks/track_object_manager.cpp @@ -81,7 +81,7 @@ void TrackObjectManager::reset() for (TrackObject* curr : m_all_objects) { curr->reset(); - curr->setEnabled(true); + curr->resetEnabled(); } } // reset // ----------------------------------------------------------------------------