svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22205:22290
[blender.git] / source / gameengine / Ketsji / KX_SCA_DynamicActuator.cpp
1 //
2 // Adjust dynamics settins for this object
3 //
4 // $Id$
5 //
6 // ***** BEGIN GPL LICENSE BLOCK *****
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software Foundation,
20 // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 // All rights reserved.
24 //
25 // The Original Code is: all of this file.
26 //
27 // Contributor(s): none yet.
28 //
29 // ***** END GPL LICENSE BLOCK *****
30
31 //
32 // Previously existed as:
33
34 // \source\gameengine\GameLogic\SCA_DynamicActuator.cpp
35
36 // Please look here for revision history.
37
38 #include "KX_SCA_DynamicActuator.h"
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 /* ------------------------------------------------------------------------- */
45 /* Python functions                                                          */
46 /* ------------------------------------------------------------------------- */
47
48 /* Integration hooks ------------------------------------------------------- */
49
50 PyTypeObject KX_SCA_DynamicActuator::Type = {
51 #if (PY_VERSION_HEX >= 0x02060000)
52         PyVarObject_HEAD_INIT(NULL, 0)
53 #else
54         /* python 2.5 and below */
55         PyObject_HEAD_INIT( NULL )  /* required py macro */
56         0,                          /* ob_size */
57 #endif
58         "KX_SCA_DynamicActuator",
59         sizeof(PyObjectPlus_Proxy),
60         0,
61         py_base_dealloc,
62         0,
63         0,
64         0,
65         0,
66         py_base_repr,
67         0,0,0,0,0,0,0,0,0,
68         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
69         0,0,0,0,0,0,0,
70         Methods,
71         0,
72         0,
73         &SCA_IActuator::Type,
74         0,0,0,0,0,0,
75         py_base_new
76 };
77
78 PyMethodDef KX_SCA_DynamicActuator::Methods[] = {
79         // ---> deprecated
80         KX_PYMETHODTABLE(KX_SCA_DynamicActuator, setOperation),
81         KX_PYMETHODTABLE(KX_SCA_DynamicActuator, getOperation),
82         {NULL,NULL} //Sentinel
83 };
84
85 PyAttributeDef KX_SCA_DynamicActuator::Attributes[] = {
86         KX_PYATTRIBUTE_SHORT_RW("mode",0,4,false,KX_SCA_DynamicActuator,m_dyn_operation),
87         KX_PYATTRIBUTE_FLOAT_RW("mass",0.0,FLT_MAX,KX_SCA_DynamicActuator,m_setmass),
88         { NULL }        //Sentinel
89 };
90
91
92 /* 1. setOperation */
93 KX_PYMETHODDEF_DOC(KX_SCA_DynamicActuator, setOperation,
94 "setOperation(operation?)\n"
95 "\t - operation? : type of dynamic operation\n"
96 "\t                0 = restore dynamics\n"
97 "\t                1 = disable dynamics\n"
98 "\t                2 = enable rigid body\n"
99 "\t                3 = disable rigid body\n"
100 "Change the dynamic status of the parent object.\n")
101 {
102         ShowDeprecationWarning("setOperation()", "the mode property");
103         int dyn_operation;
104         
105         if (!PyArg_ParseTuple(args, "i:setOperation", &dyn_operation))
106         {
107                 return NULL;    
108         }
109         if (dyn_operation <0 || dyn_operation>3) {
110                 PyErr_SetString(PyExc_IndexError, "Dynamic Actuator's setOperation() range must be between 0 and 3");
111                 return NULL;
112         }
113         m_dyn_operation= dyn_operation;
114         Py_RETURN_NONE;
115 }
116
117 KX_PYMETHODDEF_DOC(KX_SCA_DynamicActuator, getOperation,
118 "getOperation() -> integer\n"
119 "Returns the operation type of this actuator.\n"
120 )
121 {
122         ShowDeprecationWarning("getOperation()", "the mode property");
123         return PyLong_FromSsize_t((long)m_dyn_operation);
124 }
125
126
127 /* ------------------------------------------------------------------------- */
128 /* Native functions                                                          */
129 /* ------------------------------------------------------------------------- */
130
131 KX_SCA_DynamicActuator::KX_SCA_DynamicActuator(SCA_IObject *gameobj,
132                                                                                                            short dyn_operation,
133                                                                                                            float setmass) :
134
135         SCA_IActuator(gameobj),
136         m_dyn_operation(dyn_operation),
137         m_setmass(setmass)
138 {
139 } /* End of constructor */
140
141
142 KX_SCA_DynamicActuator::~KX_SCA_DynamicActuator()
143
144         // there's nothing to be done here, really....
145 } /* end of destructor */
146
147
148
149 bool KX_SCA_DynamicActuator::Update()
150 {
151         // bool result = false; /*unused*/
152         KX_GameObject *obj = (KX_GameObject*) GetParent();
153         bool bNegativeEvent = IsNegativeEvent();
154         KX_IPhysicsController* controller;
155         RemoveAllEvents();
156
157         if (bNegativeEvent)
158                 return false; // do nothing on negative events
159         
160         if (!obj)
161                 return false; // object not accessible, shouldnt happen
162         controller = obj->GetPhysicsController();
163         if (!controller)
164                 return false;   // no physic object
165
166         switch (m_dyn_operation)
167         {
168                 case 0:                 
169                         obj->RestoreDynamics(); 
170                         break;
171                 case 1:
172                         obj->SuspendDynamics();
173                         break;
174                 case 2:
175                         controller->setRigidBody(true); 
176                         break;
177                 case 3:
178                         controller->setRigidBody(false);
179                         break;
180                 case 4:
181                         controller->SetMass(m_setmass);
182                         break;
183         }
184
185         return false;
186 }
187
188
189
190 CValue* KX_SCA_DynamicActuator::GetReplica()
191 {
192         KX_SCA_DynamicActuator* replica = 
193                 new KX_SCA_DynamicActuator(*this);
194
195         if (replica == NULL)
196                 return NULL;
197
198         replica->ProcessReplica();
199         return replica;
200 };
201
202
203 /* eof */