BGE C++ API
[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()
45 {
46         KX_GameObject::ProcessReplica();
47
48         if (m_pDeformer)
49                 m_pDeformer= (BL_MeshDeformer*)m_pDeformer->GetReplica(this);
50 }
51
52 CValue*         BL_DeformableGameObject::GetReplica()
53 {
54
55         BL_DeformableGameObject* replica = new BL_DeformableGameObject(*this);//m_float,GetName());
56         replica->ProcessReplica();
57         return replica;
58 }
59
60 bool BL_DeformableGameObject::SetActiveAction(BL_ShapeActionActuator *act, short priority, double curtime)
61 {
62         if (curtime != m_lastframe){
63                 m_activePriority = 9999;
64                 m_lastframe= curtime;
65                 m_activeAct = NULL;
66         }
67
68         if (priority<=m_activePriority)
69         {
70                 if (m_activeAct && (m_activeAct!=act))
71                         m_activeAct->SetBlendTime(0.0f);        /* Reset the blend timer */
72                 m_activeAct = act;
73                 m_activePriority = priority;
74                 m_lastframe = curtime;
75         
76                 return true;
77         }
78         else{
79                 act->SetBlendTime(0.0f);
80                 return false;
81         }
82 }
83
84 bool BL_DeformableGameObject::GetShape(vector<float> &shape)
85 {
86         shape.clear();
87         if (m_pDeformer)
88         {
89                 Mesh* mesh = ((BL_MeshDeformer*)m_pDeformer)->GetMesh();
90                 // this check is normally superfluous: a shape deformer can only be created if the mesh
91                 // has relative keys
92                 if (mesh && mesh->key && mesh->key->type==KEY_RELATIVE) 
93                 {
94                         KeyBlock *kb;
95                         for (kb = (KeyBlock*)mesh->key->block.first; kb; kb = (KeyBlock*)kb->next)
96                         {
97                                 shape.push_back(kb->curval);
98                         }
99                 }
100         }
101         return !shape.empty();
102 }
103