我目前整合的实体组件系统,如看到这里,与物理引擎和图形引擎.这一切都很好,直到最近决定物理应该在自己的线程中运行.(感谢Glenn Fiedler!)
就像现在一样,我只是在访问组件时锁定所有子系统共享的互斥锁.
来自物理循环的片段:
lock_guard<mutex> lock( m_EntMutex );
entitymap::iterator it;
for ( it = m_Ents.begin(); it != m_Ents.end(); ++it )
{
// Get physics component from entity
// This is guaranteed to work ( component must exist for it to present in the map )
shared_ptr<comp_phys> phys( static_cast<comp_phys*>( it->second->getComponent( COMP_PHYS ).lock().get() ) );
// Get resulting Box2D vector
b2Vec2 vec = phys->getBody()->GetPosition();
// Get position component from entity
// Same as above, but this is the component shared …Run Code Online (Sandbox Code Playgroud)