Simulation.cpp
Go to the documentation of this file.
1 #include "Simulation.hpp"
2 #include "../Defs.hpp"
3 
4 namespace comp
5 {
6 
8  iv::FixedUpdateClient( inst, time_id ),
9  cm( inst, this, "Simulation" ),
10  sim( sim ),
11  on_restart( inst, [ this ]()
12  {
13  this->cm.log( SRC_INFO, Defs::Log::Simulation, "Game state change: ", this->sim->state.Get(), " -> ", SimulationState::Running, "." );
14  this->sim->state.Modify( &this->cm, SimulationState::Running );
15 
16  this->needs_reset = true;
17  this->fixed_update_resume();
18  }),
19  on_end( inst, [ this ]()
20  {
21  if( this->sim->state.Get() != SimulationState::Empty )
22  {
23  this->cm.log( SRC_INFO, Defs::Log::Simulation, "Game state change: ", this->sim->state.Get(), " -> ", SimulationState::Ended, "." );
24  this->sim->state.Modify( &this->cm, SimulationState::Ended );
25 
26  this->needs_reset = true;
27  this->fixed_update_pause();
28  }
29  }),
30  on_pause( inst, [ this ]( bool real )
31  {
32  if( this->on_pause.Get() )
33  { // pause
34  if( this->sim->state.Get() == SimulationState::Running )
35  {
36  this->cm.log( SRC_INFO, Defs::Log::Simulation, "Game state change: ", this->sim->state.Get(), " -> ", SimulationState::Paused, "." );
37  this->sim->state.Modify( &this->cm, SimulationState::Paused );
38 
39  this->fixed_update_pause();
40  }
41  }
42  else
43  { // unpause
44  if( this->sim->state.Get() == SimulationState::Paused )
45  {
46  this->cm.log( SRC_INFO, Defs::Log::Simulation, "Game state change: ", this->sim->state.Get(), " -> ", SimulationState::Running, "." );
47  this->sim->state.Modify( &this->cm, SimulationState::Running );
48 
49  this->fixed_update_resume();
50  }
51  }
52  }),
53  needs_reset( true )
54 {
55  this->cm.inherits( this->iv::FixedUpdateClient::cm );
56  this->cm.owns( this->on_restart.cm, this->on_end.cm, this->on_pause.cm );
57 
58  this->fixed_update_pause();
59 
60  this->on_restart.Assign_Attribute_R( &sim->restart );
61  this->on_end.Assign_Attribute_R( &sim->end );
62  this->on_pause.Assign_Attribute_R( &sim->pause );
63 }
64 
65 void Simulation::fixed_update( iv::TimeId time, int time_step, int steps )
66 {
67  if( this->needs_reset )
68  {
69  this->needs_reset = false;
70  this->sim->time_ms.Modify( &this->cm, 0 );
71  this->simulation_reset();
72  }
73 
74  for( size_t i = 0; i < steps; i++ )
75  {
76  //
77  this->simulation_step( time_step );
78 
79  //
80  this->sim->time_ms.Modify( &this->cm, this->sim->time_ms.Get() + time_step );
81  }
82 }
83 
84 }