Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .idea/QtSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/neon/structure/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ namespace neon

void Component::setEnabled(bool enabled)
{
_enabled = enabled;
if (_enabled != enabled) {
_enabled = enabled;
if (_enabled) {
onEnable();
} else {
onDisable();
}
}
}

void Component::destroy()
Expand All @@ -60,6 +67,14 @@ namespace neon
{
}

void Component::onEnable()
{
}

void Component::onDisable()
{
}

void Component::onStart()
{
}
Expand Down
123 changes: 115 additions & 8 deletions src/neon/structure/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@
#define REGISTER_COMPONENT(clazz, name) \
namespace \
{ \
struct _##clazz##_component_register_ \
inline static const struct _##clazz##_component_register_ \
{ \
_##clazz##_component_register_() \
{ \
neon::ComponentRegister::instance().registerComponent<clazz>(name); \
} \
}; \
_##clazz##_component_register_ __##clazz##_register; \
} __##clazz##_register; \
}
#define KEY_REGISTER_COMPONENT(key, clazz, name) \
struct _##key##_component_register_ \
inline static const struct _##key##_component_register_ \
{ \
_##key##_component_register_() \
{ \
neon::ComponentRegister::instance().registerComponent<clazz>(name); \
} \
}; \
_##key##_component_register_ __##key##_register;
} __##clazz##_register;

namespace neon
{
Expand Down Expand Up @@ -100,7 +98,7 @@ namespace neon
/**
* @return whether this component has started.
*/
bool hasStarted() const;
[[nodiscard]] bool hasStarted() const;

/**
* Returns whether this component is enabled.
Expand All @@ -115,7 +113,7 @@ namespace neon
/**
* Sets whether this component is enabled.
*
* Components won't be called on any event
* Components won't be called in any event
* when they are disabled.
*
* @param enabled whether this component is enabled.
Expand All @@ -139,6 +137,25 @@ namespace neon
*/
virtual void onConstruction();

/**
* Virtual method invoked when the component is enabled.
*
* This method is called when setEnabled(true) is called,
* but only if the component was previously disabled.
*
* It's also called on the first frame for components
* that start as enabled, just before onStart().
*/
virtual void onEnable();

/**
* Virtual method invoked when the component is disabled.
*
* This method is called when setEnabled(false) is called,
* but only if the component was previously enabled.
*/
virtual void onDisable();

/**
* Virtual method invoked before this component's first tick.
*/
Expand Down Expand Up @@ -206,6 +223,96 @@ namespace neon

// region UTIL

/**
* Returns the first component inside this game object
* that matches the given type.
* <p>
* If the component cannot be found, this method returns null.
* <p>
* This method may return a component whose type is a child
* of the given type.
*
* @tparam T the type of the component.
* @return the component or null.
*/
template<class T>
requires std::is_base_of_v<Component, T>
[[nodiscard]] IdentifiableWrapper<T> findComponent()
{
return _gameObject->findComponent<T>();
}

/**
* Returns the first component of type T that is found in this game object or any of its children recursively.
* <p>
* This performs a depth-first search.
*
* @tparam T The type of the component to find.
* @return A wrapper to the component, or null if not found.
*/
template<class T>
requires std::is_base_of_v<Component, T>
[[nodiscard]] IdentifiableWrapper<T> findComponentInChildren()
{
return _gameObject->findComponentInChildren<T>();
}

/**
* Returns the first component of type T that is found in this game object or any of its parents recursively.
*
* @tparam T The type of the component to find.
* @return A wrapper to the component, or null if not found.
*/
template<class T>
requires std::is_base_of_v<Component, T>
[[nodiscard]] IdentifiableWrapper<T> findComponentInParent()
{
return _gameObject->findComponentInParent<T>();
}

/**
* Returns all components of type T that are found in this game object and all its children recursively.
*
* @tparam T The type of the component to find.
* @return A list with all the found components.
*/
template<class T>
requires std::is_base_of_v<Component, T>
[[nodiscard]] std::vector<IdentifiableWrapper<T>> findComponentsInChildren()
{
return _gameObject->findComponentsInChildren<T>();
}

/**
* Returns all components of type T that are found in this game object and all its parents recursively.
*
* @tparam T The type of the component to find.
* @return A list with all the found components.
*/
template<class T>
requires std::is_base_of_v<Component, T>
[[nodiscard]] std::vector<IdentifiableWrapper<T>> findComponentsInParent()
{
return _gameObject->findComponentsInParent<T>();
}

/**
* Returns a list containing all components inside this game object
* that match the given type.
* <p>
* This method may return components whose types are children
* of the given type.
*
* @tparam T the type of the component.
* @return the list.
*/
template<class T>
requires std::is_base_of_v<Component, T>
[[nodiscard]] std::vector<IdentifiableWrapper<T>> findComponents()
{
return _gameObject->findComponents<T>();
}

/**
* Returns the room of this component's game object.
* <p>
Expand Down
18 changes: 14 additions & 4 deletions src/neon/structure/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ namespace neon

GameObject::GameObject(Room* room) :
_id(GAME_OBJECT_ID_GENERATOR++),
_name("Game Object " + std::to_string(_id)),
_name(std::format("Game Object {}", _id)),
_transform(this),
_room(room),
_components(),
_parent(nullptr),
_children()
_parent(nullptr)
{
if (_room == nullptr) {
throw std::runtime_error("Room is null!");
}
}

GameObject::GameObject(Room* room, std::string name) :
_id(GAME_OBJECT_ID_GENERATOR++),
_name(std::move(name)),
_transform(this),
_room(room),
_parent(nullptr)
{
if (_room == nullptr) {
throw std::runtime_error("Room is null!");
Expand Down
Loading