Net Object.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /******************************************************************************/
  2. namespace Net{
  3. /******************************************************************************/
  4. struct Neighbor // Net Object Neighbor
  5. {
  6. Bool direct_connection; // if the object and the neighbor share a separate external direct connection between each other, which means that they can exchange data directly between each other and not involving the server
  7. Obj& obj() {return *_obj;} // get neighbor object
  8. Neighbor() {direct_connection=false; _obj=null;}
  9. #if !EE_PRIVATE
  10. private:
  11. #endif
  12. Obj *_obj;
  13. };
  14. /******************************************************************************/
  15. struct Obj // Net Object
  16. {
  17. // get
  18. virtual Obj& pos(C Vec &pos); virtual C Vec& pos () {return _pos ;} // set/get object position
  19. World* world() {return _world;} // get object world
  20. Int neighbors( )C {return _neighbors.elms();} // get number of neighbors
  21. Neighbor& neighbor (Int i) {return _neighbors[i] ;} // get i-th neighbor
  22. Bool inGame()C {return _area!=null;} // if object is currently in game
  23. // set
  24. void setWorld(World *world, C Vec &pos); // does not automatically enters the game
  25. // operations
  26. void enterGame(); // this adds the object to a world area, and detects nearby objects and stores them as neighbors, in order to enter the game, the object must have a world set
  27. void leaveGame(); // this disconnects from all neighbors and removed object from world area (but world pointer is unchanged)
  28. #if EE_PRIVATE
  29. void removeFromArea();
  30. void putToArea(Area &area);
  31. void removeNeighbors();
  32. void updateNeighbors();
  33. #endif
  34. // callbacks
  35. virtual void disconnected(Obj &obj) {} // called when we're being disconnected from 'obj', most likely because objects got too far away from each other
  36. virtual void connected(Obj &obj) {} // called when we're being connected with 'obj', most likely because objects got close to each other
  37. // network
  38. void compress(File &f, C StrLibrary *worlds=null); // compress data so it can be sent using network connection
  39. void decompress(File &f, C StrLibrary *worlds=null); // decompress data from data obtained using network connection, 'worlds'=must point to a StrLibrary with same data as the one which was used during 'compress' (or null if none was used)
  40. // io
  41. Bool save(File &f); // save, false on fail
  42. Bool load(File &f); // load, false on fail
  43. Obj();
  44. #if !EE_PRIVATE
  45. private:
  46. #endif
  47. Vec _pos, _old_pos;
  48. Area *_area;
  49. World *_world;
  50. Memc<Neighbor> _neighbors;
  51. NO_COPY_CONSTRUCTOR(Obj);
  52. };
  53. /******************************************************************************/
  54. } // namespace
  55. /******************************************************************************/