Procházet zdrojové kódy

Add a way to query scene graph nodes in Unit

Daniele Bartolini před 12 roky
rodič
revize
c8433319e1
5 změnil soubory, kde provedl 110 přidání a 1 odebrání
  1. 40 1
      engine/SceneGraph.cpp
  2. 9 0
      engine/SceneGraph.h
  3. 18 0
      engine/Unit.cpp
  4. 4 0
      engine/Unit.h
  5. 39 0
      engine/lua/LuaUnit.cpp

+ 40 - 1
engine/SceneGraph.cpp

@@ -28,7 +28,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Quaternion.h"
 #include "Allocator.h"
 #include <string.h>
-#include "Log.h"
+#include "Hash.h"
+#include "StringUtils.h"
 
 namespace crown
 {
@@ -69,6 +70,44 @@ void SceneGraph::destroy()
 	m_allocator->deallocate(m_world_poses);
 }
 
+//-----------------------------------------------------------------------------
+int32_t SceneGraph::node(const char* name) const
+{
+	StringId32 name_hash = hash::murmur2_32(name, string::strlen(name), 0);
+
+	for (uint32_t i = 0; i < m_num_nodes; i++)
+	{
+		if (m_names[i] == name_hash)
+		{
+			return i;
+		}
+	}
+
+	CE_ASSERT(false, "Node not found: '%s'", name);
+}
+
+//-----------------------------------------------------------------------------
+bool SceneGraph::has_node(const char* name) const
+{
+	StringId32 name_hash = hash::murmur2_32(name, string::strlen(name), 0);
+
+	for (uint32_t i = 0; i < m_num_nodes; i++)
+	{
+		if (m_names[i] == name_hash)
+		{
+			return true;
+		}
+	}
+
+	return false;
+}
+
+//-----------------------------------------------------------------------------
+uint32_t SceneGraph::num_nodes() const
+{
+	return m_num_nodes;
+}
+
 //-----------------------------------------------------------------------------
 void SceneGraph::link(int32_t child, int32_t parent)
 {

+ 9 - 0
engine/SceneGraph.h

@@ -49,6 +49,15 @@ struct SceneGraph
 	/// Destroys the graph deallocating memory if necessary.
 	void			destroy();
 
+	/// Returns the index of the node with the given @a name
+	int32_t			node(const char* name) const;
+
+	/// Returns whether the graph has the node with the given @a name.
+	bool			has_node(const char* name) const;
+
+	/// Returns the number of nodes in the graph.
+	uint32_t		num_nodes() const;
+
 	/// Links the @a child node to the @a parent node.
 	/// After the linking the @a child pose is reset to identity.
 	/// @note

+ 18 - 0
engine/Unit.cpp

@@ -136,6 +136,24 @@ void Unit::destroy()
 	}
 }
 
+//-----------------------------------------------------------------------------
+int32_t Unit::node(const char* name) const
+{
+	return m_scene_graph.node(name);
+}
+
+//-----------------------------------------------------------------------------
+bool Unit::has_node(const char* name) const
+{
+	return m_scene_graph.has_node(name);
+}
+
+//-----------------------------------------------------------------------------
+uint32_t Unit::num_nodes() const
+{
+	return m_scene_graph.num_nodes();
+}
+
 //-----------------------------------------------------------------------------
 Vector3 Unit::local_position(int32_t node) const
 {

+ 4 - 0
engine/Unit.h

@@ -90,6 +90,10 @@ struct Unit
 	void				create(const Matrix4x4& pose);
 	void				destroy();
 
+	int32_t				node(const char* name) const;
+	bool				has_node(const char* name) const;
+	uint32_t			num_nodes() const;
+
 	Vector3				local_position(int32_t node) const;
 	Quaternion			local_rotation(int32_t node) const;
 	Matrix4x4			local_pose(int32_t node) const;

+ 39 - 0
engine/lua/LuaUnit.cpp

@@ -31,6 +31,41 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
+//-----------------------------------------------------------------------------
+CE_EXPORT int unit_node(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Unit* unit = stack.get_unit(1);
+	const char* name = stack.get_string(2);
+
+	stack.push_int32(unit->node(name));
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int unit_has_node(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Unit* unit = stack.get_unit(1);
+	const char* name = stack.get_string(2);
+
+	stack.push_bool(unit->has_node(name));
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int unit_num_nodes(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Unit* unit = stack.get_unit(1);
+
+	stack.push_int32(unit->num_nodes());
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 CE_EXPORT int unit_local_position(lua_State* L)
 {
@@ -206,6 +241,10 @@ CE_EXPORT int unit_sprite(lua_State* L)
 //-----------------------------------------------------------------------------
 void load_unit(LuaEnvironment& env)
 {
+	env.load_module_function("Unit", "node",					unit_node);
+	env.load_module_function("Unit", "has_node",				unit_has_node);
+	env.load_module_function("Unit", "num_nodes",				unit_num_nodes);
+
 	env.load_module_function("Unit", "local_position",			unit_local_position);
 	env.load_module_function("Unit", "local_rotation",			unit_local_rotation);
 	env.load_module_function("Unit", "local_pose",				unit_local_pose);