unit.vala 30 KB

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