JNR
laTimeTrig.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laTimeTrig.cpp
32 //
33 // Time-triggered events
34 //
35 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
36 //
37 #include "stdafx.h"
38 #include "jnr.h"
39 
40 laTimeTrig::laTimeTrig(void)
41 {
42  _dElapse = 1;
43  _dHold = 1;
44  _bCyclic = M_TRUE;
45 
46  _bEnabled = M_TRUE;
47 
48  _pEventHandler = NULL;
49  _pEventObject = NULL;
50 
51  reset();
52 }
53 
54 /*laTimeTrig::~laTimeTrig(void)
55 {
56 }*/
57 
58 // "Animate" the time triggered event; This method must be called on every frame,
59 // when the time-trigger needs to be active
60 //
61 void laTimeTrig::animate(laTimer &t)
62 {
63  //Do nothing if disalbed
64  if(!_bEnabled) return;
65 
66  //Do nothing if fired and not cyclic
67  if( (!_bCyclic) && (_bFired) ) return;
68 
69  //Elapse event
70  //
71  if(_nTimeLeft<t.delta())
72  {
73  if(_bEvent)
74  {
75  // The event flag is set, so we were counting down from the Hold interval;
76  // Unset the flag and use the Elapse interval.
77  //
78  _bEvent = M_FALSE;
79  _nTimeLeft = _dElapse;
80  //_bFired = M_TRUE;
81  }
82  else
83  {
84  // The event flag is set, so we were counting down from the Elapse interval;
85  // Unset the flag and use the Hold interval.
86  //
87  _bEvent = M_TRUE;
88  _nTimeLeft = _dHold;
89  _bFired = M_TRUE;
90 
91  //Call the event handler function
92  if(_pEventHandler) _pEventHandler(_pEventObject);
93  }
94  return;
95  }
96 
97  //Increment the time counter
98  _nTimeLeft -= t.delta();
99 }