Bladeren bron

Add World::units() to fetch all units in a world

Daniele Bartolini 11 jaren geleden
bovenliggende
commit
7bbcbbc532
3 gewijzigde bestanden met toevoegingen van 38 en 2 verwijderingen
  1. 26 2
      engine/lua/LuaWorld.cpp
  2. 9 0
      engine/world/World.cpp
  3. 3 0
      engine/world/World.h

+ 26 - 2
engine/lua/LuaWorld.cpp

@@ -28,8 +28,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "LuaEnvironment.h"
 #include "World.h"
 #include "Gui.h"
-#include "Device.h"
-#include "LuaSystem.h"
+#include "TempAllocator.h"
+#include "Array.h"
 
 namespace crown
 {
@@ -74,6 +74,29 @@ static int world_num_units(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+static int world_units(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = stack.get_world(1);
+
+	TempAllocator1024 alloc;
+	Array<UnitId> all_units(alloc);
+
+	world->units(all_units);
+
+	stack.push_table();
+	for (uint32_t i = 0; i < array::size(all_units); i++)
+	{
+		stack.push_key_begin((int32_t) i + 1);
+		stack.push_unit(world->lookup_unit(all_units[i]));
+		stack.push_key_end();
+	}
+
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 static int world_play_sound(lua_State* L)
 {
@@ -255,6 +278,7 @@ void load_world(LuaEnvironment& env)
 	env.load_module_function("World", "spawn_unit",			world_spawn_unit);
 	env.load_module_function("World", "destroy_unit",       world_destroy_unit);
 	env.load_module_function("World", "num_units",          world_num_units);
+	env.load_module_function("World", "units",              world_units);
 
 	env.load_module_function("World", "play_sound",			world_play_sound);
 	env.load_module_function("World", "stop_sound", 		world_stop_sound);

+ 9 - 0
engine/world/World.cpp

@@ -125,6 +125,15 @@ uint32_t World::num_units() const
 	return m_units.size();
 }
 
+//-----------------------------------------------------------------------------
+void World::units(Array<UnitId>& units) const
+{
+	for (uint32_t i = 0; i < m_units.size(); i++)
+	{
+		array::push_back(units, m_units[i]->id());
+	}
+}
+
 //-----------------------------------------------------------------------------
 void World::link_unit(UnitId child, UnitId parent, int32_t node)
 {

+ 3 - 0
engine/world/World.h

@@ -79,6 +79,9 @@ public:
 	/// Returns the number of units in the world.
 	uint32_t num_units() const;
 
+	/// Returns all the the units in the world.
+	void units(Array<UnitId>& units) const;
+
 	/// Links the unit @a child to the @a node of the unit @a parent.
 	/// After this call, @a child will follow the @a parent unit.
 	void link_unit(UnitId child, UnitId parent, int32_t node);