-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.hpp
147 lines (125 loc) · 4.14 KB
/
state.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#pragma once
#include <chrono>
#include <thread>
#include <vector>
#include "command.hpp"
#include "tank.hpp"
#include "terrain.hpp"
class State {
public:
static State& Instance() {
static State instance;
return instance;
}
State(State const&) = delete;
void operator=(State const&) = delete;
void Update();
bool gameHasEnded();
const std::vector<Tank> &getPlayerTanks();
const std::vector<Tank> &getEnemyTanks();
private:
State();
// Game Variables
std::chrono::milliseconds time_remaining;
std::chrono::milliseconds timestamp;
double width; /* Map width */
double height; /* Map height */
std::vector<Tank> player_tanks;
std::vector<Tank> enemy_tanks;
// std::unordered_map<std::string, Projectile> player_projectiles;
// std::unordered_map<std::string, Projectile> enemy_projectiles;
// Top left x-axis, top left y-axis, width, height
std::vector<Terrain> solid_terrain; /* Can not shoot or move */
std::vector<Terrain> impassable_terrain; /* Cannot move, can shoot */
bool gameEnded;
// Misc.
std::string team;
};
State::State() {
Command &command = Command::Instance();
team = command.GetTeamName();
}
void State::Update() {
Command &command = Command::Instance();
std::string json = command.GetStateJSON();
std::chrono::milliseconds _timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
rapidjson::Document d;
d.Parse(json.c_str());
if(d.IsObject()) {
// Got Match Data (JSON)
// TODO: Parse
const std::string comm_type = d["comm_type"].GetString();
if(comm_type.compare("GAME_START") == 0) {
// Match has started
} else if(comm_type.compare("MatchEnd") == 0) {
// Match has ended
gameEnded = true;
} else if(comm_type.compare("GAME_END") == 0) {
// Game has ended
} else if(comm_type.compare("GAMESTATE") == 0) {
// General State Stuff
timestamp = _timestamp;
// Map
const rapidjson::Value &terrains = d["map"]["terrain"];
// Clear existing map data
solid_terrain.clear();
impassable_terrain.clear();
for(rapidjson::SizeType i = 0; i < terrains.Size(); i++) {
const rapidjson::Value &terrain = terrains[i];
Terrain t;
t.x = terrain["boundingBox"]["corner"][0].GetDouble();
t.y = terrain["boundingBox"]["corner"][1].GetDouble();
t.width = terrain["boundingBox"]["size"][0].GetDouble();
t.height = terrain["boundingBox"]["size"][1].GetDouble();
std::string type = terrain["type"].GetString();
if(type.compare("IMPASSABLE") == 0) {
impassable_terrain.push_back(t);
} else if(type.compare("SOLID") == 0) {
solid_terrain.push_back(t);
}
}
// Players
rapidjson::Value &v_players = d["players"];
// Clear existing tank data
player_tanks.clear();
enemy_tanks.clear();
for(rapidjson::SizeType i = 0; i < v_players.Size(); i++) {
rapidjson::Value &v_player = v_players[i];
std::string player_name = v_player["name"].GetString();
// Set new data
rapidjson::Value &v_tanks = v_player["tanks"];
for(rapidjson::SizeType j = 0; j < v_tanks.Size(); j++) {
rapidjson::Value &v_tank = v_tanks[j];
if(!v_tank["alive"].GetBool()) {
continue;
}
Tank tank(v_tank["id"].GetString());
tank.alive = v_tank["alive"].GetBool();
tank.x = v_tank["position"][0].GetDouble();
tank.y = v_tank["position"][1].GetDouble();
tank.tracks = v_tank["tracks"].GetDouble();
tank.turret = v_tank["turret"].GetDouble();
if(team.compare(player_name) == 0) {
// It's us!
player_tanks.push_back(tank);
} else {
// It's the enemy!
enemy_tanks.push_back(tank);
}
}
}
}
} else {
// Got Mach ID (GUID)
// TODO: Assert maybe?
}
}
bool State::gameHasEnded() {
return gameEnded;
}
const std::vector<Tank> & State::getPlayerTanks() {
return player_tanks;
};
const std::vector<Tank> & State::getEnemyTanks() {
return enemy_tanks;
};