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