summary refs log tree commit diff stats
path: root/src/game.h
blob: 756ce700ed6f038525b84395684c3db806bc6bb4 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef GAME_H_E6F1396E
#define GAME_H_E6F1396E

#include <deque>
#include <list>
#include <range/v3/all.hpp>
#include <vector>
#include <memory>
#include <map>
#include <set>
#include <random>
#include "sprite.h"
#include "map.h"
#include "consts.h"
#include "system.h"
#include "mixer.h"
#include "font.h"

class Renderer;

class Game {
public:

  Game(Renderer& renderer, std::mt19937& rng) : font_("../res/font.txt", renderer), rng_(&rng) {}

  Mixer& getMixer() { return mixer_; }

  bool shouldQuit() const { return shouldQuit_; }

  void quit() { shouldQuit_ = true; }

  template <typename T>
  void emplaceSystem() {
    systems_.push_back(std::make_unique<T>(*this));
    systemByKey_[T::Key] = systems_.back().get();
  }

  template <typename T>
  T& getSystem() {
    return *dynamic_cast<T*>(systemByKey_.at(T::Key));
  }

  auto systems() {
    return systems_ | ranges::views::transform([&] (std::unique_ptr<System>& systemPtr) -> System& {
      return *systemPtr;
    });
  }

  int emplaceSprite(std::string alias);

  void destroySprite(int id);

  const Sprite& getSprite(int id) const {
    return sprites_.at(id);
  }

  Sprite& getSprite(int id) {
    return sprites_.at(id);
  }

  const std::set<int>& getSprites() {
    return spriteIds_;
  }

  int getSpriteByAlias(std::string alias) const {
    return spritesByAlias_.at(alias);
  }

  auto spriteView() const {
    return ranges::views::transform([&] (int id) -> const Sprite& {
      return sprites_.at(id);
    });
  }

  auto spriteView() {
    return ranges::views::transform([&] (int id) -> Sprite& {
      return sprites_.at(id);
    });
  }

  void loadMap(std::string filename);

  const Map& getMap() const { return *map_; }

  const Font& getFont() const { return font_; }

  std::mt19937& getRng() { return *rng_; }

  void pauseGameplay() { paused_ = true; }

  void unpauseGameplay() { paused_ = false; }

  bool isGameplayPaused() const { return paused_; }

private:

  Mixer mixer_;
  bool shouldQuit_ = false;

  std::list<std::unique_ptr<System>> systems_;
  std::map<SystemKey, System*> systemByKey_;

  std::set<int> spriteIds_;
  std::deque<int> idsToReuse_;
  std::vector<Sprite> sprites_;
  std::map<std::string, int> spritesByAlias_;
  std::unique_ptr<Map> map_;
  Font font_;
  std::mt19937* rng_;
  bool paused_ = false;
};

#endif /* end of include guard: GAME_H_E6F1396E */