Component.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Component.h"
  25. #include "Context.h"
  26. #include "Node.h"
  27. #include "ReplicationState.h"
  28. #include "XMLElement.h"
  29. #include "DebugNew.h"
  30. OBJECTTYPESTATIC(Component);
  31. Component::Component(Context* context) :
  32. Serializable(context),
  33. id_(0),
  34. node_(0)
  35. {
  36. }
  37. Component::~Component()
  38. {
  39. }
  40. bool Component::Save(Serializer& dest)
  41. {
  42. // Write type and ID
  43. if (!dest.WriteShortStringHash(GetType()))
  44. return false;
  45. if (!dest.WriteUInt(id_))
  46. return false;
  47. // Write attributes
  48. return Serializable::Save(dest);
  49. }
  50. bool Component::SaveXML(XMLElement& dest)
  51. {
  52. // Write type and ID
  53. if (!dest.SetString("type", GetTypeName()))
  54. return false;
  55. if (!dest.SetInt("id", id_))
  56. return false;
  57. // Write attributes
  58. return Serializable::SaveXML(dest);
  59. }
  60. void Component::Remove()
  61. {
  62. if (node_)
  63. node_->RemoveComponent(this);
  64. }
  65. const Matrix3x4& Component::GetWorldTransform() const
  66. {
  67. return node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  68. }
  69. void Component::AddReplicationState(ComponentReplicationState* state)
  70. {
  71. replicationStates_.Push(state);
  72. }
  73. void Component::PrepareNetworkUpdate()
  74. {
  75. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  76. if (!attributes)
  77. return;
  78. unsigned numAttributes = attributes->Size();
  79. if (currentState_.Size() != numAttributes)
  80. {
  81. currentState_.Resize(numAttributes);
  82. previousState_.Resize(numAttributes);
  83. // Copy the default attribute values to the previous state as a starting point
  84. for (unsigned i = 0; i < numAttributes; ++i)
  85. previousState_[i] = attributes->At(i).defaultValue_;
  86. }
  87. // Check for attribute changes
  88. for (unsigned i = 0; i < numAttributes; ++i)
  89. {
  90. const AttributeInfo& attr = attributes->At(i);
  91. OnGetAttribute(attr, currentState_[i]);
  92. if (currentState_[i] != previousState_[i])
  93. {
  94. previousState_[i] = currentState_[i];
  95. // Mark the attribute dirty in all replication states that are tracking this component
  96. for (PODVector<ComponentReplicationState*>::Iterator j = replicationStates_.Begin(); j != replicationStates_.End(); ++j)
  97. {
  98. (*j)->dirtyAttributes_.Set(i);
  99. // Add component's parent node to the dirty set if not added yet
  100. if (!(*j)->nodeState_->markedDirty_)
  101. {
  102. (*j)->nodeState_->markedDirty_ = true;
  103. (*j)->nodeState_->sceneState_->dirtyNodes_.Insert(node_->GetID());
  104. }
  105. }
  106. }
  107. }
  108. }
  109. void Component::CleanupConnection(Connection* connection)
  110. {
  111. for (unsigned i = replicationStates_.Size() - 1; i < replicationStates_.Size(); --i)
  112. {
  113. if (replicationStates_[i]->connection_ == connection)
  114. replicationStates_.Erase(i);
  115. }
  116. }
  117. void Component::SetID(unsigned id)
  118. {
  119. id_ = id;
  120. }
  121. void Component::SetNode(Node* node)
  122. {
  123. node_ = node;
  124. OnNodeSet(node_);
  125. }
  126. Component* Component::GetComponent(ShortStringHash type) const
  127. {
  128. return node_ ? node_->GetComponent(type) : 0;
  129. }
  130. void Component::GetComponents(PODVector<Component*>& dest, ShortStringHash type) const
  131. {
  132. if (node_)
  133. node_->GetComponents(dest, type);
  134. else
  135. dest.Clear();
  136. }