Allow creating initially hidden objects, that can later be shown through scripting

This commit is contained in:
Marianne Gagnon 2015-06-17 19:33:53 -04:00
parent b3cc5f9f0d
commit 403d61e754
5 changed files with 64 additions and 33 deletions

View File

@ -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
// ----------------------------------------------------------------------------

View File

@ -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;

View File

@ -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<void(asIScriptContext*)> 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<void(asIScriptContext*)> 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);
}

View File

@ -94,6 +94,8 @@ protected:
std::vector<TrackObject*> 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; }
// ------------------------------------------------------------------------

View File

@ -81,7 +81,7 @@ void TrackObjectManager::reset()
for (TrackObject* curr : m_all_objects)
{
curr->reset();
curr->setEnabled(true);
curr->resetEnabled();
}
} // reset
// ----------------------------------------------------------------------------