controlObjectComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/components/game/controlObjectComponent.h"
  23. //////////////////////////////////////////////////////////////////////////
  24. // Constructor/Destructor
  25. //////////////////////////////////////////////////////////////////////////
  26. ControlObjectComponent::ControlObjectComponent() : Component(),
  27. mOwnerConnection(nullptr),
  28. mOwnerConnectionId(0)
  29. {
  30. mFriendlyName = "Control Object";
  31. mComponentType = "Game";
  32. mDescription = getDescriptionText("Allows owner entity to be controlled by a client.");
  33. }
  34. ControlObjectComponent::~ControlObjectComponent()
  35. {
  36. }
  37. IMPLEMENT_CO_NETOBJECT_V1(ControlObjectComponent);
  38. bool ControlObjectComponent::onAdd()
  39. {
  40. if (!Parent::onAdd())
  41. return false;
  42. return true;
  43. }
  44. void ControlObjectComponent::onRemove()
  45. {
  46. Parent::onRemove();
  47. }
  48. //This is mostly a catch for situations where the behavior is re-added to the object and the like and we may need to force an update to the behavior
  49. void ControlObjectComponent::onComponentAdd()
  50. {
  51. Parent::onComponentAdd();
  52. if (mOwnerConnection && mOwnerConnection->getControlObject() == nullptr)
  53. {
  54. mOwnerConnection->setControlObject(mOwner);
  55. mOwnerConnectionId = mOwnerConnection->getId();
  56. }
  57. }
  58. void ControlObjectComponent::onComponentRemove()
  59. {
  60. Parent::onComponentRemove();
  61. if (mOwnerConnection)
  62. {
  63. mOwnerConnection->setControlObject(nullptr);
  64. mOwnerConnectionId = 0;
  65. }
  66. }
  67. void ControlObjectComponent::initPersistFields()
  68. {
  69. Parent::initPersistFields();
  70. addField("clientOwner", TypeS32, Offset(mOwnerConnectionId, ControlObjectComponent), "Client connection ID");
  71. }
  72. void ControlObjectComponent::setConnectionControlObject(GameConnection* conn)
  73. {
  74. if (conn)
  75. {
  76. if (conn->getControlObject() == nullptr)
  77. {
  78. conn->setControlObject(mOwner);
  79. mOwnerConnectionId = conn->getId();
  80. }
  81. else
  82. {
  83. //Inform the old control object it's no longer in control?
  84. conn->setControlObject(mOwner);
  85. mOwnerConnectionId = conn->getId();
  86. }
  87. }
  88. }
  89. void ControlObjectComponent::onClientConnect(GameConnection* conn)
  90. {
  91. if (conn && conn->getControlObject() == nullptr)
  92. {
  93. conn->setControlObject(mOwner);
  94. mOwnerConnectionId = conn->getId();
  95. }
  96. }
  97. void ControlObjectComponent::onClientDisconnect(GameConnection* conn)
  98. {
  99. if (conn && conn->getControlObject() == mOwner)
  100. {
  101. conn->setControlObject(nullptr);
  102. mOwnerConnectionId = 0;
  103. }
  104. }
  105. DefineEngineMethod(ControlObjectComponent, onClientConnect, void, (GameConnection* conn), (nullAsType<GameConnection*>()),
  106. "Triggers a signal call to all components for a certain function.")
  107. {
  108. if (conn == nullptr)
  109. return;
  110. object->onClientConnect(conn);
  111. }
  112. DefineEngineMethod(ControlObjectComponent, onClientDisconnect, void, (GameConnection* conn), (nullAsType<GameConnection*>()),
  113. "Triggers a signal call to all components for a certain function.")
  114. {
  115. if (conn == nullptr)
  116. return;
  117. object->onClientDisconnect(conn);
  118. }
  119. DefineEngineMethod(ControlObjectComponent, setConnectionControlObject, void, (GameConnection* conn), (nullAsType<GameConnection*>()),
  120. "Triggers a signal call to all components for a certain function.")
  121. {
  122. if (conn == nullptr)
  123. return;
  124. object->setConnectionControlObject(conn);
  125. }