behaviorComponent.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _BEHAVIOR_COMPONENT_H_
  23. #define _BEHAVIOR_COMPONENT_H_
  24. #ifndef _DYNAMIC_CONSOLEMETHOD_COMPONENT_H_
  25. #include "component/dynamicConsoleMethodComponent.h"
  26. #endif
  27. #ifndef _BEHAVIORINSTANCE_H_
  28. #include "behaviorInstance.h"
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. class BehaviorComponent : public DynamicConsoleMethodComponent
  32. {
  33. friend class BehaviorInterface;
  34. typedef DynamicConsoleMethodComponent Parent;
  35. private:
  36. /// Component Behaviors
  37. SimSet mBehaviors;
  38. /// Master behavior Id.
  39. U32 mMasterBehaviorId;
  40. Vector<StringTableEntry>* mpBehaviorFieldNames;
  41. public:
  42. /// A behavior port connection.
  43. struct BehaviorPortConnection
  44. {
  45. BehaviorPortConnection(
  46. BehaviorInstance* pOutputBehavior,
  47. BehaviorInstance* pInputBehavior,
  48. StringTableEntry pOutputName,
  49. StringTableEntry pInputName )
  50. {
  51. mOutputInstance = pOutputBehavior;
  52. mInputInstance = pInputBehavior;
  53. mOutputName = pOutputName;
  54. mInputName = pInputName;
  55. }
  56. BehaviorInstance* mOutputInstance;
  57. BehaviorInstance* mInputInstance;
  58. StringTableEntry mOutputName;
  59. StringTableEntry mInputName;
  60. };
  61. /// Behavior connection map.
  62. /// NOTE: This configuration provides more efficient raising of outputs as opposed to general administration.
  63. typedef Vector<BehaviorPortConnection> typePortConnectionVector;
  64. typedef HashMap<StringTableEntry, typePortConnectionVector*> typeOutputNameConnectionHash;
  65. typedef HashMap<SimObjectId, typeOutputNameConnectionHash*> typeInstanceConnectionHash;
  66. typeInstanceConnectionHash mBehaviorConnections;
  67. protected:
  68. virtual const char* _callMethod( U32 argc, const char *argv[], bool callThis = true );
  69. /// Taml callbacks.
  70. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  71. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  72. private:
  73. void destroyBehaviorOutputConnections( BehaviorInstance* pOutputBehavior );
  74. void destroyBehaviorInputConnections( BehaviorInstance* pInputBehavior );
  75. public:
  76. BehaviorComponent();
  77. virtual ~BehaviorComponent() {}
  78. /// SimObject overrides
  79. virtual bool onAdd();
  80. virtual void onRemove();
  81. virtual void onDeleteNotify( SimObject *object );
  82. virtual void copyTo(SimObject* object);
  83. /// Behavior interface.
  84. BehaviorInstance* getBehaviorByInstanceId( const U32 behaviorId );
  85. virtual bool addBehavior( BehaviorInstance *bi);
  86. virtual bool removeBehavior( BehaviorInstance *bi, bool deleteBehavior = true );
  87. virtual void clearBehaviors();
  88. virtual U32 getBehaviorCount() const { return mBehaviors.size(); }
  89. virtual const SimSet &getBehaviors() const { return mBehaviors; }
  90. virtual BehaviorInstance *getBehavior( StringTableEntry behaviorTemplateName );
  91. virtual BehaviorInstance *getBehavior( const U32 index ) { return index < (U32)mBehaviors.size() ? reinterpret_cast<BehaviorInstance *>(mBehaviors[index]) : NULL; }
  92. virtual bool reOrder( BehaviorInstance *obj, U32 desiredIndex );
  93. /// Behavior connectivity.
  94. bool connect( BehaviorInstance* pOutputBehavior, BehaviorInstance* pInputBehavior, StringTableEntry pOutputName, StringTableEntry pInputName );
  95. bool disconnect( BehaviorInstance* pOutputBehavior, BehaviorInstance* pInputBehavior, StringTableEntry pOutputName, StringTableEntry pInputName );
  96. bool raise( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName );
  97. U32 getBehaviorConnectionCount( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName );
  98. const BehaviorPortConnection* getBehaviorConnection( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName, const U32 connectionIndex );
  99. const typePortConnectionVector* getBehaviorConnections( BehaviorInstance* pOutputBehavior, StringTableEntry pOutputName );
  100. /// DynamicConsoleMethodComponent Overrides
  101. virtual bool handlesConsoleMethod( const char *fname, S32 *routingId );
  102. virtual const char* callOnBehaviors( U32 argc, const char *argv[] );
  103. /// SimComponent overrides
  104. virtual void write( Stream &stream, U32 tabStop, U32 flags = 0 );
  105. DECLARE_CONOBJECT( BehaviorComponent );
  106. };
  107. #endif