unit.vala 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. using Gee;
  6. namespace Crown
  7. {
  8. public class Unit
  9. {
  10. public Database _db;
  11. public Guid _unit;
  12. public Database _prefabs;
  13. public Unit(Database db, Guid unit, Database? prefabs)
  14. {
  15. _db = db;
  16. _unit = unit;
  17. _prefabs = prefabs != null ? prefabs : new Database();
  18. }
  19. public Value? get_component_property(Guid component_id, string key)
  20. {
  21. // Search in components
  22. {
  23. Value? components = _db.get_property(_unit, "components");
  24. if (components != null && ((HashSet<Guid?>)components).contains(component_id))
  25. return _db.get_property(component_id, key);
  26. }
  27. // Search in modified components
  28. {
  29. Value? value = _db.get_property(_unit, "modified_components.#" + component_id.to_string() + "." + key);
  30. if (value != null)
  31. return value;
  32. }
  33. // Search in prefab's components
  34. {
  35. Value? value = _db.get_property(_unit, "prefab");
  36. if (value != null)
  37. {
  38. string prefab = (string)value;
  39. Value? pcvalue = _prefabs.get_property(GUID_ZERO, prefab + ".components");
  40. if (pcvalue != null)
  41. {
  42. HashSet<Guid?> prefab_components = (HashSet<Guid?>)pcvalue;
  43. if (prefab_components.contains(component_id))
  44. return _prefabs.get_property(component_id, key);
  45. }
  46. }
  47. }
  48. assert(false);
  49. return null;
  50. }
  51. public bool get_component_property_bool(Guid component_id, string key)
  52. {
  53. return (bool)get_component_property(component_id, key);
  54. }
  55. public double get_component_property_double(Guid component_id, string key)
  56. {
  57. return (double)get_component_property(component_id, key);
  58. }
  59. public string get_component_property_string(Guid component_id, string key)
  60. {
  61. return (string)get_component_property(component_id, key);
  62. }
  63. public Guid get_component_property_guid(Guid component_id, string key)
  64. {
  65. return (Guid)get_component_property(component_id, key);
  66. }
  67. public Vector3 get_component_property_vector3(Guid component_id, string key)
  68. {
  69. return (Vector3)get_component_property(component_id, key);
  70. }
  71. public Quaternion get_component_property_quaternion(Guid component_id, string key)
  72. {
  73. return (Quaternion)get_component_property(component_id, key);
  74. }
  75. public void set_component_property_bool(Guid component_id, string key, bool val)
  76. {
  77. // Search in components
  78. {
  79. Value? components = _db.get_property(_unit, "components");
  80. if (components != null && ((HashSet<Guid?>)components).contains(component_id))
  81. {
  82. _db.set_property_bool(component_id, key, val);
  83. return;
  84. }
  85. }
  86. _db.set_property_bool(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
  87. }
  88. public void set_component_property_double(Guid component_id, string key, double val)
  89. {
  90. // Search in components
  91. {
  92. Value? components = _db.get_property(_unit, "components");
  93. if (components != null && ((HashSet<Guid?>)components).contains(component_id))
  94. {
  95. _db.set_property_double(component_id, key, val);
  96. return;
  97. }
  98. }
  99. _db.set_property_double(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
  100. }
  101. public void set_component_property_string(Guid component_id, string key, string val)
  102. {
  103. // Search in components
  104. {
  105. Value? components = _db.get_property(_unit, "components");
  106. if (components != null && ((HashSet<Guid?>)components).contains(component_id))
  107. {
  108. _db.set_property_string(component_id, key, val);
  109. return;
  110. }
  111. }
  112. _db.set_property_string(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
  113. }
  114. public void set_component_property_guid(Guid component_id, string key, Guid val)
  115. {
  116. // Search in components
  117. {
  118. Value? components = _db.get_property(_unit, "components");
  119. if (components != null && ((HashSet<Guid?>)components).contains(component_id))
  120. {
  121. _db.set_property_guid(component_id, key, val);
  122. return;
  123. }
  124. }
  125. _db.set_property_guid(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
  126. }
  127. public void set_component_property_vector3(Guid component_id, string key, Vector3 val)
  128. {
  129. // Search in components
  130. {
  131. Value? components = _db.get_property(_unit, "components");
  132. if (components != null && ((HashSet<Guid?>)components).contains(component_id))
  133. {
  134. _db.set_property_vector3(component_id, key, val);
  135. return;
  136. }
  137. }
  138. _db.set_property_vector3(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
  139. }
  140. public void set_component_property_quaternion(Guid component_id, string key, Quaternion val)
  141. {
  142. // Search in components
  143. {
  144. Value? components = _db.get_property(_unit, "components");
  145. if (components != null && ((HashSet<Guid?>)components).contains(component_id))
  146. {
  147. _db.set_property_quaternion(component_id, key, val);
  148. return;
  149. }
  150. }
  151. _db.set_property_quaternion(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
  152. }
  153. public static bool has_component_static(Database db, Database prefabs_db, Guid unit_id, string component_type, ref Guid ref_component_id)
  154. {
  155. // Search in components
  156. {
  157. Value? value = db.get_property(unit_id, "components");
  158. if (value != null)
  159. {
  160. HashSet<Guid?> components = (HashSet<Guid?>)value;
  161. foreach (Guid component_id in components)
  162. {
  163. if((string)db.get_property(component_id, "type") == component_type)
  164. {
  165. ref_component_id = component_id;
  166. return true;
  167. }
  168. }
  169. }
  170. }
  171. {
  172. string[] keys = db.get_keys(unit_id);
  173. foreach (string m in keys)
  174. {
  175. if (!m.has_prefix("modified_components.#"))
  176. continue;
  177. // 0 21 58 62
  178. // | | | |
  179. // modified_components.#f56420ad-7f9c-4cca-aca5-350f366e0dc0.type
  180. string id = m[21:57];
  181. string type_or_name = m[58:62];
  182. if (!type_or_name.has_prefix("type"))
  183. continue;
  184. Value? type = db.get_property(unit_id, m);
  185. if (type != null && (string)type == component_type)
  186. {
  187. ref_component_id = Guid.parse(id);
  188. return true;
  189. }
  190. }
  191. }
  192. {
  193. Value? value = db.get_property(unit_id, "prefab");
  194. if (value != null)
  195. {
  196. string prefab = (string)value;
  197. Value? pcvalue = prefabs_db.get_property(GUID_ZERO, prefab + ".components");
  198. if (pcvalue != null)
  199. {
  200. HashSet<Guid?> prefab_components = (HashSet<Guid?>)pcvalue;
  201. foreach (Guid component_id in prefab_components)
  202. {
  203. if((string)prefabs_db.get_property(component_id, "type") == component_type)
  204. {
  205. ref_component_id = component_id;
  206. return true;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. return false;
  213. }
  214. /// Returns whether the unit has the component_type.
  215. public bool has_component(string component_type, ref Guid ref_component_id)
  216. {
  217. return Unit.has_component_static(_db, _prefabs, _unit, component_type, ref ref_component_id);
  218. }
  219. public void remove_component(Guid component_id)
  220. {
  221. _db.remove_from_set(GUID_ZERO, "components", component_id);
  222. }
  223. /// Returns whether the unit has a prefab.
  224. public bool has_prefab()
  225. {
  226. return _db.has_property(_unit, "prefab");
  227. }
  228. /// Returns whether the unit is a light unit.
  229. public bool is_light()
  230. {
  231. return has_prefab()
  232. && _db.get_property_string(_unit, "prefab") == "core/units/light";
  233. }
  234. }
  235. }