14#include "core/reflect.h"
15#include "debug/engine_error_log.h"
17#include "ecs/entity.h"
19#include "ecs/component/core/name.h"
20#include "ecs/hierarchy_operations.h"
21#include "platform/window/window_manager.h"
22#include "system/render/render_settings.h"
23#include "core/event/event_bus.h"
24#include "net/net_session.h"
25#include "system/physics/physics_events.h"
26#include "platform/input/input_map.h"
28namespace Vkm::Engine {
43 Scene* scene =
nullptr;
52 Clock* clock =
nullptr;
54 std::vector<EntityId>* pendingDestroy =
nullptr;
55 std::string* pendingSceneLoad =
nullptr;
66struct ListenerEvent : ListenerEvent<decltype(&Fn::operator())> {};
68template<
typename C,
typename R,
typename A>
69struct ListenerEvent<R (C::*)(A) const> {
70 using type = std::decay_t<A>;
73template<
typename C,
typename R,
typename A>
74struct ListenerEvent<R (C::*)(A)> {
75 using type = std::decay_t<A>;
78template<
typename R,
typename A>
79struct ListenerEvent<R (*)(A)> {
80 using type = std::decay_t<A>;
91template<
typename EventT,
typename Fn>
92struct SubscribedEvent {
97struct SubscribedEvent<void, Fn> {
98 using type =
typename ListenerEvent<Fn>::type;
146 Behavior() =
default;
147 virtual ~Behavior() { clearSubscriptions(); }
149 Behavior(
const Behavior& other) =
delete;
150 Behavior& operator=(
const Behavior& other) =
delete;
152 Behavior(Behavior && other) =
delete;
153 Behavior& operator=(Behavior && other) =
delete;
267 virtual std::unique_ptr<Behavior>
clone()
const = 0;
288 T*
tryGet() {
return m_ctx->scene->tryGet<T>(m_entity); }
292 const T*
tryGet()
const {
return m_ctx->scene->tryGet<T>(m_entity); }
301 T&
get() {
return m_ctx->scene->get<T>(m_entity); }
305 const T&
get()
const {
return m_ctx->scene->get<T>(m_entity); }
314 bool has()
const {
return m_ctx->scene->has<T>(m_entity); }
324 auto&
add(T && component) {
325 return m_ctx->scene->add(m_entity, std::forward<T>(component));
363 return Vkm::Engine::findBehavior<T>(world,
entity);
369 return Vkm::Engine::findBehavior<T>(world,
entity);
427 const Clock&
clock()
const {
return *m_ctx->clock; }
446 return m_ctx->net->simulates(m_entity);
458 return m_ctx->net->isMine(m_entity);
481 return m_ctx->net->commandFor(m_entity);
493 if (m_ctx->net->role() == NetRole::Client) warnSpawnOnClient();
494 return m_ctx->scene->createEntity();
505 m_ctx->scene->add(
entity, makeName(name));
520 HierarchyOperations::setParent(*m_ctx->scene,
entity, parent);
548 void loadScene(
const std::string& scenePath) { *m_ctx->pendingSceneLoad = scenePath; }
563 template<
typename EventT =
void,
typename Fn>
565 using Event =
typename detail::SubscribedEvent<EventT, std::decay_t<Fn>>::type;
566 std::function<void(
const Event&)> listener = std::forward<Fn>(callback);
570 "A behavior subscribed before it started; "
571 "subscribe() belongs in onStart(), not the constructor"
577 const ListenerId
id =
events->subscribe<Event>(
578 [
this, listener = std::move(listener)](
const Event& event) {
579 runGuarded(
"event listener", [&] { listener(event); });
582 m_subscriptions.push_back([
events,
id]() {
events->unsubscribe<Event>(id); });
595 template<
typename Fn>
596 bool runGuarded(
const char* hookName, Fn&& fn) {
600 }
catch (
const std::exception& e) {
601 reportError(
"Behavior", hookLabel(hookName), e.what());
603 reportError(
"Behavior", hookLabel(hookName),
"non-std exception");
614 std::string hookLabel(
const char* hookName)
const {
615 return std::string(
typeName()) +
" / " + hookName;
624 void bindContext(EntityId
entity, BehaviorContext& context) {
635 void clearSubscriptions() {
636 for (
auto& unsubscribe : m_subscriptions) unsubscribe();
637 m_subscriptions.clear();
645 static void warnSpawnOnClient();
649 BehaviorContext* m_ctx =
nullptr;
651 std::vector<std::function<void()>> m_subscriptions;
652 bool m_started =
false;
653 bool m_disabled =
false;
Type-erased visitor over a behavior's reflected authoring fields.
Definition behavior_field_visitor.h:23
Drives the lifecycle of every entity's ScriptComponent behaviors.
Definition behavior_system.h:27
Base class for native C++ gameplay behaviors.
Definition behavior.h:144
const T & get() const
This entity's T, which it must have.
Definition behavior.h:305
InputMap & input()
Named input actions, for the frame queries and to define bindings.
Definition behavior.h:409
WindowManager * window()
The window, or null on a host that draws nothing, such as a dedicated server.
Definition behavior.h:434
virtual void onTriggerStay(EntityId other)
Called on each later tick other is still inside, on onCollisionStay's terms.
Definition behavior.h:234
const InputCommand & command() const
The input driving this entity on the tick now running.
Definition behavior.h:480
T * findBehavior(EntityId other)
other's behavior of type T, or null when it has none.
Definition behavior.h:351
T * findBehavior()
This entity's behavior of type T, or null when it has none.
Definition behavior.h:335
virtual void onUpdate(float dt)
Called every variable-step frame on which simulation time advanced.
Definition behavior.h:171
const T * tryGet() const
This entity's T, or null when it has none.
Definition behavior.h:292
NetSession & net()
The wire, or an offline session that answers as though local.
Definition behavior.h:416
virtual const char * typeName() const =0
Stable type name, identical to this type's BehaviorRegistry key; serialization uses it.
void destroy()
Destroy this behavior's own entity and its subtree, deferred likewise.
Definition behavior.h:537
virtual std::unique_ptr< Behavior > clone() const =0
Deep copy for entity duplication; authored fields only, the context is rebound.
Clock & clock()
Real and simulation time, and the play state behind them.
Definition behavior.h:426
bool isMine() const
Whether this entity belongs to the player at this end.
Definition behavior.h:457
static T * findBehavior(Scene &world, EntityId entity)
The free findBehavior, which the member overloads would otherwise hide.
Definition behavior.h:362
static const T * findBehavior(const Scene &world, EntityId entity)
The free findBehavior, which the member overloads would otherwise hide.
Definition behavior.h:368
virtual void onCollisionEnter(const Collision &hit)
Called on the first tick this entity touches another, in a resolved (non-trigger) contact.
Definition behavior.h:201
virtual void onDestroy()
Called on entity removal, and when the session ends (see BehaviorSystem::endSession).
Definition behavior.h:246
virtual void onFixedUpdate(float dt)
Called on each fixed-step tick; fed from simulation time, so pause and time-scale apply.
Definition behavior.h:188
bool isReplaying() const
Whether this tick already happened and is being run again.
Definition behavior.h:469
EntityId spawn(const char *name, EntityId parent)
Create an entity carrying name, parented under parent.
Definition behavior.h:518
virtual void onRealtimeUpdate(float dt)
Called every frame on real time, paused or not.
Definition behavior.h:181
virtual void onTriggerExit(EntityId other)
Called on the first tick other no longer overlaps, on onCollisionExit's terms.
Definition behavior.h:241
void loadScene(const std::string &scenePath)
Load scenePath, replacing everything currently in the scene.
Definition behavior.h:548
bool isSimulated() const
Whether this end decides what happens to this entity.
Definition behavior.h:445
virtual void onCollisionStay(const Collision &hit)
Called on each later tick the contact lasts.
Definition behavior.h:210
ResourceManager & resources()
The assets this session holds, to look one up or add one.
Definition behavior.h:384
virtual void onCollisionExit(const Collision &hit)
Called on the first tick the contact is over.
Definition behavior.h:220
void subscribe(Fn &&callback)
Listen for an event until the behavior is destroyed or the session ends.
Definition behavior.h:564
bool has() const
Whether this entity carries a T.
Definition behavior.h:314
virtual void onStart()
Called on the first simulation tick this instance runs in play mode.
Definition behavior.h:161
EventSender events()
The event bus's sending half, to emit or enqueue on.
Definition behavior.h:393
T & get()
This entity's T, which it must have.
Definition behavior.h:301
virtual void onTriggerEnter(EntityId other)
Called on the first tick other overlaps this entity's trigger.
Definition behavior.h:227
EntityId entity() const
The entity this behavior is attached to.
Definition behavior.h:275
T * tryGet()
This entity's T, or null when it has none.
Definition behavior.h:288
auto & add(T &&component)
Give this entity a T.
Definition behavior.h:324
EntityId spawn(const char *name)
Create an entity carrying name.
Definition behavior.h:503
RenderSettings & render()
The quality settings the frame is drawn at; a write lands on the next frame.
Definition behavior.h:400
void destroy(EntityId target)
Destroy entity and its subtree, firing onDestroy.
Definition behavior.h:532
Scene & scene()
The scene this behavior's entity lives in.
Definition behavior.h:377
virtual void visitFields(BehaviorFieldVisitor &visitor)
Visit the behavior's reflected authoring fields; ReflectedBehavior generates it.
Definition behavior.h:260
EntityId spawn()
Create a new, empty entity; add components to it through scene().
Definition behavior.h:492
The engine's frame clock: real + simulation time and the fixed-step accumulator.
Definition clock.h:18
Typed pub/sub event dispatcher.
Definition event_bus.h:22
The sending half of an EventBus: emit and enqueue, and no subscribe.
Definition event_bus.h:148
One end of a game, whether it is the authority or a guest.
Definition net_session.h:116
Open 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
Owns the application's window, GL context, input, and frame limiting.
Definition window_manager.h:61
Everything gameplay code may reach, bundled behind one pointer.
Definition behavior.h:42
WindowManager * window
The only nullable field: a host that draws nothing has none.
Definition behavior.h:47
One entity's view of a CollisionEvent, as its behaviors' collision hooks receive it.
Definition physics_events.h:45
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
Editable render tuning: pass toggles + per-effect parameters.
Definition render_settings.h:86