Merge from trunk
[blender.git] / source / gameengine / Converter / BL_DeformableGameObject.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "BL_DeformableGameObject.h"
31 #include "BL_ShapeDeformer.h"
32 #include "BL_ShapeActionActuator.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 BL_DeformableGameObject::~BL_DeformableGameObject()
39 {
40         if (m_pDeformer)
41                 delete m_pDeformer;             //      __NLA : Temporary until we decide where to put this
42 }
43
44 void BL_DeformableGameObject::ProcessReplica(KX_GameObject* replica)
45 {
46         BL_MeshDeformer *deformer;
47         KX_GameObject::ProcessReplica(replica);
48
49         if (m_pDeformer) {
50                 deformer = (BL_MeshDeformer*)m_pDeformer->GetReplica();
51                 ((BL_DeformableGameObject*)replica)->m_pDeformer = deformer;
52         }
53
54 }
55
56 CValue*         BL_DeformableGameObject::GetReplica()
57 {
58
59         BL_DeformableGameObject* replica = new BL_DeformableGameObject(*this);//m_float,GetName());
60         
61         // this will copy properties and so on...
62         CValue::AddDataToReplica(replica);
63
64         ProcessReplica(replica);
65         return replica;
66 }
67
68 bool BL_DeformableGameObject::SetActiveAction(BL_ShapeActionActuator *act, short priority, double curtime)
69 {
70         if (curtime != m_lastframe){
71                 m_activePriority = 9999;
72                 m_lastframe= curtime;
73                 m_activeAct = NULL;
74         }
75
76         if (priority<=m_activePriority)
77         {
78                 if (m_activeAct && (m_activeAct!=act))
79                         m_activeAct->SetBlendTime(0.0f);        /* Reset the blend timer */
80                 m_activeAct = act;
81                 m_activePriority = priority;
82                 m_lastframe = curtime;
83         
84                 return true;
85         }
86         else{
87                 act->SetBlendTime(0.0f);
88                 return false;
89         }
90 }
91
92 bool BL_DeformableGameObject::GetShape(vector<float> &shape)
93 {
94         shape.clear();
95         if (m_pDeformer)
96         {
97                 Mesh* mesh = ((BL_MeshDeformer*)m_pDeformer)->GetMesh();
98                 // this check is normally superfluous: a shape deformer can only be created if the mesh
99                 // has relative keys
100                 if (mesh && mesh->key && mesh->key->type==KEY_RELATIVE) 
101                 {
102                         KeyBlock *kb;
103                         for (kb = (KeyBlock*)mesh->key->block.first; kb; kb = (KeyBlock*)kb->next)
104                         {
105                                 shape.push_back(kb->curval);
106                         }
107                 }
108         }
109         return !shape.empty();
110 }
111