Lines having an ID allows removal :D
This commit is contained in:
parent
070ec974da
commit
56748e1349
3 changed files with 38 additions and 4 deletions
|
@ -23,8 +23,20 @@ namespace games {
|
|||
break;
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||
try {
|
||||
auto l = from_json<cheap_line_type>(parse_json(std::string((char*)in, len)));
|
||||
current_client->add_line(l);
|
||||
std::string instr((char*)in, len);
|
||||
auto object = parse_json(instr).getObject();
|
||||
auto command = object["command"];
|
||||
if(command == "remove lines"){
|
||||
for(auto js : object["data"].getArray()){
|
||||
auto ID = from_json<int>(js);
|
||||
current_client->remove_line(ID);
|
||||
}
|
||||
} else if (command == "add lines"){
|
||||
for(auto js : object["data"].getArray()){
|
||||
auto l = from_json<cheap_line_type>(js);
|
||||
current_client->add_line(l);
|
||||
}
|
||||
}
|
||||
} catch(std::exception& e){
|
||||
throw websockets::runtime_error(e.what());
|
||||
}
|
||||
|
@ -64,6 +76,7 @@ namespace games {
|
|||
}
|
||||
|
||||
void Client::add_line(cheap_line_type const & line_in){
|
||||
std::cout << "adding line: " << line_in.ID << std::endl;
|
||||
auto line = create_instrument(line_in);
|
||||
line->sound.reset(new motor::al::Source(sounds[line->line_kind]));
|
||||
lines.push_back(line);
|
||||
|
@ -76,6 +89,19 @@ namespace games {
|
|||
update_pitches(*this);
|
||||
}
|
||||
|
||||
void Client::remove_line(int ID){
|
||||
for(auto const & l : lines){
|
||||
if(l->ID != ID) continue;
|
||||
auto& v = sim.lines;
|
||||
v.erase(std::remove_if(v.begin(), v.end(), [l](line_type const &l2){ return l.get() == l2.information; }), v.end());
|
||||
}
|
||||
|
||||
auto& v = lines;
|
||||
v.erase(std::remove_if(v.begin(), v.end(), [ID](std::shared_ptr<Instrument> const & l){ return l->ID == ID; }), v.end());
|
||||
|
||||
lines_changed = true;
|
||||
}
|
||||
|
||||
bool Client::has_ended(){
|
||||
return false;
|
||||
}
|
||||
|
@ -170,6 +196,7 @@ namespace games {
|
|||
if(input.keys_went_down[SDLK_l]){
|
||||
lines.clear();
|
||||
sim.lines.clear();
|
||||
lines_changed = true;
|
||||
}
|
||||
|
||||
if(input.keys_went_down[SDLK_r]){
|
||||
|
|
|
@ -50,6 +50,7 @@ namespace games {
|
|||
bool pause{false};
|
||||
|
||||
void add_line(cheap_line_type const & line);
|
||||
void remove_line(int ID);
|
||||
|
||||
// Sound part
|
||||
const std::vector<std::string> scale_files{
|
||||
|
|
|
@ -32,14 +32,16 @@ struct Instrument {
|
|||
math::Vec2 starting_point;
|
||||
math::Vec2 end_point;
|
||||
|
||||
int ID;
|
||||
int line_kind;
|
||||
float color_intensity{0.0};
|
||||
|
||||
Instrument() = default;
|
||||
|
||||
Instrument(math::Vec2 starting_point, math::Vec2 end_point, int line_kind)
|
||||
Instrument(math::Vec2 starting_point, math::Vec2 end_point, int line_kind, int ID)
|
||||
: starting_point(starting_point)
|
||||
, end_point(end_point)
|
||||
, ID(ID)
|
||||
, line_kind(line_kind)
|
||||
{}
|
||||
|
||||
|
@ -109,12 +111,14 @@ BOOST_FUSION_ADAPT_STRUCT(
|
|||
struct cheap_line_type {
|
||||
IntVec2 starting_point;
|
||||
IntVec2 end_point;
|
||||
int ID;
|
||||
int line_kind;
|
||||
|
||||
cheap_line_type() = default;
|
||||
cheap_line_type(Instrument const & l)
|
||||
: starting_point(l.starting_point)
|
||||
, end_point(l.end_point)
|
||||
, ID(l.ID)
|
||||
, line_kind(l.line_kind)
|
||||
{}
|
||||
};
|
||||
|
@ -123,12 +127,14 @@ inline std::shared_ptr<Instrument> create_instrument(cheap_line_type l){
|
|||
return std::make_shared<Instrument>(
|
||||
to_FloatVec2(l.starting_point),
|
||||
to_FloatVec2(l.end_point),
|
||||
l.line_kind);
|
||||
l.line_kind,
|
||||
l.ID);
|
||||
}
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
cheap_line_type,
|
||||
(IntVec2, starting_point)
|
||||
(IntVec2, end_point)
|
||||
(int, ID)
|
||||
(int, line_kind)
|
||||
)
|
||||
|
|
Reference in a new issue