Skip to content

Commit

Permalink
getEntity now returns a reference
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmartin75 committed Oct 13, 2015
1 parent 3f8bcf5 commit fade61e
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions examples/1 Rendering/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ void Game::init()
// create the player
auto player = m_world.createEntity();

auto& playerSprite = player.addComponent<SpriteComponent>()->sprite;
auto& playerSprite = player.addComponent<SpriteComponent>().sprite;
playerSprite.setTexture(m_textureCache[PLAYER_TEXTURE_ID]);

auto& playerTransform = player.addComponent<TransformComponent>()->transform;
auto& playerTransform = player.addComponent<TransformComponent>().transform;
playerTransform.setPosition(m_renderTarget->getView().getSize().x / 2 - playerSprite.getLocalBounds().width / 2, m_renderTarget->getView().getSize().y / 2 - playerSprite.getLocalBounds().height / 2);

// activate the player
Expand Down
22 changes: 11 additions & 11 deletions examples/2 Animation/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,25 @@ void Game::init()
// create the player
m_player = m_world.createEntity();

auto& playerSprite = m_player.addComponent<SpriteComponent>()->sprite;
auto& playerSprite = m_player.addComponent<SpriteComponent>().sprite;
playerSprite.setTexture(m_textureCache[PLAYER_TEXTURE_ID]);

// load the animations
if(!m_player.addComponent<AnimationComponent>()->loadData("resources/meta/playerSpriteSheetFrames.txt"))
if(!m_player.addComponent<AnimationComponent>().loadData("resources/meta/playerSpriteSheetFrames.txt"))
{
std::cerr << "Failed to load animation data\n";
quit();
}

auto& playerAnimaton = *m_player.getComponent<AnimationComponent>();
auto& playerAnimaton = m_player.getComponent<AnimationComponent>();
playerAnimaton.repeat = true;
playerAnimaton.isPlaying = true;

auto& playerTransform = m_player.addComponent<TransformComponent>()->transform;
auto& playerTransform = m_player.addComponent<TransformComponent>().transform;
playerTransform.setPosition(m_renderTarget->getView().getSize().x / 2 - playerAnimaton.frameSize.x / 2, m_renderTarget->getView().getSize().y / 2 - playerAnimaton.frameSize.y / 2);

animationStateNames.reserve(m_player.getComponent<AnimationComponent>()->states.size());
for(auto& state : m_player.getComponent<AnimationComponent>()->states)
animationStateNames.reserve(m_player.getComponent<AnimationComponent>().states.size());
for(auto& state : m_player.getComponent<AnimationComponent>().states)
{
animationStateNames.emplace_back(state.first);
}
Expand Down Expand Up @@ -118,20 +118,20 @@ void Game::handleEvents(sf::Event event)
case sf::Keyboard::Key::Space:
{
// pause/play animation
bool isPlaying = m_player.getComponent<AnimationComponent>()->isPlaying = !m_player.getComponent<AnimationComponent>()->isPlaying;
bool isPlaying = m_player.getComponent<AnimationComponent>().isPlaying = !m_player.getComponent<AnimationComponent>().isPlaying;
std::cout << (isPlaying ? "Playing" : "Paused") << " animation\n";
}
break;
case sf::Keyboard::Key::S:
{
std::cout << "Stopped animation\n";
m_player.getComponent<AnimationComponent>()->stop();
m_player.getComponent<AnimationComponent>().stop();
}
break;
case sf::Keyboard::Key::R:
{
// toggle repeat
bool repeat = m_player.getComponent<AnimationComponent>()->repeat = !m_player.getComponent<AnimationComponent>()->repeat;
bool repeat = m_player.getComponent<AnimationComponent>().repeat = !m_player.getComponent<AnimationComponent>().repeat;
std::cout << "Turned repeat " << (repeat ? "on" : "off") << '\n';
}
break;
Expand All @@ -151,8 +151,8 @@ void Game::handleEvents(sf::Event event)
if(index >= animationStateNames.size()) index = animationStateNames.size() - 1;
std::cout << "Set animation: " << index << " - " << animationStateNames[index] << '\n';

m_player.getComponent<AnimationComponent>()->playingState = animationStateNames[index];
m_player.getComponent<AnimationComponent>()->reset();
m_player.getComponent<AnimationComponent>().playingState = animationStateNames[index];
m_player.getComponent<AnimationComponent>().reset();
break;
}
break;
Expand Down
18 changes: 9 additions & 9 deletions examples/3 Movement/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
const std::string PLAYER_TEXTURE_ID{"player"};
const sf::Color CLEAR_COLOR{60, 60, 60};

