WaitForSignal.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef _BB_WAITFORSIGNAL_H_
  23. #define _BB_WAITFORSIGNAL_H_
  24. #ifndef _BB_CORE_H_
  25. #include "BadBehavior/core/Core.h"
  26. #endif
  27. #ifndef _BB_SIGNAL_H_
  28. #include "BadBehavior/core/Signal.h"
  29. #endif
  30. #ifndef _SIMEVENTS_H_
  31. #include "console/simEvents.h"
  32. #endif
  33. namespace BadBehavior
  34. {
  35. //---------------------------------------------------------------------------
  36. // WaitForSignal leaf
  37. // Waits until it receives the requested signal.
  38. //---------------------------------------------------------------------------
  39. class WaitForSignal : public LeafNode
  40. {
  41. typedef LeafNode Parent;
  42. protected:
  43. String mSignalName;
  44. S32 mTimeoutMs;
  45. static bool _setTimeout(void *object, const char *index, const char *data);
  46. public:
  47. WaitForSignal();
  48. virtual Task *createTask(SimObject &owner, BehaviorTreeRunner &runner);
  49. static void initPersistFields();
  50. const String &getSignalName() const { return mSignalName; }
  51. S32 getTimeoutMs() const { return mTimeoutMs; }
  52. DECLARE_CONOBJECT(WaitForSignal);
  53. };
  54. //---------------------------------------------------------------------------
  55. // WaitForSignal leaf task
  56. //---------------------------------------------------------------------------
  57. class WaitForSignalTask : public Task, public virtual SignalSubscriber
  58. {
  59. typedef Task Parent;
  60. protected:
  61. U32 mEventId;
  62. virtual void onInitialize();
  63. virtual void onTerminate();
  64. virtual Task* update();
  65. void cancelEvent();
  66. // SignalSubscriber
  67. virtual void subscribe();
  68. virtual void unsubscribe();
  69. public:
  70. WaitForSignalTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner);
  71. virtual ~WaitForSignalTask();
  72. // SignalSubscriber
  73. virtual void onSignal();
  74. // timeout
  75. void onTimeout();
  76. };
  77. class WaitForSignalTimeoutEvent : public SimEvent
  78. {
  79. WaitForSignalTask *mTask;
  80. public:
  81. WaitForSignalTimeoutEvent(WaitForSignalTask &task)
  82. {
  83. mTask = &task;
  84. }
  85. void process( SimObject *object )
  86. {
  87. mTask->onTimeout();
  88. }
  89. };
  90. } // namespace BadBehavior
  91. #endif