interactComponent.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/interactComponent.h"
  23. #include "scene/sceneContainer.h"
  24. #include "T3D/components/game/interactableComponent.h"
  25. //////////////////////////////////////////////////////////////////////////
  26. // Constructor/Destructor
  27. //////////////////////////////////////////////////////////////////////////
  28. InteractComponent::InteractComponent() : Component(),
  29. mUseRaycastInteract(true),
  30. mUseRenderedRaycast(false),
  31. mUseRadiusInteract(true),
  32. mInteractRadius(1.5),
  33. mInteractRayDist(1.5),
  34. mUseNaturalReach(false)
  35. {
  36. mFriendlyName = "Interact";
  37. mComponentType = "Game";
  38. mDescription = getDescriptionText("Allows owner entity interact.");
  39. }
  40. InteractComponent::~InteractComponent()
  41. {
  42. }
  43. IMPLEMENT_CO_NETOBJECT_V1(InteractComponent);
  44. bool InteractComponent::onAdd()
  45. {
  46. if (!Parent::onAdd())
  47. return false;
  48. return true;
  49. }
  50. void InteractComponent::onRemove()
  51. {
  52. Parent::onRemove();
  53. }
  54. //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
  55. void InteractComponent::onComponentAdd()
  56. {
  57. Parent::onComponentAdd();
  58. }
  59. void InteractComponent::onComponentRemove()
  60. {
  61. Parent::onComponentRemove();
  62. }
  63. void InteractComponent::initPersistFields()
  64. {
  65. Parent::initPersistFields();
  66. }
  67. void InteractComponent::processTick()
  68. {
  69. if (isClientObject())
  70. return; //this shouldn't happen
  71. //First, if we're doing rays, try that as if we get a hit, we can always assume that's the "correct" option
  72. if (mUseRaycastInteract)
  73. {
  74. mOwner->disableCollision();
  75. Point3F start = mOwner->getPosition();
  76. Point3F end = mOwner->getTransform().getForwardVector() * mInteractRayDist + start;
  77. RayInfo rinfo;
  78. S32 ret = 0;
  79. if (mUseRenderedRaycast)
  80. {
  81. rinfo.generateTexCoord = true;
  82. if (gServerContainer.castRayRendered(mOwner->getPosition(), end, EntityObjectType, &rinfo) == true)
  83. ret = rinfo.object->getId();
  84. }
  85. else
  86. {
  87. if (gServerContainer.castRay(mOwner->getPosition(), end, EntityObjectType, &rinfo) == true)
  88. ret = rinfo.object->getId();
  89. }
  90. mOwner->enableCollision();
  91. Entity* hitEntity = static_cast<Entity*>(rinfo.object);
  92. if (hitEntity)
  93. {
  94. //call on that badboy!
  95. InteractableComponent* iComp = hitEntity->getComponent<InteractableComponent>();
  96. if (iComp)
  97. {
  98. iComp->interact(this, rinfo);
  99. return;
  100. }
  101. }
  102. }
  103. //If not using rays or no hit, then do the radius search if we have it
  104. if (mUseRadiusInteract)
  105. {
  106. gServerContainer.initRadiusSearch(mOwner->getPosition(), mInteractRadius, EntityObjectType);
  107. F32 lastBestDist = 9999;
  108. F32 lastBestWeight = 0;
  109. Entity* bestFitEntity = nullptr;
  110. Entity* e = static_cast<Entity*>(gServerContainer.containerSearchNextObject());
  111. while (e != nullptr)
  112. {
  113. InteractableComponent* iComp = e->getComponent<InteractableComponent>();
  114. if (iComp != nullptr)
  115. {
  116. F32 weight = iComp->getWeight();
  117. VectorF distVec = e->getPosition() - mOwner->getPosition();
  118. F32 dist = distVec.len();
  119. //If the weight is better, always pick it
  120. if (weight > lastBestWeight)
  121. {
  122. lastBestDist = dist;
  123. lastBestWeight = weight;
  124. bestFitEntity = e;
  125. }
  126. //Otherwise, if the weight is matched and the distance is closer, pick that
  127. else if (weight >= lastBestWeight && lastBestDist < dist)
  128. {
  129. lastBestWeight = weight;
  130. bestFitEntity = e;
  131. }
  132. }
  133. e = static_cast<Entity*>(gServerContainer.containerSearchNextObject()); //loop 'round
  134. }
  135. if (bestFitEntity)
  136. {
  137. //call on that badboy!
  138. InteractableComponent* iComp = bestFitEntity->getComponent<InteractableComponent>();
  139. iComp->interact(this);
  140. }
  141. }
  142. }