From 56748e13491f665745981968eb5c1c6e9246bde2 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Tue, 23 Apr 2013 14:45:04 +0200 Subject: [PATCH] Lines having an ID allows removal :D --- src/client/client.cpp | 31 +++++++++++++++++++++++++++++-- src/client/client.hpp | 1 + src/client/client_common.hpp | 10 ++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/client/client.cpp b/src/client/client.cpp index a62ccdb..c27efe1 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -23,8 +23,20 @@ namespace games { break; case LWS_CALLBACK_CLIENT_RECEIVE: try { - auto l = from_json(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(js); + current_client->remove_line(ID); + } + } else if (command == "add lines"){ + for(auto js : object["data"].getArray()){ + auto l = from_json(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 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]){ diff --git a/src/client/client.hpp b/src/client/client.hpp index de3c85a..1dd73a6 100644 --- a/src/client/client.hpp +++ b/src/client/client.hpp @@ -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 scale_files{ diff --git a/src/client/client_common.hpp b/src/client/client_common.hpp index a35d923..eea66be 100644 --- a/src/client/client_common.hpp +++ b/src/client/client_common.hpp @@ -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 create_instrument(cheap_line_type l){ return std::make_shared( 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) )