瀏覽代碼

Add support for .physics to Unit

Daniele Bartolini 12 年之前
父節點
當前提交
aa2cdeadcf
共有 4 個文件被更改,包括 58 次插入14 次删除
  1. 33 14
      engine/Unit.cpp
  2. 16 0
      engine/compilers/unit/UnitCompiler.cpp
  3. 2 0
      engine/compilers/unit/UnitCompiler.h
  4. 7 0
      engine/resource/UnitResource.h

+ 33 - 14
engine/Unit.cpp

@@ -32,6 +32,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "SceneGraphManager.h"
 #include "SceneGraphManager.h"
 #include "Actor.h"
 #include "Actor.h"
 #include "Controller.h"
 #include "Controller.h"
+#include "PhysicsResource.h"
+#include "Device.h"
+#include "ResourceManager.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -73,19 +76,25 @@ Unit::~Unit()
 	// Destroy meshes
 	// Destroy meshes
 	for (uint32_t i = 0; i < m_num_meshes; i++)
 	for (uint32_t i = 0; i < m_num_meshes; i++)
 	{
 	{
-		m_world.destroy_mesh(m_meshes[i].component);
+		m_world.render_world()->destroy_mesh(m_meshes[i].component);
 	}
 	}
 
 
 	// Destroy sprites
 	// Destroy sprites
 	for (uint32_t i = 0; i < m_num_sprites; i++)
 	for (uint32_t i = 0; i < m_num_sprites; i++)
 	{
 	{
-		m_world.destroy_sprite(m_sprites[i].component);
+		m_world.render_world()->destroy_sprite(m_sprites[i].component);
 	}
 	}
 
 
 	// Destroy actors
 	// Destroy actors
 	for (uint32_t i = 0; i < m_num_actors; i++)
 	for (uint32_t i = 0; i < m_num_actors; i++)
 	{
 	{
-		m_world.destroy_actor(m_actors[i].component);
+		m_world.physics_world()->destroy_actor(m_actors[i].component);
+	}
+
+	// Destroy controller
+	if (m_controller.component.id != INVALID_ID)
+	{
+		m_world.physics_world()->destroy_controller(m_controller.component);
 	}
 	}
 }
 }
 
 
@@ -135,8 +144,8 @@ void Unit::create_renderable_objects()
 
 
 		switch (renderable.type)
 		switch (renderable.type)
 		{
 		{
-			case UnitRenderable::MESH: add_mesh(renderable.name, m_world.create_mesh(renderable.resource, m_scene_graph, renderable.node)); break;
-			case UnitRenderable::SPRITE: add_sprite(renderable.name, m_world.create_sprite(renderable.resource, m_scene_graph, renderable.node)); break;
+			case UnitRenderable::MESH: add_mesh(renderable.name, m_world.render_world()->create_mesh(renderable.resource, m_scene_graph, renderable.node)); break;
+			case UnitRenderable::SPRITE: add_sprite(renderable.name, m_world.render_world()->create_sprite(renderable.resource, m_scene_graph, renderable.node)); break;
 			default: CE_FATAL("Oops, bad renderable type"); break;
 			default: CE_FATAL("Oops, bad renderable type"); break;
 		}
 		}
 	}
 	}
