hx3d  1
2D/3D Simple Game Framework
node.cpp
1 /*
2  Entity Component System: Node
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/ecs/node.hpp"
22 
23 namespace hx3d {
24 namespace ecs {
25 
26 Node::Node(const std::string name):
27  Entity(0), _name(name), _parent(nullptr)
28 {}
29 
31  if (_parent == nullptr) {
32  return transform;
33  }
34 
35  return transform.add(_parent->getFullTransform());
36 }
37 
38 void Node::removeChild(SceneGraph& sg, const std::string name) {
39  if (!childNameExists(name)) {
40  Log.Error("Node: child `%s` does not exists.", name.c_str());
41  return;
42  }
43 
44  const Ptr<Node>& obj = getChild<Node>(name);
45  sg.remove(obj->getPath());
46 }
47 
48 std::string Node::getPath() {
49  std::string path = _name;
50  Ptr<Node> cursor = _parent;
51 
52  while (cursor != nullptr) {
53 
54  // Root
55  if (cursor->_parent == nullptr) {
56  path = "/" + path;
57  }
58  else {
59  path = cursor->_name + "/" + path;
60  }
61 
62  cursor = cursor->_parent;
63  }
64 
65  return path;
66 }
67 
68 std::string Node::getName() {
69  return _name;
70 }
71 
73 
74 void Node::update(const float delta) {}
75 
77  sg.remove(getPath());
78 }
79 
80 unsigned int Node::getChildCount() {
81 
82  unsigned int totalChildren = 0;
83  for (auto& child: _children) {
84  totalChildren += child->getChildCount();
85  }
86 
87  return totalChildren + _children.size();
88 }
89 
91 
92 bool Node::childNameExists(const std::string name) {
93  for (const Ptr<Node>& o: _children) {
94  if (o->_name == name) {
95  return true;
96  }
97  }
98 
99  return false;
100 }
101 
102 } /* ecs */
103 } /* hx3d */
std::string getPath()
Get the game object full path from the root.
Definition: node.cpp:48
std::string getName()
Get the node name.
Definition: node.cpp:68
void removeChild(SceneGraph &sg, const std::string name)
Remove a child, using the SceneGraph.
Definition: node.cpp:38
2D/3D transform.
Definition: transform.hpp:35
Ptr< Node > _parent
Parent node.
Definition: node.hpp:147
Node(const std::string name)
Create a named Node. Should not be used directly.
Definition: node.cpp:26
std::string _name
Current name.
Definition: node.hpp:145
unsigned int getChildCount()
Get the recursive child count.
Definition: node.cpp:80
virtual void update(const float delta)
Update the node.
Definition: node.cpp:74
virtual void draw(graphics::BaseBatch &batch)
Draw the node.
Definition: node.cpp:72
hx3d framework namespace
Definition: audio.hpp:26
void remove(const std::string path)
Remove a game object from a path.
void destroy(SceneGraph &sg)
Destroy the node.
Definition: node.cpp:76
Transform add(const Transform &transform)
Add a transform to another.
Definition: transform.cpp:55
graphics::Transform transform
Current transform.
Definition: node.hpp:139
std::vector< Ptr< Node > > _children
Children nodes.
Definition: node.hpp:149
Node management in hierarchy.
Definition: scene_graph.hpp:43
bool childNameExists(const std::string name)
Test if the object have a child.
Definition: node.cpp:92
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
Base element, attachable with components.
Definition: entity.hpp:31
graphics::Transform getFullTransform()
Get the game object full transform.
Definition: node.cpp:30
Draw meshes and texts on screen.
Definition: base_batch.hpp:41
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34