2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[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 
51
52 KX_SCA_DynamicActuator::Type = {
53         PyObject_HEAD_INIT(NULL)
54         0,
55         "KX_SCA_DynamicActuator",
56         sizeof(PyObjectPlus_Proxy),
57         0,
58         py_base_dealloc,
59         0,
60         0,
61         0,
62         0,
63         py_base_repr,
64         0,0,0,0,0,0,
65         py_base_getattro,
66         py_base_setattro,
67         0,0,0,0,0,0,0,0,0,
68         Methods
69 };
70
71 PyParentObject KX_SCA_DynamicActuator::Parents[] = {
72         &KX_SCA_DynamicActuator::Type,
73         &SCA_IActuator::Type,
74         &SCA_ILogicBrick::Type,
75         &CValue::Type,
76         NULL
77 };
78
79
80 PyMethodDef KX_SCA_DynamicActuator::Methods[] = {
81         // ---> deprecated
82         KX_PYMETHODTABLE(KX_SCA_DynamicActuator, setOperation),
83         KX_PYMETHODTABLE(KX_SCA_DynamicActuator, getOperation),
84         {NULL,NULL} //Sentinel
85 };
86
87 PyAttributeDef KX_SCA_DynamicActuator::Attributes[] = {
88         KX_PYATTRIBUTE_SHORT_RW("operation",0,4,false,KX_SCA_DynamicActuator,m_dyn_operation),
89         KX_PYATTRIBUTE_FLOAT_RW("mass",0.0,FLT_MAX,KX_SCA_DynamicActuator,m_setmass),
90         { NULL }        //Sentinel
91 };
92
93
94 PyObject* KX_SCA_DynamicActuator::py_getattro(PyObject *attr)
95 {
96         py_getattro_up(SCA_IActuator);
97 }
98
99 int KX_SCA_DynamicActuator::py_setattro(PyObject *attr, PyObject* value)
100 {
101         py_setattro_up(SCA_IActuator);
102 }
103
104
105 /* 1. setOperation */
106 KX_PYMETHODDEF_DOC(KX_SCA_DynamicActuator, setOperation,
107 "setOperation(operation?)\n"
108 "\t - operation? : type of dynamic operation\n"
109 "\t                0 = restore dynamics\n"
110 "\t                1 = disable dynamics\n"
111 "\t                2 = enable rigid body\n"
112 "\t                3 = disable rigid body\n"
113 "Change the dynamic status of the parent object.\n")
114 {
115         ShowDeprecationWarning("setOperation()", "the operation property");
116         int dyn_operation;
117         
118         if (!PyArg_ParseTuple(args, "i:setOperation", &dyn_operation))
119         {
120                 return NULL;    
121         }
122         if (dyn_operation <0 || dyn_operation>3) {
123                 PyErr_SetString(PyExc_IndexError, "Dynamic Actuator's setOperation() range must be between 0 and 3");
124                 return NULL;
125         }
126         m_dyn_operation= dyn_operation;
127         Py_RETURN_NONE;
128 }
129
130 KX_PYMETHODDEF_DOC(KX_SCA_DynamicActuator, getOperation,
131 "getOperation() -> integer\n"
132 "Returns the operation type of this actuator.\n"
133 )
134 {
135         ShowDeprecationWarning("getOperation()", "the operation property");
136         return PyInt_FromLong((long)m_dyn_operation);
137 }
138
139
140 /* ------------------------------------------------------------------------- */
141 /* Native functions                                                          */
142 /* ------------------------------------------------------------------------- */
143
144 KX_SCA_DynamicActuator::KX_SCA_DynamicActuator(SCA_IObject *gameobj,
145                                                                                                            short dyn_operation,
146                                                                                                            float setmass,
147                                                                                                            PyTypeObject* T) : 
148
149         SCA_IActuator(gameobj, T),
150         m_dyn_operation(dyn_operation),
151         m_setmass(setmass)
152 {
153 } /* End of constructor */
154
155
156 KX_SCA_DynamicActuator::~KX_SCA_DynamicActuator()
157
158         // there's nothing to be done here, really....
159 } /* end of destructor */
160
161
162
163 bool KX_SCA_DynamicActuator::Update()
164 {
165         // bool result = false; /*unused*/
166         KX_GameObject *obj = (KX_GameObject*) GetParent();
167         bool bNegativeEvent = IsNegativeEvent();
168         KX_IPhysicsController* controller;
169         RemoveAllEvents();
170
171         if (bNegativeEvent)
172                 return false; // do nothing on negative events
173         
174         if (!obj)
175                 return false; // object not accessible, shouldnt happen
176         controller = obj->GetPhysicsController();
177         if (!controller)
178                 return false;   // no physic object
179
180         switch (m_dyn_operation)
181         {
182                 case 0:                 
183                         obj->RestoreDynamics(); 
184                         break;
185                 case 1:
186                         obj->SuspendDynamics();
187                         break;
188                 case 2:
189                         controller->setRigidBody(true); 
190                         break;
191                 case 3:
192                         controller->setRigidBody(false);
193                         break;
194                 case 4:
195                         controller->SetMass(m_setmass);
196                         break;
197         }
198
199         return false;
200 }
201
202
203
204 CValue* KX_SCA_DynamicActuator::GetReplica()
205 {
206         KX_SCA_DynamicActuator* replica = 
207                 new KX_SCA_DynamicActuator(*this);
208
209         if (replica == NULL)
210                 return NULL;
211
212         replica->ProcessReplica();
213
214         // this will copy properties and so on...
215         CValue::AddDataToReplica(replica);
216
217         return replica;
218 };
219
220
221 /* eof */