clientobject.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes_extrabloated.h"
  18. #include "activeobject.h"
  19. #include <memory>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. class ClientEnvironment;
  23. class ITextureSource;
  24. class Client;
  25. class IGameDef;
  26. class LocalPlayer;
  27. struct ItemStack;
  28. class WieldMeshSceneNode;
  29. class ClientActiveObject : public ActiveObject
  30. {
  31. public:
  32. ClientActiveObject(u16 id, Client *client, ClientEnvironment *env);
  33. virtual ~ClientActiveObject();
  34. virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) = 0;
  35. virtual void removeFromScene(bool permanent) {}
  36. virtual void updateLight(u32 day_night_ratio) {}
  37. virtual bool getCollisionBox(aabb3f *toset) const { return false; }
  38. virtual bool getSelectionBox(aabb3f *toset) const { return false; }
  39. virtual bool collideWithObjects() const { return false; }
  40. virtual const v3f getPosition() const { return v3f(0.0f); } // in BS-space
  41. virtual const v3f getVelocity() const { return v3f(0.0f); } // in BS-space
  42. virtual scene::ISceneNode *getSceneNode() const
  43. { return NULL; }
  44. virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const
  45. { return NULL; }
  46. virtual bool isLocalPlayer() const { return false; }
  47. virtual ClientActiveObject *getParent() const { return nullptr; };
  48. virtual const std::unordered_set<int> &getAttachmentChildIds() const
  49. { static std::unordered_set<int> rv; return rv; }
  50. virtual void updateAttachments() {};
  51. virtual bool doShowSelectionBox() { return true; }
  52. // Step object in time
  53. virtual void step(float dtime, ClientEnvironment *env) {}
  54. // Process a message sent by the server side object
  55. virtual void processMessage(const std::string &data) {}
  56. virtual std::string infoText() { return ""; }
  57. virtual std::string debugInfoText() { return ""; }
  58. /*
  59. This takes the return value of
  60. ServerActiveObject::getClientInitializationData
  61. */
  62. virtual void initialize(const std::string &data) {}
  63. // Create a certain type of ClientActiveObject
  64. static std::unique_ptr<ClientActiveObject> create(ActiveObjectType type,
  65. Client *client, ClientEnvironment *env);
  66. // If returns true, punch will not be sent to the server
  67. virtual bool directReportPunch(v3f dir, const ItemStack *punchitem = nullptr,
  68. float time_from_last_punch = 1000000) { return false; }
  69. protected:
  70. // Used for creating objects based on type
  71. typedef std::unique_ptr<ClientActiveObject> (*Factory)(Client *client, ClientEnvironment *env);
  72. static void registerType(u16 type, Factory f);
  73. Client *m_client;
  74. ClientEnvironment *m_env;
  75. private:
  76. // Used for creating objects based on type
  77. static std::unordered_map<u16, Factory> m_types;
  78. };
  79. class DistanceSortedActiveObject
  80. {
  81. public:
  82. ClientActiveObject *obj;
  83. DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
  84. {
  85. obj = a_obj;
  86. d = a_d;
  87. }
  88. bool operator < (const DistanceSortedActiveObject &other) const
  89. {
  90. return d < other.d;
  91. }
  92. private:
  93. f32 d;
  94. };