-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodifier.cpp
119 lines (94 loc) · 2.63 KB
/
modifier.cpp
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
#include "modifier.h"
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <json/json.h>
using namespace Chaos;
Modifier::Modifier() {
this->timer.initialize();
me = this;
pauseTimeAccumulator = 0;
totalLifespan = -1; // An erroenous value that if set should be positive
}
void Modifier::setDualshock(Controller* dualshock) {
this->dualshock = dualshock;
}
void Modifier::setChaosEngine(Engine* chaosEngine) {
this->chaosEngine = chaosEngine;
}
void Modifier::_update(bool isPaused) {
timer.update();
if (isPaused) {
pauseTimeAccumulator += timer.dTime();
}
this->update();
}
void Modifier::begin() { // virtual override
}
void Modifier::update() { // virtual override
}
void Modifier::finish() { // virtual override
}
const char* Modifier::description() { // virtual override
return "";
}
std::map<std::string, std::function<Modifier*()>> Modifier::factory;
Modifier* Modifier::build( const std::string& name ) {
Modifier* mod = NULL;
if (factory.count(name) > 0) {
mod = factory[name]();
}
return mod;
}
//std::string modifierToJsonString(Modifier* modifier) {
// std::stringstream result("");
// result <<
// return result;
//}
Json::Value modifierToJsonObject( std::string name, Modifier* mod ) {
Json::Value result;
result["name"] = name;
result["desc"] = mod->description();
result["lifespan"] = mod->lifespan();
return result;
}
std::string Modifier::getModList(double timePerModifier) {
Json::Value root;
Json::Value & data = root["mods"];
// std::stringstream result;
// result << "{\"mods\":[";
// std::map<std::string,std::function<Modifier*()>>::iterator it = factory.begin();
// if (it != factory.end()) {
// result << "{\"name\":\"" << it->first << "\", ";
// it++;
// }
// for (; it != factory.end(); it++) {
// result << "\",\"" << it->first;
// }
// result << "\"],\"voteTime\":" << (timePerModifier/3.0 - 0.5) << "}";
int i = 0;
for (std::map<std::string,std::function<Modifier*()>>::iterator it = factory.begin();
it != factory.end();
it++) {
Modifier* tempMod = it->second();
data[i++] = modifierToJsonObject( it->first, tempMod);
delete tempMod;
}
// root["mods"] = data;
Json::StreamWriterBuilder builder;
// const std::string json_file = Json::writeString(builder, root);
// std::cout << json_file << std::endl;
return Json::writeString(builder, root);
}
double Modifier::lifetime() {
return timer.runningTime() - pauseTimeAccumulator;
}
double Modifier::lifespan() {
return totalLifespan;
}
bool Modifier::tweak( DeviceEvent* event ) {
return true; // by default it is valid and we do nothing
}
void Modifier::setParentModifier(Modifier* parent) {
this->me = parent;
}