componentsbank.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. class ComponentsBank
  2. {
  3. private EntityAI m_EntityParent;
  4. private ref Component m_Components[COMP_TYPE_COUNT];
  5. void ComponentsBank(EntityAI entity_parent)
  6. {
  7. m_EntityParent = entity_parent;
  8. }
  9. Component GetComponent(int comp_type, string extended_class_name="")
  10. {
  11. if ( !Component.IsTypeExist(comp_type) )
  12. {
  13. Component.LogErrorBadCompType(comp_type, "EntityAI.GetComponent(int comp_type)");
  14. return NULL;
  15. }
  16. if ( !IsComponentAlreadyExist(comp_type) )
  17. {
  18. CreateComponent(comp_type, extended_class_name);
  19. }
  20. return m_Components[comp_type];
  21. }
  22. bool DeleteComponent(int comp_type)
  23. {
  24. if ( IsComponentAlreadyExist(comp_type) )
  25. {
  26. m_Components[comp_type] = NULL;
  27. return true;
  28. }
  29. return false;
  30. }
  31. private Component CreateComponent(int comp_type, string extended_class_name="")
  32. {
  33. if ( !Component.IsTypeExist(comp_type) )
  34. {
  35. Component.LogErrorBadCompType(comp_type, "EntityAI->CreateComponent(int comp_type)");
  36. return NULL;
  37. }
  38. if ( IsComponentAlreadyExist(comp_type) )
  39. {
  40. Component.LogWarningAlredyExist(comp_type, "EntityAI->CreateComponent(int comp_type)");
  41. return m_Components[comp_type];
  42. }
  43. string clas_name = extended_class_name;
  44. if ( clas_name == string.Empty )
  45. {
  46. clas_name = Component.GetNameByType(comp_type);
  47. }
  48. Component comp = Component.Cast(clas_name.ToType().Spawn());
  49. comp.SetParentEntityAI(m_EntityParent);
  50. comp.Event_OnAwake();
  51. m_Components[comp_type] = comp;
  52. comp.Event_OnInit();
  53. return comp;
  54. }
  55. bool IsComponentAlreadyExist(int comp_type)
  56. {
  57. if ( m_Components[comp_type] != NULL )
  58. {
  59. return true;
  60. }
  61. return false;
  62. }
  63. }