level.vala 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright (c) 2012-2024 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?>();
  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, 1.0, 0.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 void save(string name)
  84. {
  85. string path = Path.build_filename(_project.source_dir(), name + ".level");
  86. camera_encode();
  87. _db.save(path, _id);
  88. _path = path;
  89. _name = name;
  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.SPAWN_UNIT, 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. if (_db.object_type(id) == OBJECT_TYPE_UNIT) {
  102. _db.remove_from_set(_id, "units", id);
  103. _db.destroy(id);
  104. } else if (_db.object_type(id) == OBJECT_TYPE_SOUND_SOURCE) {
  105. _db.remove_from_set(_id, "sounds", id);
  106. _db.destroy(id);
  107. }
  108. }
  109. _db.add_restore_point((int)ActionType.DESTROY_OBJECTS, ids);
  110. }
  111. public void duplicate_selected_objects()
  112. {
  113. if (_selection.size > 0) {
  114. Guid?[] ids = _selection.to_array();
  115. Guid?[] new_ids = new Guid?[ids.length];
  116. for (int i = 0; i < new_ids.length; ++i)
  117. new_ids[i] = Guid.new_guid();
  118. duplicate_objects(ids, new_ids);
  119. }
  120. }
  121. public void destroy_selected_objects()
  122. {
  123. destroy_objects(_selection.to_array());
  124. _selection.clear();
  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.DUPLICATE_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 = new 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 = new 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 = new 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 = new 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. }
  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. }
  222. }
  223. }
  224. public void send_level()
  225. {
  226. Gee.ArrayList<Guid?> unit_ids = new Gee.ArrayList<Guid?>();
  227. Gee.ArrayList<Guid?> sound_ids = new Gee.ArrayList<Guid?>();
  228. units(ref unit_ids);
  229. sounds(ref sound_ids);
  230. StringBuilder sb = new StringBuilder();
  231. sb.append(LevelEditorApi.reset());
  232. Unit.generate_spawn_unit_commands(sb, unit_ids.to_array(), _db);
  233. Sound.generate_spawn_sound_commands(sb, sound_ids.to_array(), _db);
  234. _runtime.send_script(sb.str);
  235. send_selection();
  236. send_camera();
  237. }
  238. public void units(ref Gee.ArrayList<Guid?> ids)
  239. {
  240. HashSet<Guid?> units = _db.get_property_set(_id, "units", new HashSet<Guid?>());
  241. ids.add_all(units);
  242. }
  243. public void sounds(ref Gee.ArrayList<Guid?> ids)
  244. {
  245. HashSet<Guid?> sounds = _db.get_property_set(_id, "sounds", new HashSet<Guid?>());
  246. ids.add_all(sounds);
  247. }
  248. public void objects(ref Gee.ArrayList<Guid?> ids)
  249. {
  250. units(ref ids);
  251. sounds(ref ids);
  252. }
  253. public void camera_decode()
  254. {
  255. string properties[] =
  256. {
  257. "editor.camera.position",
  258. "editor.camera.rotation",
  259. "editor.camera.target_distance",
  260. "editor.camera.orthographic_size",
  261. "editor.camera.view_type"
  262. };
  263. foreach (var p in properties) {
  264. if (!_db.has_property(_id, p))
  265. return;
  266. }
  267. _camera_position = _db.get_property_vector3(_id, "editor.camera.position");
  268. _camera_rotation = _db.get_property_quaternion(_id, "editor.camera.rotation");
  269. _camera_target_distance = _db.get_property_double(_id, "editor.camera.target_distance");
  270. _camera_orthographic_size = _db.get_property_double(_id, "editor.camera.orthographic_size");
  271. _camera_view_type = (CameraViewType)_db.get_property_double(_id, "editor.camera.view_type");
  272. }
  273. public void camera_encode()
  274. {
  275. _db.set_property_internal(0, _id, "editor.camera.position", _camera_position);
  276. _db.set_property_internal(0, _id, "editor.camera.rotation", _camera_rotation);
  277. _db.set_property_internal(0, _id, "editor.camera.target_distance", _camera_target_distance);
  278. _db.set_property_internal(0, _id, "editor.camera.orthographic_size", _camera_orthographic_size);
  279. _db.set_property_internal(0, _id, "editor.camera.view_type", (double)_camera_view_type);
  280. }
  281. public void send_camera()
  282. {
  283. _runtime.send_script(LevelEditorApi.camera_restore(_camera_position
  284. , _camera_rotation
  285. , _camera_orthographic_size
  286. , _camera_target_distance
  287. , _camera_view_type
  288. ));
  289. }
  290. public void on_camera(Hashtable msg)
  291. {
  292. _camera_position = Vector3.from_array((ArrayList<Value?>)msg["position"]);
  293. _camera_rotation = Quaternion.from_array((ArrayList<Value?>)msg["rotation"]);
  294. _camera_orthographic_size = (double)msg["orthographic_size"];
  295. _camera_target_distance = (double)msg["target_distance"];
  296. }
  297. }
  298. } /* namespace Crown */