Skip to content

Commit

Permalink
Merge pull request miguelmartin75#80 from spitofland/master
Browse files Browse the repository at this point in the history
Make isValid more useful/user-friendly
  • Loading branch information
miguelmartin75 authored Dec 17, 2016
2 parents 448e6c3 + 9a080e4 commit fa01241
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/anax/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ namespace anax

bool World::isActivated(const Entity& entity) const
{
ANAX_ASSERT(isValid(entity), "invalid entity passed to isActivated");

return m_entityAttributes.attributes[entity.getId().index].activated;
if(isValid(entity))
return m_entityAttributes.attributes[entity.getId().index].activated;
else
return false;
}

bool World::isValid(const anax::Entity &entity) const
Expand Down
14 changes: 8 additions & 6 deletions src/anax/detail/EntityIdPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ namespace anax

Entity::Id EntityIdPool::get(std::size_t index) const
{
ANAX_ASSERT(index < m_counts.size(), "Entity index is out of range");
ANAX_ASSERT(!(m_counts[index] == 0), "Entity ID does not exist");
return Entity::Id{index, m_counts[index]};
if(index < m_counts.size())
return Entity::Id{index, m_counts[index]};
else
return Entity::Id{index, 0};
}

bool EntityIdPool::isValid(Entity::Id id) const
{
return id.counter == m_counts[id.index];
if(id.index >= m_counts.size())
return false;
else
return (id.counter == m_counts[id.index]) && (id.counter > 0);
}

std::size_t EntityIdPool::getSize() const
Expand All @@ -94,8 +98,6 @@ namespace anax
m_counts.clear();
m_freeList.clear();
m_nextId = 0;

m_counts.resize(m_defaultPoolSize);
}
}
}
4 changes: 2 additions & 2 deletions tests/Test_Entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ using namespace anax;
// ✓ Removing a component => does hasComponent return false?
// ✓ Removing all components => does hasComponent return false?
// 6. Retrieving an entity via index
// ✓ Invalid index => assertion occurs?
// ✓ Invalid index => invalid entity returned?
// ✓ Valid index => appropriate entity returned?
// ✓ Multiple entities added/removed => appropriate entity returned?

Expand Down Expand Up @@ -414,7 +414,7 @@ const lest::test specification[] =
anax::World world;

auto e = world.createEntity();
EXPECT_THROWS_AS(world.getEntity(-1), anax::TestException);
EXPECT(!(world.getEntity(-1).isValid()));
}
};

Expand Down

0 comments on commit fa01241

Please sign in to comment.