level.vala 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. /// Manages objects in a level.
  8. public class Level
  9. {
  10. public Project _project;
  11. // Engine connections
  12. public RuntimeInstance _runtime;
  13. // Data
  14. public Database _db;
  15. public Gee.ArrayList<Guid?> _selection;
  16. public Gee.HashMap<string, uint> _unit_names;
  17. public Gee.HashMap<string, uint> _sound_names;
  18. public string _name;
  19. public string _path;
  20. public Guid _id;
  21. public Vector3 _camera_position;
  22. public Quaternion _camera_rotation;
  23. public double _camera_orthographic_size;
  24. public double _camera_target_distance;
  25. public CameraViewType _camera_view_type;
  26. // Signals
  27. public signal void selection_changed(Gee.ArrayList<Guid?> selection);
  28. public signal void object_editor_name_changed(Guid object_id, string name);
  29. public Level(Database db, RuntimeInstance runtime, Project project)
  30. {
  31. _project = project;
  32. // Engine connections
  33. _runtime = runtime;
  34. // Data
  35. _db = db;
  36. _selection = new Gee.ArrayList<Guid?>(Guid.equal_func);
  37. _unit_names = new Gee.HashMap<string, uint>();
  38. _sound_names = new Gee.HashMap<string, uint>();
  39. reset();
  40. }
  41. /// Resets the level
  42. public void reset()
  43. {
  44. _db.reset();
  45. _selection.clear();
  46. selection_changed(_selection);
  47. _unit_names.clear();
  48. _sound_names.clear();
  49. _name = null;
  50. _path = null;
  51. _id = GUID_ZERO;
  52. _camera_position = Vector3(20, -20, 20);
  53. _camera_rotation = Quaternion.look(Vector3(-20, 20, -20).normalize(), Vector3(0.0, 0.0, 1.0));
  54. _camera_target_distance = _camera_position.length();
  55. _camera_orthographic_size = _camera_target_distance;
  56. _camera_view_type = CameraViewType.PERSPECTIVE;
  57. }
  58. public int load_from_path(string name)
  59. {
  60. string resource_path = name + ".level";
  61. string path = _project.absolute_path(resource_path);
  62. FileStream fs = FileStream.open(path, "rb");
  63. if (fs == null)
  64. return 1;
  65. reset();
  66. int ret = _db.load_from_file(out _id, fs, resource_path);
  67. if (ret != 0)
  68. return ret;
  69. camera_decode();
  70. GLib.Application.get_default().activate_action("camera-view", new GLib.Variant.int32(_camera_view_type));
  71. _name = name;
  72. _path = path;
  73. // Level files loaded from outside the source directory can be visualized and
  74. // modified in-memory, but never overwritten on disk, because they might be
  75. // shared with other projects (e.g. toolchain). Ensure that _path is null to
  76. // force save functions to choose a different path (inside the source
  77. // directory).
  78. if (!_project.path_is_within_source_dir(path))
  79. _path = null;
  80. return 0;
  81. }
  82. public int save(string name)
  83. {
  84. string path = Path.build_filename(_project.source_dir(), name + ".level");
  85. camera_encode();
  86. int err = _db.save(path, _id);
  87. _path = path;
  88. _name = name;
  89. return err;
  90. }
  91. public void spawn_unit(string? name)
  92. {
  93. Guid id = Guid.new_guid();
  94. on_unit_spawned(id, name, VECTOR3_ZERO, QUATERNION_IDENTITY, VECTOR3_ONE);
  95. _db.add_restore_point((int)ActionType.CREATE_OBJECTS, new Guid?[] { id });
  96. selection_set(new Guid?[] { id });
  97. }
  98. public void destroy_objects(Guid?[] ids)
  99. {
  100. foreach (Guid id in ids) {
  101. _selection.remove(id);
  102. if (_db.object_type(id) == OBJECT_TYPE_UNIT) {
  103. _db.remove_from_set(_id, "units", id);
  104. _db.destroy(id);
  105. } else if (_db.object_type(id) == OBJECT_TYPE_SOUND_SOURCE) {
  106. _db.remove_from_set(_id, "sounds", id);
  107. _db.destroy(id);
  108. }
  109. }
  110. _db.add_restore_point((int)ActionType.DESTROY_OBJECTS, ids);
  111. }
  112. public void duplicate_selected_objects()
  113. {
  114. if (_selection.size > 0) {
  115. Guid?[] ids = _selection.to_array();
  116. Guid?[] new_ids = new Guid?[ids.length];
  117. for (int i = 0; i < new_ids.length; ++i)
  118. new_ids[i] = Guid.new_guid();
  119. duplicate_objects(ids, new_ids);
  120. }
  121. }
  122. public void destroy_selected_objects()
  123. {
  124. destroy_objects(_selection.to_array());
  125. }
  126. public void duplicate_objects(Guid?[] ids, Guid?[] new_ids)
  127. {
  128. for (int i = 0; i < ids.length; ++i) {
  129. _db.duplicate(ids[i], new_ids[i]);
  130. if (_db.object_type(ids[i]) == OBJECT_TYPE_UNIT) {
  131. _db.add_to_set(_id, "units", new_ids[i]);
  132. } else if (_db.object_type(ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  133. _db.add_to_set(_id, "sounds", new_ids[i]);
  134. }
  135. }
  136. _db.add_restore_point((int)ActionType.CREATE_OBJECTS, new_ids);
  137. selection_set(ids);
  138. }
  139. public void replace_unit(Guid unit_id, string prefab_name)
  140. {
  141. string unit_editor_name = _db.object_name(unit_id);
  142. Unit unit = Unit(_db, unit_id);
  143. Vector3 unit_pos = unit.local_position();
  144. Quaternion unit_rot = unit.local_rotation();
  145. Vector3 unit_scl = unit.local_scale();
  146. destroy_objects(new Guid?[] { unit_id });
  147. Guid new_id = Guid.new_guid();
  148. Unit new_unit = Unit(_db, new_id);
  149. new_unit.create(prefab_name);
  150. new_unit.set_local_position(unit_pos);
  151. new_unit.set_local_rotation(unit_rot);
  152. new_unit.set_local_scale(unit_scl);
  153. _db.set_object_name(new_id, unit_editor_name);
  154. _db.add_to_set(_id, "units", new_id);
  155. _db.add_restore_point((int)ActionType.CREATE_OBJECTS, new Guid?[] { new_id });
  156. }
  157. public string add_object_name(Gee.HashMap<string, uint> names, string resource_name)
  158. {
  159. string basename = GLib.Path.get_basename(resource_name);
  160. uint num = 0;
  161. if (!names.has_key(basename)) {
  162. names[basename] = 1;
  163. } else {
  164. num = names[basename];
  165. names.set(basename, num + 1);
  166. }
  167. return num > 0 ? "%s%u".printf(basename, num + 1) : basename;
  168. }
  169. public void on_unit_spawned(Guid id, string? name, Vector3 pos, Quaternion rot, Vector3 scl)
  170. {
  171. Unit unit = Unit(_db, id);
  172. unit.create(name);
  173. unit.set_local_position(pos);
  174. unit.set_local_rotation(rot);
  175. unit.set_local_scale(scl);
  176. _db.set_object_name(id, add_object_name(_unit_names, name != null ? name : "unit"));
  177. _db.add_to_set(_id, "units", id);
  178. }
  179. public void on_sound_spawned(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl, double range, double volume, bool loop)
  180. {
  181. Sound sound = Sound(_db, id);
  182. sound.create(name, pos, rot, scl, range, volume, loop);
  183. _db.set_object_name(id, add_object_name(_sound_names, name));
  184. _db.add_to_set(_id, "sounds", id);
  185. }
  186. public void on_move_objects(Guid?[] ids, Vector3[] positions, Quaternion[] rotations, Vector3[] scales)
  187. {
  188. for (int i = 0; i < ids.length; ++i) {
  189. if (_db.object_type(ids[i]) == OBJECT_TYPE_UNIT) {
  190. Unit unit = Unit(_db, ids[i]);
  191. unit.set_local_position(positions[i]);
  192. unit.set_local_rotation(rotations[i]);
  193. unit.set_local_scale(scales[i]);
  194. } else if (_db.object_type(ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  195. Sound sound = Sound(_db, ids[i]);
  196. sound.set_local_position(positions[i]);
  197. sound.set_local_rotation(rotations[i]);
  198. sound.set_local_scale(scales[i]);
  199. }
  200. }
  201. }
  202. public void on_selection(Guid[] ids)
  203. {
  204. _selection.clear();
  205. foreach (Guid id in ids)
  206. _selection.add(id);
  207. selection_changed(_selection);
  208. }
  209. public void selection_set(Guid?[] ids)
  210. {
  211. _selection.clear();
  212. for (int i = 0; i < ids.length; ++i)
  213. _selection.add(ids[i]);
  214. send_selection();
  215. _runtime.send(DeviceApi.frame());
  216. selection_changed(_selection);
  217. }
  218. public void send_selection()
  219. {
  220. _runtime.send_script(LevelEditorApi.selection_set(_selection.to_array()));
  221. }
  222. public string object_editor_name(Guid object_id)
  223. {
  224. return _db.object_name(object_id);
  225. }
  226. public void object_set_editor_name(Guid object_id, string name)
  227. {
  228. _db.set_object_name(object_id, name);
  229. _db.add_restore_point((int)ActionType.OBJECT_SET_EDITOR_NAME, new Guid?[] { object_id });
  230. }
  231. public void generate_spawn_objects(StringBuilder sb, Guid?[] object_ids)
  232. {
  233. int i = 0;
  234. while (i < object_ids.length) {
  235. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  236. i += Unit.generate_spawn_unit_commands(sb, object_ids[i:object_ids.length], _db);
  237. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  238. i += Sound.generate_spawn_sound_commands(sb, object_ids[i:object_ids.length], _db);
  239. } else {
  240. ++i; // Skip object.
  241. }
  242. }
  243. }
  244. public void generate_destroy_objects(StringBuilder sb, Guid?[] object_ids)
  245. {
  246. int i = 0;
  247. while (i < object_ids.length) {
  248. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  249. i += Unit.generate_destroy_commands(sb, object_ids[i:object_ids.length], _db);
  250. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  251. sb.append(LevelEditorApi.destroy(object_ids[i]));
  252. ++i;
  253. } else {
  254. ++i; // Skip object.
  255. }
  256. }
  257. }
  258. public void generate_change_objects(StringBuilder sb, Guid?[] object_ids)
  259. {
  260. int i = 0;
  261. while (i < object_ids.length) {
  262. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  263. i += Unit.generate_change_commands(sb, object_ids[i:object_ids.length], _db);
  264. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  265. i += Sound.generate_change_sound_commands(sb, object_ids[i:object_ids.length], _db);
  266. } else {
  267. ++i; // Skip object.
  268. }
  269. }
  270. }
  271. public void send_level()
  272. {
  273. Gee.ArrayList<Guid?> unit_ids = new Gee.ArrayList<Guid?>();
  274. Gee.ArrayList<Guid?> sound_ids = new Gee.ArrayList<Guid?>();
  275. units(ref unit_ids);
  276. sounds(ref sound_ids);
  277. StringBuilder sb = new StringBuilder();
  278. sb.append(LevelEditorApi.reset());
  279. Unit.generate_spawn_unit_commands(sb, unit_ids.to_array(), _db);
  280. Sound.generate_spawn_sound_commands(sb, sound_ids.to_array(), _db);
  281. sb.append(LevelEditorApi.spawn_skydome(_db.get_property_string(_id, "skydome_unit", "core/units/skydome/skydome")));
  282. _runtime.send_script(sb.str);
  283. send_selection();
  284. send_camera();
  285. }
  286. public void units(ref Gee.ArrayList<Guid?> ids)
  287. {
  288. Gee.HashSet<Guid?> units = _db.get_property_set(_id, "units", new Gee.HashSet<Guid?>());
  289. ids.add_all(units);
  290. }
  291. public void sounds(ref Gee.ArrayList<Guid?> ids)
  292. {
  293. Gee.HashSet<Guid?> sounds = _db.get_property_set(_id, "sounds", new Gee.HashSet<Guid?>());
  294. ids.add_all(sounds);
  295. }
  296. public void objects(ref Gee.ArrayList<Guid?> ids)
  297. {
  298. units(ref ids);
  299. sounds(ref ids);
  300. }
  301. public void camera_decode()
  302. {
  303. string properties[] =
  304. {
  305. "editor.camera.position",
  306. "editor.camera.rotation",
  307. "editor.camera.target_distance",
  308. "editor.camera.orthographic_size",
  309. "editor.camera.view_type"
  310. };
  311. foreach (var p in properties) {
  312. if (!_db.has_property(_id, p))
  313. return;
  314. }
  315. _camera_position = _db.get_property_vector3(_id, "editor.camera.position");
  316. _camera_rotation = _db.get_property_quaternion(_id, "editor.camera.rotation");
  317. _camera_target_distance = _db.get_property_double(_id, "editor.camera.target_distance");
  318. _camera_orthographic_size = _db.get_property_double(_id, "editor.camera.orthographic_size");
  319. _camera_view_type = (CameraViewType)_db.get_property_double(_id, "editor.camera.view_type");
  320. }
  321. public void camera_encode()
  322. {
  323. _db.set_property_internal(0, _id, "editor.camera.position", _camera_position);
  324. _db.set_property_internal(0, _id, "editor.camera.rotation", _camera_rotation);
  325. _db.set_property_internal(0, _id, "editor.camera.target_distance", _camera_target_distance);
  326. _db.set_property_internal(0, _id, "editor.camera.orthographic_size", _camera_orthographic_size);
  327. _db.set_property_internal(0, _id, "editor.camera.view_type", (double)_camera_view_type);
  328. }
  329. public void send_camera()
  330. {
  331. _runtime.send_script(LevelEditorApi.camera_restore(_camera_position
  332. , _camera_rotation
  333. , _camera_orthographic_size
  334. , _camera_target_distance
  335. , _camera_view_type
  336. ));
  337. }
  338. public void on_camera(Hashtable msg)
  339. {
  340. _camera_position = Vector3.from_array((Gee.ArrayList<Value?>)msg["position"]);
  341. _camera_rotation = Quaternion.from_array((Gee.ArrayList<Value?>)msg["rotation"]);
  342. _camera_orthographic_size = (double)msg["orthographic_size"];
  343. _camera_target_distance = (double)msg["target_distance"];
  344. }
  345. }
  346. } /* namespace Crown */