vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
script_component.h
1#pragma once
2
3#include <memory>
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include "core/reflect.h"
9#include "ecs/entity.h"
10#include "ecs/scene.h"
11#include "system/script/behavior.h"
12#include "system/script/behavior_registry.h"
13
14namespace Vkm::Engine {
15
23 std::string type;
24 std::string properties;
25
27 size_t index = 0;
28};
29
37 std::vector<std::unique_ptr<Behavior>> behaviors;
38
40 std::vector<UnknownBehavior> unknown;
41};
42
50EntityId findEntityWithBehaviorNamed(Scene& scene, std::string_view typeName);
51
57void reportUnregisteredBehavior(const char* typeName);
58
68template<typename T>
69EntityId findEntityWithBehavior(Scene& scene) {
70 return findEntityWithBehaviorNamed(scene, Reflect::Traits<T>::NAME);
71}
72
89template<typename T>
90T& addBehavior(Scene& scene, EntityId entity) {
91 const char* name = Reflect::Traits<T>::NAME;
92 if (!BehaviorRegistry::get().contains(name)) reportUnregisteredBehavior(name);
93
94 auto instance = std::make_unique<T>();
95 T& behavior = *instance;
96 ScriptComponent* script = scene.tryGet<ScriptComponent>(entity);
97 if (!script) script = &scene.add(entity, ScriptComponent{});
98 script->behaviors.push_back(std::move(instance));
99 return behavior;
100}
101
102} // namespace Vkm::Engine
Central registry managing entities and an open set of component types.
Definition scene.h:29
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
The reflection trait, specialised per type by VKM_REFLECT_BEGIN / VKM_F / VKM_REFLECT_END.
Definition reflect.h:41
ECS component attaching owned, polymorphic Behaviors to an entity.
Definition script_component.h:36
std::vector< UnknownBehavior > unknown
Behaviors held as text because no type of that name is registered.
Definition script_component.h:40
A behavior whose type the registry does not know, kept verbatim.
Definition script_component.h:22
size_t index
Position in the list read from; list order is run order, so it must survive a save.
Definition script_component.h:27
std::string properties
Its property object, as JSON text, untouched.
Definition script_component.h:24