-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.hpp
127 lines (91 loc) · 2.69 KB
/
config.hpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef CONFIG_HPP
#define CONFIG_HPP
#include <memory>
#include <string>
#include <unordered_map>
#include <exception>
#include <cstdint>
#include <json/json.h>
class Config;
class ConfigError;
struct LeagueInfo;
struct UnitKindInfo;
struct SpriteInfo;
/*
* Config.
*/
class Config {
private:
Json::Value root;
std::unordered_map<std::string, LeagueInfo> leagueInfo;
void parseGame();
void parseTest();
public:
void parse(const char *path);
Config(const char *path);
Config(const std::string &path) : Config(path.c_str()) {};
int getSpriteWidth() const {return root.get("spriteWidth", 20).asInt();}
int getSpriteHeight() const {return root.get("spriteHeight", 20).asInt();}
int getColumnNumber() const {return root.get("columnNumber", 20).asInt();}
int getRowNumber() const {return root.get("rowNumber", 20).asInt();}
void setSpriteSize(int w, int h) {
root["spriteWidth"] = w;
root["spriteHeight"] = h;
}
int getMoveDelay() const {return root.get("moveDelay", 250).asInt();}
void setMoveDelay(int delay) {root["moveDelay"] = delay;}
int getMaxMoves() const {return root.get("maxMoves", 1000000).asInt();}
int getUnitsPerLeague() const noexcept {return root.get("unitsPerLeague", 10).asInt();}
const std::unordered_map<std::string, LeagueInfo> &getLeagueInfo() const noexcept {return leagueInfo;}
};
/*
* ConfigError.
*/
class ConfigError : public std::exception {
private:
std::string reason;
public:
ConfigError(const std::string &reason);
ConfigError(const Json::Reader &reader);
virtual const char *what() const noexcept {return reason.c_str();}
};
/*
* LeagueInfo.
*/
struct LeagueInfo {
std::string directory;
std::string startKind;
std::unordered_map<std::string, UnitKindInfo> unitKinds;
};
/*
* UnitKindInfo.
*/
struct UnitKindInfo {
std::string exec;
std::shared_ptr<SpriteInfo> sprite;
};
/*
* SpriteInfo.
*/
struct SpriteInfo {
enum class Type {
SquareRGB,
ImagePath
};
virtual Type getType() const noexcept = 0;
};
struct SpriteInfoSquareRGB : public SpriteInfo {
virtual Type getType() const noexcept {return Type::SquareRGB;}
std::uint8_t red, green, blue;
SpriteInfoSquareRGB(int i) {
red = i >> 16;
green = i >> 8;
blue = i;
}
};
struct SpriteInfoImagePath : std::string, SpriteInfo {
virtual Type getType() const noexcept {return Type::ImagePath;}
SpriteInfoImagePath(const std::string &string) : std::string(string) {}
};
std::ostream &operator <<(std::ostream &os, const SpriteInfo *info);
#endif