followBehaviorAction.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Guy Allard
  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 "followBehaviorAction.h"
  23. #include "T3D/aiPlayer.h"
  24. using namespace BadBehavior;
  25. //------------------------------------------------------------------------------
  26. // FollowBehavior node
  27. //------------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT(FollowBehaviorAction);
  29. FollowBehaviorAction::FollowBehaviorAction()
  30. {
  31. }
  32. bool FollowBehaviorAction::precondition( SimObject *owner )
  33. {
  34. PROFILE_SCOPE( FollowBehaviorAction_precondition);
  35. // check that our owner is an AIPlayer
  36. AIPlayer *aiPlayer = dynamic_cast<AIPlayer *>(owner);
  37. if(!aiPlayer)
  38. return false;
  39. return true;
  40. }
  41. void FollowBehaviorAction::onEnter( SimObject *owner )
  42. {
  43. //PROFILE_SCOPE( FollowBehaviorAction_onEnter );
  44. }
  45. void FollowBehaviorAction::onExit( SimObject *owner )
  46. {
  47. //PROFILE_SCOPE( FollowBehaviorAction_onExit );
  48. }
  49. Status FollowBehaviorAction::behavior( SimObject *owner )
  50. {
  51. PROFILE_SCOPE( FollowBehaviorAction_behavior );
  52. // get the owning AIPlayer object
  53. AIPlayer *aiPlayer = dynamic_cast<AIPlayer *>(owner);
  54. if(!aiPlayer)
  55. return FAILURE;
  56. // get the script-specified followObject
  57. const char *followFieldName = StringTable->insert("followObject");
  58. const char *followFieldValue = owner->getDataField(followFieldName, NULL);
  59. if(!followFieldValue || !followFieldValue[0])
  60. return FAILURE;
  61. SimObject *followSimObj = Sim::findObject(dAtoi(followFieldValue));
  62. if(!followSimObj)
  63. return FAILURE;
  64. // check that the follow object is a SceneObject
  65. SceneObject *followObject = dynamic_cast<SceneObject *>(followSimObj);
  66. if(!followObject)
  67. return FAILURE;
  68. // get the script-specified followDistance field
  69. const char *distanceFieldName = StringTable->insert("followDistance");
  70. const char *distanceFieldValue = owner->getDataField(distanceFieldName, NULL);
  71. float followDistance = 1.f;
  72. if(distanceFieldValue && distanceFieldValue[0])
  73. followDistance = dAtof(distanceFieldValue);
  74. // try and stay at followDistance from the followObject
  75. Point3F targetPos = followObject->getPosition();
  76. Point3F followVec = aiPlayer->getPosition() - targetPos;
  77. // get current distance (ignore z component)
  78. F32 curDist = Point3F(followVec.x, followVec.y, 0.f).len();
  79. if(mFabs(curDist - followDistance) > aiPlayer->getMoveTolerance())
  80. {
  81. followVec.normalize();
  82. followVec *= followDistance;
  83. Point3F destination = targetPos + followVec;
  84. aiPlayer->setMoveDestination(destination, true);
  85. return RUNNING;
  86. }
  87. return SUCCESS;
  88. }