Merge with -r 22620:23107.
[blender.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef WIN32
37 // This warning tells us about truncation of __long__ stl-generated names.
38 // It can occasionally cause DevStudio to have internal compiler warnings.
39 #pragma warning( disable : 4786 )     
40 #endif
41
42 #include "SCA_DelaySensor.h"
43 #include "SCA_LogicManager.h"
44 #include "SCA_EventManager.h"
45
46 /* ------------------------------------------------------------------------- */
47 /* Native functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 SCA_DelaySensor::SCA_DelaySensor(class SCA_EventManager* eventmgr,
51                                                                  SCA_IObject* gameobj,
52                                                                  int delay,
53                                                                  int duration,
54                                                                  bool repeat)
55         : SCA_ISensor(gameobj,eventmgr),
56         m_repeat(repeat),
57         m_delay(delay),
58         m_duration(duration)
59 {
60         Init();
61 }
62
63 void SCA_DelaySensor::Init()
64 {
65         m_lastResult = false;
66         m_frameCount = -1;
67         m_reset = true;
68 }
69
70 SCA_DelaySensor::~SCA_DelaySensor()
71 {
72         /* intentionally empty */
73 }
74
75 CValue* SCA_DelaySensor::GetReplica()
76 {
77         CValue* replica = new SCA_DelaySensor(*this);
78         // this will copy properties and so on...
79         replica->ProcessReplica();
80
81         return replica;
82 }
83
84
85
86 bool SCA_DelaySensor::IsPositiveTrigger()
87
88         return (m_invert ? !m_lastResult : m_lastResult);
89 }
90
91 bool SCA_DelaySensor::Evaluate()
92 {
93         bool trigger = false;
94         bool result;
95
96         if (m_frameCount==-1) {
97                 // this is needed to ensure ON trigger in case delay==0
98                 // and avoid spurious OFF trigger when duration==0
99                 m_lastResult = false;
100                 m_frameCount = 0;
101         }
102
103         if (m_frameCount<m_delay) {
104                 m_frameCount++;
105                 result = false;
106         } else if (m_duration > 0) {
107                 if (m_frameCount < m_delay+m_duration) {
108                         m_frameCount++;
109                         result = true;
110                 } else {
111                         result = false;
112                         if (m_repeat)
113                                 m_frameCount = -1;
114                 }
115         } else {
116                 result = true;
117                 if (m_repeat)
118                         m_frameCount = -1;
119         }
120         if ((m_reset && m_level) || result != m_lastResult)
121                 trigger = true;
122         m_reset = false;
123         m_lastResult = result;
124         return trigger;
125 }
126
127 /* ------------------------------------------------------------------------- */
128 /* Python functions                                                          */
129 /* ------------------------------------------------------------------------- */
130
131 /* Integration hooks ------------------------------------------------------- */
132 PyTypeObject SCA_DelaySensor::Type = {
133         PyVarObject_HEAD_INIT(NULL, 0)
134         "SCA_DelaySensor",
135         sizeof(PyObjectPlus_Proxy),
136         0,
137         py_base_dealloc,
138         0,
139         0,
140         0,
141         0,
142         py_base_repr,
143         0,0,0,0,0,0,0,0,0,
144         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
145         0,0,0,0,0,0,0,
146         Methods,
147         0,
148         0,
149         &SCA_ISensor::Type,
150         0,0,0,0,0,0,
151         py_base_new
152 };
153
154 PyMethodDef SCA_DelaySensor::Methods[] = {
155         {NULL,NULL} //Sentinel
156 };
157
158 PyAttributeDef SCA_DelaySensor::Attributes[] = {
159         KX_PYATTRIBUTE_INT_RW("delay",0,100000,true,SCA_DelaySensor,m_delay),
160         KX_PYATTRIBUTE_INT_RW("duration",0,100000,true,SCA_DelaySensor,m_duration),
161         KX_PYATTRIBUTE_BOOL_RW("repeat",SCA_DelaySensor,m_repeat),
162         { NULL }        //Sentinel
163 };
164
165 /* eof */