Blender V2.61 - r43446

SCA_ActuatorSensor.cpp

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