Wait.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "Wait.h"
  23. #include "BadBehavior/core/Runner.h"
  24. #include "math/mMathFn.h"
  25. #include "console/consoleTypes.h"
  26. using namespace BadBehavior;
  27. //------------------------------------------------------------------------------
  28. // Wait leaf node
  29. //------------------------------------------------------------------------------
  30. IMPLEMENT_CONOBJECT(Wait);
  31. Wait::Wait()
  32. : mWaitMs(1000)
  33. {
  34. }
  35. void Wait::initPersistFields()
  36. {
  37. addGroup( "Behavior" );
  38. addProtectedField( "waitMs", TypeS32, Offset(mWaitMs, Wait), &_setWait, &defaultProtectedGetFn,
  39. "The time in ms that the node should wait before completion." );
  40. endGroup( "Behavior" );
  41. Parent::initPersistFields();
  42. }
  43. bool Wait::_setWait(void *object, const char *index, const char *data)
  44. {
  45. Wait *node = static_cast<Wait *>( object );
  46. node->mWaitMs = getMax(0, dAtoi( data ));
  47. return false;
  48. }
  49. Task *Wait::createTask(SimObject &owner, BehaviorTreeRunner &runner)
  50. {
  51. return new WaitTask(*this, owner, runner);
  52. }
  53. //------------------------------------------------------------------------------
  54. // Wait task
  55. //------------------------------------------------------------------------------
  56. WaitTask::WaitTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
  57. : Parent(node, owner, runner),
  58. mEventId(0)
  59. {
  60. }
  61. WaitTask::~WaitTask()
  62. {
  63. cancelEvent();
  64. }
  65. void WaitTask::cancelEvent()
  66. {
  67. if(Sim::isEventPending(mEventId))
  68. {
  69. Sim::cancelEvent(mEventId);
  70. mEventId = 0;
  71. }
  72. }
  73. void WaitTask::onInitialize()
  74. {
  75. Parent::onInitialize();
  76. cancelEvent();
  77. }
  78. void WaitTask::onTerminate()
  79. {
  80. Parent::onTerminate();
  81. cancelEvent();
  82. }
  83. Task* WaitTask::update()
  84. {
  85. if(mStatus == RESUME)
  86. {
  87. mStatus = SUCCESS;
  88. mIsComplete = true;
  89. }
  90. else if(mStatus == INVALID)
  91. {
  92. mEventId = Sim::postEvent(mRunner, new TaskReactivateEvent(*this), Sim::getCurrentTime() + static_cast<Wait*>(mNodeRep)->getWaitMs());
  93. mStatus = SUSPENDED;
  94. }
  95. return NULL;
  96. }