Blender  V2.59
SCA_ActuatorSensor.cpp
Go to the documentation of this file.
00001 /*
00002  * Actuator sensor
00003  *
00004  * $Id: SCA_ActuatorSensor.cpp 35169 2011-02-25 13:32:11Z jesterking $
00005  *
00006  * ***** BEGIN GPL LICENSE BLOCK *****
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00023  * All rights reserved.
00024  *
00025  * The Original Code is: all of this file.
00026  *
00027  * Contributor(s): none yet.
00028  *
00029  * ***** END GPL LICENSE BLOCK *****
00030  */
00031 
00037 #include <stddef.h>
00038 
00039 #include <iostream>
00040 #include "SCA_ActuatorSensor.h"
00041 #include "SCA_EventManager.h"
00042 #include "SCA_LogicManager.h"
00043 
00044 SCA_ActuatorSensor::SCA_ActuatorSensor(SCA_EventManager* eventmgr,
00045                                                                          SCA_IObject* gameobj,
00046                                                                          const STR_String& actname)
00047         : SCA_ISensor(gameobj,eventmgr),
00048           m_checkactname(actname)
00049 {
00050         m_actuator = GetParent()->FindActuator(m_checkactname);
00051         Init();
00052 }
00053 
00054 void SCA_ActuatorSensor::Init()
00055 {
00056         m_lastresult = m_invert?true:false;
00057         m_midresult = m_lastresult;
00058         m_reset = true;
00059 }
00060 
00061 CValue* SCA_ActuatorSensor::GetReplica()
00062 {
00063         SCA_ActuatorSensor* replica = new SCA_ActuatorSensor(*this);
00064         // m_range_expr must be recalculated on replica!
00065         replica->ProcessReplica();
00066         replica->Init();
00067 
00068         return replica;
00069 }
00070 
00071 void SCA_ActuatorSensor::ReParent(SCA_IObject* parent)
00072 {
00073         m_actuator = parent->FindActuator(m_checkactname);
00074         SCA_ISensor::ReParent(parent);
00075 }
00076 
00077 bool SCA_ActuatorSensor::IsPositiveTrigger()
00078 {
00079         bool result = m_lastresult;
00080         if (m_invert)
00081                 result = !result;
00082 
00083         return result;
00084 }
00085 
00086 
00087 
00088 SCA_ActuatorSensor::~SCA_ActuatorSensor()
00089 {
00090 }
00091 
00092 
00093 
00094 bool SCA_ActuatorSensor::Evaluate()
00095 {
00096         if (m_actuator)
00097         {
00098                 bool result = m_actuator->IsActive();
00099                 bool reset = m_reset && m_level;
00100                 
00101                 m_reset = false;
00102                 if (m_lastresult != result || m_midresult != result)
00103                 {
00104                         m_lastresult = m_midresult = result;
00105                         return true;
00106                 }
00107                 return (reset) ? true : false;
00108         }
00109         return false;
00110 }
00111 
00112 void SCA_ActuatorSensor::Update()
00113 {
00114         if (m_actuator)
00115         {
00116                 m_midresult = m_actuator->IsActive() && !m_actuator->IsNegativeEvent();
00117         }
00118 }
00119 
00120 #ifdef WITH_PYTHON
00121 
00122 /* ------------------------------------------------------------------------- */
00123 /* Python functions                                                          */
00124 /* ------------------------------------------------------------------------- */
00125 
00126 /* Integration hooks ------------------------------------------------------- */
00127 PyTypeObject SCA_ActuatorSensor::Type = {
00128         PyVarObject_HEAD_INIT(NULL, 0)
00129         "SCA_ActuatorSensor",
00130         sizeof(PyObjectPlus_Proxy),
00131         0,
00132         py_base_dealloc,
00133         0,
00134         0,
00135         0,
00136         0,
00137         py_base_repr,
00138         0,0,0,0,0,0,0,0,0,
00139         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00140         0,0,0,0,0,0,0,
00141         Methods,
00142         0,
00143         0,
00144         &SCA_ISensor::Type,
00145         0,0,0,0,0,0,
00146         py_base_new
00147 };
00148 
00149 PyMethodDef SCA_ActuatorSensor::Methods[] = {
00150         {NULL,NULL} //Sentinel
00151 };
00152 
00153 PyAttributeDef SCA_ActuatorSensor::Attributes[] = {
00154         KX_PYATTRIBUTE_STRING_RW_CHECK("actuator",0,100,false,SCA_ActuatorSensor,m_checkactname,CheckActuator),
00155         { NULL }        //Sentinel
00156 };
00157 
00158 int SCA_ActuatorSensor::CheckActuator(void *self, const PyAttributeDef*)
00159 {
00160         SCA_ActuatorSensor* sensor = reinterpret_cast<SCA_ActuatorSensor*>(self);
00161         SCA_IActuator* act = sensor->GetParent()->FindActuator(sensor->m_checkactname);
00162         if (act) {
00163                 sensor->m_actuator = act;
00164                 return 0;
00165         }
00166         PyErr_SetString(PyExc_AttributeError, "string does not correspond to an actuator");
00167         return 1;
00168 }
00169 
00170 #endif // WITH_PYTHON
00171 
00172 /* eof */