You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.0 KiB
C++

3 years ago
#include "scenenode.h"
#include <algorithm>
SceneNode::SceneNode()
{}
SceneNode::~SceneNode()
{}
bool SceneNode::attachChild(const SceneNodeSPtr &child)
{
child->parent = shared_from_this();
vec_children.emplace_back(std::move(child));
return true;
}
SceneNodeSPtr SceneNode::detachChild(const SceneNode &node)
{
const auto found = std::find_if(vec_children.begin(), vec_children.end(),
[&] (SceneNodeSPtr& p) -> bool
{
return p.get() == &node;
});
if (found == vec_children.end())
return nullptr;
SceneNodeSPtr result = *found;
result->parent = nullptr;
vec_children.erase(found);
return result;
}
sf::Transform SceneNode::getWorldTransform() const
{
sf::Transform transform = sf::Transform::Identity;
for (SceneNodeConstSPtr node = shared_from_this(); node != nullptr; node = node->parent)
{
transform *= node->getTransform();
}
return transform;
}
sf::Vector2f SceneNode::getWorldPosition() const
{
return getWorldTransform() * sf::Vector2f();
}
void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
drawCurrent(target, states);
for (const auto& ch : vec_children)
ch->draw(target, states);
}
void SceneNode::update(const sf::Time& dt)
{
updateCurrent(dt);
updateChildren(dt);
}
void SceneNode::onCommand(const Command& command, const sf::Time& dt)
{
if (command.category & category())
command.action(*shared_from_this(), dt);
for (auto& ch : vec_children)
ch->onCommand(command, dt);
}
Category::Type SceneNode::category() const
{
return Category::Type::Scene;
}
void SceneNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{}
void SceneNode::updateCurrent(const sf::Time& dt)
{}
void SceneNode::updateChildren(const sf::Time &dt)
{
for (SceneNodeSPtr& child : vec_children)
child->update(dt);
}