level.vala 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 uint _num_units;
  17. public uint _num_sounds;
  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. reset();
  38. }
  39. /// Resets the level
  40. public void reset()
  41. {
  42. _db.reset();
  43. _selection.clear();
  44. selection_changed(_selection);
  45. _num_units = 0;
  46. _num_sounds = 0;
  47. _name = null;
  48. _path = null;
  49. _id = GUID_ZERO;
  50. _camera_position = Vector3(20, -20, 20);
  51. _camera_rotation = Quaternion.look(Vector3(-20, 20, -20).normalize(), Vector3(0.0, 0.0, 1.0));
  52. _camera_target_distance = _camera_position.length();
  53. _camera_orthographic_size = _camera_target_distance;
  54. _camera_view_type = CameraViewType.PERSPECTIVE;
  55. }
  56. public int load_from_path(string name)
  57. {
  58. string resource_path = name + ".level";
  59. string path = _project.absolute_path(resource_path);
  60. FileStream fs = FileStream.open(path, "rb");
  61. if (fs == null)
  62. return 1;
  63. reset();
  64. int ret = _db.load_from_file(out _id, fs, resource_path);
  65. if (ret != 0)
  66. return ret;
  67. camera_decode();
  68. GLib.Application.get_default().activate_action("camera-view", new GLib.Variant.int32(_camera_view_type));
  69. _name = name;
  70. _path = path;
  71. // Level files loaded from outside the source directory can be visualized and
  72. // modified in-memory, but never overwritten on disk, because they might be
  73. // shared with other projects (e.g. toolchain). Ensure that _path is null to
  74. // force save functions to choose a different path (inside the source
  75. // directory).
  76. if (!_project.path_is_within_source_dir(path))
  77. _path = null;
  78. // FIXME: hack to keep the LevelTreeView working.
  79. _db.key_changed(_id, "units");
  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_empty_unit()
  92. {
  93. Guid id = Guid.new_guid();
  94. on_unit_spawned(id, null, 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 on_unit_spawned(Guid id, string? name, Vector3 pos, Quaternion rot, Vector3 scl)
  140. {
  141. Unit unit = Unit(_db, id);
  142. unit.create(name, pos, rot, scl);
  143. _db.set_property_string(id, "editor.name", "unit_%04u".printf(_num_units++));
  144. _db.add_to_set(_id, "units", id);
  145. }
  146. public void on_sound_spawned(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl, double range, double volume, bool loop)
  147. {
  148. Sound sound = Sound(_db, id);
  149. sound.create(name, pos, rot, scl, range, volume, loop);
  150. _db.set_property_string (id, "editor.name", "sound_%04u".printf(_num_sounds++));
  151. _db.add_to_set(_id, "sounds", id);
  152. }
  153. public void on_move_objects(Guid?[] ids, Vector3[] positions, Quaternion[] rotations, Vector3[] scales)
  154. {
  155. for (int i = 0; i < ids.length; ++i) {
  156. if (_db.object_type(ids[i]) == OBJECT_TYPE_UNIT) {
  157. Unit unit = Unit(_db, ids[i]);
  158. unit.set_local_position(positions[i]);
  159. unit.set_local_rotation(rotations[i]);
  160. unit.set_local_scale(scales[i]);
  161. } else if (_db.object_type(ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  162. Sound sound = Sound(_db, ids[i]);
  163. sound.set_local_position(positions[i]);
  164. sound.set_local_rotation(rotations[i]);
  165. sound.set_local_scale(scales[i]);
  166. }
  167. }
  168. }
  169. public void on_selection(Guid[] ids)
  170. {
  171. _selection.clear();
  172. foreach (Guid id in ids)
  173. _selection.add(id);
  174. selection_changed(_selection);
  175. }
  176. public void selection_set(Guid?[] ids)
  177. {
  178. _selection.clear();
  179. for (int i = 0; i < ids.length; ++i)
  180. _selection.add(ids[i]);
  181. send_selection();
  182. _runtime.send(DeviceApi.frame());
  183. selection_changed(_selection);
  184. }
  185. public void send_selection()
  186. {
  187. _runtime.send_script(LevelEditorApi.selection_set(_selection.to_array()));
  188. }
  189. public string object_editor_name(Guid object_id)
  190. {
  191. if (_db.has_property(object_id, "editor.name"))
  192. return _db.get_property_string(object_id, "editor.name");
  193. else
  194. return "<unnamed>";
  195. }
  196. public void object_set_editor_name(Guid object_id, string name)
  197. {
  198. _db.set_property_string(object_id, "editor.name", name);
  199. _db.add_restore_point((int)ActionType.OBJECT_SET_EDITOR_NAME, new Guid?[] { object_id });
  200. }
  201. public void generate_spawn_objects(StringBuilder sb, Guid?[] object_ids)
  202. {
  203. int i = 0;
  204. while (i < object_ids.length) {
  205. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  206. i += Unit.generate_spawn_unit_commands(sb, object_ids[i:object_ids.length], _db);
  207. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  208. i += Sound.generate_spawn_sound_commands(sb, object_ids[i:object_ids.length], _db);
  209. } else {
  210. ++i; // Skip object.
  211. }
  212. }
  213. }
  214. public void generate_destroy_objects(StringBuilder sb, Guid?[] object_ids)
  215. {
  216. int i = 0;
  217. while (i < object_ids.length) {
  218. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  219. i += Unit.generate_destroy_commands(sb, object_ids[i:object_ids.length], _db);
  220. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  221. sb.append(LevelEditorApi.destroy(object_ids[i]));
  222. ++i;
  223. } else {
  224. ++i; // Skip object.
  225. }
  226. }
  227. }
  228. public void generate_change_objects(StringBuilder sb, Guid?[] object_ids)
  229. {
  230. int i = 0;
  231. while (i < object_ids.length) {
  232. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  233. i += Unit.generate_change_commands(sb, object_ids[i:object_ids.length], _db);
  234. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  235. i += Sound.generate_change_sound_commands(sb, object_ids[i:object_ids.length], _db);
  236. } else {
  237. ++i; // Skip object.
  238. }
  239. }
  240. }
  241. public void send_level()
  242. {
  243. Gee.ArrayList<Guid?> unit_ids = new Gee.ArrayList<Guid?>();
  244. Gee.ArrayList<Guid?> sound_ids = new Gee.ArrayList<Guid?>();
  245. units(ref unit_ids);
  246. sounds(ref sound_ids);
  247. StringBuilder sb = new StringBuilder();
  248. sb.append(LevelEditorApi.reset());
  249. Unit.generate_spawn_unit_commands(sb, unit_ids.to_array(), _db);
  250. Sound.generate_spawn_sound_commands(sb, sound_ids.to_array(), _db);
  251. _runtime.send_script(sb.str);
  252. send_selection();
  253. send_camera();
  254. }
  255. public void units(ref Gee.ArrayList<Guid?> ids)
  256. {
  257. Gee.HashSet<Guid?> units = _db.get_property_set(_id, "units", new Gee.HashSet<Guid?>());
  258. ids.add_all(units);
  259. }
  260. public void sounds(ref Gee.ArrayList<Guid?> ids)
  261. {
  262. Gee.HashSet<Guid?> sounds = _db.get_property_set(_id, "sounds", new Gee.HashSet<Guid?>());
  263. ids.add_all(sounds);
  264. }
  265. public void objects(ref Gee.ArrayList<Guid?> ids)
  266. {
  267. units(ref ids);
  268. sounds(ref ids);
  269. }
  270. public void camera_decode()
  271. {
  272. string properties[] =
  273. {
  274. "editor.camera.position",
  275. "editor.camera.rotation",
  276. "editor.camera.target_distance",
  277. "editor.camera.orthographic_size",
  278. "editor.camera.view_type"
  279. };
  280. foreach (var p in properties) {
  281. if (!_db.has_property(_id, p))
  282. return;
  283. }
  284. _camera_position = _db.get_property_vector3(_id, "editor.camera.position");
  285. _camera_rotation = _db.get_property_quaternion(_id, "editor.camera.rotation");
  286. _camera_target_distance = _db.get_property_double(_id, "editor.camera.target_distance");
  287. _camera_orthographic_size = _db.get_property_double(_id, "editor.camera.orthographic_size");
  288. _camera_view_type = (CameraViewType)_db.get_property_double(_id, "editor.camera.view_type");
  289. }
  290. public void camera_encode()
  291. {
  292. _db.set_property_internal(0, _id, "editor.camera.position", _camera_position);
  293. _db.set_property_internal(0, _id, "editor.camera.rotation", _camera_rotation);
  294. _db.set_property_internal(0, _id, "editor.camera.target_distance", _camera_target_distance);
  295. _db.set_property_internal(0, _id, "editor.camera.orthographic_size", _camera_orthographic_size);
  296. _db.set_property_internal(0, _id, "editor.camera.view_type", (double)_camera_view_type);
  297. }
  298. public void send_camera()
  299. {
  300. _runtime.send_script(LevelEditorApi.camera_restore(_camera_position
  301. , _camera_rotation
  302. , _camera_orthographic_size
  303. , _camera_target_distance
  304. , _camera_view_type
  305. ));
  306. }
  307. public void on_camera(Hashtable msg)
  308. {
  309. _camera_position = Vector3.from_array((Gee.ArrayList<Value?>)msg["position"]);
  310. _camera_rotation = Quaternion.from_array((Gee.ArrayList<Value?>)msg["rotation"]);
  311. _camera_orthographic_size = (double)msg["orthographic_size"];
  312. _camera_target_distance = (double)msg["target_distance"];
  313. }
  314. }
  315. } /* namespace Crown */