V-ART
baseaction.cpp
Go to the documentation of this file.
1 
5 #include "vart/baseaction.h"
6 #include "vart/time.h"
7 #include "vart/callback.h"
8 
9 //#include <iostream>
10 using namespace std;
11 
12 // Initialization of static attributes
13 list<VART::BaseAction*> VART::BaseAction::activeInstances;
14 list<VART::BaseAction::Initializer*> VART::BaseAction::initializers;
15 
16 
17 VART::BaseAction::BaseAction() : callbackPtr(NULL), cyclic(false), active(false),
18  speedModifier(1.0), timeToLive(604800.0f)
19 {
20 }
21 
22 // virtual
24 {
25  if (active)
26  {
27  // Update status
28  active = false;
29 
30  // Remove this instance from list of active actions.
31  list<VART::BaseAction*>::iterator iter = activeInstances.begin();
32  while (iter != activeInstances.end())
33  {
34  if ((*iter) == this)
35  {
36  activeInstances.erase(iter);
37  iter = activeInstances.end(); // force the way out of the while loop
38  }
39  else
40  ++iter;
41  }
42 
43  // Call its callback
44  if (callbackPtr)
45  callbackPtr->Activate();
46 
47  // Take "timeToLive" out of the way, in case the action is activated later and
48  // no value is given to it by the programmer.
49  timeToLive = 604800.0f;
50  }
51 }
52 
53 void VART::BaseAction::Deactivate(float seconds)
54 {
55  timeToLive = seconds;
56 }
57 
58 // virtual
60 {
61  if (!active)
62  {
63  active = true;
64  lastPositionIndex = 0;
65  // Store current time so that we had data do compute on next Move()
66  lastUpdateTime.Set();
67 
68  // Add action to list of active instances
69  activeInstances.push_back(this);
70  }
71 }
72 
73 // static
75 {
76  // Run all initializers
77  list<VART::BaseAction::Initializer*>::iterator i = initializers.begin();
78  while (i != initializers.end())
79  {
80  (*i)->Initialize();
81  ++i;
82  }
83 
84  // Move all actions
85  list<VART::BaseAction*>::iterator iter = activeInstances.begin();
86  list<VART::BaseAction*>::iterator tempIter;
87 
88  while (iter != activeInstances.end())
89  {
90  tempIter = iter;
91  ++iter;
92  // the action could remove itself from the list, so use a private iterator copy
93  (*tempIter)->Move();
94  }
95 
96  return activeInstances.size();
97 }
98 
99 // virtual
101 {
102  *cycled = false; // default value
103  Time currentTime = Time::NOW();
104  // Compute how much time has passed since last update
105  float diffTime = (currentTime - lastUpdateTime).AsFloat();
106  // Compute current position index
107  positionIndex = ((diffTime * speedModifier) / duration) + lastPositionIndex;
108  if (positionIndex > 1.0)
109  {
110  if (cyclic)
111  {
112  *cycled = true;
113  positionIndex -= 1.0;
114  }
115  else
116  {
117  positionIndex = 1.0;
118  Deactivate();
119  }
120  }
121  // Enforce timeToLive
122  timeToLive -= diffTime;
123  if (timeToLive < 0)
124  Deactivate();
125  // Prepare for next iteration
126  lastPositionIndex = positionIndex;
127  lastUpdateTime = currentTime;
128 }
static const Time & NOW()
Returns current time.
Definition: time.cpp:68
virtual void Deactivate()
Deactivate now.
Definition: baseaction.cpp:23
Header file for V-ART class "Time".
Header file for V-ART class "CallBack".
static std::list< Initializer * > initializers
List of action initializers.
Definition: baseaction.h:93
BaseAction()
Creates an action with sane defaults.
Definition: baseaction.cpp:17
Header file for V-ART class "BaseAction".
static unsigned int MoveAllActive()
Moves all active actions.
Definition: baseaction.cpp:74
static std::list< BaseAction * > activeInstances
List of active actions.
Definition: baseaction.h:90
virtual void Activate()
Activates the action.
Definition: baseaction.cpp:59
virtual void ComputePositionIndex(bool *cycled)
Computes current position index.
Definition: baseaction.cpp:100
Elapsed time representation.
Definition: time.h:15