Loading...
Searching...
No Matches
ScriptComponent.h
Go to the documentation of this file.
1//
2// Created by denzel on 10/08/2025.
3//
4
5#pragma once
6#include <string>
7#include <unordered_map>
8
9#include "Entity.h"
10#include "Component.h"
11#include "glm/detail/type_vec.hpp"
12
13#define SCRIPT_CLASS(ClassName)
14 public:
15 const char* get_class_name() const override { return #ClassName; }
16 static const char* static_class_name() { return #ClassName; }
17
18// Property macro that registers the variable for serialization
19#define SCRIPT_VAR(Type, Name, Default) Type
20 Name = Default
21
22// Helper macro for registration of variables
23#define REGISTER_VAR(Name, Type) register_property
24 (#Name, &Name, PropertyType::Type)
25
26namespace hellfire {
28
29 class ScriptComponent : public Component {
30 public:
31 enum class PropertyType {
33 };
34
35 struct PropertyInfo {
36 std::string name;
38 void *data_ptr; // Pointer to actual member variable
39
40 // Helper to get/set value through pointer
41 template<typename T>
42 T &get() { return *static_cast<T *>(data_ptr); }
43 };
44
45 protected:
47
48 // Register a property for editor/serialization
49 template<typename T>
50 void register_property(const std::string &name, T *ptr, PropertyType type) {
51 properties_.push_back({name, type, static_cast<void *>(ptr)});
52 }
53
54 bool enabled_ = true;
55 public:
56 ScriptComponent() = default;
57
58 virtual ~ScriptComponent() = default;
59
60 const std::vector<PropertyInfo> &get_properties() const { return properties_; }
61
62 // Virtual lifecycle methods
63 virtual void on_init() {
64 }
65
66 virtual void on_update(float delta_time) {
67 }
68
69 virtual void on_remove() {
70 }
71
72 virtual void on_event(const std::string &event_name, void *data = nullptr) {
73 }
74
75 // Called by the entity system
76 void init() { on_init(); }
77 void update(const float delta_time) { on_update(delta_time); }
78 void remove() { on_remove(); }
79
80 void trigger_event(const std::string &event_name, void *data = nullptr) { on_event(event_name, data); }
81
82 void set_enabled(const bool enabled) { enabled_ = enabled; }
83
84 bool is_enabled() const { return enabled_; }
85
86 // Utility methods for common operations
88
89 // Helper method to check if the owner has a specific component
90 template<typename T>
91 bool has_component() const {
92 return get_owner().has_component<T>();
93 }
94
95 template<typename T>
96 T *get_component() const {
97 return get_owner().get_component<T>();
98 }
99
100 virtual const char *get_class_name() const {
101 return {};
102 }
103 };
104}
Entity & get_owner() const
Definition Component.h:13
const std::string & get_name() const
Definition Entity.h:46
const TransformComponent * transform() const
Definition Entity.cpp:52
TransformComponent * transform()
Definition Entity.cpp:42
const std::vector< ScriptComponent * > & get_script_components() const
Definition Entity.cpp:11
void initialize_scripts() const
Definition Entity.cpp:16
void cleanup_scripts() const
Definition Entity.cpp:30
std::vector< ScriptComponent * > script_components_
Definition Entity.h:88
void broadcast_event(const std::string &event_name, void *data=nullptr) const
Definition Entity.cpp:36
const std::vector< PropertyInfo > & get_properties() const
virtual void on_update(float delta_time)
void set_enabled(const bool enabled)
virtual const char * get_class_name() const
TransformComponent * get_transform() const
std::vector< PropertyInfo > properties_
void register_property(const std::string &name, T *ptr, PropertyType type)
virtual ~ScriptComponent()=default
void trigger_event(const std::string &event_name, void *data=nullptr)
virtual void on_event(const std::string &event_name, void *data=nullptr)
void update(const float delta_time)