StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
Component.hpp
1#pragma once
2#include "utilities/ID.hpp"
3#include "utilities/Matrix4.hpp"
4#include "utilities/Stream.hpp"
5
6#include <sys/types.h>
7#include <functional>
8#include <unordered_map>
9
10namespace StevEngine {
11 class GameObject;
12 class Scene;
13
20 class Component {
21 friend class StevEngine::GameObject;
22 //Properties
23 private:
26
28 std::string scene;
29
31 static const bool unique = false;
32
33 //Functions
34 public:
38 Component();
39
43 virtual ~Component();
44
49 GameObject& GetParent() const;
50
55 Scene& GetScene() const;
56
61 virtual std::string GetType() const = 0;
62
68 virtual Utilities::Stream Export(Utilities::StreamType stream) const = 0;
69
73 virtual void Start() {};
74
75 private:
79 virtual void Deactivate() {};
80
85 virtual void Update(double deltaTime) {};
86
87 #ifdef StevEngine_SHOW_WINDOW
92 virtual void Draw(const Utilities::Matrix4& transform) {};
93 #endif
94
100 void SetObject(GameObject& object, std::string scene);
101
102 //Events
103 protected:
105 std::vector<std::pair<Utilities::ID, std::string>> handlers;
106 };
107
116 static inline std::unordered_map<std::string, std::function<Component*(Utilities::Stream& stream)>> factories = std::unordered_map<std::string, std::function<Component*(Utilities::Stream& stream)>>();
117
118 public:
125 static Component* Create(const std::string& type, Utilities::Stream& stream);
126
133 template <class T> static bool RegisterComponentType(std::string type) {
134 if(factories.contains(type)) return false;
135 factories.insert({type, [](Utilities::Stream& stream) -> Component* {
136 return (Component*) (new T(stream));
137 }});
138 return true;
139 }
140 };
141}
Base class for all game object components.
Definition Component.hpp:20
virtual std::string GetType() const =0
Get component type.
virtual void Start()
Initialize component after creation.
Definition Component.hpp:73
virtual ~Component()
Clean up component resources.
Definition Component.cpp:28
Scene & GetScene() const
Get containing Scene.
Definition Component.cpp:25
GameObject & GetParent() const
Get parent GameObject.
Definition Component.cpp:22
Component()
Create new component.
Definition Component.cpp:10
std::vector< std::pair< Utilities::ID, std::string > > handlers
Event handler registrations.
Definition Component.hpp:105
virtual Utilities::Stream Export(Utilities::StreamType stream) const =0
Serialize component to a stream.
Factory system for creating components from serialized data.
Definition Component.hpp:114
static bool RegisterComponentType(std::string type)
Register a component type for creation.
Definition Component.hpp:133
static Component * Create(const std::string &type, Utilities::Stream &stream)
Create component from serialized data.
Definition Component.cpp:35
Core game object class.
Definition GameObject.hpp:35
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
Stream for serialization of data.
Definition Stream.hpp:20