2
0
Эх сурвалжийг харах

Use WorldManager to create/destory worlds in Device and add support for reloading Units

Daniele Bartolini 12 жил өмнө
parent
commit
56c0415922

+ 28 - 8
engine/Device.cpp

@@ -55,6 +55,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "SoundRenderer.h"
 #include "World.h"
 #include "LuaStack.h"
+#include "WorldManager.h"
 
 #if defined(LINUX) || defined(WINDOWS)
 	#include "BundleCompiler.h"
@@ -97,6 +98,9 @@ Device::Device()
 	, m_resource_manager(NULL)
 	, m_resource_bundle(NULL)
 
+	, m_physx(NULL)
+	, m_world_manager(NULL)
+
 	, m_renderer_init_request(false)
 {
 	// Bundle dir is current dir by default.
@@ -137,6 +141,10 @@ void Device::init()
 	Log::d("Resource manager created.");
 	Log::d("Resource seed: %d", m_resource_manager->seed());
 
+	// Create world manager
+	m_world_manager = CE_NEW(m_allocator, WorldManager)();
+	Log::d("World manager created.");
+
 	// Create window
 	m_window = CE_NEW(m_allocator, OsWindow);
 
@@ -225,6 +233,9 @@ void Device::shutdown()
 		CE_DELETE(m_allocator, m_renderer);
 	}
 
+	Log::d("Releasing WorldManager...");
+	CE_DELETE(m_allocator, m_world_manager);
+
 	Log::d("Releasing ResourceManager...");
 	if (m_resource_manager)
 	{
@@ -423,17 +434,15 @@ void Device::render_world(World* world, Camera* camera)
 }
 
 //-----------------------------------------------------------------------------
-World* Device::create_world()
+WorldId Device::create_world()
 {
-	return CE_NEW(default_allocator(), World);
+	return m_world_manager->create_world();
 }
 
 //-----------------------------------------------------------------------------
-void Device::destroy_world(World* world)
+void Device::destroy_world(WorldId world)
 {
-	CE_ASSERT_NOT_NULL(world);
-
-	CE_DELETE(default_allocator(), world);
+	m_world_manager->destroy_world(world);
 }
 
 //-----------------------------------------------------------------------------
@@ -474,19 +483,30 @@ void Device::reload(const char* type, const char* name)
 		filename += '.';
 		filename += type;
 
+		const void* old_res = m_resource_manager->lookup(type, name);
+
 		if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir, filename.c_str()))
 		{
 			Log::d("Compilation failed.");
 			return;
 		}
 
+		ResourceId res_id = m_resource_manager->load(type, name);
+		m_resource_manager->flush();
+		const void* new_res = m_resource_manager->data(res_id);
+
 		uint32_t type_hash = hash::murmur2_32(type, string::strlen(type), 0);
 
 		switch (type_hash)
 		{
-			case LUA_TYPE:
+			case UNIT_TYPE:
 			{
-				m_lua_environment->load_and_execute(name);
+				Log::d("Reloading unit: %s", name);
+				/// Reload unit in all worlds
+				for (uint32_t i = 0; i < m_world_manager->worlds().size(); i++)
+				{
+					m_world_manager->worlds()[i]->reload_units((UnitResource*) old_res, (UnitResource*) new_res);
+				}
 				break;
 			}
 			default:

+ 6 - 2
engine/Device.h

@@ -52,8 +52,10 @@ class ResourcePackage;
 class ConsoleServer;
 class World;
 class Camera;
+class WorldManager;
 
 typedef Id CameraId;
+typedef Id WorldId;
 
 /// The Engine.
 /// It is the place where to look for accessing all of
@@ -123,8 +125,8 @@ public:
 	/// Renders the given @a world from the point of view of the given @æ camera.
 	void					render_world(World* world, Camera* camera);
 
-	World*					create_world();
-	void					destroy_world(World* world);
+	WorldId					create_world();
+	void					destroy_world(WorldId world);
 
 	/// Returns the resource package with the given @a package_name name.
 	ResourcePackage*		create_resource_package(const char* name);
@@ -154,6 +156,7 @@ public:
 	Accelerometer*			accelerometer();
 	ConsoleServer*			console() { return m_console; }
 	physx::PxPhysics*		physx() { return m_physx->m_physics; };
+	WorldManager*			world_manager() { return m_world_manager; }
 
 protected:
 
@@ -202,6 +205,7 @@ protected:
 	Bundle*					m_resource_bundle;
 
 	Physics*				m_physx;
+	WorldManager*			m_world_manager;
 
 	bool 					m_renderer_init_request;
 

+ 4 - 6
engine/lua/LuaDevice.cpp

@@ -27,6 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Device.h"
 #include "ResourcePackage.h"
 #include "World.h"
+#include "WorldManager.h"
 #include "LuaEnvironment.h"
 #include "LuaStack.h"
 
@@ -97,9 +98,8 @@ CE_EXPORT int device_stop(lua_State* /*L*/)
 CE_EXPORT int device_create_world(lua_State* L)
 {
 	LuaStack stack(L);
-
-	stack.push_world(device()->create_world());
-
+	const WorldId world_id = device()->create_world();
+	stack.push_world(device()->world_manager()->lookup_world(world_id));
 	return 1;
 }
 
@@ -107,9 +107,7 @@ CE_EXPORT int device_create_world(lua_State* L)
 CE_EXPORT int device_destroy_world(lua_State* L)
 {
 	LuaStack stack(L);
-
-	device()->destroy_world(stack.get_world(1));
-
+	device()->destroy_world(stack.get_world(1)->id());
 	return 0;
 }