1
Fork 0

Lines having an ID allows removal :D

This commit is contained in:
Joshua Moerman 2013-04-23 14:45:04 +02:00
parent 070ec974da
commit 56748e1349
3 changed files with 38 additions and 4 deletions

View file

@ -23,8 +23,20 @@ namespace games {
break; break;
case LWS_CALLBACK_CLIENT_RECEIVE: case LWS_CALLBACK_CLIENT_RECEIVE:
try { try {
auto l = from_json<cheap_line_type>(parse_json(std::string((char*)in, len))); 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); current_client->add_line(l);
}
}
} catch(std::exception& e){ } catch(std::exception& e){
throw websockets::runtime_error(e.what()); throw websockets::runtime_error(e.what());
} }
@ -64,6 +76,7 @@ namespace games {
} }
void Client::add_line(cheap_line_type const & line_in){ 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); auto line = create_instrument(line_in);
line->sound.reset(new motor::al::Source(sounds[line->line_kind])); line->sound.reset(new motor::al::Source(sounds[line->line_kind]));
lines.push_back(line); lines.push_back(line);
@ -76,6 +89,19 @@ namespace games {
update_pitches(*this); 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(){ bool Client::has_ended(){
return false; return false;
} }
@ -170,6 +196,7 @@ namespace games {
if(input.keys_went_down[SDLK_l]){ if(input.keys_went_down[SDLK_l]){
lines.clear(); lines.clear();
sim.lines.clear(); sim.lines.clear();
lines_changed = true;
} }
if(input.keys_went_down[SDLK_r]){ if(input.keys_went_down[SDLK_r]){

View file

@ -50,6 +50,7 @@ namespace games {
bool pause{false}; bool pause{false};
void add_line(cheap_line_type const & line); void add_line(cheap_line_type const & line);
void remove_line(int ID);
// Sound part // Sound part
const std::vector<std::string> scale_files{ const std::vector<std::string> scale_files{

View file

@ -32,14 +32,16 @@ struct Instrument {
math::Vec2 starting_point; math::Vec2 starting_point;
math::Vec2 end_point; math::Vec2 end_point;
int ID;
int line_kind; int line_kind;
float color_intensity{0.0}; float color_intensity{0.0};
Instrument() = default; 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) : starting_point(starting_point)
, end_point(end_point) , end_point(end_point)
, ID(ID)
, line_kind(line_kind) , line_kind(line_kind)
{} {}
@ -109,12 +111,14 @@ BOOST_FUSION_ADAPT_STRUCT(
struct cheap_line_type { struct cheap_line_type {
IntVec2 starting_point; IntVec2 starting_point;
IntVec2 end_point; IntVec2 end_point;
int ID;
int line_kind; int line_kind;
cheap_line_type() = default; cheap_line_type() = default;
cheap_line_type(Instrument const & l) cheap_line_type(Instrument const & l)
: starting_point(l.starting_point) : starting_point(l.starting_point)
, end_point(l.end_point) , end_point(l.end_point)
, ID(l.ID)
, line_kind(l.line_kind) , 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>( return std::make_shared<Instrument>(
to_FloatVec2(l.starting_point), to_FloatVec2(l.starting_point),
to_FloatVec2(l.end_point), to_FloatVec2(l.end_point),
l.line_kind); l.line_kind,
l.ID);
} }
BOOST_FUSION_ADAPT_STRUCT( BOOST_FUSION_ADAPT_STRUCT(
cheap_line_type, cheap_line_type,
(IntVec2, starting_point) (IntVec2, starting_point)
(IntVec2, end_point) (IntVec2, end_point)
(int, ID)
(int, line_kind) (int, line_kind)
) )