level.vala 10 KB

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