Implemented some tools. Also, implemented inventory.
This commit is contained in:
@@ -81,13 +81,28 @@ GameObject* GameObject::instantiate(GameObject* gameObject, std::string name, Ga
|
||||
return instance;
|
||||
}
|
||||
|
||||
size_t GameObject::componentsSize() const
|
||||
{
|
||||
return m_components.size();
|
||||
}
|
||||
|
||||
GameObject::ComponentIterator GameObject::componentsBegin()
|
||||
{
|
||||
return m_components.begin();
|
||||
}
|
||||
|
||||
GameObject::ComponentIterator GameObject::componentsEnd()
|
||||
{
|
||||
return m_components.end();
|
||||
}
|
||||
|
||||
void GameObject::addChild(GameObject* obj)
|
||||
{
|
||||
m_children.push_back(obj);
|
||||
obj->m_parent = this;
|
||||
}
|
||||
|
||||
GameObject* GameObject::removeChild(GameObject::iterator it)
|
||||
GameObject* GameObject::removeChild(GameObject::ChildrenIterator it)
|
||||
{
|
||||
m_children.erase(it);
|
||||
return *it;
|
||||
@@ -107,7 +122,7 @@ GameObject* GameObject::removeChild(GameObject* obj)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void GameObject::destroyChild(GameObject::iterator it)
|
||||
void GameObject::destroyChild(GameObject::ChildrenIterator it)
|
||||
{
|
||||
delete *it;
|
||||
m_children.erase(it);
|
||||
@@ -131,12 +146,12 @@ size_t GameObject::childrenSize() const
|
||||
return m_children.size();
|
||||
}
|
||||
|
||||
GameObject::iterator GameObject::childrenBegin()
|
||||
GameObject::ChildrenIterator GameObject::childrenBegin()
|
||||
{
|
||||
return m_children.begin();
|
||||
}
|
||||
|
||||
GameObject::iterator GameObject::childrenEnd()
|
||||
GameObject::ChildrenIterator GameObject::childrenEnd()
|
||||
{
|
||||
return m_children.end();
|
||||
}
|
||||
@@ -146,6 +161,20 @@ GameObject* GameObject::parent()
|
||||
return m_parent;
|
||||
}
|
||||
|
||||
GameObject::ChildrenIterator GameObject::findByName(std::string name)
|
||||
{
|
||||
return findByName(name, childrenBegin());
|
||||
}
|
||||
|
||||
GameObject::ChildrenIterator GameObject::findByName(std::string name, ChildrenIterator begin)
|
||||
{
|
||||
for (auto it = begin; it != childrenEnd(); it++)
|
||||
if ((*it)->name == name)
|
||||
return it;
|
||||
|
||||
return childrenEnd();
|
||||
}
|
||||
|
||||
void GameObject::onCreate()
|
||||
{
|
||||
// Enable self
|
||||
@@ -303,3 +332,4 @@ void GameObject::dumpTree(unsigned level)
|
||||
|
||||
} /* namespace model */
|
||||
} /* namespace farmlands */
|
||||
|
||||
|
@@ -27,7 +27,11 @@ namespace model {
|
||||
class GameObject : public INonAssignable, public ICloneable<GameObject>
|
||||
{
|
||||
public:
|
||||
typedef std::vector<GameObject*>::iterator iterator;
|
||||
typedef std::unordered_map<std::type_index, Component*> ComponentContainer;
|
||||
typedef std::vector<GameObject*> ChildrenContainer;
|
||||
|
||||
typedef ComponentContainer::iterator ComponentIterator;
|
||||
typedef ChildrenContainer::iterator ChildrenIterator;
|
||||
|
||||
// Constructors
|
||||
GameObject();
|
||||
@@ -40,21 +44,36 @@ namespace model {
|
||||
template <typename T> T* component();
|
||||
template <typename T> bool haveComponent();
|
||||
template <typename T> void addComponent(T* component);
|
||||
template <typename T> void removeComponent();
|
||||
template <typename T> T* removeComponent();
|
||||
template <typename T> void destroyComponent();
|
||||
|
||||
size_t componentsSize() const;
|
||||
ComponentIterator componentsBegin();
|
||||
ComponentIterator componentsEnd();
|
||||
|
||||
// Tree methods
|
||||
void addChild(GameObject* obj);
|
||||
GameObject* removeChild(iterator it);
|
||||
GameObject* removeChild(ChildrenIterator it);
|
||||
GameObject* removeChild(GameObject* obj);
|
||||
void destroyChild(iterator it);
|
||||
void destroyChild(ChildrenIterator it);
|
||||
void destroyChild(GameObject* obj);
|
||||
|
||||
size_t childrenSize() const;
|
||||
iterator childrenBegin();
|
||||
iterator childrenEnd();
|
||||
ChildrenIterator childrenBegin();
|
||||
ChildrenIterator childrenEnd();
|
||||
|
||||
GameObject* parent();
|
||||
|
||||
// Search method
|
||||
ChildrenIterator findByName(std::string name);
|
||||
ChildrenIterator findByName(std::string name, ChildrenIterator begin);
|
||||
|
||||
template <typename T>
|
||||
ChildrenIterator findByComponent();
|
||||
|
||||
template <typename T>
|
||||
ChildrenIterator findByComponent(ChildrenIterator begin);
|
||||
|
||||
// Game object methods
|
||||
void onCreate();
|
||||
void onInitialize();
|
||||
@@ -81,10 +100,10 @@ namespace model {
|
||||
|
||||
private:
|
||||
// Components
|
||||
std::unordered_map<std::type_index, Component*> m_components;
|
||||
ComponentContainer m_components;
|
||||
|
||||
// Tree
|
||||
std::vector<GameObject*> m_children;
|
||||
ChildrenContainer m_children;
|
||||
GameObject* m_parent;
|
||||
|
||||
// Properties
|
||||
@@ -131,10 +150,42 @@ namespace model {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void GameObject::removeComponent()
|
||||
T* GameObject::removeComponent()
|
||||
{
|
||||
std::type_index typeIndex(typeid(T));
|
||||
return m_components.erase(typeIndex);
|
||||
T* comp = nullptr;
|
||||
|
||||
auto it = m_components.find(typeIndex);
|
||||
if (it != m_components.end())
|
||||
{
|
||||
comp = it->second;
|
||||
m_components.erase(it);
|
||||
}
|
||||
|
||||
return comp;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void GameObject::destroyComponent()
|
||||
{
|
||||
std::type_index typeIndex(typeid(T));
|
||||
m_components.erase(typeIndex);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
GameObject::ChildrenIterator GameObject::findByComponent()
|
||||
{
|
||||
return findByComponent<T>(childrenBegin());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
GameObject::ChildrenIterator GameObject::findByComponent(GameObject::ChildrenIterator begin)
|
||||
{
|
||||
for (auto it = begin; it != childrenEnd(); it++)
|
||||
if ((*it)->haveComponent<T>())
|
||||
return it;
|
||||
|
||||
return childrenEnd();
|
||||
}
|
||||
|
||||
} /* namespace model */
|
||||
|
Reference in New Issue
Block a user