Component.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Node.h"
  25. #include "ReplicationUtils.h"
  26. #include "ResourceCache.h"
  27. #include "Scene.h"
  28. #include "DebugNew.h"
  29. Component::Component(const std::string& name) :
  30. mEntity(0),
  31. mName(name),
  32. mNameHash(name),
  33. mNetFlags(NET_SYNCTOALL)
  34. {
  35. }
  36. void Component::save(Serializer& dest)
  37. {
  38. // Write type hash & name
  39. dest.writeShortStringHash(getType());
  40. dest.writeString(mName);
  41. // Write netflags
  42. dest.writeUByte(mNetFlags);
  43. }
  44. void Component::load(Deserializer& source, ResourceCache* cache)
  45. {
  46. // Type and name are handled on the Entity level
  47. // Read netflags
  48. mNetFlags = source.readUByte();
  49. }
  50. void Component::saveXML(XMLElement& dest)
  51. {
  52. // Write typename & name
  53. dest.setString("type", getTypeName());
  54. if (!mName.empty())
  55. dest.setString("name", mName);
  56. // Write netflags
  57. dest.setInt("netflags", mNetFlags);
  58. }
  59. void Component::loadXML(const XMLElement& source, ResourceCache* cache)
  60. {
  61. // Type and name are handled on the Entity level
  62. // Read netflags
  63. mNetFlags = source.getInt("netflags");
  64. }
  65. void Component::postLoad(ResourceCache* cache)
  66. {
  67. }
  68. bool Component::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
  69. {
  70. return false; // No data to write
  71. }
  72. void Component::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  73. {
  74. }
  75. void Component::postNetUpdate(ResourceCache* cache)
  76. {
  77. }
  78. void Component::interpolate(bool snapToEnd)
  79. {
  80. }
  81. void Component::getComponentRefs(std::vector<ComponentRef>& dest)
  82. {
  83. }
  84. void Component::setName(const std::string& name)
  85. {
  86. mName = name;
  87. mNameHash = StringHash(name);
  88. }
  89. void Component::setNetFlags(unsigned char flags)
  90. {
  91. // Respect the authority and proxy flags if they have been set already
  92. if (mNetFlags & NET_MODEFLAGS)
  93. mNetFlags = (mNetFlags & NET_MODEFLAGS) | (flags & ~NET_MODEFLAGS);
  94. else
  95. mNetFlags = flags;
  96. }
  97. bool Component::isPlayback() const
  98. {
  99. if (!mEntity)
  100. return false;
  101. return mEntity->isPlayback();
  102. }
  103. bool Component::checkSync(Connection* connection) const
  104. {
  105. if (!mEntity)
  106. return false;
  107. if ((mNetFlags & NET_SYNCTONONE) || (mEntity->getNetFlags() & NET_SYNCTONONE))
  108. return false;
  109. if ((mNetFlags & NET_SYNCTOOWNER) || (mEntity->getNetFlags() & NET_SYNCTOOWNER))
  110. return mEntity->getOwner() == connection;
  111. return true;
  112. }
  113. bool Component::checkPrediction(Connection* connection) const
  114. {
  115. if ((!mEntity) || (!(mNetFlags & NET_PREDICTIONFLAGS)))
  116. return false;
  117. return mEntity->checkPrediction(connection);
  118. }
  119. ComponentRef::ComponentRef(Component* component, bool forXML) :
  120. mEntityID(0),
  121. mDirty(false)
  122. {
  123. if (!component)
  124. return;
  125. if (component->getEntity())
  126. {
  127. mEntityID = component->getEntity()->getID();
  128. mHash = component->getCombinedHash();
  129. }
  130. else
  131. {
  132. // The component does not directly belong to an entity. However, if it is a scene node, check if any of the components
  133. // in the parent chain belongs to an entity
  134. Node* node = dynamic_cast<Node*>(component);
  135. while (node)
  136. {
  137. node = node->getParent();
  138. if ((node) && (node->getEntity()))
  139. {
  140. mEntityID = node->getEntity()->getID();
  141. mHash = component->getCombinedHash();
  142. break;
  143. }
  144. }
  145. }
  146. if (forXML)
  147. {
  148. mTypeName = component->getTypeName();
  149. mName = component->getName();
  150. }
  151. }
  152. void ComponentRef::write(Serializer& dest) const
  153. {
  154. dest.writeUInt(mEntityID);
  155. if (mEntityID)
  156. dest.writeShortStringHash(mHash);
  157. }
  158. void ComponentRef::read(Deserializer& source)
  159. {
  160. mEntityID = source.readUInt();
  161. if (mEntityID)
  162. mHash = source.readShortStringHash();
  163. else
  164. mHash = ShortStringHash();
  165. mDirty = true;
  166. }
  167. void ComponentRef::writePacked(Serializer& dest) const
  168. {
  169. dest.writeUShort(mEntityID);
  170. if (mEntityID)
  171. dest.writeShortStringHash(mHash);
  172. }
  173. void ComponentRef::readPacked(Deserializer& source)
  174. {
  175. mEntityID = source.readUShort();
  176. if (mEntityID)
  177. mHash = source.readShortStringHash();
  178. else
  179. mHash = ShortStringHash();
  180. mDirty = true;
  181. }
  182. void ComponentRef::writeXML(XMLElement& dest) const
  183. {
  184. if ((mHash.mData) && (mTypeName.empty()))
  185. SAFE_EXCEPTION("Component reference was not initialized with typename and name information");
  186. dest.setInt("id", mEntityID);
  187. dest.setString("type", mTypeName);
  188. if (!mName.empty())
  189. dest.setString("name", mName);
  190. }
  191. void ComponentRef::readXML(const XMLElement& source)
  192. {
  193. if (source)
  194. {
  195. mEntityID = source.getInt("id");
  196. mHash = ShortStringHash(source.getString("type")) + ShortStringHash(source.getString("name"));
  197. }
  198. else
  199. {
  200. mEntityID = 0;
  201. mHash = ShortStringHash();
  202. }
  203. mDirty = true;
  204. }