Browse Source

Lines have an ID :D removing them works

master
Joshua Moerman 12 years ago
parent
commit
39ed1414d0
  1. 2
      simulation
  2. 22
      src/app.h
  3. 30
      src/app_common.h
  4. 25
      src/main.cpp

2
simulation

@ -1 +1 @@
Subproject commit 61c889b0c697f522a66a7bfaf002cd143d018060
Subproject commit 092045a2c26552b9494ac0e2a708a56a775904e8

22
src/app.h

@ -22,6 +22,7 @@ struct App{
int lineuid{0};
struct OfflineSim {
std::vector<int> lines_to_remove;
std::vector<cheap_line_type> lines_to_add;
std::vector<cheap_line_type> lines;
std::vector<cheap_ball_type> balls;
@ -37,22 +38,37 @@ struct App{
App(){
online_sim.beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f});
AbstractLine line(Vec2{50.0f, 100.0f}, Vec2{150.0f, 200.0f}, simulation::kOneWay);
AbstractLine line(Vec2{50.0f, 100.0f}, Vec2{150.0f, 200.0f}, simulation::kOneWay, lineuid++);
add_line(line);
}
void add_line(cheap_line_type const & line){
void add_line(cheap_line_type line){
line.ID = lineuid++;
std::cout << "adding line: " << line.ID << std::endl;
if(uberclient){
offline_sim.lines_to_add.push_back(line);
} else {
add_line(AbstractLine{
to_FloatVec2(line.starting_point),
to_FloatVec2(line.end_point),
line.line_kind});
line.line_kind,
line.ID});
}
lines_changed();
}
void remove_line(int ID){
if(uberclient){
offline_sim.lines_to_remove.push_back(ID);
} else {
auto& v = online_sim.sim.lines;
v.erase(std::remove_if(v.begin(), v.end(), [ID](line_type const &l){ return l.information == ID; }), v.end());
auto& v2 = online_sim.lines;
v2.erase(std::remove_if(v2.begin(), v2.end(), [ID](AbstractLine const &l){ return l.ID == ID; }), v2.end());
}
}
void add_line(AbstractLine const & line){
online_sim.lines.push_back(line);
for(auto & l : line.calculate_lines()){

30
src/app_common.h

@ -10,7 +10,7 @@ using Vec2 = math::Vec2;
using LineKind = simulation::LineKind;
using ball_info = int;
using line_info = void;
using line_info = int;
using ball_type = simulation::Ball<ball_info>;
using line_type = simulation::Line<line_info>;
using simu_type = simulation::Simulation<ball_info, line_info>;
@ -19,18 +19,24 @@ using note_info = Vec2;
using note_type = Note<note_info>;
using beat_type = Beat<note_info>;
// Pretty unuseful in server
struct AbstractLine {
math::Vec2 starting_point;
math::Vec2 end_point;
int ID;
int line_kind;
const float hsize = 5.0f;
static constexpr float hsize = 5.0f;
AbstractLine() = default;
AbstractLine(AbstractLine const&) = default;
AbstractLine& operator=(AbstractLine const&) = default;
AbstractLine(math::Vec2 starting_point, math::Vec2 end_point, int line_kind)
AbstractLine(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)
{}
@ -42,16 +48,16 @@ struct AbstractLine {
if(line_kind == simulation::kOneWay){
std::vector<line_type> ret;
ret.emplace_back(starting_point + hsize*normal, end_point + hsize*normal, lk);
ret.emplace_back(end_point + hsize*normal, end_point + hsize*dir, lk);
ret.emplace_back(end_point + hsize*dir, end_point - hsize*normal, lk);
ret.emplace_back(end_point - hsize*normal, starting_point - hsize*normal, lk);
ret.emplace_back(starting_point - hsize*normal, starting_point - hsize*dir, lk);
ret.emplace_back(starting_point - hsize*dir, starting_point + hsize*normal, lk);
ret.emplace_back(starting_point + hsize*normal, end_point + hsize*normal, lk, ID);
ret.emplace_back(end_point + hsize*normal, end_point + hsize*dir, lk, ID);
ret.emplace_back(end_point + hsize*dir, end_point - hsize*normal, lk, ID);
ret.emplace_back(end_point - hsize*normal, starting_point - hsize*normal, lk, ID);
ret.emplace_back(starting_point - hsize*normal, starting_point - hsize*dir, lk, ID);
ret.emplace_back(starting_point - hsize*dir, starting_point + hsize*normal, lk, ID);
return ret;
} else {
std::vector<line_type> ret;
ret.emplace_back(starting_point, end_point, lk);
ret.emplace_back(starting_point, end_point, lk, ID);
return ret;
}
}
@ -61,6 +67,7 @@ BOOST_FUSION_ADAPT_STRUCT(
AbstractLine,
(::math::Vec2, starting_point)
(::math::Vec2, end_point)
(int, ID)
(int, line_kind)
)
@ -94,12 +101,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(AbstractLine const & l)
: starting_point(l.starting_point)
, end_point(l.end_point)
, ID(l.ID)
, line_kind(l.line_kind)
{}
};
@ -108,5 +117,6 @@ BOOST_FUSION_ADAPT_STRUCT(
cheap_line_type,
(IntVec2, starting_point)
(IntVec2, end_point)
(int, ID)
(int, line_kind)
)

25
src/main.cpp

@ -55,6 +55,9 @@ websockets::TestProtocol<User> default_protocol{
app->add_line(from_json<cheap_line_type>(object["data"]));
std::cout << "Line from: " << &user << ": " << user.name << std::endl;
request_write(binfo);
} else if (command == "remove line"){
app->remove_line(from_json<int>(object["data"]));
request_write(binfo);
} else if (command == "update user") {
user = from_json<IncomingPacket>(object["data"]);
std::cout << "Updated user: " << &user << ": " << user.name << std::endl;
@ -80,11 +83,21 @@ websockets::TestProtocol<Empty> uberclient_protocol{
},
// write (will always come after receive)
[](Empty& user, basic_websocket_info) -> std::string{
if(app->offline_sim.lines_to_add.empty()) return "";
auto it = app->offline_sim.lines_to_add.begin();
auto str = write_json(to_json(*it));
app->offline_sim.lines_to_add.erase(it);
return str;
if(!app->offline_sim.lines_to_remove.empty()){
js::Object ret;
ret["command"] = "remove lines";
ret["data"] = vector_to_json(app->offline_sim.lines_to_remove);
app->offline_sim.lines_to_remove.clear();
return write_json(ret);
}
if(!app->offline_sim.lines_to_add.empty()){
js::Object ret;
ret["command"] = "add lines";
ret["data"] = vector_to_json(app->offline_sim.lines_to_add);
app->offline_sim.lines_to_add.clear();
return write_json(ret);
}
return "";
},
// receive
[](Empty& user, std::string in, basic_websocket_info binfo){
@ -105,7 +118,7 @@ websockets::TestProtocol<Empty> uberclient_protocol{
}
}
if(!app->offline_sim.lines_to_add.empty()){
if(!app->offline_sim.lines_to_add.empty() || !app->offline_sim.lines_to_remove.empty()){
request_write(binfo);
}
}