2#include "main/EventSystem.hpp"
3#include "utilities/ID.hpp"
4#include "utilities/Vector3.hpp"
5#include "utilities/Quaternion.hpp"
6#include "utilities/Matrix4.hpp"
8#include "main/Component.hpp"
20 template<
typename EventType>
21 class ChildEvent :
public Event {
25 const std::string
GetEventType()
const override {
return GetStaticEventType(); };
26 static const std::string GetStaticEventType() {
return "Child" + EventType::GetStaticEventType(); }
36 friend class SceneManager;
52 bool isActive =
false;
161 void Update(
double deltaTime);
163 #ifdef StevEngine_SHOW_WINDOW
188 template<
typename EventType>
192 return events.Subscribe<EventType>(handler);
200 template<
typename EventType>
202 events.Publish(event);
220 template<
typename EventType>
221 void ChildPublish(
const EventType& event,
Utilities::ID object) {
227 std::vector<std::pair<Utilities::ID, std::string>> handlers;
236 int AddChild(
const Utilities::ID& gameObjectID);
283 std::vector<Utilities::ID> children;
287 std::vector<std::unique_ptr<Component>> components;
297 typename std::enable_if<std::is_base_of<Component,T>::value,T*>::type
300 for (
int i = 0; i < components.size(); i++) {
301 if (
dynamic_cast<T*
>(components[i].get())) {
302 T* component = (T*)components[i].get();
307 if (log) Log::Error(std::format(
"No component of type \"{}\" found on object {}",
typeid(T).
name(),
id.GetString()),
true);
317 typename std::enable_if<std::is_base_of<Component,T>::value, std::vector<T*> >::type
320 std::vector<T*> foundComponents;
322 for (
int i = 0; i < components.size(); i++) {
323 if (
dynamic_cast<T*
>(components[i].get())) {
324 T* component = (T*)components[i].get();
325 foundComponents.push_back(component);
329 return foundComponents;
337 std::vector<Component*> vec;
338 for(
auto& component : components) vec.push_back(component.get());
348 typename std::enable_if<std::is_base_of<Component,T>::value, std::vector<T*> >::type
351 std::vector<T*> allComponents;
354 allComponents.insert(allComponents.end(), current.begin(), current.end());
356 for (
int i = 0; i < children.size(); i++) {
358 allComponents.insert(allComponents.end(), childComp.begin(), childComp.end());
361 return allComponents;
370 typename std::enable_if<std::is_base_of<Component,T>::value, T* >::type
372 if(!component)
return nullptr;
374 if (component->unique) {
376 Log::Error(std::format(
"Object {} already has a component of type \"{}\", and this component requires to be unique",
id.GetString(),
typeid(T).
name()),
true);
381 component->SetObject(*
this, this->scene);
382 components.emplace_back(component);
383 if(isActive) component->Start();
392 typename std::enable_if<std::is_base_of<Component,T>::value,
void >::type
395 auto i = components.begin();
396 while (i != components.end()) {
397 if (
dynamic_cast<T*
>(*i)) {
401 i = components.erase(i);
415 typename std::enable_if<std::is_base_of<Component,T>::value,
void >::type
418 for (
int i = 0; i < components.size(); i++) {
419 if (components[i].get() == component) {
421 components.erase(components.begin() + i);
427 Log::Error(std::format(
"No component of type \"{}\" found on object {}",
typeid(T).
name(),
id.GetString()),
true);
443 : position(position), rotation(rotation),
scale(
scale) {}
444 const std::string
GetEventType()
const override {
return GetStaticEventType(); };
445 static const std::string GetStaticEventType() {
return "TransformUpdateEvent"; }
449 #ifdef StevEngine_SHOW_WINDOW
460 const std::string
GetEventType()
const override {
return GetStaticEventType(); };
461 static const std::string GetStaticEventType() {
return "DrawEvent"; }
469 class DeactivateEvent :
public Event {
472 const std::string
GetEventType()
const override {
return GetStaticEventType(); };
473 static const std::string GetStaticEventType() {
return "DeactivateEvent"; }
Wrapper for events from child objects.
Definition GameObject.hpp:21
const std::string GetEventType() const override
Get type identifier for this event.
Definition GameObject.hpp:25
const EventType & event
Original event.
Definition GameObject.hpp:24
const std::string GetEventType() const override
Get type identifier for this event.
Definition GameObject.hpp:472
DrawEvent(Utilities::Matrix4 transform)
Create draw event.
Definition GameObject.hpp:459
const std::string GetEventType() const override
Get type identifier for this event.
Definition GameObject.hpp:460
Utilities::Matrix4 transform
World transform matrix.
Definition GameObject.hpp:462
void Publish(const Event &event)
Publish event to subscribers.
Definition EventSystem.cpp:28
Base class for all engine events.
Definition EventSystem.hpp:12
Core game object class.
Definition GameObject.hpp:35
Utilities::Vector3 GetPosition() const
Get local position.
Definition GameObject.cpp:40
Utilities::Quaternion GetRotation() const
Get local rotation.
Definition GameObject.cpp:41
~GameObject()
Definition GameObject.cpp:160
Utilities::Stream Export(Utilities::StreamType type) const
Serialize object to a stream.
Definition GameObject.cpp:128
std::string name
Object name/label.
Definition GameObject.hpp:41
GameObject & GetChild(int index) const
Get child object.
Definition GameObject.cpp:100
Scene & GetScene() const
Get containing Scene.
Definition GameObject.cpp:109
std::enable_if< std::is_base_of< Component, T >::value, std::vector< T * > >::type GetAllComponents()
Get all components of specified type.
Definition GameObject.hpp:318
void Import(Utilities::Stream &stream)
Load object from serialized data.
Definition GameObject.cpp:144
void Unsubscribe(const std::string eventId, const Utilities::ID handlerId)
Unsubscribe from events.
Definition GameObject.hpp:211
std::vector< Component * > GetAllComponents() const
Get all components of any type.
Definition GameObject.hpp:336
Utilities::Quaternion GetWorldRotation()
Get world rotation.
Definition GameObject.cpp:69
GameObject(Utilities::ID id, std::string name, std::string scene)
Should never be called directly, use Scene::CreateObject.
Definition GameObject.cpp:85
std::enable_if< std::is_base_of< Component, T >::value, void >::type RemoveComponent(T *component)
Remove specific component instance.
Definition GameObject.hpp:416
std::enable_if< std::is_base_of< Component, T >::value, T * >::type AddComponent(T *component)
Add component to object.
Definition GameObject.hpp:371
Utilities::Vector3 GetWorldScale()
Get world scale.
Definition GameObject.cpp:76
void Publish(const EventType &event)
Publish an event.
Definition GameObject.hpp:201
void SetScale(Utilities::Vector3 scale, bool announce=true)
Set local scale.
Definition GameObject.cpp:51
void RemoveChild(int index)
Remove child at index.
Definition GameObject.cpp:96
Utilities::ID Subscribe(EventFunction< EventType > handler, bool allowFromChild=true)
Subscribe to object events.
Definition GameObject.hpp:189
Utilities::Vector3 GetScale() const
Get local scale.
Definition GameObject.cpp:42
void SetPosition(Utilities::Vector3 position, bool announce=true)
Set local position.
Definition GameObject.cpp:43
std::enable_if< std::is_base_of< Component, T >::value, T * >::type GetComponent(bool log=true)
Get component of specified type.
Definition GameObject.hpp:298
int AddChild(const Utilities::ID &gameObjectID)
Add child object.
Definition GameObject.cpp:90
GameObject & GetParent() const
Get parent object.
Definition GameObject.cpp:106
void SetRotation(Utilities::Quaternion rotation, bool announce=true)
Set local rotation.
Definition GameObject.cpp:47
void SetTransform(Utilities::Vector3 position, Utilities::Quaternion rotation, Utilities::Vector3 scale, bool announce=true)
Set full local transform.
Definition GameObject.cpp:55
bool HasParent() const
Check if object has a parent.
Definition GameObject.hpp:261
std::enable_if< std::is_base_of< Component, T >::value, std::vector< T * > >::type GetAllComponentsInChildren()
Get components of type from this object and all children.
Definition GameObject.hpp:349
Utilities::Vector3 GetWorldPosition()
Get world position.
Definition GameObject.cpp:61
uint GetChildCount() const
Get number of children.
Definition GameObject.cpp:103
Utilities::ID Id() const
Get object's unique ID.
Definition GameObject.hpp:47
std::enable_if< std::is_base_of< Component, T >::value, void >::type RemoveAllComponents()
Remove all components of specified type.
Definition GameObject.hpp:393
Container for game objects and scene state.
Definition Scene.hpp:21
UUID-based unique identifier.
Definition ID.hpp:13
static ID empty
Null/empty ID value.
Definition ID.hpp:80
4x4 matrix for 3D transformations
Definition Matrix4.hpp:12
Quaternion for 3D rotations.
Definition Quaternion.hpp:19
Stream for serialization of data.
Definition Stream.hpp:20
3D vector class
Definition Vector3.hpp:19