Changed Python _getattr/_setattr methods to use const STR_String& instead of char...
[blender.git] / source / gameengine / Ketsji / KX_NearSensor.cpp
1 /**
2  * Sense if other objects are near
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include "KX_NearSensor.h"
36 #include "SCA_LogicManager.h"
37 #include "KX_GameObject.h"
38 #include "KX_TouchEventManager.h"
39 #include "KX_Scene.h" // needed to create a replica
40
41 #include "SM_Object.h"
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
47                                                          KX_GameObject* gameobj,
48                                                          void *vshape,
49                                                          double margin,
50                                                          double resetmargin,
51                                                          bool bFindMaterial,
52                                                          const STR_String& touchedpropname,
53                                                          class KX_Scene* scene,
54                                                          PyTypeObject* T)
55                          :KX_TouchSensor(eventmgr,
56                                                          gameobj,
57                                                          bFindMaterial,
58                                                          touchedpropname,
59                                                          /* scene, */
60                                                          T),
61                          m_Margin(margin),
62                          m_ResetMargin(resetmargin)
63
64 {
65         m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::NEAR);
66         
67         DT_ShapeHandle shape = (DT_ShapeHandle) vshape;
68         m_sumoObj = new SM_Object(shape,NULL,NULL,NULL);
69         m_sumoObj->setMargin(m_Margin);
70         m_sumoObj->setClientObject(m_client_info);
71         
72         SynchronizeTransform();
73 }
74
75 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
76                                                          KX_GameObject* gameobj,
77                                                          double margin,
78                                                          double resetmargin,
79                                                          bool bFindMaterial,
80                                                          const STR_String& touchedpropname,
81                                                          class KX_Scene* scene,
82                                                          PyTypeObject* T)
83                          :KX_TouchSensor(eventmgr,
84                                                          gameobj,
85                                                          bFindMaterial,
86                                                          touchedpropname,
87                                                          /* scene, */
88                                                          T),
89                          m_Margin(margin),
90                          m_ResetMargin(resetmargin)
91
92 {
93         m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::NEAR);
94         
95         m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
96         m_sumoObj->setMargin(m_Margin);
97         m_sumoObj->setClientObject(m_client_info);
98         
99         SynchronizeTransform();
100 }
101
102 void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
103 {
104         touchman->GetSumoScene()->addSensor(*m_sumoObj);
105 }
106
107 CValue* KX_NearSensor::GetReplica()
108 {
109         KX_NearSensor* replica = new KX_NearSensor(*this);
110         replica->m_colliders = new CListValue();
111         replica->m_bCollision = false;
112         replica->m_bTriggered= false;
113         replica->m_hitObject = NULL;
114         replica->m_bLastTriggered = false;
115         // this will copy properties and so on...
116         CValue::AddDataToReplica(replica);
117         
118         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_clientobject, KX_ClientObjectInfo::NEAR);
119         
120         replica->m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
121         replica->m_sumoObj->setMargin(m_Margin);
122         replica->m_sumoObj->setClientObject(replica->m_client_info);
123         
124         replica->SynchronizeTransform();
125         
126         return replica;
127 }
128
129
130
131 void KX_NearSensor::ReParent(SCA_IObject* parent)
132 {
133         SCA_ISensor::ReParent(parent);
134         
135         m_client_info->m_clientobject = static_cast<KX_GameObject*>(parent); 
136         
137         SynchronizeTransform();
138 }
139
140
141
142 KX_NearSensor::~KX_NearSensor()
143 {
144         // for nearsensor, the sensor is the 'owner' of sumoobj
145         // for touchsensor, it's the parent
146         static_cast<KX_TouchEventManager*>(m_eventmgr)->GetSumoScene()->remove(*m_sumoObj);
147
148         if (m_sumoObj)
149                 delete m_sumoObj;
150                 
151         if (m_client_info)
152                 delete m_client_info;
153 }
154
155
156 bool KX_NearSensor::Evaluate(CValue* event)
157 {
158         bool result = false;
159         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
160
161         if (m_bTriggered != m_bLastTriggered)
162         {
163                 m_bLastTriggered = m_bTriggered;
164                 if (m_bTriggered)
165                 {
166                         if (m_sumoObj)
167                         {
168                                 m_sumoObj->setMargin(m_ResetMargin);
169                         }
170                 } else
171                 {
172                         if (m_sumoObj)
173                         {
174                                 m_sumoObj->setMargin(m_Margin);
175                         }
176
177                 }
178                 result = true;
179         }
180
181         return result;
182 }
183
184
185
186 DT_Bool KX_NearSensor::HandleCollision(void* obj1,void* obj2,const DT_CollData * coll_data)
187 {
188         KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
189         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
190         
191         // need the mapping from SM_Objects to gameobjects now
192         
193         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_sumoObj? 
194                                         ((SM_Object*)obj2)->getClientObject() : 
195                                         ((SM_Object*)obj1)->getClientObject());
196
197         KX_GameObject* gameobj = ( client_info ? 
198                         static_cast<KX_GameObject*>(client_info->m_clientobject) : 
199                         NULL);
200         
201         if (gameobj && (gameobj != parent))
202         {
203                 if (!m_colliders->SearchValue(gameobj))
204                         m_colliders->Add(gameobj->AddRef());
205                 // only take valid colliders
206                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
207                 {
208                         if ((m_touchedpropname.Length() == 0) || 
209                                 (gameobj->GetProperty(m_touchedpropname)))
210                         {
211                                 m_bTriggered = true;
212                                 m_hitObject = gameobj;
213                         }
214                 }
215         }
216         
217         return DT_CONTINUE;
218 }
219
220
221
222 // python embedding
223 PyTypeObject KX_NearSensor::Type = {
224         PyObject_HEAD_INIT(&PyType_Type)
225         0,
226         "KX_NearSensor",
227         sizeof(KX_NearSensor),
228         0,
229         PyDestructor,
230         0,
231         __getattr,
232         __setattr,
233         0, //&MyPyCompare,
234         __repr,
235         0, //&cvalue_as_number,
236         0,
237         0,
238         0,
239         0
240 };
241
242
243
244 PyParentObject KX_NearSensor::Parents[] = {
245         &KX_NearSensor::Type,
246         &KX_TouchSensor::Type,
247         &SCA_ISensor::Type,
248         &SCA_ILogicBrick::Type,
249         &CValue::Type,
250         NULL
251 };
252
253
254
255 PyMethodDef KX_NearSensor::Methods[] = {
256         {"setProperty", 
257          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
258         {"getProperty", 
259          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
260         {"getHitObject", 
261          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
262         {"getHitObjectList", 
263          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
264         {NULL,NULL} //Sentinel
265 };
266
267
268 PyObject*
269 KX_NearSensor::_getattr(const STR_String& attr)
270 {
271   _getattr_up(KX_TouchSensor);
272 }
273