billboardset.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. class BillboardSetHandler
  2. {
  3. protected bool m_BillboardSetIndex = -1;
  4. protected ref array<ref BillboardSet> m_BillboardSets;
  5. protected static const string ROOT_CLASS = "BillboardPresets";
  6. protected static int m_SetIndexCached = -1;//once we resolve the name into an index, we cache it(this disallows dynamic index swapping during mission's runtime)
  7. string GetTextureByType(string type)
  8. {
  9. if (m_BillboardSetIndex == -1)
  10. return "";
  11. BillboardSet bbset = m_BillboardSets.Get(m_BillboardSetIndex);
  12. return bbset.GetTextureByType(type);
  13. }
  14. void OnRPCIndex(int index)
  15. {
  16. if (!m_BillboardSets)
  17. LoadBillboardConfigs();
  18. if (m_BillboardSets && m_BillboardSets.IsValidIndex(index))
  19. {
  20. m_BillboardSetIndex = index;
  21. }
  22. }
  23. protected bool LoadBillboardConfigs()
  24. {
  25. m_BillboardSets = new array<ref BillboardSet>();
  26. int setCount = g_Game.ConfigGetChildrenCount(ROOT_CLASS);
  27. for (int setIndex = 0; setIndex < setCount; setIndex++)
  28. {
  29. string setName;
  30. GetGame().ConfigGetChildName(ROOT_CLASS, setIndex, setName);
  31. string path = ROOT_CLASS + " " + setName;
  32. m_BillboardSets.Insert(new BillboardSet(path));
  33. }
  34. return true;
  35. }
  36. static bool ActivateBillboardSet(string setClassName, PlayerIdentity identity)
  37. {
  38. if (!g_Game)
  39. return false;
  40. if (m_SetIndexCached == -1)
  41. {
  42. int setCount = g_Game.ConfigGetChildrenCount(ROOT_CLASS);
  43. for (int setIndex = 0; setIndex < setCount; setIndex++)
  44. {
  45. string setName;
  46. GetGame().ConfigGetChildName(ROOT_CLASS, setIndex, setName);
  47. if (setName == setClassName)
  48. {
  49. m_SetIndexCached = setIndex;
  50. break
  51. }
  52. }
  53. }
  54. if (m_SetIndexCached != -1)
  55. {
  56. auto param = new Param1<int>(m_SetIndexCached);
  57. GetGame().RPCSingleParam( identity.GetPlayer(), ERPCs.RPC_SET_BILLBOARDS, param, true, identity );
  58. return true;
  59. }
  60. return false;
  61. }
  62. }
  63. class BillboardSet
  64. {
  65. protected ref map<string, string> m_TypeTextureMapping = new map<string, string>();
  66. void BillboardSet(string path)
  67. {
  68. LoadConfig(path);
  69. }
  70. string GetTextureByType(string type)
  71. {
  72. return m_TypeTextureMapping.Get(type);
  73. }
  74. protected void LoadConfig(string path)
  75. {
  76. int count = g_Game.ConfigGetChildrenCount(path);
  77. for ( int i = 0; i < count; i++ )
  78. {
  79. string itemName;
  80. GetGame().ConfigGetChildName(path, i, itemName);
  81. string typesPath = path + " " + itemName + " types";
  82. string texturePath = path + " " + itemName + " texture";
  83. if (GetGame().ConfigIsExisting(typesPath) && GetGame().ConfigIsExisting(texturePath))
  84. {
  85. TStringArray types = new TStringArray();
  86. string texture;
  87. GetGame().ConfigGetText(texturePath, texture);
  88. GetGame().ConfigGetTextArray(typesPath, types);
  89. foreach (string s: types)
  90. {
  91. m_TypeTextureMapping.Insert(s,texture);
  92. }
  93. }
  94. else
  95. {
  96. ErrorEx("Billboard set incorrect configuration, type or texture param missing: " + path);
  97. }
  98. }
  99. }
  100. }