Initial revision
[blender-staging.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
42 #ifdef PHYSICS_NOT_YET
43
44 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
45                                                          KX_GameObject* gameobj,
46                                                          double margin,
47                                                          double resetmargin,
48                                                          bool bFindMaterial,
49                                                          const STR_String& touchedpropname,
50                                                          class KX_Scene* scene,
51                                                          PyTypeObject* T)
52                          :KX_TouchSensor(eventmgr,
53                                                          gameobj,
54                                                          bFindMaterial,
55                                                          touchedpropname,
56                                                          scene,
57                                                          T),
58                          m_Margin(margin),
59                          m_ResetMargin(resetmargin),
60                          m_sumoScene(sumoscene)
61
62 {
63         m_client_info.m_type = 4;
64         m_client_info.m_clientobject = gameobj;
65         m_client_info.m_auxilary_info = NULL;
66         sumoObj->setClientObject(&m_client_info);
67 }
68
69
70
71 CValue* KX_NearSensor::GetReplica()
72 {
73         KX_NearSensor* replica = new KX_NearSensor(*this);
74         replica->m_colliders = new CListValue();
75         replica->m_bCollision = false;
76         replica->m_bTriggered= false;
77         replica->m_hitObject = NULL;
78         replica->m_bLastTriggered = false;
79         // this will copy properties and so on...
80         CValue::AddDataToReplica(replica);
81         
82         return replica;
83 }
84
85
86
87 void KX_NearSensor::ReParent(SCA_IObject* parent)
88 {
89         DT_ShapeHandle shape = DT_Sphere(0.0);
90                                 
91         // this sumoObject is not deleted by a gameobj, so delete it ourself
92         // later (memleaks)!
93
94         SM_Object* sumoObj = new SM_Object(shape,NULL,NULL,NULL);
95         sumoObj->setMargin(m_Margin);
96
97         //sumoObj->setPosition(gameobj->NodeGetWorldPosition());
98         //sumoobj->setPosition(m_sumoObj->getPosition());
99         //sumoobj->setOrientation(m_sumoObj->getOrientation());
100         //newobj->setRigidBody(this->m_sumoObj->isRigidBody());
101
102         m_sumoObj = sumoObj;
103         m_solidHandle = m_sumoObj->getObjectHandle();
104
105         double radius = m_sumoObj->getMargin();
106         sumoObj->setMargin(m_sumoObj->getMargin());
107         
108         m_client_info.m_type = 4;
109         m_client_info.m_clientobject = parent;
110         m_client_info.m_auxilary_info = NULL;
111         sumoObj->setClientObject(&m_client_info);
112
113         //m_sumoScene->add(*newobj);
114         
115         if (m_sumoObj)
116         {
117                 DT_SetObjectResponse(m_resptable,
118                         m_sumoObj->getObjectHandle(),
119                         collisionResponse,
120                         DT_SIMPLE_RESPONSE,
121                         this);
122         }
123
124         SCA_ISensor::ReParent(parent);
125 }
126
127
128
129 KX_NearSensor::~KX_NearSensor()
130 {
131         // for nearsensor, the sensor is the 'owner' of sumoobj
132         // for touchsensor, it's the parent
133
134         m_sumoScene->remove(*m_sumoObj);
135
136         if (m_sumoObj)
137                 delete m_sumoObj;
138 }
139
140
141
142 bool KX_NearSensor::Evaluate(CValue* event)
143 {
144         bool result = false;
145         KX_GameObject* parent = (KX_GameObject*)GetParent();
146
147         if (m_bTriggered != m_bLastTriggered)
148         {
149                 m_bLastTriggered = m_bTriggered;
150                 if (m_bTriggered)
151                 {
152                         if (m_sumoObj)
153                         {
154                                 m_sumoObj->setMargin(m_ResetMargin);
155                         }
156                 } else
157                 {
158                         if (m_sumoObj)
159                         {
160                                 m_sumoObj->setMargin(m_Margin);
161                         }
162
163                 }
164                 result = true;
165         }
166
167         return result;
168 }
169
170
171
172 void KX_NearSensor::HandleCollision(void* obj1,void* obj2,const DT_CollData * coll_data)
173 {
174         KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr;
175         KX_GameObject* parent = (KX_GameObject*)GetParent();
176
177         // need the mapping from SM_Objects to gameobjects now
178         
179         SM_ClientObjectInfo* client_info =(SM_ClientObjectInfo*) (obj1 == m_sumoObj? 
180                                         ((SM_Object*)obj2)->getClientObject() : 
181                                         ((SM_Object*)obj1)->getClientObject());
182
183         KX_GameObject* gameobj = ( client_info ? 
184                         (KX_GameObject*)client_info->m_clientobject : 
185                         NULL);
186
187         if (gameobj && (gameobj != parent))
188         {
189                 if (!m_colliders->SearchValue(gameobj))
190                         m_colliders->Add(gameobj->AddRef());
191                 
192                 // only take valid colliders
193                 if (client_info->m_type == 1)
194                 {
195                         if ((m_touchedpropname.Length() == 0) || 
196                                 (gameobj->GetProperty(m_touchedpropname)))
197                         {
198                                 m_bTriggered = true;
199                                 m_hitObject = gameobj;
200                         }
201                 }
202         } else
203         {
204                 
205         }
206 }
207
208
209
210 // python embedding
211 PyTypeObject KX_NearSensor::Type = {
212         PyObject_HEAD_INIT(&PyType_Type)
213         0,
214         "KX_NearSensor",
215         sizeof(KX_NearSensor),
216         0,
217         PyDestructor,
218         0,
219         __getattr,
220         __setattr,
221         0, //&MyPyCompare,
222         __repr,
223         0, //&cvalue_as_number,
224         0,
225         0,
226         0,
227         0
228 };
229
230
231
232 PyParentObject KX_NearSensor::Parents[] = {
233         &KX_NearSensor::Type,
234         &KX_TouchSensor::Type,
235         &SCA_ISensor::Type,
236         &SCA_ILogicBrick::Type,
237         &CValue::Type,
238         NULL
239 };
240
241
242
243 PyMethodDef KX_NearSensor::Methods[] = {
244         {"setProperty", 
245          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
246         {"getProperty", 
247          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
248         {"getHitObject", 
249          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
250         {"getHitObjectList", 
251          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
252         {NULL,NULL} //Sentinel
253 };
254
255
256 PyObject*
257 KX_NearSensor::_getattr(char* attr)
258 {
259   _getattr_up(KX_TouchSensor);
260 }
261
262 #endif //PHYSICS_NOT_YET