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