summary refs log tree commit diff stats
path: root/src/game.h
blob: 36398bc35dfd7c84c95fb0f309dd09f6e99b4410 (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
#ifndef GAME_H_E6F1396E
#define GAME_H_E6F1396E

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

class Game {
public:

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

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

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

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

  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 setMap(std::unique_ptr<Map> map) { map_ = std::move(map); }

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

private:

  Mixer mixer_;
  bool shouldQuit_ = false;

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

  std::vector<int> spriteIds_;
  std::vector<Sprite> sprites_;
  std::unique_ptr<Map> map_;
};

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