refactoring; sometimes a component was added twice to the list of components

This commit is contained in:
Mikulas Florek 2013-11-22 22:01:36 +01:00
parent e45025739c
commit b6b7e4c32c
5 changed files with 55 additions and 54 deletions

View file

@ -92,7 +92,7 @@ namespace Lux
int entity_index;
serializer.deserializeArrayItem(entity_index);
Entity e(m_impl->universe, entity_index);
const Universe::EntityComponentList& cmps = m_impl->universe->getComponents(e);
const Entity::ComponentList& cmps = e.getComponents();
m_impl->animables[i].renderable = Component::INVALID;
for(int j = 0; j < cmps.size(); ++j)
{
@ -117,7 +117,7 @@ namespace Lux
ComponentEvent& e = static_cast<ComponentEvent&>(event);
if(e.component.type == renderable_type)
{
const Universe::EntityComponentList& cmps = e.component.entity.universe->getComponents(e.component.entity);
const Entity::ComponentList& cmps = e.component.entity.getComponents();
for(int i = 0; i < cmps.size(); ++i)
{
if(cmps[i].type == animable_type)
@ -138,7 +138,7 @@ namespace Lux
animable.time = 0;
animable.renderable = Component::INVALID;
const Universe::EntityComponentList& cmps = entity.universe->getComponents(entity);
const Entity::ComponentList& cmps = entity.getComponents();
for(int i = 0; i < cmps.size(); ++i)
{
if(cmps[i].type == renderable_type)

View file

@ -127,6 +127,7 @@ private:
Universe* m_universe;
Navigation m_navigation;
PhysicsSystem* m_physics_system;
PhysicsScene* m_physics_scene;
ResponceCallback m_response_callback;
MemoryStream m_stream;
map<unsigned int, vector<PropertyDescriptor> > m_component_properties;
@ -247,7 +248,7 @@ void EditorServer::save(IStream& stream)
m_universe->serialize(serializer);
m_script_system->serialize(serializer);
m_renderer->serialize(serializer);
m_universe->getPhysicsScene()->serialize(serializer);
m_physics_scene->serialize(serializer);
m_animation_system->serialize(serializer);
serializer.serialize("cam_pos_x", m_camera_pos.x);
serializer.serialize("cam_pos_y", m_camera_pos.y);
@ -340,7 +341,7 @@ void EditorServer::gameModeLoop()
m_script_system->update(0.05f);
m_navigation.update(0.05f);
m_universe->getPhysicsScene()->update(0.05f);
m_physics_scene->update(0.05f);
input_system.update(0.05f);
}
HDC hdc;
@ -391,7 +392,7 @@ void EditorServer::sendComponent(unsigned int type_crc)
{
m_stream.flush();
m_stream.write(2);
const Universe::EntityComponentList& cmps = m_universe->getComponents(m_selected_entity);
const Entity::ComponentList& cmps = m_selected_entity.getComponents();
string value;
for(int i = 0; i < cmps.size(); ++i)
{
@ -443,7 +444,7 @@ void EditorServer::addComponent(unsigned int type_crc)
{
if(m_selected_entity.isValid())
{
const Universe::EntityComponentList& cmps = m_selected_entity.universe->getComponents(m_selected_entity);
const Entity::ComponentList& cmps = m_selected_entity.getComponents();
for(int i = 0; i < cmps.size(); ++i)
{
if(cmps[i].type == type_crc)
@ -458,7 +459,7 @@ void EditorServer::addComponent(unsigned int type_crc)
}
else if(type_crc == physical_type)
{
m_universe->getPhysicsScene()->createActor(m_selected_entity);
m_physics_scene->createActor(m_selected_entity);
}
else if(type_crc == point_light_type)
{
@ -466,7 +467,7 @@ void EditorServer::addComponent(unsigned int type_crc)
}
else if(type_crc == physical_controller_type)
{
m_universe->getPhysicsScene()->createController(m_selected_entity);
m_physics_scene->createController(m_selected_entity);
}
else if(type_crc == script_type)
{
@ -508,7 +509,7 @@ void EditorServer::lookAtSelected()
void EditorServer::editScript()
{
string path;
const Universe::EntityComponentList& cmps = m_universe->getComponents(m_selected_entity);
const Entity::ComponentList& cmps = m_selected_entity.getComponents();
for(int i = 0; i < cmps.size(); ++i)
{
if(cmps[i].type == script_type)
@ -531,7 +532,7 @@ void EditorServer::removeEntity()
void EditorServer::removeComponent(unsigned int type_crc)
{
const Universe::EntityComponentList& cmps = m_selected_entity.universe->getComponents(m_selected_entity);
const Entity::ComponentList& cmps = m_selected_entity.getComponents();
Component cmp;
for(int i = 0; i < cmps.size(); ++i)
{
@ -547,7 +548,7 @@ void EditorServer::removeComponent(unsigned int type_crc)
}
else if(type_crc == physical_type)
{
m_universe->getPhysicsScene()->destroyActor(cmp);
m_physics_scene->destroyActor(cmp);
}
else if(type_crc == point_light_type)
{
@ -587,7 +588,7 @@ void EditorServer::load(IStream& stream)
m_universe->deserialize(serializer);
m_script_system->deserialize(serializer);
m_renderer->deserialize(serializer);
m_universe->getPhysicsScene()->deserialize(serializer);
m_physics_scene->deserialize(serializer);
m_animation_system->deserialize(serializer);
serializer.deserialize("cam_pos_x", m_camera_pos.x);
serializer.deserialize("cam_pos_y", m_camera_pos.y);
@ -716,8 +717,8 @@ void EditorServer::renderPhysics()
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
int nbac = m_universe->getPhysicsScene()->getRawScene()->getNbActors(physx::PxActorTypeSelectionFlag::eRIGID_STATIC);
const physx::PxRenderBuffer& rb = m_universe->getPhysicsScene()->getRawScene()->getRenderBuffer();
int nbac = m_physics_scene->getRawScene()->getNbActors(physx::PxActorTypeSelectionFlag::eRIGID_STATIC);
const physx::PxRenderBuffer& rb = m_physics_scene->getRawScene()->getRenderBuffer();
const physx::PxU32 numLines = rb.getNbLines();
const physx::PxU32 numPoints = rb.getNbPoints();
const physx::PxU32 numTri = rb.getNbTriangles();
@ -781,7 +782,7 @@ void EditorServer::setProperty(void* data, int size)
Component cmp = Component::INVALID;
if(m_selected_entity.isValid())
{
const Universe::EntityComponentList& cmps = m_universe->getComponents(m_selected_entity);
const Entity::ComponentList& cmps = m_selected_entity.getComponents();
for(int i = 0; i < cmps.size(); ++i)
{
if(cmps[i].type == component_type)
@ -873,7 +874,7 @@ void EditorServer::selectEntity(Entity e)
m_stream.flush();
m_stream.write(1);
m_stream.write(e.index);
const Universe::EntityComponentList& cmps = m_universe->getComponents(e);
const Entity::ComponentList& cmps = e.getComponents();
m_stream.write(cmps.size());
for(int i = 0; i < cmps.size(); ++i)
{
@ -941,7 +942,7 @@ void EditorServer::onEvent(Event& evt)
}
if(e.is_created)
{
Lux::Universe::EntityComponentList cmps = e.component.entity.universe->getComponents(e.component.entity);
Lux::Entity::ComponentList cmps = e.component.entity.getComponents();
bool found = false;
for(int i = 0; i < cmps.size(); ++i)
{
@ -960,7 +961,7 @@ void EditorServer::onEvent(Event& evt)
}
else
{
if(e.component.entity.universe->getComponents(e.component.entity).empty())
if(e.component.entity.getComponents().empty())
{
EditorIcon* er = new EditorIcon();
er->create(e.component.entity, Component::INVALID);
@ -1001,6 +1002,9 @@ void EditorServer::destroyUniverse()
m_animation_system->setUniverse(0);
m_script_system->setUniverse(0);
m_gizmo.setUniverse(0);
m_physics_scene->destroy();
delete m_physics_scene;
m_physics_scene = 0;
m_universe->destroy();
delete m_universe;
m_universe = 0;
@ -1009,7 +1013,9 @@ void EditorServer::destroyUniverse()
void EditorServer::createUniverse(bool create_scene, const char* base_path)
{
m_universe = new Universe();
m_universe->create(*m_physics_system);
m_physics_scene = new PhysicsScene();
m_physics_scene->create(*m_physics_system, *m_universe);
m_universe->create();
m_universe->getEventManager()->registerListener(EntityMovedEvent::type, this, &EditorServer::onEvent);
m_universe->getEventManager()->registerListener(ComponentEvent::type, this, &EditorServer::onEvent);
m_universe->getEventManager()->registerListener(EntityDestroyedEvent::type, this, &EditorServer::onEvent);

View file

@ -195,7 +195,6 @@ void Renderer::renderScene()
Entity(m_impl->universe, m_impl->renderables[i].entity).getMatrix(mtx);
h3dSetNodeTransMat(node, &mtx.m11);
Entity e(m_impl->universe, m_impl->renderables[i].entity);
m_impl->universe->getEventManager()->emitEvent(ComponentEvent(Component(e, rend_type, this, i)));
}
}
m_impl->update_bb = false;
@ -268,15 +267,12 @@ void Renderer::setMesh(Component cmp, const string& str)
Component Renderer::getRenderable(Universe& universe, H3DNode node)
{
for(int i = 0, c = universe.getComponents().size(); i < c; ++i)
for(int i = 0; i < m_impl->renderables.size(); ++i)
{
const vector<Component>& cmps = universe.getComponents()[i];
for(int j = 0, cj = cmps.size(); j < cj; ++j)
{
if(cmps[j].type == rend_type && m_impl->renderables[cmps[j].index].node == node)
{
return cmps[j];
}
if(m_impl->renderables[i].node == node)
{
Entity e(m_impl->universe, i);
return Component(e, rend_type, this, i);
}
}
return Component::INVALID;
@ -314,8 +310,8 @@ void RendererImpl::postDeserialize()
H3DNode& node = lights[i].node;
node = h3dAddLightNode(H3DRootNode, "", 0, "LIGHTING", "SHADOWMAP");
Matrix mtx;
universe->getRotations()[lights[i].entity].toMatrix(mtx);
mtx.setTranslation(universe->getPositions()[lights[i].entity]);
universe->getRotation(lights[i].entity).toMatrix(mtx);
mtx.setTranslation(universe->getPosition(lights[i].entity));
h3dSetNodeTransMat(node, &mtx.m11);
h3dSetNodeParamF(node, H3DLight::RadiusF, 0, 20.0f);
h3dSetNodeParamF(node, H3DLight::FovF, 0, 90);
@ -492,7 +488,7 @@ void RendererImpl::onEvent(Event& event)
{
Matrix mtx;
Entity entity = static_cast<EntityMovedEvent&>(event).entity;
const vector<Component>& cmps = entity.universe->getComponents(entity);
const Entity::ComponentList& cmps = entity.getComponents();
for(int i = 0, c = cmps.size(); i < c; ++i)
{
if(cmps[i].type == rend_type)
@ -511,7 +507,7 @@ void RendererImpl::onEvent(Event& event)
else if(event.getType() == EntityDestroyedEvent::type)
{
Entity entity = static_cast<EntityDestroyedEvent&>(event).entity;
const vector<Component>& cmps = entity.universe->getComponents(entity);
const Entity::ComponentList& cmps = entity.getComponents();
for(int i = 0, c = cmps.size(); i < c; ++i)
{
if(cmps[i].type == rend_type)

View file

@ -29,16 +29,11 @@ void Universe::destroy()
m_positions.clear();
m_rotations.clear();
m_component_list.clear();
m_physics_scene->destroy();
delete m_physics_scene;
m_physics_scene = 0;
}
void Universe::create(PhysicsSystem& physics_system)
void Universe::create()
{
m_physics_scene = new PhysicsScene();
m_physics_scene->create(physics_system, *this);
m_positions.reserve(1000);
m_rotations.reserve(1000);
m_component_list.reserve(1000);
@ -64,9 +59,16 @@ const Vec3& Entity::getPosition() const
}
const Entity::ComponentList& Entity::getComponents() const
{
assert(isValid());
return universe->m_component_list[index];
}
const Component& Entity::getComponent(unsigned int type)
{
const Universe::EntityComponentList& cmps = universe->getComponents(*this);
const Entity::ComponentList& cmps = getComponents();
for(int i = 0, c = cmps.size(); i < c; ++i)
{
if(cmps[i].type == type)
@ -89,8 +91,8 @@ Matrix Entity::getMatrix() const
void Entity::getMatrix(Matrix& mtx) const
{
universe->getRotations()[index].toMatrix(mtx);
mtx.setTranslation(universe->getPositions()[index]);
universe->m_rotations[index].toMatrix(mtx);
mtx.setTranslation(universe->m_positions[index]);
}

View file

@ -13,8 +13,6 @@ namespace Lux
class EventManager;
class PhysicsScene;
class PhysicsSystem;
struct Vec3;
struct Quat;
class Event;
@ -26,6 +24,8 @@ class ISerializer;
struct LUX_ENGINE_API Entity LUX_FINAL
{
typedef vector<Component> ComponentList;
Entity() {}
Entity(Universe* uni, int i) : index(i), universe(uni) {}
@ -42,6 +42,8 @@ struct LUX_ENGINE_API Entity LUX_FINAL
void translate(const Vec3& t);
bool isValid() const { return index >= 0; }
const Component& getComponent(unsigned int type);
const ComponentList& getComponents() const;
bool operator ==(const Entity& rhs) const;
@ -78,28 +80,24 @@ struct LUX_ENGINE_API Component LUX_FINAL
};
class LUX_ENGINE_API Universe
class LUX_ENGINE_API Universe LUX_FINAL
{
friend struct Entity;
public:
typedef vector<Component> EntityComponentList;
typedef vector<EntityComponentList> ComponentList;
typedef vector<Entity::ComponentList> ComponentList;
public:
Universe();
~Universe();
void create(PhysicsSystem& physics_system);
void create();
void destroy();
Entity createEntity();
void destroyEntity(Entity entity);
Vec3 getPosition(int index) { return m_positions[index]; }
Quat getRotation(int index) { return m_rotations[index]; }
EventManager* getEventManager() const { return m_event_manager; }
PhysicsScene* getPhysicsScene() const { return m_physics_scene; }
const vector<Vec3>& getPositions() const { return m_positions; }
const vector<Quat>& getRotations() const { return m_rotations; }
const EntityComponentList& getComponents(Entity e) const { return m_component_list[e.index]; }
const ComponentList& getComponents() const { return m_component_list; }
void serialize(ISerializer& serializer);
void deserialize(ISerializer& serializer);
@ -114,7 +112,6 @@ class LUX_ENGINE_API Universe
vector<int> m_free_slots;
ComponentList m_component_list;
EventManager* m_event_manager;
PhysicsScene* m_physics_scene;
};