aboutsummaryrefslogtreecommitdiff
path: root/extension/src/Simulation.cpp
blob: 5ea99c57a83ef10444177847ddf7ca7fbd2de87e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "Simulation.hpp"

namespace OpenVic2 {
   void Simulation::togglePauseState() {
      this->isPaused = !isPaused;
   }

   bool Simulation::getPauseState() {
      return this->isPaused;
   }

   void Simulation::increaseSimulationSpeed() {
      switch (this->currentSpeed) {
      case(Speed::Speed1):
         this->currentSpeed = Speed::Speed2;
         break;
      case(Speed::Speed2):
         this->currentSpeed = Speed::Speed3;
         break;
      case(Speed::Speed3):
         this->currentSpeed = Speed::Speed4;
         break;
      case(Speed::Speed4):
         this->currentSpeed = Speed::Speed5;
         break;
      }
   }

   void Simulation::decreaseSimulationSpeed() {
      switch (this->currentSpeed) {
      case(Speed::Speed2):
         this->currentSpeed = Speed::Speed1;
         break;
      case(Speed::Speed3):
         this->currentSpeed = Speed::Speed2;
         break;
      case(Speed::Speed4):
         this->currentSpeed = Speed::Speed3;
         break;
      case(Speed::Speed5):
         this->currentSpeed = Speed::Speed4;
         break;
      }
   }

   void Simulation::setSimulationSpeed(Speed speed) {
         this->currentSpeed = speed;
   }

   int Simulation::getSimulationSpeed() {
      return static_cast<int>(this->currentSpeed);
   }

   void Simulation::conditionallyAdvanceSimulation() {
      if (!(this->isPaused)) {
         std::chrono::time_point<std::chrono::high_resolution_clock> previousTime = this->lastPolledTime;
         std::chrono::time_point<std::chrono::high_resolution_clock> currentTime = std::chrono::high_resolution_clock::now();
         if (std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - previousTime).count() >= this->getSimulationSpeed()) {
            this->lastPolledTime = currentTime;
            this->inGameDate++;
         }
      }
   }
}