behaviorTemplate.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include "platform/platform.h"
  23. #include "sim/simBase.h"
  24. #include "console/consoleTypes.h"
  25. #include "component/simComponent.h"
  26. #include "component/behaviors/behaviorTemplate.h"
  27. #include "memory/safeDelete.h"
  28. #include "io/resource/resourceManager.h"
  29. // Script bindings.
  30. #include "behaviorTemplate_ScriptBinding.h"
  31. //-----------------------------------------------------------------------------
  32. IMPLEMENT_CONOBJECT(BehaviorTemplate);
  33. //-----------------------------------------------------------------------------
  34. BehaviorTemplate::BehaviorTemplate() :
  35. mFriendlyName( StringTable->EmptyString ),
  36. mDescription( StringTable->EmptyString ),
  37. mBehaviorType( StringTable->EmptyString )
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. bool BehaviorTemplate::onAdd()
  42. {
  43. if(! Parent::onAdd())
  44. return false;
  45. Sim::gBehaviorSet->addObject(this);
  46. return true;
  47. }
  48. //-----------------------------------------------------------------------------
  49. void BehaviorTemplate::onRemove()
  50. {
  51. Sim::gBehaviorSet->removeObject(this);
  52. Parent::onRemove();
  53. }
  54. //-----------------------------------------------------------------------------
  55. void BehaviorTemplate::initPersistFields()
  56. {
  57. addGroup("Behavior");
  58. addField("friendlyName", TypeCaseString, Offset(mFriendlyName, BehaviorTemplate), "Human friendly name of this behavior");
  59. addProtectedField("description", TypeCaseString, Offset(mDescription, BehaviorTemplate), &setDescription, &getDescription, "The description of this behavior.\n");
  60. addField("behaviorType", TypeString, Offset(mBehaviorType, BehaviorTemplate), "?? Organizational keyword ??");
  61. endGroup("Behavior");
  62. Parent::initPersistFields();
  63. }
  64. //-----------------------------------------------------------------------------
  65. BehaviorInstance* BehaviorTemplate::createInstance( void )
  66. {
  67. // Create behavior instance.
  68. BehaviorInstance* pBehavior = new BehaviorInstance( this );
  69. // Register object.
  70. if( pBehavior->registerObject() )
  71. return pBehavior;
  72. // Registration failed so delete behavior.
  73. delete pBehavior;
  74. return NULL;
  75. }
  76. //-----------------------------------------------------------------------------
  77. bool BehaviorTemplate::addBehaviorField( const char* name, const char* description, const char* type, const char* defaultValue, const char* userData )
  78. {
  79. // Does the behavior already have the field?
  80. if ( hasBehaviorField( name ) )
  81. {
  82. // Yes, so warn.
  83. Con::warnf("Behavior field named '%s' on template '%s' already exists.", name, mFriendlyName );
  84. return false;
  85. }
  86. // Create field.
  87. BehaviorField field( name, description, type, defaultValue, userData );
  88. // Use field.
  89. mFields.push_back( field );
  90. return true;
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool BehaviorTemplate::addBehaviorOutput( const char* name, const char* label, const char* description )
  94. {
  95. // Does the behavior already have the output?
  96. if ( hasBehaviorOutput( name ) )
  97. {
  98. // Yes, so warn.
  99. Con::warnf("Behavior output named '%s' on template '%s' already exists.", name, mFriendlyName );
  100. return false;
  101. }
  102. // Create output.
  103. BehaviorPortOutput output( name, label, description );
  104. // Use port.
  105. mPortOutputs.push_back( output );
  106. return true;
  107. }
  108. //-----------------------------------------------------------------------------
  109. bool BehaviorTemplate::addBehaviorInput( const char* name, const char* label, const char* description )
  110. {
  111. // Does the behavior already have the input?
  112. if ( hasBehaviorInput( name ) )
  113. {
  114. // Yes, so warn.
  115. Con::warnf("Behavior input named '%s' on template '%s' already exists.", name, mFriendlyName );
  116. return false;
  117. }
  118. // Create input.
  119. BehaviorPortInput input( name, label, description );
  120. // Use port.
  121. mPortInputs.push_back( input );
  122. return true;
  123. }