aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic2/GameAdvancementHook.hpp
blob: 1fe7737fe808194e8e7bdb400312c1ed10d0569c (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
#pragma once

#include <chrono>
#include <functional>
#include <vector>

namespace OpenVic2 {
   // Conditionally advances game with provided behaviour
   // Class governs game speed and pause state
   class GameAdvancementHook {
   public:
      using AdvancementFunction = std::function<void()>;
      using RefreshFunction = std::function<void()>;
      using speed_t = int8_t;

      // Minimum number of miliseconds before the simulation advances
      static const std::vector<std::chrono::milliseconds> GAME_SPEEDS;

   private:
      std::chrono::time_point<std::chrono::high_resolution_clock> lastPolledTime;
      // A function pointer that advances the simulation, intended to be a capturing lambda or something similar. May need to be reworked later
      AdvancementFunction triggerFunction;
      RefreshFunction refreshFunction;
      speed_t currentSpeed;

   public:
      bool isPaused;

      GameAdvancementHook(AdvancementFunction tickFunction, RefreshFunction updateFunction, bool startPaused = true, speed_t startingSpeed = 0);

      void setSimulationSpeed(speed_t speed);
      speed_t getSimulationSpeed() const;
      void increaseSimulationSpeed();
      void decreaseSimulationSpeed();
      bool canIncreaseSimulationSpeed() const;
      bool canDecreaseSimulationSpeed() const;
      GameAdvancementHook& operator++();
      GameAdvancementHook& operator--();
      void conditionallyAdvanceGame();
      void reset();
   };
}