@@ -146,9 +155,19 @@ void Unit::create_renderable_objects()
 void Unit::create_physics_objects()
 void Unit::create_physics_objects()
 {
 {
 	const StringId32 name_hash = hash::murmur2_32("actor", string::strlen("actor"), 0);
 	const StringId32 name_hash = hash::murmur2_32("actor", string::strlen("actor"), 0);
-	if (count != 0)
-	add_actor(name_hash, m_world.create_actor(m_scene_graph, 0, (count == 1) ? ActorType::DYNAMIC_KINEMATIC : ActorType::DYNAMIC_PHYSICAL));
+	if (count > 1)
+	add_actor(name_hash, m_world.physics_world()->create_actor(m_scene_graph, 0, ActorType::DYNAMIC_PHYSICAL));
 	count++;
 	count++;
+
+	if (m_resource->physics_resource().id != 0)
+	{
+		const PhysicsResource* pr = (PhysicsResource*) device()->resource_manager()->data(m_resource->physics_resource());
+
+		if (pr->has_controller())
+		{
+			set_controller(pr->controller().name, m_world.physics_world()->create_controller(pr, m_scene_graph, 0));
+		}
+	}
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -350,7 +369,7 @@ Mesh* Unit::mesh(const char* name)
 
 
 	CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
 	CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
 
 
-	return m_world.lookup_mesh(mesh);
+	return m_world.render_world()->lookup_mesh(mesh);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -360,7 +379,7 @@ Mesh* Unit::mesh(uint32_t i)
 
 
 	CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
 	CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
 
 
-	return m_world.lookup_mesh(mesh);
+	return m_world.render_world()->lookup_mesh(mesh);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -370,7 +389,7 @@ Sprite*	Unit::sprite(const char* name)
 
 
 	CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
 	CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
 
 
-	return m_world.lookup_sprite(sprite);
+	return m_world.render_world()->lookup_sprite(sprite);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -380,7 +399,7 @@ Sprite*	Unit::sprite(uint32_t i)
 
 
 	CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
 	CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
 
 
-	return m_world.lookup_sprite(sprite);
+	return m_world.render_world()->lookup_sprite(sprite);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -390,7 +409,7 @@ Actor* Unit::actor(const char* name)
 
 
 	CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
 	CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
 
 
-	return m_world.lookup_actor(actor);
+	return m_world.physics_world()->lookup_actor(actor);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -400,7 +419,7 @@ Actor* Unit::actor(uint32_t i)
 
 
 	CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", i);
 	CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", i);
 
 
-	return m_world.lookup_actor(actor);
+	return m_world.physics_world()->lookup_actor(actor);
 }	
 }	
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -408,7 +427,7 @@ Controller* Unit::controller()
 {
 {
 	if (m_controller.component.id != INVALID_ID)
 	if (m_controller.component.id != INVALID_ID)
 	{
 	{
-		return m_world.lookup_controller(m_controller.component);
+		return m_world.physics_world()->lookup_controller(m_controller.component);
 	}
 	}
 
 
 	return NULL;
 	return NULL;

+ 16 - 0
engine/compilers/unit/UnitCompiler.cpp

@@ -119,6 +119,21 @@ size_t UnitCompiler::compile_impl(Filesystem& fs, const char* resource_path)
 		}
 		}
 	}
 	}
 
 
+	// Check if the unit has a .physics resource
+	DynamicString unit_name(resource_path);
+	unit_name.strip_trailing("unit");
+	unit_name += "physics";
+	Log::d("Checking %s", unit_name.c_str());
+	if (fs.is_file(unit_name.c_str()))
+	{
+		Log::d("YES");
+		m_physics_resource.id = hash::murmur2_64(unit_name.c_str(), string::strlen(unit_name.c_str()), 0);
+	}
+	else
+	{
+		m_physics_resource.id = 0;
+	}
+
 	return 1;
 	return 1;
 }
 }
 
 
@@ -275,6 +290,7 @@ int32_t UnitCompiler::find_node_parent_index(uint32_t node)
 void UnitCompiler::write_impl(File* out_file)
 void UnitCompiler::write_impl(File* out_file)
 {
 {
 	UnitHeader h;
 	UnitHeader h;
+	h.physics_resource = m_physics_resource;
 	h.num_renderables = m_renderables.size();
 	h.num_renderables = m_renderables.size();
 	h.num_cameras = m_cameras.size();
 	h.num_cameras = m_cameras.size();
 	h.num_actors = m_actors.size();
 	h.num_actors = m_actors.size();

+ 2 - 0
engine/compilers/unit/UnitCompiler.h

@@ -78,6 +78,8 @@ public:
 
 
 private:
 private:
 
 
+	ResourceId				m_physics_resource;
+
 	List<GraphNode>			m_nodes;
 	List<GraphNode>			m_nodes;
 	List<GraphNodeDepth>	m_node_depths;
 	List<GraphNodeDepth>	m_node_depths;
 
 

+ 7 - 0
engine/resource/UnitResource.h

@@ -40,6 +40,7 @@ namespace crown
 // All offsets are absolute
 // All offsets are absolute
 struct UnitHeader
 struct UnitHeader
 {
 {
+	ResourceId physics_resource;
 	uint32_t num_renderables;
 	uint32_t num_renderables;
 	uint32_t renderables_offset;
 	uint32_t renderables_offset;
 	uint32_t num_cameras;
 	uint32_t num_cameras;
@@ -109,6 +110,12 @@ struct UnitResource
 	{
 	{
 	}
 	}
 
 
+	//-----------------------------------------------------------------------------
+	ResourceId physics_resource() const
+	{
+		return ((UnitHeader*) this)->physics_resource;
+	}
+
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	uint32_t num_renderables() const
 	uint32_t num_renderables() const
 	{
 	{