unit.vala 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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_FOG) {
  290. _db.set_property_vector3(component_id, "data.color", Vector3(0.83, 0.83, 0.83));
  291. _db.set_property_double (component_id, "data.density", 0.02);
  292. _db.set_property_double (component_id, "data.range_min", 0.0);
  293. _db.set_property_double (component_id, "data.range_max", 1000.0);
  294. _db.set_property_double (component_id, "data.sun_blend", 0.0);
  295. _db.set_property_bool (component_id, "data.enabled", false);
  296. } else if (component_type == OBJECT_TYPE_SCRIPT) {
  297. _db.set_property_string(component_id, "data.script_resource", "core/components/noop");
  298. } else if (component_type == OBJECT_TYPE_COLLIDER) {
  299. _db.set_property_string (component_id, "data.shape", "box");
  300. _db.set_property_string (component_id, "data.source", "mesh"); // "inline" or "mesh"
  301. // if "mesh"
  302. _db.set_property_string (component_id, "data.scene", "core/components/noop");
  303. _db.set_property_string (component_id, "data.name", "Noop");
  304. // if "inline"
  305. _db.set_property_vector3 (component_id, "data.collider_data.position", VECTOR3_ZERO);
  306. _db.set_property_quaternion(component_id, "data.collider_data.rotation", QUATERNION_IDENTITY);
  307. _db.set_property_vector3 (component_id, "data.collider_data.half_extents", VECTOR3_ZERO); // for "box"
  308. _db.set_property_double (component_id, "data.collider_data.radius", 0.0); // for "sphere" and "capsule"
  309. _db.set_property_double (component_id, "data.collider_data.height", 0.0); // for "capsule"
  310. } else if (component_type == OBJECT_TYPE_ACTOR) {
  311. _db.set_property_bool (component_id, "data.lock_translation_x", false);
  312. _db.set_property_bool (component_id, "data.lock_translation_y", false);
  313. _db.set_property_bool (component_id, "data.lock_translation_z", false);
  314. _db.set_property_bool (component_id, "data.lock_rotation_x", false);
  315. _db.set_property_bool (component_id, "data.lock_rotation_y", false);
  316. _db.set_property_bool (component_id, "data.lock_rotation_z", false);
  317. _db.set_property_string(component_id, "data.class", "static");
  318. _db.set_property_double(component_id, "data.mass", 1.0);
  319. _db.set_property_string(component_id, "data.collision_filter", "default");
  320. _db.set_property_string(component_id, "data.material", "default");
  321. } else if (component_type == OBJECT_TYPE_ANIMATION_STATE_MACHINE) {
  322. _db.set_property_string(component_id, "data.state_machine_resource", "core/components/noop");
  323. } else {
  324. logw("Unregistered component type `%s`".printf(component_type));
  325. }
  326. _db.add_to_set(_id, "components", component_id);
  327. return component_id;
  328. }
  329. /// Removes the @a component_type from the unit.
  330. public void remove_component_type(string component_type)
  331. {
  332. Guid component_id;
  333. Guid owner_id;
  334. if (has_component_with_owner(out component_id, out owner_id, component_type)) {
  335. if (_id == owner_id) {
  336. _db.remove_from_set(_id, "components", component_id);
  337. } else {
  338. _db.set_property_bool(_id, "deleted_components.#" + component_id.to_string(), false);
  339. // Clean all modified_components keys that matches the deleted component ID.
  340. string[] unit_keys = _db.get_keys(_id);
  341. for (int ii = 0; ii < unit_keys.length; ++ii) {
  342. if (unit_keys[ii].has_prefix("modified_components.#" + component_id.to_string()))
  343. _db.set_property_null(_id, unit_keys[ii]);
  344. }
  345. }
  346. _db.add_restore_point((int)ActionType.DESTROY_OBJECTS, new Guid?[] { _id, component_id });
  347. } else {
  348. logw("The unit has no such component type `%s`".printf(component_type));
  349. }
  350. }
  351. public static void register_component_type(string type, string depends_on)
  352. {
  353. if (_component_registry == null)
  354. _component_registry = new Hashtable();
  355. _component_registry[type] = depends_on;
  356. }
  357. public string? prefab()
  358. {
  359. return _db.has_property(_id, "prefab")
  360. ? _db.get_property_string(_id, "prefab")
  361. : null
  362. ;
  363. }
  364. /// Returns whether the unit has a prefab.
  365. public bool has_prefab()
  366. {
  367. return _db.has_property(_id, "prefab");
  368. }
  369. /// Returns whether the unit is a light unit.
  370. public bool is_light()
  371. {
  372. return has_prefab()
  373. && _db.get_property_string(_id, "prefab") == "core/units/light";
  374. }
  375. /// Returns whether the unit is a camera unit.
  376. public bool is_camera()
  377. {
  378. return has_prefab()
  379. && _db.get_property_string(_id, "prefab") == "core/units/camera";
  380. }
  381. public static int generate_spawn_unit_commands(StringBuilder sb, Guid?[] object_ids, Database db)
  382. {
  383. int i;
  384. if (object_ids.length > 1 && Unit.is_component(object_ids[1], db)) {
  385. for (i = 1; i < object_ids.length; ++i) {
  386. if (!is_component(object_ids[i], db))
  387. break;
  388. Guid unit_id = object_ids[0];
  389. Guid component_id = object_ids[i];
  390. string component_type = db.object_type(component_id);
  391. Unit unit = Unit(db, unit_id);
  392. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  393. if (component_type == OBJECT_TYPE_TRANSFORM) {
  394. sb.append(LevelEditorApi.add_tranform_component(unit_id
  395. , component_id
  396. , unit.get_component_property_vector3 (component_id, "data.position")
  397. , unit.get_component_property_quaternion(component_id, "data.rotation")
  398. , unit.get_component_property_vector3 (component_id, "data.scale")
  399. ));
  400. } else if (component_type == OBJECT_TYPE_CAMERA) {
  401. sb.append(LevelEditorApi.add_camera_component(unit_id
  402. , component_id
  403. , unit.get_component_property_string(component_id, "data.projection")
  404. , unit.get_component_property_double(component_id, "data.fov")
  405. , unit.get_component_property_double(component_id, "data.far_range")
  406. , unit.get_component_property_double(component_id, "data.near_range")
  407. ));
  408. } else if (component_type == OBJECT_TYPE_MESH_RENDERER) {
  409. sb.append(LevelEditorApi.add_mesh_renderer_component(unit_id
  410. , component_id
  411. , unit.get_component_property_string(component_id, "data.mesh_resource")
  412. , unit.get_component_property_string(component_id, "data.geometry_name")
  413. , unit.get_component_property_string(component_id, "data.material")
  414. , unit.get_component_property_bool (component_id, "data.visible")
  415. , unit.get_component_property_bool (component_id, "data.cast_shadows", true)
  416. ));
  417. } else if (component_type == OBJECT_TYPE_SPRITE_RENDERER) {
  418. sb.append(LevelEditorApi.add_sprite_renderer_component(unit_id
  419. , component_id
  420. , unit.get_component_property_string(component_id, "data.sprite_resource")
  421. , unit.get_component_property_string(component_id, "data.material")
  422. , unit.get_component_property_double(component_id, "data.layer")
  423. , unit.get_component_property_double(component_id, "data.depth")
  424. , unit.get_component_property_bool (component_id, "data.visible")
  425. , unit.get_component_property_bool (component_id, "data.flip_x")
  426. , unit.get_component_property_bool (component_id, "data.flip_y")
  427. ));
  428. } else if (component_type == OBJECT_TYPE_LIGHT) {
  429. sb.append(LevelEditorApi.add_light_component(unit_id
  430. , component_id
  431. , unit.get_component_property_string (component_id, "data.type")
  432. , unit.get_component_property_double (component_id, "data.range")
  433. , unit.get_component_property_double (component_id, "data.intensity")
  434. , unit.get_component_property_double (component_id, "data.spot_angle")
  435. , unit.get_component_property_vector3(component_id, "data.color")
  436. , unit.get_component_property_double (component_id, "data.shadow_bias", 0.0001)
  437. , unit.get_component_property_bool (component_id, "data.cast_shadows", true)
  438. ));
  439. } else if (component_type == OBJECT_TYPE_FOG) {
  440. sb.append(LevelEditorApi.add_fog_component(unit_id, component_id));
  441. sb.append(LevelEditorApi.set_fog(unit_id
  442. , unit.get_component_property_vector3(component_id, "data.color")
  443. , unit.get_component_property_double (component_id, "data.density")
  444. , unit.get_component_property_double (component_id, "data.range_min")
  445. , unit.get_component_property_double (component_id, "data.range_max")
  446. , unit.get_component_property_double (component_id, "data.sun_blend")
  447. , unit.get_component_property_bool (component_id, "data.enabled")
  448. ));
  449. } else if (component_type == OBJECT_TYPE_SCRIPT) {
  450. /*
  451. * sb.append(LevelEditorApi.add_script_component(unit_id
  452. * , component_id
  453. * , unit.get_component_property_string(component_id, "data.script_resource")
  454. * ));
  455. */
  456. } else if (component_type == OBJECT_TYPE_COLLIDER) {
  457. /*
  458. * sb.append(LevelEditorApi.add_collider_component(unit_id
  459. * , component_id
  460. * , unit.get_component_property_string (component_id, "data.shape")
  461. * , unit.get_component_property_string (component_id, "data.source")
  462. * // if "mesh"
  463. * , unit.get_component_property_string (component_id, "data.scene")
  464. * , unit.get_component_property_string (component_id, "data.name")
  465. * // if "inline"
  466. * , unit.get_component_property_vector3 (component_id, "data.collider_data.position")
  467. * , unit.get_component_property_quaternion(component_id, "data.collider_data.rotation")
  468. * , unit.get_component_property_vector3 (component_id, "data.collider_data.half_extents")
  469. * , unit.get_component_property_double (component_id, "data.collider_data.radius")
  470. * , unit.get_component_property_double (component_id, "data.collider_data.height")
  471. * ));
  472. */
  473. } else if (component_type == OBJECT_TYPE_ACTOR) {
  474. /*
  475. * sb.append(LevelEditorApi.add_actor_component(unit_id
  476. * , component_id
  477. * , unit.get_component_property_bool (component_id, "data.lock_translation_x")
  478. * , unit.get_component_property_bool (component_id, "data.lock_translation_y")
  479. * , unit.get_component_property_bool (component_id, "data.lock_translation_z")
  480. * , unit.get_component_property_bool (component_id, "data.lock_rotation_x")
  481. * , unit.get_component_property_bool (component_id, "data.lock_rotation_y")
  482. * , unit.get_component_property_bool (component_id, "data.lock_rotation_z")
  483. * , unit.get_component_property_string(component_id, "data.class")
  484. * , unit.get_component_property_double(component_id, "data.mass")
  485. * , unit.get_component_property_string(component_id, "data.collision_filter")
  486. * , unit.get_component_property_string(component_id, "data.material")
  487. * ));
  488. */
  489. } else if (component_type == OBJECT_TYPE_ANIMATION_STATE_MACHINE) {
  490. /*
  491. * sb.append(LevelEditorApi.add_animation_state_machine_component(unit_id
  492. * , component_id
  493. * , unit.get_component_property_string(component_id, "data.state_machine_resource")
  494. * ));
  495. */
  496. } else {
  497. logw("Unregistered component type `%s`".printf(component_type));
  498. }
  499. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  500. }
  501. } else {
  502. for (i = 0; i < object_ids.length; ++i) {
  503. if (db.object_type(object_ids[i]) != OBJECT_TYPE_UNIT)
  504. break;
  505. Guid unit_id = object_ids[i];
  506. Unit unit = Unit(db, unit_id);
  507. string? prefab = unit.prefab();
  508. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  509. if (prefab != null) {
  510. sb.append(LevelEditorApi.spawn_unit(unit_id
  511. , prefab
  512. , unit.local_position()
  513. , unit.local_rotation()
  514. , unit.local_scale()
  515. ));
  516. foreach (var entry in Unit._component_registry.entries) {
  517. Guid component_id;
  518. if (!unit.has_component(out component_id, entry.key))
  519. continue;
  520. generate_change_commands(sb, { unit_id, component_id }, db);
  521. }
  522. } else {
  523. sb.append(LevelEditorApi.spawn_empty_unit(unit_id));
  524. Guid component_id;
  525. if (unit.has_component(out component_id, OBJECT_TYPE_TRANSFORM)) {
  526. string s = LevelEditorApi.add_tranform_component(unit_id
  527. , component_id
  528. , unit.get_component_property_vector3 (component_id, "data.position")
  529. , unit.get_component_property_quaternion(component_id, "data.rotation")
  530. , unit.get_component_property_vector3 (component_id, "data.scale")
  531. );
  532. sb.append(s);
  533. }
  534. if (unit.has_component(out component_id, OBJECT_TYPE_CAMERA)) {
  535. string s = LevelEditorApi.add_camera_component(unit_id
  536. , component_id
  537. , unit.get_component_property_string(component_id, "data.projection")
  538. , unit.get_component_property_double(component_id, "data.fov")
  539. , unit.get_component_property_double(component_id, "data.far_range")
  540. , unit.get_component_property_double(component_id, "data.near_range")
  541. );
  542. sb.append(s);
  543. }
  544. if (unit.has_component(out component_id, OBJECT_TYPE_MESH_RENDERER)) {
  545. string s = LevelEditorApi.add_mesh_renderer_component(unit_id
  546. , component_id
  547. , unit.get_component_property_string(component_id, "data.mesh_resource")
  548. , unit.get_component_property_string(component_id, "data.geometry_name")
  549. , unit.get_component_property_string(component_id, "data.material")
  550. , unit.get_component_property_bool (component_id, "data.visible")
  551. , unit.get_component_property_bool (component_id, "data.cast_shadows", true)
  552. );
  553. sb.append(s);
  554. }
  555. if (unit.has_component(out component_id, OBJECT_TYPE_SPRITE_RENDERER)) {
  556. string s = LevelEditorApi.add_sprite_renderer_component(unit_id
  557. , component_id
  558. , unit.get_component_property_string(component_id, "data.sprite_resource")
  559. , unit.get_component_property_string(component_id, "data.material")
  560. , unit.get_component_property_double(component_id, "data.layer")
  561. , unit.get_component_property_double(component_id, "data.depth")
  562. , unit.get_component_property_bool (component_id, "data.visible")
  563. , unit.get_component_property_bool (component_id, "data.flip_x")
  564. , unit.get_component_property_bool (component_id, "data.flip_y")
  565. );
  566. sb.append(s);
  567. }
  568. if (unit.has_component(out component_id, OBJECT_TYPE_LIGHT)) {
  569. string s = LevelEditorApi.add_light_component(unit_id
  570. , component_id
  571. , unit.get_component_property_string (component_id, "data.type")
  572. , unit.get_component_property_double (component_id, "data.range")
  573. , unit.get_component_property_double (component_id, "data.intensity")
  574. , unit.get_component_property_double (component_id, "data.spot_angle")
  575. , unit.get_component_property_vector3(component_id, "data.color")
  576. , unit.get_component_property_double (component_id, "data.shadow_bias", 0.0001)
  577. , unit.get_component_property_bool (component_id, "data.cast_shadows", true)
  578. );
  579. sb.append(s);
  580. }
  581. if (unit.has_component(out component_id, OBJECT_TYPE_FOG)) {
  582. sb.append(LevelEditorApi.add_fog_component(unit_id, component_id));
  583. sb.append(LevelEditorApi.set_fog(unit_id
  584. , unit.get_component_property_vector3(component_id, "data.color")
  585. , unit.get_component_property_double (component_id, "data.density")
  586. , unit.get_component_property_double (component_id, "data.range_min")
  587. , unit.get_component_property_double (component_id, "data.range_max")
  588. , unit.get_component_property_double (component_id, "data.sun_blend")
  589. , unit.get_component_property_bool (component_id, "data.enabled")
  590. ));
  591. }
  592. }
  593. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  594. }
  595. }
  596. return i;
  597. }
  598. public static int generate_destroy_commands(StringBuilder sb, Guid?[] object_ids, Database db)
  599. {
  600. int i;
  601. if (object_ids.length > 1 && Unit.is_component(object_ids[1], db)) {
  602. for (i = 1; i < object_ids.length; ++i) {
  603. if (!is_component(object_ids[i], db))
  604. break;
  605. Guid unit_id = object_ids[0];
  606. Guid component_id = object_ids[i];
  607. string component_type = db.object_type(component_id);
  608. sb.append(LevelEditorApi.unit_destroy_component_type(unit_id, component_type));
  609. }
  610. } else {
  611. for (i = 0; i < object_ids.length; ++i) {
  612. if (db.object_type(object_ids[i]) != OBJECT_TYPE_UNIT)
  613. break;
  614. sb.append(LevelEditorApi.destroy(object_ids[i]));
  615. }
  616. }
  617. return i;
  618. }
  619. public static int generate_change_commands(StringBuilder sb, Guid?[] object_ids, Database db)
  620. {
  621. int i;
  622. if (object_ids.length > 1 && Unit.is_component(object_ids[1], db)) {
  623. for (i = 1; i < object_ids.length; ++i) {
  624. if (!is_component(object_ids[i], db))
  625. break;
  626. Guid unit_id = object_ids[0];
  627. Guid component_id = object_ids[i];
  628. string component_type = db.object_type(component_id);
  629. Unit unit = Unit(db, unit_id);
  630. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  631. if (component_type == OBJECT_TYPE_TRANSFORM) {
  632. sb.append(LevelEditorApi.move_object(unit_id
  633. , unit.get_component_property_vector3 (component_id, "data.position")
  634. , unit.get_component_property_quaternion(component_id, "data.rotation")
  635. , unit.get_component_property_vector3 (component_id, "data.scale")
  636. ));
  637. } else if (component_type == OBJECT_TYPE_CAMERA) {
  638. sb.append(LevelEditorApi.set_camera(unit_id
  639. , unit.get_component_property_string(component_id, "data.projection")
  640. , unit.get_component_property_double(component_id, "data.fov")
  641. , unit.get_component_property_double(component_id, "data.far_range")
  642. , unit.get_component_property_double(component_id, "data.near_range")
  643. ));
  644. } else if (component_type == OBJECT_TYPE_MESH_RENDERER) {
  645. sb.append(LevelEditorApi.set_mesh(unit_id
  646. , unit.get_component_property_string(component_id, "data.mesh_resource")
  647. , unit.get_component_property_string(component_id, "data.geometry_name")
  648. , unit.get_component_property_string(component_id, "data.material")
  649. , unit.get_component_property_bool (component_id, "data.visible")
  650. , unit.get_component_property_bool (component_id, "data.cast_shadows", true)
  651. ));
  652. } else if (component_type == OBJECT_TYPE_SPRITE_RENDERER) {
  653. sb.append(LevelEditorApi.set_sprite(unit_id
  654. , unit.get_component_property_string(component_id, "data.sprite_resource")
  655. , unit.get_component_property_string(component_id, "data.material")
  656. , unit.get_component_property_double(component_id, "data.layer")
  657. , unit.get_component_property_double(component_id, "data.depth")
  658. , unit.get_component_property_bool (component_id, "data.visible")
  659. , unit.get_component_property_bool (component_id, "data.flip_x")
  660. , unit.get_component_property_bool (component_id, "data.flip_y")
  661. ));
  662. } else if (component_type == OBJECT_TYPE_LIGHT) {
  663. sb.append(LevelEditorApi.set_light(unit_id
  664. , unit.get_component_property_string (component_id, "data.type")
  665. , unit.get_component_property_double (component_id, "data.range")
  666. , unit.get_component_property_double (component_id, "data.intensity")
  667. , unit.get_component_property_double (component_id, "data.spot_angle")
  668. , unit.get_component_property_vector3(component_id, "data.color")
  669. , unit.get_component_property_double (component_id, "data.shadow_bias", 0.0001)
  670. , unit.get_component_property_bool (component_id, "data.cast_shadows", true)
  671. ));
  672. } else if (component_type == OBJECT_TYPE_FOG) {
  673. sb.append(LevelEditorApi.set_fog(unit_id
  674. , unit.get_component_property_vector3(component_id, "data.color")
  675. , unit.get_component_property_double (component_id, "data.density")
  676. , unit.get_component_property_double (component_id, "data.range_min")
  677. , unit.get_component_property_double (component_id, "data.range_max")
  678. , unit.get_component_property_double (component_id, "data.sun_blend")
  679. , unit.get_component_property_bool (component_id, "data.enabled")
  680. ));
  681. } else if (component_type == OBJECT_TYPE_SCRIPT) {
  682. /* No sync. */
  683. } else if (component_type == OBJECT_TYPE_COLLIDER) {
  684. /* No sync. */
  685. } else if (component_type == OBJECT_TYPE_ACTOR) {
  686. /* No sync. */
  687. } else if (component_type == OBJECT_TYPE_ANIMATION_STATE_MACHINE) {
  688. /* No sync. */
  689. } else {
  690. logw("Unregistered component type `%s`".printf(component_type));
  691. }
  692. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  693. }
  694. } else {
  695. for (i = 0; i < object_ids.length; ++i) {
  696. if (db.object_type(object_ids[i]) != OBJECT_TYPE_UNIT)
  697. break;
  698. Guid unit_id = object_ids[i];
  699. Unit unit = Unit(db, unit_id);
  700. sb.append("editor_nv, editor_nq, editor_nm = Device.temp_count()");
  701. sb.append(LevelEditorApi.move_object(unit_id
  702. , unit.local_position()
  703. , unit.local_rotation()
  704. , unit.local_scale()
  705. ));
  706. sb.append("Device.set_temp_count(editor_nv, editor_nq, editor_nm)");
  707. }
  708. }
  709. return i;
  710. }
  711. public static bool is_component(Guid id, Database db)
  712. {
  713. string type = db.object_type(id);
  714. return type == OBJECT_TYPE_TRANSFORM
  715. || type == OBJECT_TYPE_CAMERA
  716. || type == OBJECT_TYPE_MESH_RENDERER
  717. || type == OBJECT_TYPE_SPRITE_RENDERER
  718. || type == OBJECT_TYPE_LIGHT
  719. || type == OBJECT_TYPE_FOG
  720. || type == OBJECT_TYPE_SCRIPT
  721. || type == OBJECT_TYPE_COLLIDER
  722. || type == OBJECT_TYPE_ACTOR
  723. || type == OBJECT_TYPE_ANIMATION_STATE_MACHINE
  724. ;
  725. }
  726. public void add_component_type_dependencies(ref Gee.ArrayList<Guid?> components_added, string component_type)
  727. {
  728. Guid dummy;
  729. if (has_component(out dummy, component_type))
  730. return;
  731. string[] component_type_dependencies = ((string)Unit._component_registry[component_type]).split(", ");
  732. foreach (unowned string dependency in component_type_dependencies) {
  733. Guid dependency_component_id;
  734. if (!has_component(out dependency_component_id, dependency))
  735. add_component_type_dependencies(ref components_added, dependency);
  736. }
  737. components_added.add(add_component_type(component_type));
  738. }
  739. }
  740. } /* namespace Crown */