AutorechargeBehavior.cpp
Go to the documentation of this file.
2 
3 namespace comp
4 {
5 
6 AutorechargeBehavior::AutorechargeBehavior( iv::Instance * inst, iv::TimeId time, iv::Attr< int > * m_charges, uint64_t recharge_ms, int max ) :
7  cm( inst, this, "AutorechargeBehavior" ),
8  m_charges( m_charges ),
9  recharge_ms( recharge_ms ),
10  max( max ),
11  cooldown_ms( 0 )
12 {
13  this->m_charges->Modify( &this->cm, this->max );
14 }
15 
17 {
18  this->cooldown_ms = 0;
19  this->m_charges->Modify( &this->cm, this->max );
20 }
21 
22 void AutorechargeBehavior::step( int step_ms )
23 {
24  if( this->cooldown_ms > 0 )
25  {
26  if( this->cooldown_ms > step_ms )
27  {
28  this->cooldown_ms -= step_ms;
29  }
30  else
31  {
32  this->cooldown_ms = 0;
33  if( this->m_charges->Get() < this->max )
34  this->m_charges->Modify( &this->cm, this->m_charges->Get() + 1 );
35  }
36  }
37 
38  if( this->cooldown_ms == 0 && this->m_charges->Get() < this->max )
39  this->cooldown_ms = this->recharge_ms;
40 }
41 
42 }