level.vala 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. return 0;
  79. }
  80. public int save(string name)
  81. {
  82. string path = Path.build_filename(_project.source_dir(), name + ".level");
  83. camera_encode();
  84. int err = _db.save(path, _id);
  85. _path = path;
  86. _name = name;
  87. return err;
  88. }
  89. public void spawn_empty_unit()
  90. {
  91. Guid id = Guid.new_guid();
  92. on_unit_spawned(id, null, VECTOR3_ZERO, QUATERNION_IDENTITY, VECTOR3_ONE);
  93. _db.add_restore_point((int)ActionType.CREATE_OBJECTS, new Guid?[] { id });
  94. selection_set(new Guid?[] { id });
  95. }
  96. public void destroy_objects(Guid?[] ids)
  97. {
  98. foreach (Guid id in ids) {
  99. _selection.remove(id);
  100. if (_db.object_type(id) == OBJECT_TYPE_UNIT) {
  101. _db.remove_from_set(_id, "units", id);
  102. _db.destroy(id);
  103. } else if (_db.object_type(id) == OBJECT_TYPE_SOUND_SOURCE) {
  104. _db.remove_from_set(_id, "sounds", id);
  105. _db.destroy(id);
  106. }
  107. }
  108. _db.add_restore_point((int)ActionType.DESTROY_OBJECTS, ids);
  109. }
  110. public void duplicate_selected_objects()
  111. {
  112. if (_selection.size > 0) {
  113. Guid?[] ids = _selection.to_array();
  114. Guid?[] new_ids = new Guid?[ids.length];
  115. for (int i = 0; i < new_ids.length; ++i)
  116. new_ids[i] = Guid.new_guid();
  117. duplicate_objects(ids, new_ids);
  118. }
  119. }
  120. public void destroy_selected_objects()
  121. {
  122. destroy_objects(_selection.to_array());
  123. }
  124. public void duplicate_objects(Guid?[] ids, Guid?[] new_ids)
  125. {
  126. for (int i = 0; i < ids.length; ++i) {
  127. _db.duplicate(ids[i], new_ids[i]);
  128. if (_db.object_type(ids[i]) == OBJECT_TYPE_UNIT) {
  129. _db.add_to_set(_id, "units", new_ids[i]);
  130. } else if (_db.object_type(ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  131. _db.add_to_set(_id, "sounds", new_ids[i]);
  132. }
  133. }
  134. _db.add_restore_point((int)ActionType.CREATE_OBJECTS, new_ids);
  135. selection_set(ids);
  136. }
  137. public void on_unit_spawned(Guid id, string? name, Vector3 pos, Quaternion rot, Vector3 scl)
  138. {
  139. Unit unit = Unit(_db, id);
  140. unit.create(name, pos, rot, scl);
  141. _db.set_property_string(id, "editor.name", "unit_%04u".printf(_num_units++));
  142. _db.add_to_set(_id, "units", id);
  143. }
  144. public void on_sound_spawned(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl, double range, double volume, bool loop)
  145. {
  146. Sound sound = Sound(_db, id);
  147. sound.create(name, pos, rot, scl, range, volume, loop);
  148. _db.set_property_string (id, "editor.name", "sound_%04u".printf(_num_sounds++));
  149. _db.add_to_set(_id, "sounds", id);
  150. }
  151. public void on_move_objects(Guid?[] ids, Vector3[] positions, Quaternion[] rotations, Vector3[] scales)
  152. {
  153. for (int i = 0; i < ids.length; ++i) {
  154. if (_db.object_type(ids[i]) == OBJECT_TYPE_UNIT) {
  155. Unit unit = Unit(_db, ids[i]);
  156. unit.set_local_position(positions[i]);
  157. unit.set_local_rotation(rotations[i]);
  158. unit.set_local_scale(scales[i]);
  159. } else if (_db.object_type(ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  160. Sound sound = Sound(_db, ids[i]);
  161. sound.set_local_position(positions[i]);
  162. sound.set_local_rotation(rotations[i]);
  163. sound.set_local_scale(scales[i]);
  164. }
  165. }
  166. }
  167. public void on_selection(Guid[] ids)
  168. {
  169. _selection.clear();
  170. foreach (Guid id in ids)
  171. _selection.add(id);
  172. selection_changed(_selection);
  173. }
  174. public void selection_set(Guid?[] ids)
  175. {
  176. _selection.clear();
  177. for (int i = 0; i < ids.length; ++i)
  178. _selection.add(ids[i]);
  179. send_selection();
  180. _runtime.send(DeviceApi.frame());
  181. selection_changed(_selection);
  182. }
  183. public void send_selection()
  184. {
  185. _runtime.send_script(LevelEditorApi.selection_set(_selection.to_array()));
  186. }
  187. public string object_editor_name(Guid object_id)
  188. {
  189. if (_db.has_property(object_id, "editor.name"))
  190. return _db.get_property_string(object_id, "editor.name");
  191. else
  192. return "<unnamed>";
  193. }
  194. public void object_set_editor_name(Guid object_id, string name)
  195. {
  196. _db.set_property_string(object_id, "editor.name", name);
  197. _db.add_restore_point((int)ActionType.OBJECT_SET_EDITOR_NAME, new Guid?[] { object_id });
  198. }
  199. public void generate_spawn_objects(StringBuilder sb, Guid?[] object_ids)
  200. {
  201. int i = 0;
  202. while (i < object_ids.length) {
  203. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  204. i += Unit.generate_spawn_unit_commands(sb, object_ids[i:object_ids.length], _db);
  205. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  206. i += Sound.generate_spawn_sound_commands(sb, object_ids[i:object_ids.length], _db);
  207. } else {
  208. ++i; // Skip object.
  209. }
  210. }
  211. }
  212. public void generate_destroy_objects(StringBuilder sb, Guid?[] object_ids)
  213. {
  214. int i = 0;
  215. while (i < object_ids.length) {
  216. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  217. i += Unit.generate_destroy_commands(sb, object_ids[i:object_ids.length], _db);
  218. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  219. sb.append(LevelEditorApi.destroy(object_ids[i]));
  220. ++i;
  221. } else {
  222. ++i; // Skip object.
  223. }
  224. }
  225. }
  226. public void generate_change_objects(StringBuilder sb, Guid?[] object_ids)
  227. {
  228. int i = 0;
  229. while (i < object_ids.length) {
  230. if (_db.object_type(object_ids[i]) == OBJECT_TYPE_UNIT) {
  231. i += Unit.generate_change_commands(sb, object_ids[i:object_ids.length], _db);
  232. } else if (_db.object_type(object_ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  233. i += Sound.generate_change_sound_commands(sb, object_ids[i:object_ids.length], _db);
  234. } else {
  235. ++i; // Skip object.
  236. }
  237. }
  238. }
  239. public void send_level()
  240. {
  241. Gee.ArrayList<Guid?> unit_ids = new Gee.ArrayList<Guid?>();
  242. Gee.ArrayList<Guid?> sound_ids = new Gee.ArrayList<Guid?>();
  243. units(ref unit_ids);
  244. sounds(ref sound_ids);
  245. StringBuilder sb = new StringBuilder();
  246. sb.append(LevelEditorApi.reset());
  247. Unit.generate_spawn_unit_commands(sb, unit_ids.to_array(), _db);
  248. Sound.generate_spawn_sound_commands(sb, sound_ids.to_array(), _db);
  249. _runtime.send_script(sb.str);
  250. send_selection();
  251. send_camera();
  252. }
  253. public void units(ref Gee.ArrayList<Guid?> ids)
  254. {
  255. Gee.HashSet<Guid?> units = _db.get_property_set(_id, "units", new Gee.HashSet<Guid?>());
  256. ids.add_all(units);
  257. }
  258. public void sounds(ref Gee.ArrayList<Guid?> ids)
  259. {
  260. Gee.HashSet<Guid?> sounds = _db.get_property_set(_id, "sounds", new Gee.HashSet<Guid?>());
  261. ids.add_all(sounds);
  262. }
  263. public void objects(ref Gee.ArrayList<Guid?> ids)
  264. {
  265. units(ref ids);
  266. sounds(ref ids);
  267. }
  268. public void camera_decode()
  269. {
  270. string properties[] =
  271. {
  272. "editor.camera.position",
  273. "editor.camera.rotation",
  274. "editor.camera.target_distance",
  275. "editor.camera.orthographic_size",
  276. "editor.camera.view_type"
  277. };
  278. foreach (var p in properties) {
  279. if (!_db.has_property(_id, p))
  280. return;
  281. }
  282. _camera_position = _db.get_property_vector3(_id, "editor.camera.position");
  283. _camera_rotation = _db.get_property_quaternion(_id, "editor.camera.rotation");
  284. _camera_target_distance = _db.get_property_double(_id, "editor.camera.target_distance");
  285. _camera_orthographic_size = _db.get_property_double(_id, "editor.camera.orthographic_size");
  286. _camera_view_type = (CameraViewType)_db.get_property_double(_id, "editor.camera.view_type");
  287. }
  288. public void camera_encode()
  289. {
  290. _db.set_property_internal(0, _id, "editor.camera.position", _camera_position);
  291. _db.set_property_internal(0, _id, "editor.camera.rotation", _camera_rotation);
  292. _db.set_property_internal(0, _id, "editor.camera.target_distance", _camera_target_distance);
  293. _db.set_property_internal(0, _id, "editor.camera.orthographic_size", _camera_orthographic_size);
  294. _db.set_property_internal(0, _id, "editor.camera.view_type", (double)_camera_view_type);
  295. }
  296. public void send_camera()
  297. {
  298. _runtime.send_script(LevelEditorApi.camera_restore(_camera_position
  299. , _camera_rotation
  300. , _camera_orthographic_size
  301. , _camera_target_distance
  302. , _camera_view_type
  303. ));
  304. }
  305. public void on_camera(Hashtable msg)
  306. {
  307. _camera_position = Vector3.from_array((Gee.ArrayList<Value?>)msg["position"]);
  308. _camera_rotation = Quaternion.from_array((Gee.ArrayList<Value?>)msg["rotation"]);
  309. _camera_orthographic_size = (double)msg["orthographic_size"];
  310. _camera_target_distance = (double)msg["target_distance"];
  311. }
  312. }
  313. } /* namespace Crown */