Răsfoiți Sursa

Fix component fetching

Daniele Bartolini 11 ani în urmă
părinte
comite
6166232fa0
2 a modificat fișierele cu 75 adăugiri și 88 ștergeri
  1. 11 24
      engine/world/Unit.cpp
  2. 64 64
      engine/world/Unit.h

+ 11 - 24
engine/world/Unit.cpp

@@ -348,24 +348,12 @@ void Unit::add_component(StringId32 name, Id component, uint32_t& size, Componen
 //-----------------------------------------------------------------------------
 Id Unit::find_component(const char* name, uint32_t size, Component* array)
 {
-	uint32_t name_hash = string::murmur2_32(name, string::strlen(name), 0);
-
-	Id comp;
-	comp.id = INVALID_ID;
-
-	for (uint32_t i = 0; i < size; i++)
-	{
-		if (name_hash == array[i].name)
-		{
-			comp = array[i].component;
-		}
-	}
-
-	return comp;
+	const uint32_t name_hash = string::murmur2_32(name, string::strlen(name), 0);
+	return find_component_by_name(name_hash, size, array);
 }
 
 //-----------------------------------------------------------------------------
-Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
+Id Unit::find_component_by_index(uint32_t index, uint32_t size, Component* array)
 {
 	Id comp;
 	comp.id = INVALID_ID;
@@ -379,7 +367,7 @@ Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
 }
 
 //-----------------------------------------------------------------------------
-Id Unit::find_component_by_index(StringId32 name, uint32_t size, Component* array)
+Id Unit::find_component_by_name(StringId32 name, uint32_t size, Component* array)
 {
 	Id comp;
 	comp.id = INVALID_ID;
@@ -455,7 +443,7 @@ Camera* Unit::camera(const char* name)
 //-----------------------------------------------------------------------------
 Camera* Unit::camera(uint32_t i)
 {
-	CameraId cam = find_component(i, m_num_cameras, m_cameras);
+	CameraId cam = find_component_by_index(i, m_num_cameras, m_cameras);
 
 	CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
 
@@ -475,7 +463,7 @@ Mesh* Unit::mesh(const char* name)
 //-----------------------------------------------------------------------------
 Mesh* Unit::mesh(uint32_t i)
 {
-	MeshId mesh = find_component(i, m_num_meshes, m_meshes);
+	MeshId mesh = find_component_by_index(i, m_num_meshes, m_meshes);
 
 	CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
 
@@ -495,7 +483,7 @@ Sprite*	Unit::sprite(const char* name)
 //-----------------------------------------------------------------------------
 Sprite*	Unit::sprite(uint32_t i)
 {
-	SpriteId sprite = find_component(i, m_num_sprites, m_sprites);
+	SpriteId sprite = find_component_by_index(i, m_num_sprites, m_sprites);
 
 	CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
 
@@ -515,9 +503,9 @@ Actor* Unit::actor(const char* name)
 //-----------------------------------------------------------------------------
 Actor* Unit::actor(uint32_t i)
 {
-	ActorId actor = find_component(i, m_num_actors, m_actors);
+	ActorId actor = find_component_by_index(i, m_num_actors, m_actors);
 
-	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 index '%d'", i);
 
 	return m_world.physics_world()->lookup_actor(actor);
 }	
@@ -525,7 +513,7 @@ Actor* Unit::actor(uint32_t i)
 //-----------------------------------------------------------------------------
 Actor* Unit::actor_by_index(StringId32 name)
 {
-	ActorId actor = find_component_by_index(name, m_num_actors, m_actors);
+	ActorId actor = find_component_by_name(name, m_num_actors, m_actors);
 
 	// CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", name);
 
@@ -556,7 +544,7 @@ Material* Unit::material(const char* name)
 //-----------------------------------------------------------------------------
 Material* Unit::material(uint32_t i)
 {
-	MaterialId material = find_component(i, m_num_materials, m_materials);
+	MaterialId material = find_component_by_index(i, m_num_materials, m_materials);
 
 	CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%d'", i);
 
@@ -572,5 +560,4 @@ bool Unit::is_a(const char* name)
 	return m_resource_id.id == string::murmur2_64(unit.c_str(), string::strlen(unit.c_str()), 0);
 }
 
-
 } // namespace crown

+ 64 - 64
engine/world/Unit.h

@@ -76,101 +76,101 @@ struct UnitResource;
 /// @ingroup World
 struct Unit
 {
-						Unit(World& w, UnitId unit_id, const ResourceId id, const UnitResource* ur, const Matrix4x4& pose);
-						~Unit();
+	Unit(World& w, UnitId unit_id, const ResourceId id, const UnitResource* ur, const Matrix4x4& pose);
+	~Unit();
 
-	void				set_id(const UnitId id);
-	UnitId				id();
+	void set_id(const UnitId id);
+	UnitId id();
 
 	const UnitResource*	resource() const;
 
-	int32_t				node(const char* name) const;
-	bool				has_node(const char* name) const;
-	uint32_t			num_nodes() const;
+	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;
+	Vector3 local_position(int32_t node) const;
+	Quaternion local_rotation(int32_t node) const;
+	Matrix4x4 local_pose(int32_t node) const;
 
-	Vector3				world_position(int32_t node) const;
-	Quaternion			world_rotation(int32_t node) const;
-	Matrix4x4			world_pose(int32_t node) const;
+	Vector3 world_position(int32_t node) const;
+	Quaternion world_rotation(int32_t node) const;
+	Matrix4x4 world_pose(int32_t node) const;
 
-	void				set_local_position(int32_t node, const Vector3& pos);
-	void				set_local_rotation(int32_t node, const Quaternion& rot);
-	void				set_local_pose(int32_t node, const Matrix4x4& pose);
+	void set_local_position(int32_t node, const Vector3& pos);
+	void set_local_rotation(int32_t node, const Quaternion& rot);
+	void set_local_pose(int32_t node, const Matrix4x4& pose);
 
-	void				link_node(int32_t child, int32_t parent);
-	void				unlink_node(int32_t child);
+	void link_node(int32_t child, int32_t parent);
+	void unlink_node(int32_t child);
 
-	void				update();
-	void				reload(UnitResource* new_ur);
+	void update();
+	void reload(UnitResource* new_ur);
 
-	void				add_component(StringId32 name, Id component, uint32_t& size, Component* array);
-	Id					find_component(const char* name, uint32_t size, Component* array);
-	Id					find_component(uint32_t index, uint32_t size, Component* array);
-	Id 					find_component_by_index(StringId32 name, uint32_t size, Component* array);
+	void add_component(StringId32 name, Id component, uint32_t& size, Component* array);
+	Id find_component(const char* name, uint32_t size, Component* array);
+	Id find_component_by_index(uint32_t index, uint32_t size, Component* array);
+	Id find_component_by_name(StringId32 name, uint32_t size, Component* array);
 
-	void				add_camera(StringId32 name, CameraId camera);
-	void				add_mesh(StringId32 name, MeshId mesh);
-	void				add_sprite(StringId32 name, SpriteId sprite);
-	void				add_actor(StringId32 name, ActorId actor);
-	void				set_controller(StringId32 name, ControllerId controller);
-	void				add_material(StringId32 name, MaterialId material);
+	void add_camera(StringId32 name, CameraId camera);
+	void add_mesh(StringId32 name, MeshId mesh);
+	void add_sprite(StringId32 name, SpriteId sprite);
+	void add_actor(StringId32 name, ActorId actor);
+	void set_controller(StringId32 name, ControllerId controller);
+	void add_material(StringId32 name, MaterialId material);
 
-	Camera*				camera(const char* name);
-	Camera*				camera(uint32_t i);
+	Camera* camera(const char* name);
+	Camera* camera(uint32_t i);
 
-	Mesh*				mesh(const char* name);
-	Mesh*				mesh(uint32_t i);
+	Mesh* mesh(const char* name);
+	Mesh* mesh(uint32_t i);
 
-	Sprite*				sprite(const char* name);
-	Sprite*				sprite(uint32_t i);
+	Sprite* sprite(const char* name);
+	Sprite* sprite(uint32_t i);
 
-	Actor*				actor(const char* name);
-	Actor*				actor(uint32_t i);
-	Actor*				actor_by_index(StringId32 name);
+	Actor* actor(const char* name);
+	Actor* actor(uint32_t i);
+	Actor* actor_by_index(StringId32 name);
 
-	Material*			material(const char* name);
-	Material*			material(uint32_t i);
+	Material* material(const char* name);
+	Material* material(uint32_t i);
 
-	Controller*			controller();
+	Controller* controller();
 
-	bool				is_a(const char* name);
+	bool is_a(const char* name);
 
 private:
 
-	void				create_objects(const Matrix4x4& pose);
-	void				destroy_objects();
-	void				create_camera_objects();
-	void				create_renderable_objects();
-	void				create_physics_objects();
-	void				set_default_material();
+	void create_objects(const Matrix4x4& pose);
+	void destroy_objects();
+	void create_camera_objects();
+	void create_renderable_objects();
+	void create_physics_objects();
+	void set_default_material();
 
 public:
 
-	World&				m_world;
-	SceneGraph&			m_scene_graph;
-	const ResourceId	m_resource_id;
+	World& m_world;
+	SceneGraph& m_scene_graph;
+	const ResourceId m_resource_id;
 	const UnitResource*	m_resource;
-	UnitId				m_id;
+	UnitId m_id;
 
-	uint32_t			m_num_cameras;
-	Component			m_cameras[CE_MAX_CAMERA_COMPONENTS];
+	uint32_t m_num_cameras;
+	Component m_cameras[CE_MAX_CAMERA_COMPONENTS];
 
-	uint32_t			m_num_meshes;
-	Component			m_meshes[CE_MAX_MESH_COMPONENTS];
+	uint32_t m_num_meshes;
+	Component m_meshes[CE_MAX_MESH_COMPONENTS];
 
-	uint32_t			m_num_sprites;
-	Component			m_sprites[CE_MAX_SPRITE_COMPONENTS];
+	uint32_t m_num_sprites;
+	Component m_sprites[CE_MAX_SPRITE_COMPONENTS];
 
-	uint32_t			m_num_actors;
-	Component 			m_actors[CE_MAX_ACTOR_COMPONENTS];
+	uint32_t m_num_actors;
+	Component m_actors[CE_MAX_ACTOR_COMPONENTS];
 
-	uint32_t			m_num_materials;
-	Component			m_materials[CE_MAX_MATERIAL_COMPONENTS];
+	uint32_t m_num_materials;
+	Component m_materials[CE_MAX_MATERIAL_COMPONENTS];
 
-	Component			m_controller;
+	Component m_controller;
 };
 
 } // namespace crown