Blender  V2.59
SCA_DelaySensor.cpp
Go to the documentation of this file.
00001 /*
00002  * Delay trigger
00003  *
00004  * $Id: SCA_DelaySensor.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 #if defined(WIN32) && !defined(FREE_WINDOWS)
00038 // This warning tells us about truncation of __long__ stl-generated names.
00039 // It can occasionally cause DevStudio to have internal compiler warnings.
00040 #pragma warning( disable : 4786 )     
00041 #endif
00042 
00043 #include <stddef.h>
00044 
00045 #include "SCA_DelaySensor.h"
00046 #include "SCA_LogicManager.h"
00047 #include "SCA_EventManager.h"
00048 
00049 /* ------------------------------------------------------------------------- */
00050 /* Native functions                                                          */
00051 /* ------------------------------------------------------------------------- */
00052 
00053 SCA_DelaySensor::SCA_DelaySensor(class SCA_EventManager* eventmgr,
00054                                                                  SCA_IObject* gameobj,
00055                                                                  int delay,
00056                                                                  int duration,
00057                                                                  bool repeat)
00058         : SCA_ISensor(gameobj,eventmgr),
00059         m_repeat(repeat),
00060         m_delay(delay),
00061         m_duration(duration)
00062 {
00063         Init();
00064 }
00065 
00066 void SCA_DelaySensor::Init()
00067 {
00068         m_lastResult = false;
00069         m_frameCount = -1;
00070         m_reset = true;
00071 }
00072 
00073 SCA_DelaySensor::~SCA_DelaySensor()
00074 {
00075         /* intentionally empty */
00076 }
00077 
00078 CValue* SCA_DelaySensor::GetReplica()
00079 {
00080         CValue* replica = new SCA_DelaySensor(*this);
00081         // this will copy properties and so on...
00082         replica->ProcessReplica();
00083 
00084         return replica;
00085 }
00086 
00087 
00088 
00089 bool SCA_DelaySensor::IsPositiveTrigger()
00090 { 
00091         return (m_invert ? !m_lastResult : m_lastResult);
00092 }
00093 
00094 bool SCA_DelaySensor::Evaluate()
00095 {
00096         bool trigger = false;
00097         bool result;
00098 
00099         if (m_frameCount==-1) {
00100                 // this is needed to ensure ON trigger in case delay==0
00101                 // and avoid spurious OFF trigger when duration==0
00102                 m_lastResult = false;
00103                 m_frameCount = 0;
00104         }
00105 
00106         if (m_frameCount<m_delay) {
00107                 m_frameCount++;
00108                 result = false;
00109         } else if (m_duration > 0) {
00110                 if (m_frameCount < m_delay+m_duration) {
00111                         m_frameCount++;
00112                         result = true;
00113                 } else {
00114                         result = false;
00115                         if (m_repeat)
00116                                 m_frameCount = -1;
00117                 }
00118         } else {
00119                 result = true;
00120                 if (m_repeat)
00121                         m_frameCount = -1;
00122         }
00123         if ((m_reset && m_level) || result != m_lastResult)
00124                 trigger = true;
00125         m_reset = false;
00126         m_lastResult = result;
00127         return trigger;
00128 }
00129 
00130 #ifdef WITH_PYTHON
00131 
00132 /* ------------------------------------------------------------------------- */
00133 /* Python functions                                                          */
00134 /* ------------------------------------------------------------------------- */
00135 
00136 /* Integration hooks ------------------------------------------------------- */
00137 PyTypeObject SCA_DelaySensor::Type = {
00138         PyVarObject_HEAD_INIT(NULL, 0)
00139         "SCA_DelaySensor",
00140         sizeof(PyObjectPlus_Proxy),
00141         0,
00142         py_base_dealloc,
00143         0,
00144         0,
00145         0,
00146         0,
00147         py_base_repr,
00148         0,0,0,0,0,0,0,0,0,
00149         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00150         0,0,0,0,0,0,0,
00151         Methods,
00152         0,
00153         0,
00154         &SCA_ISensor::Type,
00155         0,0,0,0,0,0,
00156         py_base_new
00157 };
00158 
00159 PyMethodDef SCA_DelaySensor::Methods[] = {
00160         {NULL,NULL} //Sentinel
00161 };
00162 
00163 PyAttributeDef SCA_DelaySensor::Attributes[] = {
00164         KX_PYATTRIBUTE_INT_RW("delay",0,100000,true,SCA_DelaySensor,m_delay),
00165         KX_PYATTRIBUTE_INT_RW("duration",0,100000,true,SCA_DelaySensor,m_duration),
00166         KX_PYATTRIBUTE_BOOL_RW("repeat",SCA_DelaySensor,m_repeat),
00167         { NULL }        //Sentinel
00168 };
00169 
00170 #endif // WITH_PYTHON
00171 
00172 /* eof */