Former-commit-id: a6c2075cfbfd0eccf2b77def71c0d42684bed590 [formerly 36ece2bc6a148bde6cacf45084821d20edcd115e] [formerly 4a6988792ec026e65be6850c46dfe8ddda92a885 [formerly fd3f4f975de5c427f3adc98b220446fd255be396]] Former-commit-id: c87fdc9483202842267c60eff3d619f0df2963bf [formerly ee35202f1b2df7ca20da5b6d8b13147f2b92c933] Former-commit-id: dad5de1b00bb4413f7aa191ca06b7d43b659f32a
109 lines
2.2 KiB
C++
109 lines
2.2 KiB
C++
// Copyright (C) 2015 Jérôme Leclercq
|
|
// This file is part of the "Nazara Development Kit"
|
|
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
|
|
|
#include <NDK/BaseComponent.hpp>
|
|
#include <Nazara/Core/Error.hpp>
|
|
|
|
namespace Ndk
|
|
{
|
|
/*!
|
|
* \brief Constructs a BaseComponent object with an index
|
|
*
|
|
* \param index Index of the component
|
|
*/
|
|
|
|
inline BaseComponent::BaseComponent(ComponentIndex index) :
|
|
m_componentIndex(index),
|
|
m_entity(nullptr)
|
|
{
|
|
}
|
|
|
|
/*!
|
|
* \brief Gets the index of the component
|
|
* \return Index of the component
|
|
*/
|
|
|
|
inline ComponentIndex BaseComponent::GetIndex() const
|
|
{
|
|
return m_componentIndex;
|
|
}
|
|
|
|
/*!
|
|
* \brief Gets the maximal index of the components
|
|
* \return Index of the maximal component
|
|
*/
|
|
|
|
inline ComponentIndex BaseComponent::GetMaxComponentIndex()
|
|
{
|
|
return static_cast<ComponentIndex>(s_entries.size());
|
|
}
|
|
|
|
/*!
|
|
* \brief Registers a component
|
|
* \return Index of the registered component
|
|
*
|
|
* \param id Index of the component
|
|
* \param factory Factory to create the component
|
|
*
|
|
* \remark Produces a NazaraAssert if the identifier is already in use
|
|
*/
|
|
|
|
inline ComponentIndex BaseComponent::RegisterComponent(ComponentId id, Factory factoryFunc)
|
|
{
|
|
// We add our component to the end
|
|
ComponentIndex index = static_cast<ComponentIndex>(s_entries.size());
|
|
s_entries.resize(index + 1);
|
|
|
|
// We retrieve it and affect it
|
|
ComponentEntry& entry = s_entries.back();
|
|
entry.factory = factoryFunc;
|
|
entry.id = id;
|
|
|
|
// We ensure that id is not already in use
|
|
NazaraAssert(s_idToIndex.find(id) == s_idToIndex.end(), "This id is already in use");
|
|
|
|
s_idToIndex[id] = index;
|
|
|
|
return index;
|
|
}
|
|
|
|
/*!
|
|
* \brief Sets the entity on which the component operates
|
|
*/
|
|
|
|
inline void BaseComponent::SetEntity(Entity* entity)
|
|
{
|
|
if (m_entity != entity)
|
|
{
|
|
if (m_entity)
|
|
OnDetached();
|
|
|
|
m_entity = entity;
|
|
if (m_entity)
|
|
OnAttached();
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* \brief Initializes the BaseComponent
|
|
* \return true
|
|
*/
|
|
|
|
inline bool BaseComponent::Initialize()
|
|
{
|
|
// Nothing to do
|
|
return true;
|
|
}
|
|
|
|
/*!
|
|
* \brief Uninitializes the BaseComponent
|
|
*/
|
|
|
|
inline void BaseComponent::Uninitialize()
|
|
{
|
|
s_entries.clear();
|
|
s_idToIndex.clear();
|
|
}
|
|
}
|