aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic2/GameAdvancementHook.cpp
blob: 3db32249a36807b94e48083d5a43d426ce421af1 (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
65
66
67
68
69
#include "GameAdvancementHook.hpp"

namespace OpenVic2 {
   GameAdvancementHook::GameAdvancementHook(AdvancementFunction function, bool startPaused, GameSpeed startingSpeed) {
      triggerFunction = function;
      lastPolledTime = std::chrono::high_resolution_clock::now();
      isPaused = startPaused;
      currentSpeed = startingSpeed;
   }

   void GameAdvancementHook::increaseSimulationSpeed() {
      switch (currentSpeed) {
      case(GameSpeed::Speed1):
         currentSpeed = GameSpeed::Speed2;
         break;
      case(GameSpeed::Speed2):
         currentSpeed = GameSpeed::Speed3;
         break;
      case(GameSpeed::Speed3):
         currentSpeed = GameSpeed::Speed4;
         break;
      case(GameSpeed::Speed4):
         currentSpeed = GameSpeed::Speed5;
         break;
      }
   }

   void GameAdvancementHook::decreaseSimulationSpeed() {
      switch (currentSpeed) {
      case(GameSpeed::Speed2):
         currentSpeed = GameSpeed::Speed1;
         break;
      case(GameSpeed::Speed3):
         currentSpeed = GameSpeed::Speed2;
         break;
      case(GameSpeed::Speed4):
         currentSpeed = GameSpeed::Speed3;
         break;
      case(GameSpeed::Speed5):
         currentSpeed = GameSpeed::Speed4;
         break;
      }
   }

   GameAdvancementHook GameAdvancementHook::operator++(int) {
      GameAdvancementHook oldCopy = *this;
      increaseSimulationSpeed();
      return oldCopy;
   };

   GameAdvancementHook GameAdvancementHook::operator--(int) {
      GameAdvancementHook oldCopy = *this;
      decreaseSimulationSpeed();
      return oldCopy;
   };

   void GameAdvancementHook::conditionallyAdvanceGame() {
      if (!isPaused) {
         std::chrono::time_point<std::chrono::high_resolution_clock> previousTime = 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() >= static_cast<int64_t>(currentSpeed)) {
            lastPolledTime = currentTime;
            if (triggerFunction) {
               triggerFunction();
            }
         }
      }
   }
}