level.vala 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2012-2026 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.HashMap<string, uint> _unit_names;
  16. public Gee.HashMap<string, uint> _sound_names;
  17. public string _name;
  18. public string _path;
  19. public Guid _id;
  20. public Level(Database db, RuntimeInstance runtime)
  21. {
  22. _project = db._project;
  23. // Engine connections
  24. _runtime = runtime;
  25. // Data
  26. _db = db;
  27. _unit_names = new Gee.HashMap<string, uint>();
  28. _sound_names = new Gee.HashMap<string, uint>();
  29. reset();
  30. }
  31. /// Resets the level
  32. public void reset()
  33. {
  34. _db.reset();
  35. _unit_names.clear();
  36. _sound_names.clear();
  37. _name = null;
  38. _path = null;
  39. _id = GUID_ZERO;
  40. }
  41. /// Loads the level @a level_name.
  42. public int load(string level_name)
  43. {
  44. string resource_path = level_name + ".level";
  45. string path = _project.absolute_path(resource_path);
  46. reset();
  47. int ret = _db.load_from_path(out _id, path, resource_path);
  48. if (ret != 0)
  49. return ret;
  50. // Level files loaded from outside the source directory can be visualized and
  51. // modified in-memory, but never overwritten on disk, because they might be
  52. // shared with other projects (e.g. toolchain). Ensure that _path is null to
  53. // force save functions to choose a different path (inside the source
  54. // directory).
  55. if (!_project.path_is_within_source_dir(path))
  56. _path = null;
  57. else
  58. _path = path;
  59. _name = level_name;
  60. return 0;
  61. }
  62. // Creates a new level based on a @a template_level_name.
  63. public int create(string template_level_name)
  64. {
  65. string resource_path = template_level_name + ".level";
  66. string path = _project.absolute_path(resource_path);
  67. reset();
  68. Guid template_id;
  69. int ret = _db.load_from_path(out template_id, path, resource_path);
  70. if (ret != 0)
  71. return ret;
  72. _id = Guid.new_guid();
  73. UndoRedo undo_redo = _db.disable_undo();
  74. _db.duplicate(template_id, _id);
  75. _db.restore_undo(undo_redo);
  76. _path = null;
  77. _name = template_level_name;
  78. return 0;
  79. }
  80. public int save(string name)
  81. {
  82. string path = Path.build_filename(_project.source_dir(), name + ".level");
  83. int err = _db.save(path, _id);
  84. _path = path;
  85. _name = name;
  86. return err;
  87. }
  88. public void spawn_unit(string? name)
  89. {
  90. Guid id = Guid.new_guid();
  91. on_unit_spawned(id, name, VECTOR3_ZERO, QUATERNION_IDENTITY, VECTOR3_ONE);
  92. _db.add_restore_point((int)ActionType.CREATE_OBJECTS, new Guid?[] { id });
  93. }
  94. public void replace_unit(Guid unit_id, string prefab_name)
  95. {
  96. string unit_editor_name = _db.name(unit_id);
  97. Unit unit = Unit(_db, unit_id);
  98. Vector3 unit_pos = unit.local_position();
  99. Quaternion unit_rot = unit.local_rotation();
  100. Vector3 unit_scl = unit.local_scale();
  101. _db.destroy(unit_id);
  102. _db.add_restore_point((int)ActionType.DESTROY_OBJECTS, { unit_id });
  103. Guid new_id = Guid.new_guid();
  104. Unit new_unit = Unit(_db, new_id);
  105. new_unit.create(prefab_name);
  106. new_unit.set_local_position(unit_pos);
  107. new_unit.set_local_rotation(unit_rot);
  108. new_unit.set_local_scale(unit_scl);
  109. _db.set_name(new_id, unit_editor_name);
  110. _db.add_to_set(_id, "units", new_id);
  111. _db.add_restore_point((int)ActionType.CREATE_OBJECTS, { new_id });
  112. }
  113. public string add_object_name(Gee.HashMap<string, uint> names, string resource_name)
  114. {
  115. string basename = GLib.Path.get_basename(resource_name);
  116. uint num = 0;
  117. if (!names.has_key(basename)) {
  118. names[basename] = 1;
  119. } else {
  120. num = names[basename];
  121. names.set(basename, num + 1);
  122. }
  123. return num > 0 ? "%s%u".printf(basename, num + 1) : basename;
  124. }
  125. public void on_unit_spawned(Guid id, string? name, Vector3 pos, Quaternion rot, Vector3 scl)
  126. {
  127. Unit unit = Unit(_db, id);
  128. unit.create(name);
  129. unit.set_local_position(pos);
  130. unit.set_local_rotation(rot);
  131. unit.set_local_scale(scl);
  132. _db.set_name(id, add_object_name(_unit_names, name != null ? name : "unit"));
  133. _db.add_to_set(_id, "units", id);
  134. }
  135. public void on_sound_spawned(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl, double range, double volume, bool loop)
  136. {
  137. Sound sound = Sound(_db, id);
  138. sound.create(name, pos, rot, scl, range, volume, loop);
  139. _db.set_name(id, add_object_name(_sound_names, name));
  140. _db.add_to_set(_id, "sounds", id);
  141. }
  142. public void on_move_objects(Guid?[] ids, Vector3[] positions, Quaternion[] rotations, Vector3[] scales)
  143. {
  144. for (int i = 0; i < ids.length; ++i) {
  145. if (_db.object_type(ids[i]) == OBJECT_TYPE_UNIT) {
  146. Unit unit = Unit(_db, ids[i]);
  147. unit.set_local_position(positions[i]);
  148. unit.set_local_rotation(rotations[i]);
  149. unit.set_local_scale(scales[i]);
  150. } else if (_db.object_type(ids[i]) == OBJECT_TYPE_SOUND_SOURCE) {
  151. Sound sound = Sound(_db, ids[i]);
  152. sound.set_local_position(positions[i]);
  153. sound.set_local_rotation(rotations[i]);
  154. sound.set_local_scale(scales[i]);
  155. }
  156. }
  157. }
  158. public void generate_spawn_objects(StringBuilder sb, Guid?[] object_ids)
  159. {
  160. int n;
  161. int i = 0;
  162. while (i < object_ids.length) {
  163. n = 0;
  164. n += Unit.generate_spawn_unit_commands(sb, object_ids[i:object_ids.length], _db);
  165. n += Sound.generate_spawn_sound_commands(sb, object_ids[i:object_ids.length], _db);
  166. i += n == 0 ? 1 : n;
  167. }
  168. }
  169. public void generate_destroy_objects(StringBuilder sb, Guid?[] object_ids)
  170. {
  171. int n;
  172. int i = 0;
  173. while (i < object_ids.length) {
  174. n = 0;
  175. n += Unit.generate_destroy_commands(sb, object_ids[i:object_ids.length], _db);
  176. n += Sound.generate_destroy_commands(sb, object_ids[i:object_ids.length], _db);
  177. i += n == 0 ? 1 : n;
  178. }
  179. }
  180. public void generate_change_objects(StringBuilder sb, Guid?[] object_ids)
  181. {
  182. int n;
  183. int i = 0;
  184. while (i < object_ids.length) {
  185. n = 0;
  186. n += Unit.generate_change_commands(sb, object_ids[i:object_ids.length], _db);
  187. n += Sound.generate_change_sound_commands(sb, object_ids[i:object_ids.length], _db);
  188. i += n == 0 ? 1 : n;
  189. }
  190. }
  191. public void send_level()
  192. {
  193. Gee.ArrayList<Guid?> unit_ids = new Gee.ArrayList<Guid?>();
  194. Gee.ArrayList<Guid?> sound_ids = new Gee.ArrayList<Guid?>();
  195. units(ref unit_ids);
  196. sounds(ref sound_ids);
  197. StringBuilder sb = new StringBuilder();
  198. sb.append(LevelEditorApi.reset());
  199. Unit.generate_spawn_unit_commands(sb, unit_ids.to_array(), _db);
  200. Sound.generate_spawn_sound_commands(sb, sound_ids.to_array(), _db);
  201. sb.append(LevelEditorApi.spawn_skydome(_db.get_resource(_id, "skydome_unit", "core/units/skydome/skydome")));
  202. _runtime.send_script(sb.str);
  203. send_camera();
  204. }
  205. public void units(ref Gee.ArrayList<Guid?> ids)
  206. {
  207. Gee.HashSet<Guid?> units = _db.get_set(_id, "units", new Gee.HashSet<Guid?>());
  208. ids.add_all(units);
  209. }
  210. public void sounds(ref Gee.ArrayList<Guid?> ids)
  211. {
  212. Gee.HashSet<Guid?> sounds = _db.get_set(_id, "sounds", new Gee.HashSet<Guid?>());
  213. ids.add_all(sounds);
  214. }
  215. public void objects(ref Gee.ArrayList<Guid?> ids)
  216. {
  217. units(ref ids);
  218. sounds(ref ids);
  219. }
  220. public void send_camera()
  221. {
  222. _runtime.send_script(LevelEditorApi.camera_restore(_db.get_vector3(_id, "editor.camera.position")
  223. , _db.get_quaternion(_id, "editor.camera.rotation")
  224. , _db.get_double(_id, "editor.camera.orthographic_size")
  225. , _db.get_double(_id, "editor.camera.target_distance")
  226. , (CameraViewType)_db.get_double(_id, "editor.camera.view_type")
  227. ));
  228. }
  229. public void on_camera(Hashtable msg)
  230. {
  231. _db.set(0, _id, "editor.camera.position", Vector3.from_array((Gee.ArrayList<Value?>)msg["position"]));
  232. _db.set(0, _id, "editor.camera.rotation", Quaternion.from_array((Gee.ArrayList<Value?>)msg["rotation"]));
  233. _db.set(0, _id, "editor.camera.orthographic_size", (double)msg["orthographic_size"]);
  234. _db.set(0, _id, "editor.camera.target_distance", (double)msg["target_distance"]);
  235. }
  236. }
  237. } /* namespace Crown */