Fix for bug #28979 "Action actuator breaks animation" reported by Goran Milovanovic...
[blender.git] / source / gameengine / Ketsji / BL_Action.cpp
index 8ed6a33696a6cbea6ed13e809928f4a9d39edbed..9e8f53ad21980c17712689d60b6188f1c2423e7a 100644 (file)
@@ -49,24 +49,25 @@ extern "C" {
 
 BL_Action::BL_Action(class KX_GameObject* gameobj)
 :
+       m_action(NULL),
+       m_pose(NULL),
+       m_blendpose(NULL),
+       m_blendinpose(NULL),
+       m_ptrrna(NULL),
        m_obj(gameobj),
        m_startframe(0.f),
        m_endframe(0.f),
-       m_blendin(0.f),
-       m_playmode(0),
        m_endtime(0.f),
        m_localtime(0.f),
+       m_blendin(0.f),
        m_blendframe(0.f),
        m_blendstart(0.f),
        m_speed(0.f),
        m_priority(0),
+       m_playmode(0),
        m_ipo_flags(0),
-       m_pose(NULL),
-       m_blendpose(NULL),
-       m_blendinpose(NULL),
-       m_sg_contr(NULL),
-       m_ptrrna(NULL),
-       m_done(true)
+       m_done(true),
+       m_calc_localtime(true)
 {
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
        {
@@ -96,13 +97,22 @@ BL_Action::~BL_Action()
                game_free_pose(m_blendpose);
        if (m_blendinpose)
                game_free_pose(m_blendinpose);
-       if (m_sg_contr)
-       {
-               m_obj->GetSGNode()->RemoveSGController(m_sg_contr);
-               delete m_sg_contr;
-       }
        if (m_ptrrna)
                delete m_ptrrna;
+       ClearControllerList();
+}
+
+void BL_Action::ClearControllerList()
+{
+       // Clear out the controller list
+       std::vector<SG_Controller*>::iterator it;
+       for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
+       {
+               m_obj->GetSGNode()->RemoveSGController((*it));
+               delete *it;
+       }
+
+       m_sg_contr_list.clear();
 }
 
 bool BL_Action::Play(const char* name,
@@ -134,10 +144,30 @@ bool BL_Action::Play(const char* name,
 
        if (prev_action != m_action)
        {
+               // First get rid of any old controllers
+               ClearControllerList();
+
                // Create an SG_Controller
-               m_sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
-               m_obj->GetSGNode()->AddSGController(m_sg_contr);
-               m_sg_contr->SetObject(m_obj->GetSGNode());
+               SG_Controller *sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+               m_sg_contr_list.push_back(sg_contr);
+               m_obj->GetSGNode()->AddSGController(sg_contr);
+               sg_contr->SetObject(m_obj->GetSGNode());
+
+               // Extra controllers
+               if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_LIGHT)
+               {
+                       sg_contr = BL_CreateLampIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+                       m_sg_contr_list.push_back(sg_contr);
+                       m_obj->GetSGNode()->AddSGController(sg_contr);
+                       sg_contr->SetObject(m_obj->GetSGNode());
+               }
+               else if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_CAMERA)
+               {
+                       sg_contr = BL_CreateCameraIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
+                       m_sg_contr_list.push_back(sg_contr);
+                       m_obj->GetSGNode()->AddSGController(sg_contr);
+                       sg_contr->SetObject(m_obj->GetSGNode());
+               }
        }
        
        m_ipo_flags = ipo_flags;
@@ -154,14 +184,16 @@ bool BL_Action::Play(const char* name,
                BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
                BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
                
-               obj->GetShape(m_blendinshape);
-
-               // Now that we have the previous blend shape saved, we can clear out the key to avoid any
-               // further interference.
-               KeyBlock *kb;
-               for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
-                       kb->curval = 0.f;
+               if (shape_deformer && shape_deformer->GetKey())
+               {
+                       obj->GetShape(m_blendinshape);
 
+                       // Now that we have the previous blend shape saved, we can clear out the key to avoid any
+                       // further interference.
+                       KeyBlock *kb;
+                       for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
+                               kb->curval = 0.f;
+               }
        }
 
        // Now that we have an action, we have something we can play
@@ -193,11 +225,15 @@ bool BL_Action::IsDone()
 
 void BL_Action::InitIPO()
 {
-               // Initialize the IPO
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
-               m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
+       // Initialize the IPOs
+       std::vector<SG_Controller*>::iterator it;
+       for (it = m_sg_contr_list.begin(); it != m_sg_contr_list.end(); it++)
+       {
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
+               (*it)->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
+       }
 }
 
 bAction *BL_Action::GetAction()
@@ -212,24 +248,25 @@ float BL_Action::GetFrame()
 
 void BL_Action::SetFrame(float frame)
 {
-       float dt;
-
        // Clamp the frame to the start and end frame
        if (frame < min(m_startframe, m_endframe))
                frame = min(m_startframe, m_endframe);
        else if (frame > max(m_startframe, m_endframe))
                frame = max(m_startframe, m_endframe);
        
-       // We don't set m_localtime directly since it's recalculated
-       // in the next update. So, we modify the value (m_starttime) 
-       // used to calculate m_localtime the next time SetLocalTime() is called.
-
-       dt = frame-m_startframe;
+       m_localtime = frame;
+       m_calc_localtime = false;
+}
 
-       if (m_endframe < m_startframe)
-               dt = -dt;
+void BL_Action::SetPlayMode(short play_mode)
+{
+       m_playmode = play_mode;
+}
 
-       m_starttime -= dt / (KX_KetsjiEngine::GetAnimFrameRate()*m_speed);
+void BL_Action::SetTimes(float start, float end)
+{
+       m_startframe = start;
+       m_endframe = end;
 }
 
 void BL_Action::SetLocalTime(float curtime)
@@ -242,6 +279,14 @@ void BL_Action::SetLocalTime(float curtime)
        m_localtime = m_startframe + dt;
 }
 
+void BL_Action::ResetStartTime(float curtime)
+{
+       float dt = m_localtime - m_startframe;
+
+       m_starttime = curtime - dt / (KX_KetsjiEngine::GetAnimFrameRate()*m_speed);
+       SetLocalTime(curtime);
+}
+
 void BL_Action::IncrementBlending(float curtime)
 {
        // Setup m_blendstart if we need to
@@ -282,7 +327,14 @@ void BL_Action::Update(float curtime)
                return;
 
        curtime -= KX_KetsjiEngine::GetSuspendedDelta();
-       SetLocalTime(curtime);
+
+       if (m_calc_localtime)
+               SetLocalTime(curtime);
+       else
+       {
+               ResetStartTime(curtime);
+               m_calc_localtime = true;
+       }
 
        // Handle wrap around
        if (m_localtime < min(m_startframe, m_endframe) || m_localtime > max(m_startframe, m_endframe))
@@ -309,9 +361,6 @@ void BL_Action::Update(float curtime)
 
                        break;
                }
-
-               if (!m_done && m_sg_contr)
-                       InitIPO();
        }
 
        if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
@@ -360,7 +409,7 @@ void BL_Action::Update(float curtime)
                BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
 
                // Handle shape actions if we have any
-               if (shape_deformer)
+               if (shape_deformer && shape_deformer->GetKey())
                {
                        Key *key = shape_deformer->GetKey();
 
@@ -394,8 +443,6 @@ void BL_Action::Update(float curtime)
                        obj->SetActiveAction(NULL, 0, curtime);
                }
 
-
-               InitIPO();
                m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
        }
 }