Game::Game(sf::RenderTarget& renderTarget)
: m_renderTarget(&renderTarget),
Game::Game(sf::RenderTarget& renderTarget) :
m_renderTarget(&renderTarget),
m_spriteRenderingSystem(renderTarget)
{
}
Expand All @@ -60,23 +60,23 @@ void Game::init()
// create the player
m_player = m_world.createEntity();

auto& playerSprite = m_player.addComponent<SpriteComponent>()->sprite;
auto& playerSprite = m_player.addComponent<SpriteComponent>().sprite;
playerSprite.setTexture(m_textureCache[PLAYER_TEXTURE_ID]);

// load the animations
if(!m_player.addComponent<AnimationComponent>()->loadData("resources/meta/playerSpriteSheetFrames.txt"))
if(!m_player.addComponent<AnimationComponent>().loadData("resources/meta/playerSpriteSheetFrames.txt"))
{
std::cerr << "Failed to load animation data\n";
quit();
}

auto& playerAnimaton = *m_player.getComponent<AnimationComponent>();
auto& playerTransform = m_player.addComponent<TransformComponent>()->transform;
auto& playerAnimaton = m_player.getComponent<AnimationComponent>();
auto& playerTransform = m_player.addComponent<TransformComponent>().transform;
playerTransform.setPosition(m_renderTarget->getView().getSize().x / 2 - playerAnimaton.frameSize.x / 2, m_renderTarget->getView().getSize().y / 2 - playerAnimaton.frameSize.y / 2);

m_player.addComponent<VelocityComponent>();

auto& playerComp = *m_player.addComponent<PlayerComponent>();
auto& playerComp = m_player.addComponent<PlayerComponent>();
playerComp.baseSpeed = 100;

