Browse Source

Only send balls which are visible

master
Joshua Moerman 12 years ago
parent
commit
7645bdbe3a
  1. 8
      src/basics.h
  2. 16
      src/json_ext.h
  3. 4
      src/main.cpp

8
src/basics.h

@ -32,6 +32,14 @@ struct User {
Size size{0,0};
Position position{0,0};
template <typename Ball>
bool is_inside(Ball b){
float x = b.position.x;
float y = b.position.y;
return position.x < x && x < position.x + size.width
&& position.y < y && y < position.y + size.height;
}
User& operator=(IncomingPacket const & p){
name = p.name;
size = p.size;

16
src/json_ext.h

@ -4,16 +4,24 @@
struct Identity{
template <typename T>
T operator()(T t){
T operator()(T t) const {
return t;
}
};
template <typename C, typename F = Identity>
inline js::Value vector_to_json(C const & container, F f = Identity()){
struct All{
template <typename T>
bool operator()(T const & t) const {
return true;
}
};
template <typename C, typename F1 = Identity, typename F2 = All>
inline js::Value vector_to_json(C const & container, F1 const & transform = Identity(), F2 const & filter = All()){
js::Array array;
for(auto x : container){
array.push_back(to_json(f(x)));
if(!filter(x)) continue;
array.push_back(to_json(transform(x)));
}
return array;
}

4
src/main.cpp

@ -39,9 +39,9 @@ websockets::TestProtocol<User> default_protocol{
user.update_lines = false;
} else {
if(app->uberclient){
ret["balls"] = vector_to_json(app->offline_sim.balls);
ret["balls"] = vector_to_json(app->offline_sim.balls, Identity(), [&user](cheap_ball_type const & b){ return user.is_inside(b); });
} else {
ret["balls"] = vector_to_json(app->online_sim.sim.balls, [](ball_type const & b){return cheap_ball_type(b);});
ret["balls"] = vector_to_json(app->online_sim.sim.balls, [](ball_type const & b){return cheap_ball_type(b);}, [&user](ball_type const & b){ return user.is_inside(b); });
}
}
return write_json(ret);