Blender V2.61 - r43446

SCA_DelaySensor.cpp

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