vkmEngine 1.0.15
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
48void reportUnregisteredBehavior(const char* typeName);
49
66template<typename T>
67T& addBehavior(Scene& scene, EntityId entity) {
68 const char* name = Reflect::Traits<T>::NAME;
69 if (!BehaviorRegistry::get().contains(name)) reportUnregisteredBehavior(name);
70
71 auto instance = std::make_unique<T>();
72 T& behavior = *instance;
73 ScriptComponent* script = scene.tryGet<ScriptComponent>(entity);
74 if (!script) script = &scene.add(entity, ScriptComponent{});
75 script->behaviors.push_back(std::move(instance));
76 return behavior;
77}
78
79} // namespace Vkm::Engine
Central registry managing entities and an open set of component types.
Definition scene.h:29
T * tryGet(EntityId entity)
The entity's T, or null when it has none.
Definition scene.h:206
auto & add(EntityId entity, T &&component)
Give an entity a component it does not have yet.
Definition scene.h:128
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