update for MingW/CMake
[blender-staging.git] / source / gameengine / GameLogic / SCA_DelaySensor.cpp
1 /**
2  * Delay trigger
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL 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.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #if defined(WIN32) && !defined(FREE_WINDOWS)
33 // This warning tells us about truncation of __long__ stl-generated names.
34 // It can occasionally cause DevStudio to have internal compiler warnings.
35 #pragma warning( disable : 4786 )     
36 #endif
37
38 #include "SCA_DelaySensor.h"
39 #include "SCA_LogicManager.h"
40 #include "SCA_EventManager.h"
41
42 /* ------------------------------------------------------------------------- */
43 /* Native functions                                                          */
44 /* ------------------------------------------------------------------------- */
45
46 SCA_DelaySensor::SCA_DelaySensor(class SCA_EventManager* eventmgr,
47                                                                  SCA_IObject* gameobj,
48                                                                  int delay,
49                                                                  int duration,
50                                                                  bool repeat)
51         : SCA_ISensor(gameobj,eventmgr),
52         m_repeat(repeat),
53         m_delay(delay),
54         m_duration(duration)
55 {
56         Init();
57 }
58
59 void SCA_DelaySensor::Init()
60 {
61         m_lastResult = false;
62         m_frameCount = -1;
63         m_reset = true;
64 }
65
66 SCA_DelaySensor::~SCA_DelaySensor()
67 {
68         /* intentionally empty */
69 }
70
71 CValue* SCA_DelaySensor::GetReplica()
72 {
73         CValue* replica = new SCA_DelaySensor(*this);
74         // this will copy properties and so on...
75         replica->ProcessReplica();
76
77         return replica;
78 }
79
80
81
82 bool SCA_DelaySensor::IsPositiveTrigger()
83
84         return (m_invert ? !m_lastResult : m_lastResult);
85 }
86
87 bool SCA_DelaySensor::Evaluate()
88 {
89         bool trigger = false;
90         bool result;
91
92         if (m_frameCount==-1) {
93                 // this is needed to ensure ON trigger in case delay==0
94                 // and avoid spurious OFF trigger when duration==0
95                 m_lastResult = false;
96                 m_frameCount = 0;
97         }
98
99         if (m_frameCount<m_delay) {
100                 m_frameCount++;
101                 result = false;
102         } else if (m_duration > 0) {
103                 if (m_frameCount < m_delay+m_duration) {
104                         m_frameCount++;
105                         result = true;
106                 } else {
107                         result = false;
108                         if (m_repeat)
109                                 m_frameCount = -1;
110                 }
111         } else {
112                 result = true;
113                 if (m_repeat)
114                         m_frameCount = -1;
115         }
116         if ((m_reset && m_level) || result != m_lastResult)
117                 trigger = true;
118         m_reset = false;
119         m_lastResult = result;
120         return trigger;
121 }
122
123 #ifndef DISABLE_PYTHON
124
125 /* ------------------------------------------------------------------------- */
126 /* Python functions                                                          */
127 /* ------------------------------------------------------------------------- */
128
129 /* Integration hooks ------------------------------------------------------- */
130 PyTypeObject SCA_DelaySensor::Type = {
131         PyVarObject_HEAD_INIT(NULL, 0)
132         "SCA_DelaySensor",
133         sizeof(PyObjectPlus_Proxy),
134         0,
135         py_base_dealloc,
136         0,
137         0,
138         0,
139         0,
140         py_base_repr,
141         0,0,0,0,0,0,0,0,0,
142         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
143         0,0,0,0,0,0,0,
144         Methods,
145         0,
146         0,
147         &SCA_ISensor::Type,
148         0,0,0,0,0,0,
149         py_base_new
150 };
151
152 PyMethodDef SCA_DelaySensor::Methods[] = {
153         {NULL,NULL} //Sentinel
154 };
155
156 PyAttributeDef SCA_DelaySensor::Attributes[] = {
157         KX_PYATTRIBUTE_INT_RW("delay",0,100000,true,SCA_DelaySensor,m_delay),
158         KX_PYATTRIBUTE_INT_RW("duration",0,100000,true,SCA_DelaySensor,m_duration),
159         KX_PYATTRIBUTE_BOOL_RW("repeat",SCA_DelaySensor,m_repeat),
160         { NULL }        //Sentinel
161 };
162
163 #endif // DISABLE_PYTHON
164
165 /* eof */