Jelajahi Sumber

tools: add ability to spawn empty units

Daniele Bartolini 4 tahun lalu
induk
melakukan
3ae40981fa

+ 1 - 0
docs/changelog.rst

@@ -20,6 +20,7 @@ Changelog
 * Changing the sprite in the Sprite Renderer component is now reflected to the Runtime.
 * Added noop resources in ``core/components/noop.*``.
 * Changed the fallback shader to output Color4(255, 0, 255, 255).
+* Added the ability to spawn emtpy units.
 
 **Lua API**
 

+ 15 - 7
tools/level_editor/level.vala

@@ -102,10 +102,15 @@ public class Level
 		_name = name;
 	}
 
-	public void spawn_unit(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl)
+	public void spawn_empty_unit()
 	{
-		on_unit_spawned(id, name, pos, rot, scl);
-		send_spawn_units(new Guid[] { id });
+		StringBuilder sb = new StringBuilder();
+		Guid id = Guid.new_guid();
+		on_unit_spawned(id, null, VECTOR3_ZERO, QUATERNION_IDENTITY, VECTOR3_ONE);
+		generate_spawn_unit_commands(new Guid[] { id }, sb);
+		_client.send_script(sb.str);
+		_client.send(DeviceApi.frame());
+		selection_set(new Guid[] { id });
 	}
 
 	public void destroy_objects(Guid[] ids)
@@ -208,14 +213,17 @@ public class Level
 		selection_set(ids);
 	}
 
-	public void on_unit_spawned(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl)
+	public void on_unit_spawned(Guid id, string? name, Vector3 pos, Quaternion rot, Vector3 scl)
 	{
-		_project.load_unit(name);
-
 		_db.add_restore_point((int)ActionType.SPAWN_UNIT, new Guid[] { id });
 		_db.create(id);
 		_db.set_property_string(id, "editor.name", "unit_%04u".printf(_num_units++));
-		_db.set_property_string(id, "prefab", name);
+
+		if (name != null)
+		{
+			_project.load_unit(name);
+			_db.set_property_string(id, "prefab", name);
+		}
 
 		Unit unit = new Unit(_db, id);
 		Guid component_id;

+ 7 - 1
tools/level_editor/level_editor.vala

@@ -178,7 +178,8 @@ public class LevelEditorApplication : Gtk.Application
 		{ "primitive-plane",    on_create_primitive, null, null },
 		{ "camera",             on_create_primitive, null, null },
 		{ "light",              on_create_primitive, null, null },
-		{ "sound-source",       on_create_primitive, null, null }
+		{ "sound-source",       on_create_primitive, null, null },
+		{ "unit-empty",         on_create_unit,      null, null }
 	};
 
 	private const GLib.ActionEntry[] action_entries_camera =
@@ -1992,6 +1993,11 @@ public class LevelEditorApplication : Gtk.Application
 		activate_action("tool", new GLib.Variant.string("place"));
 	}
 
+	private void on_create_unit(GLib.SimpleAction action, GLib.Variant? param)
+	{
+		_level.spawn_empty_unit();
+	}
+
 	private void on_camera_view(GLib.SimpleAction action, GLib.Variant? param)
 	{
 		string name = param.get_string();

+ 6 - 0
tools/level_editor/resources/gtk/menus.ui

@@ -287,6 +287,12 @@
           <attribute name="action">app.sound-source</attribute>
         </item>
       </section>
+        <item>
+          <attribute name="label" translatable="yes">Empty Unit</attribute>
+          <attribute name="action">app.unit-empty</attribute>
+        </item>
+      <section>
+      </section>
     </submenu>
     <submenu>
       <attribute name="label" translatable="yes">Camera</attribute>