aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/GameAdvancementHook.hpp
blob: 2a27ff96151d156483630020629cd9bdf3debd28 (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
#pragma once

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

namespace OpenVic {
   // 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:
      using time_point_t = std::chrono::time_point<std::chrono::high_resolution_clock>;

      time_point_t 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();
   };
}