示例#1
0
void EntitySystem::CreateAndAttachComponent(
	Entity e,
	COMPONENT_TYPE type,
	BaseComponent::Data& data )
{
	// unfortunately, it appears we have to create the component
	// and try to insert it. It's easier than iterating through the set.
	BaseComponent* c = _create_component_map()[ type ]();
	c->SetData( data );

	// result.second indicates success
	std::pair< BaseComponentPtrSet::iterator, bool > result =
		_entity_map[ e ].insert( c );
	if( !result.second )
	{
		c->~BaseComponent();
		_aligned_free( c );
		c = 0;
	}
	else
	{
		// The insertion succeeded, so we need to add the entity to the 
		// component's list now
		_component_type_map[ type ].insert( e );
	}
}