// activate the player
Expand Down Expand Up @@ -123,11 +123,11 @@ void Game::onPlayerStateChanged(anax::Entity& e, PlayerComponent::State state)
{
static const std::string stateNames[] = { "idle", "run", "run", "shoot_run", "shoot_run", "jump", "shoot", "shoot_jump" };

auto& spriteComp = *e.getComponent<SpriteComponent>();
auto& spriteComp = e.getComponent<SpriteComponent>();

if(e.hasComponent<AnimationComponent>())
{
auto& animationComp = *e.getComponent<AnimationComponent>();
auto& animationComp = e.getComponent<AnimationComponent>();
auto& stateName = stateNames[static_cast<unsigned>(state)];

auto width = animationComp.frameSize.x;
Expand Down
26 changes: 13 additions & 13 deletions examples/4 Collision/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ void Game::init()

m_animationSystem.setFps(ANIMATION_FPS);

auto& playerSprite = m_player.addComponent<SpriteComponent>()->sprite;
auto& playerSprite = m_player.addComponent<SpriteComponent>().sprite;

playerSprite.setTexture(m_textureCache[PLAYER_TEXTURE_ID]);

if(!m_player.addComponent<AnimationComponent>()->loadData("resources/meta/playerSpriteSheetFrames.txt"))
if(!m_player.addComponent<AnimationComponent>().loadData("resources/meta/playerSpriteSheetFrames.txt"))
{
std::cerr << "Failed to load animation data\n";
quit();
}

auto& playerAnimation = *m_player.getComponent<AnimationComponent>();
auto& playerTransform = m_player.addComponent<TransformComponent>()->transform;
auto& playerAnimation = m_player.getComponent<AnimationComponent>();
auto& playerTransform = m_player.addComponent<TransformComponent>().transform;

auto& playerCollision = *m_player.addComponent<CollisionComponent>();
auto& playerCollision = m_player.addComponent<CollisionComponent>();
playerCollision.causesEvents = true;
// NOTE: should have this in a file, but this'll do for now
playerSprite.setOrigin(playerAnimation.frameSize.x / 2, playerAnimation.frameSize.y / 2);
Expand All @@ -95,18 +95,18 @@ void Game::init()
playerTransform.setPosition(m_renderTarget->getView().getSize().x / 2 - playerAnimation.frameSize.x / 2, m_renderTarget->getView().getSize().y / 2 - playerAnimation.frameSize.y / 2);

m_player.addComponent<VelocityComponent>();
auto& playerComp = *m_player.addComponent<PlayerComponent>();
auto& playerComp = m_player.addComponent<PlayerComponent>();
playerComp.baseSpeed = 100;

m_wall = m_world.createEntity();

// get wall sprite
auto& wallSprite = m_wall.addComponent<SpriteComponent>()->sprite;
auto& wallSprite = m_wall.addComponent<SpriteComponent>().sprite;
wallSprite.setTexture(m_textureCache[WALL_TEXTURE_ID]);

auto& wallTransform = m_wall.addComponent<TransformComponent>()->transform;
auto& wallTransform = m_wall.addComponent<TransformComponent>().transform;

auto& wallCollision = *m_wall.addComponent<CollisionComponent>();
auto& wallCollision = m_wall.addComponent<CollisionComponent>();
wallCollision.causesEvents = false;
wallCollision.boundingBox = { { 0, 0 }, { wallSprite.getLocalBounds().width, wallSprite.getLocalBounds().height} };

Expand Down Expand Up @@ -176,11 +176,11 @@ void Game::onPlayerStateChanged(anax::Entity& e, PlayerComponent::State state)
{
static const std::string stateNames[] = { "idle", "run", "run", "shoot_run", "shoot_run", "jump", "shoot", "shoot_jump" };

auto& sprite = e.getComponent<SpriteComponent>()->sprite;
auto& sprite = e.getComponent<SpriteComponent>().sprite;

if(e.hasComponent<AnimationComponent>())
{
auto& animationComp = *e.getComponent<AnimationComponent>();
auto& animationComp = e.getComponent<AnimationComponent>();
auto& stateName = stateNames[static_cast<unsigned>(state)];

animationComp.play(stateName);
Expand Down Expand Up @@ -218,7 +218,7 @@ void Game::onCollisionOccured(anax::Entity& e1, anax::Entity& e2)
return;
}

auto& velocity = m_player.getComponent<VelocityComponent>()->velocity;
auto& transform = m_player.getComponent<TransformComponent>()->transform;
auto& velocity = m_player.getComponent<VelocityComponent>().velocity;
auto& transform = m_player.getComponent<TransformComponent>().transform;
transform.move(-velocity);
}
4 changes: 2 additions & 2 deletions examples/common/src/Systems/AnimationSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ void AnimationSystem::update(double deltaTime)
auto entities = getEntities();
for(auto& e : entities)
{
auto& animation = *e.getComponent<AnimationComponent>();
auto& sprite = e.getComponent<SpriteComponent>()->sprite;
auto& animation = e.getComponent<AnimationComponent>();
auto& sprite = e.getComponent<SpriteComponent>().sprite;

AnimationComponent::State* animationState = nullptr;
if(!animation.playingState.empty())
Expand Down
6 changes: 3 additions & 3 deletions examples/common/src/Systems/CollisionSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ static sf::FloatRect getBBoxRectFor(const sf::Transformable& transformable, cons

static sf::FloatRect getBBoxRectFor(const anax::Entity& entity)
{
auto& transform = entity.getComponent<TransformComponent>()->transform;
auto& bbox = entity.getComponent<CollisionComponent>()->boundingBox;
auto& transform = entity.getComponent<TransformComponent>().transform;
auto& bbox = entity.getComponent<CollisionComponent>().boundingBox;
return getBBoxRectFor(transform, bbox);
}

Expand All @@ -58,7 +58,7 @@ void CollisionSystem::update(double)
for(std::size_t i = 0; i < colliders.size(); ++i)
{
auto& e1 = colliders[i];
if(!e1.getComponent<CollisionComponent>()->causesEvents)
if(!e1.getComponent<CollisionComponent>().causesEvents)
{
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/common/src/Systems/MovementSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void MovementSystem::update(double deltaTime)
auto entities = getEntities();
for(auto& entity : entities)
{
auto& transform = entity.getComponent<TransformComponent>()->transform;
auto& velocity = entity.getComponent<VelocityComponent>()->velocity;
auto& transform = entity.getComponent<TransformComponent>().transform;
auto& velocity = entity.getComponent<VelocityComponent>().velocity;

velocity *= (float)deltaTime;

Expand Down
4 changes: 2 additions & 2 deletions examples/common/src/Systems/PlayerInputSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ void PlayerInputSystem::update(double deltaTime)
auto entities = getEntities();
for(auto e : entities)
{
auto& playerComp = *e.getComponent<PlayerComponent>();
auto& velocity = e.getComponent<VelocityComponent>()->velocity;
auto& playerComp = e.getComponent<PlayerComponent>();
auto& velocity = e.getComponent<VelocityComponent>().velocity;

bool shootKeyPressed = sf::Keyboard::isKeyPressed(playerComp.controls.shoot);

Expand Down
4 changes: 2 additions & 2 deletions examples/common/src/Systems/SpriteRenderingSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void SpriteRenderingSystem::render()
auto entities = getEntities();
for(auto& entity : entities)
{
auto& sprite = entity.getComponent<SpriteComponent>()->sprite;
auto& transform = entity.getComponent<TransformComponent>()->transform;
auto& sprite = entity.getComponent<SpriteComponent>().sprite;
auto& transform = entity.getComponent<TransformComponent>().transform;

getRenderTarget().draw(sprite, transform.getTransform());
}
Expand Down
14 changes: 7 additions & 7 deletions include/anax/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace anax
/// \tparam The type of component you wish to add
/// \param args The arguments for the constructor of the component
template <typename T, typename... Args>
ComponentPtr<T> addComponent(Args&&... args);
T& addComponent(Args&&... args);

/// Removes a component
/// \tparam The type of component you wish to remove
Expand All @@ -174,7 +174,7 @@ namespace anax
/// \tparam The type of component you wish to retrieve
/// \return A pointer to the component
template <typename T>
ComponentPtr<T> getComponent() const;
T& getComponent() const;

/// Determines if this Entity has a component or not
/// \tparam The type of component you wish to check for
Expand All @@ -199,7 +199,7 @@ namespace anax
// so I may call them from templated public interfaces
void addComponent(Component* component, detail::TypeId componentTypeId);
void removeComponent(detail::TypeId componentTypeId);
Component* getComponent(detail::TypeId componentTypeId) const;
Component& getComponent(detail::TypeId componentTypeId) const;
bool hasComponent(detail::TypeId componentTypeId) const;


Expand All @@ -213,13 +213,13 @@ namespace anax
};

template <typename T, typename... Args>
ComponentPtr<T> Entity::addComponent(Args&&... args)
T& Entity::addComponent(Args&&... args)
{
static_assert(std::is_base_of<Component, T>(), "T is not a component, cannot add T to entity");
// TODO: align components by type
auto component = new T{std::forward<Args>(args)...};
addComponent(component, ComponentTypeId<T>());
return component;
return *component;
}

template <typename T>
Expand All @@ -230,10 +230,10 @@ namespace anax
}

template <typename T>
ComponentPtr<T> Entity::getComponent() const
T& Entity::getComponent() const
{
static_assert(std::is_base_of<Component, T>(), "T is not a component, cannot retrieve T from entity");
return static_cast<T*>(getComponent(ComponentTypeId<T>()));
return static_cast<T&>(getComponent(ComponentTypeId<T>()));
}

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions src/anax/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ namespace anax
getWorld().m_entityAttributes.componentStorage.removeComponent(*this, componentTypeId);
}

Component* Entity::getComponent(detail::TypeId componentTypeId) const
Component& Entity::getComponent(detail::TypeId componentTypeId) const
{
return &getWorld().m_entityAttributes.componentStorage.getComponent(*this, componentTypeId);
return getWorld().m_entityAttributes.componentStorage.getComponent(*this, componentTypeId);
}

bool Entity::hasComponent(detail::TypeId componentTypeId) const
Expand Down
4 changes: 2 additions & 2 deletions tests/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class MovementSystem : public anax::System<anax::Requires<PositionComponent, Vel
auto entities = getEntities();
for(auto& e : entities)
{
auto& position = *e.getComponent<PositionComponent>();
auto& velocity = *e.getComponent<VelocityComponent>();
auto& position = e.getComponent<PositionComponent>();
auto& velocity = e.getComponent<VelocityComponent>();

position.x += velocity.x;
position.y += velocity.y;
Expand Down
6 changes: 3 additions & 3 deletions tests/Test_Entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ const lest::test specification[] =
e.addComponent<PositionComponent>();

EXPECT(e.hasComponent<PositionComponent>() == true);
EXPECT(e.getComponent<PositionComponent>() != nullptr);
EXPECT_NO_THROW(e.getComponent<PositionComponent>());
EXPECT(countNonNull(e.getComponents()) == 1);
},

Expand All @@ -368,8 +368,8 @@ const lest::test specification[] =

EXPECT(e.hasComponent<PositionComponent>() == true);
EXPECT(e.hasComponent<VelocityComponent>() == true);
EXPECT(e.getComponent<PositionComponent>() != nullptr);
EXPECT(e.getComponent<VelocityComponent>() != nullptr);
EXPECT_NO_THROW(e.getComponent<PositionComponent>());
EXPECT_NO_THROW(e.getComponent<VelocityComponent>());

e.removeAllComponents();

Expand Down

0 comments on commit fade61e

Please sign in to comment.