Only send balls which are visible
This commit is contained in:
parent
39ed1414d0
commit
7645bdbe3a
3 changed files with 22 additions and 6 deletions
|
@ -32,6 +32,14 @@ struct User {
|
||||||
Size size{0,0};
|
Size size{0,0};
|
||||||
Position position{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){
|
User& operator=(IncomingPacket const & p){
|
||||||
name = p.name;
|
name = p.name;
|
||||||
size = p.size;
|
size = p.size;
|
||||||
|
|
|
@ -4,16 +4,24 @@
|
||||||
|
|
||||||
struct Identity{
|
struct Identity{
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T operator()(T t){
|
T operator()(T t) const {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename C, typename F = Identity>
|
struct All{
|
||||||
inline js::Value vector_to_json(C const & container, F f = Identity()){
|
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;
|
js::Array array;
|
||||||
for(auto x : container){
|
for(auto x : container){
|
||||||
array.push_back(to_json(f(x)));
|
if(!filter(x)) continue;
|
||||||
|
array.push_back(to_json(transform(x)));
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,9 @@ websockets::TestProtocol<User> default_protocol{
|
||||||
user.update_lines = false;
|
user.update_lines = false;
|
||||||
} else {
|
} else {
|
||||||
if(app->uberclient){
|
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 {
|
} 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);
|
return write_json(ret);
|
||||||
|
|
Reference in a new issue