-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneNode.cpp
126 lines (107 loc) · 2.29 KB
/
SceneNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "SceneNode.h"
#include <algorithm>
#include <assert.h>
void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= this->getTransform();
this->drawCurrent(target, states);
for (auto &child : this->childs)
{
child->draw(target, states);
}
}
void SceneNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
}
void SceneNode::attachChild(SceneNodePtr child)
{
child->parent = this;
this->childs.push_back(std::move(child));
}
SceneNode::SceneNodePtr SceneNode::detachChild(SceneNode& childNode)
{
auto iter = std::find_if(this->childs.begin(), this->childs.end(),
[&childNode](SceneNodePtr& node)
{
return node.get() == &childNode;
});
assert(iter != this->childs.end());
SceneNodePtr res = std::move(*iter);
res->parent = nullptr;
this->childs.erase(iter);
return res;
}
int SceneNode::getSize() const
{
return this->childs.size();
}
void SceneNode::setName(const std::string& name)
{
this->name = name;
}
void SceneNode::setName(std::string&& name)
{
this->name = std::move(name);
}
std::string SceneNode::getName() const
{
return this->name;
}
SceneNode::categoryType SceneNode::getCategory() const
{
return this->category;
}
void SceneNode::setCategory(const categoryType& category)
{
this->category = category;
}
sf::Transform SceneNode::getWorldTransform() const
{
auto result = sf::Transform::Identity;
const SceneNode* current = this;
while (current)
{
result *= current->getTransform();
current = current->parent;
}
return result;
}
sf::Vector2f SceneNode::getWorldPosition() const
{
//this->get
return this->getWorldTransform() * sf::Vector2f();
}
sf::Transform SceneNode::getParentTransform() const
{
if (!this->parent)
{
throw std::logic_error("Node has not parent");
}
return this->parent->getTransform();
}
void SceneNode::onCommand(const CommandType& command, const sf::Time& dt)
{
if (this->getCategory() & command.getCategory())
{
command(*this, dt);
}
else if (this->name == command.getName())
{
command(*this, dt);
}
for (auto& child : this->childs)
{
child->onCommand(command, dt);
}
}
void SceneNode::update(const sf::Time& dt)
{
this->updateCurrent(dt);
for (auto& child : this->childs)
{
child->update(dt);
}
}
void SceneNode::updateCurrent(const sf::Time& dt)
{
}