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