Răsfoiți Sursa

Add support for loading levels in World

Daniele Bartolini 11 ani în urmă
părinte
comite
e1acf7f3c0
3 a modificat fișierele cu 39 adăugiri și 2 ștergeri
  1. 12 0
      engine/lua/LuaWorld.cpp
  2. 23 2
      engine/world/World.cpp
  3. 4 0
      engine/world/World.h

+ 12 - 0
engine/lua/LuaWorld.cpp

@@ -263,6 +263,16 @@ static int world_destroy_debug_line(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
+static int world_load_level(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = stack.get_world(1);
+	world->load_level(stack.get_string(2));
+	return 0;
+}
+
 //-----------------------------------------------------------------------------
 static int world_tostring(lua_State* L)
 {
@@ -297,6 +307,8 @@ void load_world(LuaEnvironment& env)
 	env.load_module_function("World", "create_debug_line",  world_create_debug_line);
 	env.load_module_function("World", "destroy_debug_line", world_destroy_debug_line);
 
+	env.load_module_function("World", "load_level",			world_load_level);
+
 	env.load_module_function("World", "__index",			"World");
 	env.load_module_function("World", "__tostring",			world_tostring);
 }

+ 23 - 2
engine/world/World.cpp

@@ -33,6 +33,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "DebugLine.h"
 #include "Actor.h"
 #include "LuaEnvironment.h"
+#include "LevelResource.h"
 
 namespace crown
 {
@@ -75,8 +76,14 @@ void World::set_id(WorldId id)
 //-----------------------------------------------------------------------------
 UnitId World::spawn_unit(const char* name, const Vector3& pos, const Quaternion& rot)
 {
-	UnitResource* ur = (UnitResource*) device()->resource_manager()->lookup(UNIT_EXTENSION, name);
-	ResourceId id = device()->resource_manager()->resource_id(UNIT_EXTENSION, name);
+	const ResourceId id = device()->resource_manager()->resource_id(UNIT_EXTENSION, name);
+	return spawn_unit(id, pos, rot);
+}
+
+//-----------------------------------------------------------------------------
+UnitId World::spawn_unit(ResourceId id, const Vector3& pos, const Quaternion& rot)
+{
+	UnitResource* ur = (UnitResource*) device()->resource_manager()->data(id);
 	return spawn_unit(id, ur, pos, rot);
 }
 
@@ -279,6 +286,20 @@ void World::destroy_debug_line(DebugLine* line)
 	CE_DELETE(default_allocator(), line);
 }
 
+//-----------------------------------------------------------------------------
+void World::load_level(const char* name)
+{
+	Log::d("Loading level...");
+	const LevelResource* res = (LevelResource*) device()->resource_manager()->lookup(LEVEL_EXTENSION, name);
+
+	for (uint32_t i = 0; i < res->num_units(); i++)
+	{
+		Log::d("Loading ...");
+		const LevelUnit* lu = res->get_unit(i);
+		spawn_unit(lu->name, lu->position, lu->rotation);
+	}
+}
+
 //-----------------------------------------------------------------------------
 SceneGraphManager* World::scene_graph_manager()
 {

+ 4 - 0
engine/world/World.h

@@ -70,6 +70,7 @@ public:
 
 	/// Spawns a new instance of the unit @a name at the given @a position and @a rotation.
 	UnitId spawn_unit(const char* name, const Vector3& position = vector3::ZERO, const Quaternion& rotation = quaternion::IDENTITY);
+	UnitId spawn_unit(ResourceId id, const Vector3& pos, const Quaternion& rot);
 	UnitId spawn_unit(const ResourceId id, UnitResource* ur, const Vector3& pos, const Quaternion& rot);
 
 	/// Destroys the unit with the given @a id.
@@ -134,6 +135,9 @@ public:
 	DebugLine* create_debug_line(bool depth_test);
 	void destroy_debug_line(DebugLine* line);
 
+	/// Loads the level @a name into the world.
+	void load_level(const char* name);
+
 	SceneGraphManager* scene_graph_manager();
 	RenderWorld* render_world();
 	PhysicsWorld* physics_world();