unit.vala 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gee;
  6. namespace Crown
  7. {
  8. public struct Unit
  9. {
  10. public static Hashtable _component_registry;
  11. public Database _db;
  12. public Guid _id;
  13. public Unit(Database db, Guid id)
  14. {
  15. _db = db;
  16. _id = id;
  17. }
  18. /// Loads the unit @a name.
  19. public static void load_unit(Database db, string name)
  20. {
  21. string resource_path = name + ".unit";
  22. Guid prefab_id = GUID_ZERO;
  23. if (db.add_from_resource_path(out prefab_id, resource_path) != 0)
  24. return; // Caller can query the database to check for error.
  25. assert(prefab_id != GUID_ZERO);
  26. }
  27. public void create_empty()
  28. {
  29. _db.create(_id, OBJECT_TYPE_UNIT);
  30. }
  31. public void create(string? prefab, Vector3 pos, Quaternion rot, Vector3 scl)
  32. {
  33. create_empty();
  34. if (prefab != null)
  35. _db.set_property_string(_id, "prefab", prefab);
  36. set_local_position(pos);
  37. set_local_rotation(rot);
  38. set_local_scale(scl);
  39. }
  40. public Value? get_component_property(Guid component_id, string key)
  41. {
  42. Value? val;
  43. // Search in components
  44. val = _db.get_property(_id, "components");
  45. if (val != null) {
  46. if (((HashSet<Guid?>)val).contains(component_id))
  47. return _db.get_property(component_id, key);
  48. }
  49. // Search in modified_components
  50. val = _db.get_property(_id, "modified_components.#" + component_id.to_string() + "." + key);
  51. if (val != null)
  52. return val;
  53. // Search in prefab
  54. val = _db.get_property(_id, "prefab");
  55. if (val != null) {
  56. // Convert prefab path to object ID.
  57. string prefab = (string)val;
  58. Unit.load_unit(_db, prefab);
  59. Guid prefab_id = _db.get_property_guid(GUID_ZERO, prefab + ".unit");
  60. Unit unit = Unit(_db, prefab_id);
  61. return unit.get_component_property(component_id, key);
  62. }
  63. return null;
  64. }
  65. public bool get_component_property_bool(Guid component_id, string key)
  66. {
  67. return (bool)get_component_property(component_id, key);
  68. }
  69. public double get_component_property_double(Guid component_id, string key)
  70. {
  71. return (double)get_component_property(component_id, key);
  72. }
  73. public string get_component_property_string(Guid component_id, string key)
  74. {
  75. return (string)get_component_property(component_id, key);
  76. }
  77. public Guid get_component_property_guid(Guid component_id, string key)
  78. {
  79. return (Guid)get_component_property(component_id, key);
  80. }
  81. public Vector3 get_component_property_vector3(Guid component_id, string key)
  82. {
  83. return (Vector3)get_component_property(component_id, key);
  84. }
  85. public Quaternion get_component_property_quaternion(Guid component_id, string key)
  86. {
  87. return (Quaternion)get_component_property(component_id, key);
  88. }
  89. public void set_component_property_bool(Guid component_id, string key, bool val)
  90. {
  91. // Search in components
  92. Value? components = _db.get_property(_id, "components");
  93. if (components != null && ((HashSet<Guid?>)components).contains(component_id)) {
  94. _db.set_property_bool(component_id, key, val);
  95. return;
  96. }
  97. _db.set_property_bool(_id, "modified_components.#" + component_id.to_string() + "." + key, val);
  98. }
  99. public void set_component_property_double(Guid component_id, string key, double val)
  100. {
  101. // Search in components
  102. Value? components = _db.get_property(_id, "components");
  103. if (components != null && ((HashSet<Guid?>)components).contains(component_id)) {
  104. _db.set_property_double(component_id, key, val);
  105. return;
  106. }
  107. _db.set_property_double(_id, "modified_components.#" + component_id.to_string() + "." + key, val);
  108. }
  109. public void set_component_property_string(Guid component_id, string key, string val)
  110. {
  111. // Search in components
  112. Value? components = _db.get_property(_id, "components");
  113. if (components != null && ((HashSet<Guid?>)components).contains(component_id)) {
  114. _db.set_property_string(component_id, key, val);
  115. return;
  116. }
  117. _db.set_property_string(_id, "modified_components.#" + component_id.to_string() + "." + key, val);
  118. }
  119. public void set_component_property_guid(Guid component_id, string key, Guid val)
  120. {
  121. // Search in components
  122. Value? components = _db.get_property(_id, "components");
  123. if (components != null && ((HashSet<Guid?>)components).contains(component_id)) {
  124. _db.set_property_guid(component_id, key, val);
  125. return;
  126. }
  127. _db.set_property_guid(_id, "modified_components.#" + component_id.to_string() + "." + key, val);
  128. }
  129. public void set_component_property_vector3(Guid component_id, string key, Vector3 val)
  130. {
  131. // Search in components
  132. Value? components = _db.get_property(_id, "components");
  133. if (components != null && ((HashSet<Guid?>)components).contains(component_id)) {
  134. _db.set_property_vector3(component_id, key, val);
  135. return;
  136. }
  137. _db.set_property_vector3(_id, "modified_components.#" + component_id.to_string() + "." + key, val);
  138. }
  139. public void set_component_property_quaternion(Guid component_id, string key, Quaternion val)
  140. {
  141. // Search in components
  142. Value? components = _db.get_property(_id, "components");
  143. if (components != null && ((HashSet<Guid?>)components).contains(component_id)) {
  144. _db.set_property_quaternion(component_id, key, val);
  145. return;
  146. }
  147. _db.set_property_quaternion(_id, "modified_components.#" + component_id.to_string() + "." + key, val);
  148. }
  149. /// Returns whether the @a unit_id has a component of type @a component_type.
  150. public static bool has_component_static(out Guid component_id, out Guid owner_id, string component_type, Database db, Guid unit_id)
  151. {
  152. Value? val;
  153. component_id = GUID_ZERO;
  154. owner_id = GUID_ZERO;
  155. bool prefab_has_component = false;
  156. // If the component type is found inside the "components" array, the unit has the component
  157. // and it owns it.
  158. val = db.get_property(unit_id, "components");
  159. if (val != null) {
  160. foreach (Guid id in (HashSet<Guid?>)val) {
  161. if ((string)db.object_type(id) == component_type) {
  162. component_id = id;
  163. owner_id = unit_id;
  164. return true;
  165. }
  166. }
  167. }
  168. // Otherwise, search if any prefab has the component.
  169. val = db.get_property(unit_id, "prefab");
  170. if (val != null) {
  171. // Convert prefab path to object ID.
  172. string prefab = (string)val;
  173. Unit.load_unit(db, prefab);
  174. Guid prefab_id = db.get_property_guid(GUID_ZERO, prefab + ".unit");
  175. prefab_has_component = has_component_static(out component_id
  176. , out owner_id
  177. , component_type
  178. , db
  179. , prefab_id
  180. );
  181. }
  182. // If the prefab does not have the component, so does this unit.
  183. if (prefab_has_component)
  184. return db.get_property(unit_id, "deleted_components.#" + component_id.to_string()) == null;
  185. component_id = GUID_ZERO;
  186. owner_id = GUID_ZERO;
  187. return false;
  188. }
  189. /// Returns whether the unit has the component_type.
  190. public bool has_component_with_owner(out Guid component_id, out Guid owner_id, string component_type)
  191. {
  192. return Unit.has_component_static(out component_id, out owner_id, component_type, _db, _id);
  193. }
  194. /// Returns whether the unit has the component_type.
  195. public bool has_component(out Guid component_id, string component_type)
  196. {
  197. Guid owner_id;
  198. return has_component_with_owner(out component_id, out owner_id, component_type);
  199. }
  200. public Vector3 local_position()
  201. {
  202. Vector3 position;
  203. Guid component_id;
  204. if (has_component(out component_id, OBJECT_TYPE_TRANSFORM))
  205. position = get_component_property_vector3(component_id, "data.position");
  206. else
  207. position = _db.get_property_vector3(_id, "position");
  208. return position;
  209. }
  210. public Quaternion local_rotation()
  211. {
  212. Quaternion rotation;
  213. Guid component_id;
  214. if (has_component(out component_id, OBJECT_TYPE_TRANSFORM))
  215. rotation = get_component_property_quaternion(component_id, "data.rotation");
  216. else
  217. rotation = _db.get_property_quaternion(_id, "rotation");
  218. return rotation;
  219. }
  220. public Vector3 local_scale()
  221. {
  222. Vector3 scale;
  223. Guid component_id;
  224. if (has_component(out component_id, OBJECT_TYPE_TRANSFORM))
  225. scale = get_component_property_vector3(component_id, "data.scale");
  226. else
  227. scale = _db.get_property_vector3(_id, "scale");
  228. return scale;
  229. }
  230. public void set_local_position(Vector3 position)
  231. {
  232. Guid component_id;
  233. if (has_component(out component_id, OBJECT_TYPE_TRANSFORM))
  234. set_component_property_vector3(component_id, "data.position", position);
  235. else
  236. _db.set_property_vector3(_id, "position", position);
  237. }
  238. public void set_local_rotation(Quaternion rotation)
  239. {
  240. Guid component_id;
  241. if (has_component(out component_id, OBJECT_TYPE_TRANSFORM))
  242. set_component_property_quaternion(component_id, "data.rotation", rotation);
  243. else
  244. _db.set_property_quaternion(_id, "rotation", rotation);
  245. }
  246. public void set_local_scale(Vector3 scale)
  247. {
  248. Guid component_id;
  249. if (has_component(out component_id, OBJECT_TYPE_TRANSFORM))
  250. set_component_property_vector3(component_id, "data.scale", scale);
  251. else
  252. _db.set_property_vector3(_id, "scale", scale);
  253. }
  254. // Adds the @a component_type to the unit and returns its ID.
  255. public Guid add_component_type(string component_type)
  256. {
  257. // Create a new component.
  258. Guid component_id = Guid.new_guid();
  259. _db.create(component_id, component_type);
  260. // Initialize component data based on its type.
  261. if (component_type == OBJECT_TYPE_TRANSFORM) {
  262. _db.set_property_vector3 (component_id, "data.position", VECTOR3_ZERO);
  263. _db.set_property_quaternion(component_id, "data.rotation", QUATERNION_IDENTITY);
  264. _db.set_property_vector3 (component_id, "data.scale", VECTOR3_ONE);
  265. } else if (component_type == OBJECT_TYPE_CAMERA) {
  266. _db.set_property_string(component_id, "data.projection", "perspective");
  267. _db.set_property_double(component_id, "data.fov", 45.0 * (Math.PI/180.0));
  268. _db.set_property_double(component_id, "data.far_range", 0.01);
  269. _db.set_property_double(component_id, "data.near_range", 1000.0);
  270. } else if (component_type == OBJECT_TYPE_MESH_RENDERER) {
  271. _db.set_property_string(component_id, "data.mesh_resource", "core/components/noop");
  272. _db.set_property_string(component_id, "data.geometry_name", "Noop");
  273. _db.set_property_string(component_id, "data.material", "core/components/noop");
  274. _db.set_property_bool (component_id, "data.visible", true);
  275. } else if (component_type == OBJECT_TYPE_SPRITE_RENDERER) {
  276. _db.set_property_string(component_id, "data.sprite_resource", "core/components/noop");
  277. _db.set_property_string(component_id, "data.material", "core/components/noop");
  278. _db.set_property_double(component_id, "data.layer", 0);
  279. _db.set_property_double(component_id, "data.depth", 0);
  280. _db.set_property_bool (component_id, "data.visible", true);
  281. } else if (component_type == OBJECT_TYPE_LIGHT) {
  282. _db.set_property_string (component_id, "data.type", "directional");
  283. _db.set_property_double (component_id, "data.range", 1.0);
  284. _db.set_property_double (component_id, "data.intensity", 1.0);
  285. _db.set_property_double (component_id, "data.spot_angle", 45.0 * (Math.PI/180.0));
  286. _db.set_property_vector3(component_id, "data.color", VECTOR3_ONE);
  287. } else if (component_type == OBJECT_TYPE_SCRIPT) {
  288. _db.set_property_string(component_id, "data.script_resource", "core/components/noop");
  289. } else if (component_type == OBJECT_TYPE_COLLIDER) {
  290. _db.set_property_string (component_id, "data.shape", "box");
  291. _db.set_property_string (component_id, "data.source", "mesh"); // "inline" or "mesh"
  292. // if "mesh"
  293. _db.set_property_string (component_id, "data.scene", "core/components/noop");
  294. _db.set_property_string (component_id, "data.name", "Noop");
  295. // if "inline"
  296. _db.set_property_vector3 (component_id, "data.collider_data.position", VECTOR3_ZERO);
  297. _db.set_property_quaternion(component_id, "data.collider_data.rotation", QUATERNION_IDENTITY);
  298. _db.set_property_vector3 (component_id, "data.collider_data.half_extents", VECTOR3_ZERO); // for "box"
  299. _db.set_property_double (component_id, "data.collider_data.radius", 0.0); // for "sphere" and "capsule"
  300. _db.set_property_double (component_id, "data.collider_data.height", 0.0); // for "capsule"
  301. } else if (component_type == OBJECT_TYPE_ACTOR) {
  302. _db.set_property_bool (component_id, "data.lock_translation_x", false);
  303. _db.set_property_bool (component_id, "data.lock_translation_y", false);
  304. _db.set_property_bool (component_id, "data.lock_translation_z", false);
  305. _db.set_property_bool (component_id, "data.lock_rotation_x", false);
  306. _db.set_property_bool (component_id, "data.lock_rotation_y", false);
  307. _db.set_property_bool (component_id, "data.lock_rotation_z", false);
  308. _db.set_property_string(component_id, "data.class", "static");
  309. _db.set_property_double(component_id, "data.mass", 1.0);
  310. _db.set_property_string(component_id, "data.collision_filter", "default");
  311. _db.set_property_string(component_id, "data.material", "default");
  312. } else if (component_type == OBJECT_TYPE_ANIMATION_STATE_MACHINE) {
  313. _db.set_property_string(component_id, "data.state_machine_resource", "core/components/noop");
  314. } else {
  315. logw("Unregistered component type `%s`".printf(component_type));
  316. }
  317. _db.add_to_set(_id, "components", component_id);
  318. return component_id;
  319. }
  320. /// Removes the @a component_type from the unit.
  321. public void remove_component_type(string component_type)
  322. {
  323. Guid component_id;
  324. Guid owner_id;
  325. if (has_component_with_owner(out component_id, out owner_id, component_type)) {
  326. if (_id == owner_id) {
  327. _db.remove_from_set(_id, "components", component_id);
  328. } else {
  329. _db.set_property_bool(_id, "deleted_components.#" + component_id.to_string(), false);
  330. // Clean all modified_components keys that matches the deleted component ID.
  331. string[] unit_keys = _db.get_keys(_id);
  332. for (int ii = 0; ii < unit_keys.length; ++ii) {
  333. if (unit_keys[ii].has_prefix("modified_components.#" + component_id.to_string()))
  334. _db.set_property_null(_id, unit_keys[ii]);
  335. }
  336. }
  337. _db.add_restore_point((int)ActionType.UNIT_REMOVE_COMPONENT, new Guid?[] { _id, component_id });
  338. } else {
  339. logw("The unit has no such component type `%s`".printf(component_type));
  340. }
  341. }
  342. public static void register_component_type(string type, string depends_on)
  343. {
  344. if (_component_registry == null)
  345. _component_registry = new Hashtable();
  346. _component_registry[type] = depends_on;
  347. }
  348. public string? prefab()
  349. {
  350. return _db.has_property(_id, "prefab")
  351. ? _db.get_property_string(_id, "prefab")
  352. : null
  353. ;
  354. }
  355. /// Returns whether the unit has a prefab.
  356. public bool has_prefab()
  357. {
  358. return _db.has_property(_id, "prefab");
  359. }
  360. /// Returns whether the unit is a light unit.
  361. public bool is_light()
  362. {
  363. return has_prefab()
  364. && _db.get_property_string(_id, "prefab") == "core/units/light";
  365. }
  366. /// Returns whether the unit is a camera unit.
  367. public bool is_camera()
  368. {
  369. return has_prefab()
  370. && _db.get_property_string(_id, "prefab") == "core/units/camera";
  371. }
  372. public static int generate_spawn_unit_commands(StringBuilder sb, Guid?[] object_ids, Database db)
  373. {
  374. int i;
  375. if (object_ids.length > 1 && Unit.is_component(object_ids[1], db)) {
  376. for (i = 1; i < object_ids.length; ++i) {
  377. if (!is_component(object_ids[i], db))
  378. break;
  379. Guid unit_id = object_ids[0];
  380. Guid component_id = object_ids[i];
  381. string component_type = db.object_type(component_id);
  382. Unit unit = Unit(db, unit_id);
  383. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  384. if (component_type == OBJECT_TYPE_TRANSFORM) {
  385. sb.append(LevelEditorApi.add_tranform_component(unit_id
  386. , component_id
  387. , unit.get_component_property_vector3 (component_id, "data.position")
  388. , unit.get_component_property_quaternion(component_id, "data.rotation")
  389. , unit.get_component_property_vector3 (component_id, "data.scale")
  390. ));
  391. } else if (component_type == OBJECT_TYPE_CAMERA) {
  392. sb.append(LevelEditorApi.add_camera_component(unit_id
  393. , component_id
  394. , unit.get_component_property_string(component_id, "data.projection")
  395. , unit.get_component_property_double(component_id, "data.fov")
  396. , unit.get_component_property_double(component_id, "data.far_range")
  397. , unit.get_component_property_double(component_id, "data.near_range")
  398. ));
  399. } else if (component_type == OBJECT_TYPE_MESH_RENDERER) {
  400. sb.append(LevelEditorApi.add_mesh_renderer_component(unit_id
  401. , component_id
  402. , unit.get_component_property_string(component_id, "data.mesh_resource")
  403. , unit.get_component_property_string(component_id, "data.geometry_name")
  404. , unit.get_component_property_string(component_id, "data.material")
  405. , unit.get_component_property_bool (component_id, "data.visible")
  406. ));
  407. } else if (component_type == OBJECT_TYPE_SPRITE_RENDERER) {
  408. sb.append(LevelEditorApi.add_sprite_renderer_component(unit_id
  409. , component_id
  410. , unit.get_component_property_string(component_id, "data.sprite_resource")
  411. , unit.get_component_property_string(component_id, "data.material")
  412. , unit.get_component_property_double(component_id, "data.layer")
  413. , unit.get_component_property_double(component_id, "data.depth")
  414. , unit.get_component_property_bool (component_id, "data.visible")
  415. ));
  416. } else if (component_type == OBJECT_TYPE_LIGHT) {
  417. sb.append(LevelEditorApi.add_light_component(unit_id
  418. , component_id
  419. , unit.get_component_property_string (component_id, "data.type")
  420. , unit.get_component_property_double (component_id, "data.range")
  421. , unit.get_component_property_double (component_id, "data.intensity")
  422. , unit.get_component_property_double (component_id, "data.spot_angle")
  423. , unit.get_component_property_vector3(component_id, "data.color")
  424. ));
  425. } else if (component_type == OBJECT_TYPE_SCRIPT) {
  426. /*
  427. * sb.append(LevelEditorApi.add_script_component(unit_id
  428. * , component_id
  429. * , unit.get_component_property_string(component_id, "data.script_resource")
  430. * ));
  431. */
  432. } else if (component_type == OBJECT_TYPE_COLLIDER) {
  433. /*
  434. * sb.append(LevelEditorApi.add_collider_component(unit_id
  435. * , component_id
  436. * , unit.get_component_property_string (component_id, "data.shape")
  437. * , unit.get_component_property_string (component_id, "data.source")
  438. * // if "mesh"
  439. * , unit.get_component_property_string (component_id, "data.scene")
  440. * , unit.get_component_property_string (component_id, "data.name")
  441. * // if "inline"
  442. * , unit.get_component_property_vector3 (component_id, "data.collider_data.position")
  443. * , unit.get_component_property_quaternion(component_id, "data.collider_data.rotation")
  444. * , unit.get_component_property_vector3 (component_id, "data.collider_data.half_extents")
  445. * , unit.get_component_property_double (component_id, "data.collider_data.radius")
  446. * , unit.get_component_property_double (component_id, "data.collider_data.height")
  447. * ));
  448. */
  449. } else if (component_type == OBJECT_TYPE_ACTOR) {
  450. /*
  451. * sb.append(LevelEditorApi.add_actor_component(unit_id
  452. * , component_id
  453. * , unit.get_component_property_bool (component_id, "data.lock_translation_x")
  454. * , unit.get_component_property_bool (component_id, "data.lock_translation_y")
  455. * , unit.get_component_property_bool (component_id, "data.lock_translation_z")
  456. * , unit.get_component_property_bool (component_id, "data.lock_rotation_x")
  457. * , unit.get_component_property_bool (component_id, "data.lock_rotation_y")
  458. * , unit.get_component_property_bool (component_id, "data.lock_rotation_z")
  459. * , unit.get_component_property_string(component_id, "data.class")
  460. * , unit.get_component_property_double(component_id, "data.mass")
  461. * , unit.get_component_property_string(component_id, "data.collision_filter")
  462. * , unit.get_component_property_string(component_id, "data.material")
  463. * ));
  464. */
  465. } else if (component_type == OBJECT_TYPE_ANIMATION_STATE_MACHINE) {
  466. /*
  467. * sb.append(LevelEditorApi.add_animation_state_machine_component(unit_id
  468. * , component_id
  469. * , unit.get_component_property_string(component_id, "data.state_machine_resource")
  470. * ));
  471. */
  472. } else {
  473. logw("Unregistered component type `%s`".printf(component_type));
  474. }
  475. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  476. }
  477. } else {
  478. for (i = 0; i < object_ids.length; ++i) {
  479. if (db.object_type(object_ids[i]) != OBJECT_TYPE_UNIT)
  480. break;
  481. Guid unit_id = object_ids[i];
  482. Unit unit = Unit(db, unit_id);
  483. string? prefab = unit.prefab();
  484. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  485. if (prefab != null) {
  486. sb.append(LevelEditorApi.spawn_unit(unit_id
  487. , prefab
  488. , unit.local_position()
  489. , unit.local_rotation()
  490. , unit.local_scale()
  491. ));
  492. foreach (var entry in Unit._component_registry.entries) {
  493. Guid component_id;
  494. if (!unit.has_component(out component_id, entry.key))
  495. continue;
  496. generate_change_commands(sb, { unit_id, component_id }, db);
  497. }
  498. } else {
  499. sb.append(LevelEditorApi.spawn_empty_unit(unit_id));
  500. Guid component_id;
  501. if (unit.has_component(out component_id, "transform")) {
  502. string s = LevelEditorApi.add_tranform_component(unit_id
  503. , component_id
  504. , unit.get_component_property_vector3 (component_id, "data.position")
  505. , unit.get_component_property_quaternion(component_id, "data.rotation")
  506. , unit.get_component_property_vector3 (component_id, "data.scale")
  507. );
  508. sb.append(s);
  509. }
  510. if (unit.has_component(out component_id, "camera")) {
  511. string s = LevelEditorApi.add_camera_component(unit_id
  512. , component_id
  513. , unit.get_component_property_string(component_id, "data.projection")
  514. , unit.get_component_property_double(component_id, "data.fov")
  515. , unit.get_component_property_double(component_id, "data.far_range")
  516. , unit.get_component_property_double(component_id, "data.near_range")
  517. );
  518. sb.append(s);
  519. }
  520. if (unit.has_component(out component_id, "mesh_renderer")) {
  521. string s = LevelEditorApi.add_mesh_renderer_component(unit_id
  522. , component_id
  523. , unit.get_component_property_string(component_id, "data.mesh_resource")
  524. , unit.get_component_property_string(component_id, "data.geometry_name")
  525. , unit.get_component_property_string(component_id, "data.material")
  526. , unit.get_component_property_bool (component_id, "data.visible")
  527. );
  528. sb.append(s);
  529. }
  530. if (unit.has_component(out component_id, "sprite_renderer")) {
  531. string s = LevelEditorApi.add_sprite_renderer_component(unit_id
  532. , component_id
  533. , unit.get_component_property_string(component_id, "data.sprite_resource")
  534. , unit.get_component_property_string(component_id, "data.material")
  535. , unit.get_component_property_double(component_id, "data.layer")
  536. , unit.get_component_property_double(component_id, "data.depth")
  537. , unit.get_component_property_bool (component_id, "data.visible")
  538. );
  539. sb.append(s);
  540. }
  541. if (unit.has_component(out component_id, "light")) {
  542. string s = LevelEditorApi.add_light_component(unit_id
  543. , component_id
  544. , unit.get_component_property_string (component_id, "data.type")
  545. , unit.get_component_property_double (component_id, "data.range")
  546. , unit.get_component_property_double (component_id, "data.intensity")
  547. , unit.get_component_property_double (component_id, "data.spot_angle")
  548. , unit.get_component_property_vector3(component_id, "data.color")
  549. );
  550. sb.append(s);
  551. }
  552. }
  553. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  554. }
  555. }
  556. return i;
  557. }
  558. public static int generate_destroy_commands(StringBuilder sb, Guid?[] object_ids, Database db)
  559. {
  560. int i;
  561. if (object_ids.length > 1 && Unit.is_component(object_ids[1], db)) {
  562. for (i = 1; i < object_ids.length; ++i) {
  563. if (!is_component(object_ids[i], db))
  564. break;
  565. Guid unit_id = object_ids[0];
  566. Guid component_id = object_ids[i];
  567. string component_type = db.object_type(component_id);
  568. sb.append(LevelEditorApi.unit_destroy_component_type(unit_id, component_type));
  569. }
  570. } else {
  571. for (i = 0; i < object_ids.length; ++i) {
  572. if (db.object_type(object_ids[i]) != OBJECT_TYPE_UNIT)
  573. break;
  574. sb.append(LevelEditorApi.destroy(object_ids[i]));
  575. }
  576. }
  577. return i;
  578. }
  579. public static int generate_change_commands(StringBuilder sb, Guid?[] object_ids, Database db)
  580. {
  581. int i;
  582. if (object_ids.length > 1 && Unit.is_component(object_ids[1], db)) {
  583. for (i = 1; i < object_ids.length; ++i) {
  584. if (!is_component(object_ids[i], db))
  585. break;
  586. Guid unit_id = object_ids[0];
  587. Guid component_id = object_ids[i];
  588. string component_type = db.object_type(component_id);
  589. Unit unit = Unit(db, unit_id);
  590. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  591. if (component_type == OBJECT_TYPE_TRANSFORM) {
  592. sb.append(LevelEditorApi.move_object(unit_id
  593. , unit.get_component_property_vector3 (component_id, "data.position")
  594. , unit.get_component_property_quaternion(component_id, "data.rotation")
  595. , unit.get_component_property_vector3 (component_id, "data.scale")
  596. ));
  597. } else if (component_type == OBJECT_TYPE_CAMERA) {
  598. sb.append(LevelEditorApi.set_camera(unit_id
  599. , unit.get_component_property_string(component_id, "data.projection")
  600. , unit.get_component_property_double(component_id, "data.fov")
  601. , unit.get_component_property_double(component_id, "data.far_range")
  602. , unit.get_component_property_double(component_id, "data.near_range")
  603. ));
  604. } else if (component_type == OBJECT_TYPE_MESH_RENDERER) {
  605. sb.append(LevelEditorApi.set_mesh(unit_id
  606. , unit.get_component_property_string(component_id, "data.mesh_resource")
  607. , unit.get_component_property_string(component_id, "data.geometry_name")
  608. , unit.get_component_property_string(component_id, "data.material")
  609. , unit.get_component_property_bool (component_id, "data.visible")
  610. ));
  611. } else if (component_type == OBJECT_TYPE_SPRITE_RENDERER) {
  612. sb.append(LevelEditorApi.set_sprite(unit_id
  613. , unit.get_component_property_string(component_id, "data.sprite_resource")
  614. , unit.get_component_property_string(component_id, "data.material")
  615. , unit.get_component_property_double(component_id, "data.layer")
  616. , unit.get_component_property_double(component_id, "data.depth")
  617. , unit.get_component_property_bool (component_id, "data.visible")
  618. ));
  619. } else if (component_type == OBJECT_TYPE_LIGHT) {
  620. sb.append(LevelEditorApi.set_light(unit_id
  621. , unit.get_component_property_string (component_id, "data.type")
  622. , unit.get_component_property_double (component_id, "data.range")
  623. , unit.get_component_property_double (component_id, "data.intensity")
  624. , unit.get_component_property_double (component_id, "data.spot_angle")
  625. , unit.get_component_property_vector3(component_id, "data.color")
  626. ));
  627. } else if (component_type == OBJECT_TYPE_SCRIPT) {
  628. /* No sync. */
  629. } else if (component_type == OBJECT_TYPE_COLLIDER) {
  630. /* No sync. */
  631. } else if (component_type == OBJECT_TYPE_ACTOR) {
  632. /* No sync. */
  633. } else if (component_type == OBJECT_TYPE_ANIMATION_STATE_MACHINE) {
  634. /* No sync. */
  635. } else {
  636. logw("Unregistered component type `%s`".printf(component_type));
  637. }
  638. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  639. }
  640. } else {
  641. for (i = 0; i < object_ids.length; ++i) {
  642. if (db.object_type(object_ids[i]) != OBJECT_TYPE_UNIT)
  643. break;
  644. Guid unit_id = object_ids[i];
  645. Unit unit = Unit(db, unit_id);
  646. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  647. sb.append(LevelEditorApi.move_object(unit_id
  648. , unit.local_position()
  649. , unit.local_rotation()
  650. , unit.local_scale()
  651. ));
  652. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  653. }
  654. }
  655. return i;
  656. }
  657. public static bool is_component(Guid id, Database db)
  658. {
  659. string type = db.object_type(id);
  660. return type == OBJECT_TYPE_TRANSFORM
  661. || type == OBJECT_TYPE_CAMERA
  662. || type == OBJECT_TYPE_MESH_RENDERER
  663. || type == OBJECT_TYPE_SPRITE_RENDERER
  664. || type == OBJECT_TYPE_LIGHT
  665. || type == OBJECT_TYPE_SCRIPT
  666. || type == OBJECT_TYPE_COLLIDER
  667. || type == OBJECT_TYPE_ACTOR
  668. || type == OBJECT_TYPE_ANIMATION_STATE_MACHINE
  669. ;
  670. }
  671. public void add_component_type_dependencies(ref ArrayList<Guid?> components_added, string component_type)
  672. {
  673. Guid dummy;
  674. if (has_component(out dummy, component_type))
  675. return;
  676. string[] component_type_dependencies = ((string)Unit._component_registry[component_type]).split(", ");
  677. foreach (unowned string dependency in component_type_dependencies) {
  678. Guid dependency_component_id;
  679. if (!has_component(out dependency_component_id, dependency))
  680. add_component_type_dependencies(ref components_added, dependency);
  681. }
  682. components_added.add(add_component_type(component_type));
  683. }
  684. }
  685. } /* namespace Crown */