behaviorInstance.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "component/behaviors/behaviorInstance.h"
  23. #include "component/behaviors/behaviorTemplate.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/consoleInternal.h"
  26. #include "io/stream.h"
  27. // Script bindings.
  28. #include "behaviorInstance_ScriptBinding.h"
  29. //-----------------------------------------------------------------------------
  30. IMPLEMENT_CONOBJECT(BehaviorInstance);
  31. //-----------------------------------------------------------------------------
  32. BehaviorInstance::BehaviorInstance( BehaviorTemplate* pTemplate ) :
  33. mTemplate( pTemplate ),
  34. mBehaviorOwner( NULL ),
  35. mBehaviorId( 0 )
  36. {
  37. if ( pTemplate != NULL )
  38. {
  39. // Fetch field prototype count.
  40. const U32 fieldCount = pTemplate->getBehaviorFieldCount();
  41. // Set field prototypes.
  42. for( U32 index = 0; index < fieldCount; ++index )
  43. {
  44. // Fetch fields.
  45. BehaviorTemplate::BehaviorField* pField = pTemplate->getBehaviorField( index );
  46. // Set cloned field.
  47. setDataField( pField->mName, NULL, pField->mDefaultValue );
  48. }
  49. }
  50. }
  51. //-----------------------------------------------------------------------------
  52. bool BehaviorInstance::onAdd()
  53. {
  54. if(! Parent::onAdd())
  55. return false;
  56. // Store this object's namespace
  57. mNameSpace = Namespace::global()->find( getTemplateName() );
  58. return true;
  59. }
  60. //-----------------------------------------------------------------------------
  61. void BehaviorInstance::onRemove()
  62. {
  63. Parent::onRemove();
  64. }
  65. //-----------------------------------------------------------------------------
  66. void BehaviorInstance::initPersistFields()
  67. {
  68. addGroup("Behavior");
  69. addField("template", TypeSimObjectName, Offset(mTemplate, BehaviorInstance), "Template this instance was created from.");
  70. addProtectedField( "Owner", TypeSimObjectPtr, Offset(mBehaviorOwner, BehaviorInstance), &setOwner, &defaultProtectedGetFn, "Behavior component owner." );
  71. endGroup("Behavior");
  72. Parent::initPersistFields();
  73. }
  74. //-----------------------------------------------------------------------------
  75. const char* BehaviorInstance::getTemplateName( void )
  76. {
  77. return mTemplate ? mTemplate->getName() : NULL;
  78. }
  79. // Get template.
  80. const char* BehaviorInstance::getTemplate(void* obj, const char* data)
  81. {
  82. return static_cast<BehaviorInstance*>(obj)->getTemplate()->getIdString();
  83. }