VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
scriptComponentSystem.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 05.09.2025.
5//
6#pragma once
7
9#include "scriptComponent.h"
11
12namespace visutwin::canvas
13{
14 /*
15 * Allows scripts to be attached to an Entity and executed
16 */
17 class ScriptComponentSystem : public ComponentSystem<ScriptComponent, ScriptComponentData>
18 {
19 public:
21 : ComponentSystem(engine, "script"),
23 .keyExtractor = [](ScriptComponent* component) {
24 return component ? static_cast<float>(component->executionOrder()) : 0.0f;
25 }
26 })
27 {
28 }
29
30 std::unique_ptr<Component> addComponent(Entity* entity) override
31 {
32 auto component = std::make_unique<ScriptComponent>(this, entity);
33 component->initializeComponentData();
34 component->setExecutionOrder(_executionCounter++);
35 _components.append(component.get());
36 return component;
37 }
38
39 void fixedUpdate(float fixedDt)
40 {
41 for (_components.loopIndex = 0; _components.loopIndex < static_cast<int>(_components.length); _components.loopIndex++) {
42 auto* component = _components.items[_components.loopIndex];
43 if (component) {
44 component->fixedUpdateScripts(fixedDt);
45 }
46 }
47 }
48
49 void update(float dt)
50 {
51 for (_components.loopIndex = 0; _components.loopIndex < static_cast<int>(_components.length); _components.loopIndex++) {
52 auto* component = _components.items[_components.loopIndex];
53 if (component) {
54 component->updateScripts(dt);
55 }
56 }
57 }
58
59 void postUpdate(float dt)
60 {
61 for (_components.loopIndex = 0; _components.loopIndex < static_cast<int>(_components.length); _components.loopIndex++) {
62 auto* component = _components.items[_components.loopIndex];
63 if (component) {
64 component->postUpdateScripts(dt);
65 }
66 }
67 }
68
69 private:
71 int _executionCounter = 0;
72 };
73}
Central application orchestrator managing scenes, rendering, input, and resource loading.
Definition engine.h:38
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
std::unique_ptr< Component > addComponent(Entity *entity) override