vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
behavior_system.h
1#pragma once
2
3#include <functional>
4
5#include <type_traits>
6#include <vector>
7
8#include "core/system.h"
9#include "ecs/entity.h"
10#include "ecs/scene_observer.h"
11#include "system/physics/physics_events.h"
12#include "system/script/behavior.h"
13
14namespace Vkm::Engine {
15
16class Scene;
17class ResourceManager;
18class EventBus;
19
30class BehaviorSystem : public System, public ISceneObserver {
31 public:
32 BehaviorSystem() {
33 m_context.pendingDestroy = &m_pendingDestroy;
34 m_context.pendingSceneLoad = &m_pendingSceneLoad;
35 m_context.pendingRestart = &m_pendingRestart;
36 }
37 ~BehaviorSystem() override = default;
38
39 BehaviorSystem(const BehaviorSystem& other) = delete;
40 BehaviorSystem& operator=(const BehaviorSystem& other) = delete;
41
42 BehaviorSystem(BehaviorSystem && other) = delete;
43 BehaviorSystem& operator=(BehaviorSystem && other) = delete;
44
45 public:
46 void init(FrameContext& ctx) override;
47 void update(FrameContext& ctx) override;
48 void fixedUpdate(FrameContext& ctx) override;
49
55 bool isReplayed() const override { return true; }
56
57 void shutdown() override;
58
66 void onEntityDestroyed(EntityId entity) override;
67
74 void setPlayedScene(std::string path) { m_playedScene = std::move(path); }
75
84 void setRestartHandler(std::function<void()> restart) { m_restartHandler = std::move(restart); }
85
95 void endSession(Scene& scene);
96
97 private:
104 void ensureStarted(Behavior& behavior, EntityId entity);
105
116 void tickBehaviors(
117 FrameContext& ctx,
118 float dt,
119 const char* hookName,
120 void (Behavior::*hook)(float),
121 bool startIfNeeded
122 );
123
134 template<typename Arg>
135 void dispatchEntityHook(
136 Scene& scene,
137 EntityId target,
138 const std::remove_reference_t<Arg>& arg,
139 const char* hookName,
140 void (Behavior::*hook)(Arg)
141 );
142
148 void drainPendingDestroy(Scene& scene);
149
156 void drainPendingSceneLoad(FrameContext& ctx);
157
166 template<typename Fn>
167 static void guard(Behavior& behavior, const char* hookName, Fn&& fn);
168
176 static void fireDestroy(Behavior& behavior);
177
178 private:
179 std::vector<CollisionEvent> m_collisions;
180 std::vector<TriggerEvent> m_triggers;
181
183 std::vector<CollisionEvent> m_dispatchCollisions;
184 std::vector<TriggerEvent> m_dispatchTriggers;
185
187 ListenerId m_collisionListener = 0;
188 ListenerId m_triggerListener = 0;
189
196 std::vector<EntityId> m_tickList;
197
198 std::vector<EntityId> m_pendingDestroy;
200 std::vector<EntityId> m_destroying;
201 std::string m_pendingSceneLoad;
202 bool m_pendingRestart = false;
203 std::string m_loadedScene;
204 std::string m_playedScene;
205 std::function<void()> m_restartHandler;
206
207 BehaviorContext m_context;
208};
209
210} // namespace Vkm::Engine
void onEntityDestroyed(EntityId entity) override
ISceneObserver: fire onDestroy on entity's behaviors before its ScriptComponent goes.
void shutdown() override
Called once on engine shutdown.
void init(FrameContext &ctx) override
Called once after all systems are registered, before the first update.
void endSession(Scene &scene)
End the session: onDestroy on every started behavior, reset, and discard queued requests.
void setPlayedScene(std::string path)
The file a host began play with, which a restart reloads while no loadScene has replaced it: a runtim...
Definition behavior_system.h:74
void setRestartHandler(std::function< void()> restart)
Hand a restart of the host's own scene to restart, which puts it back itself.
Definition behavior_system.h:84
bool isReplayed() const override
Re-run during a replay: it moves the world from state and command, repeatably.
Definition behavior_system.h:55
void update(FrameContext &ctx) override
Execute this system for the current frame.
void fixedUpdate(FrameContext &ctx) override
Execute this system at the fixed simulation rate.
Base class for native C++ gameplay behaviors.
Definition behavior.h:192
Typed pub/sub event dispatcher.
Definition event_bus.h:22
Type-erased resource registry with typed handles and generational lifetimes.
Definition resource_manager.h:46
Central registry managing entities and an open set of component types.
Definition scene.h:29
Everything gameplay code may reach, bundled behind one pointer.
Definition behavior.h:46
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
Per-frame state bundle passed to every system.
Definition system.h:57