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"
27#include "system/ui/screen_view.h"
29namespace Vkm::Engine {
47 Scene* scene =
nullptr;
56 Clock* clock =
nullptr;
60 std::vector<EntityId>* pendingDestroy =
nullptr;
61 std::string* pendingSceneLoad =
nullptr;
62 bool* pendingRestart =
nullptr;
74struct ListenerEvent : ListenerEvent<decltype(&Fn::operator())> {};
76template<
typename C,
typename R,
typename A>
77struct ListenerEvent<R (C::*)(A) const> {
78 using type = std::decay_t<A>;
81template<
typename C,
typename R,
typename A>
82struct ListenerEvent<R (C::*)(A)> {
83 using type = std::decay_t<A>;
86template<
typename R,
typename A>
87struct ListenerEvent<R (*)(A)> {
88 using type = std::decay_t<A>;
99template<
typename EventT,
typename Fn>
100struct SubscribedEvent {
105struct SubscribedEvent<void, Fn> {
106 using type =
typename ListenerEvent<Fn>::type;
131EntityId findEntityWithBehaviorNamed(
Scene& scene, std::string_view typeName);
181T* findBehavior(
Scene& scene) {
182 return findBehavior<T>(scene, findEntityWithBehavior<T>(scene));
194 Behavior() =
default;
195 virtual ~Behavior() { clearSubscriptions(); }
197 Behavior(
const Behavior& other) =
delete;
198 Behavior& operator=(
const Behavior& other) =
delete;
200 Behavior(Behavior && other) =
delete;
201 Behavior& operator=(Behavior && other) =
delete;
315 virtual std::unique_ptr<Behavior>
clone()
const = 0;
339 T*
tryGet() {
return m_ctx->scene->tryGet<T>(m_entity); }
343 const T*
tryGet()
const {
return m_ctx->scene->tryGet<T>(m_entity); }
352 T&
get() {
return m_ctx->scene->get<T>(m_entity); }
356 const T&
get()
const {
return m_ctx->scene->get<T>(m_entity); }
365 bool has()
const {
return m_ctx->scene->has<T>(m_entity); }
375 auto&
add(T && component) {
376 return m_ctx->scene->add(m_entity, std::forward<T>(component));
395 T*
findInScene() { return ::Vkm::Engine::findBehavior<T>(*m_ctx->scene); }
423 return Vkm::Engine::findBehavior<T>(world,
entity);
429 return Vkm::Engine::findBehavior<T>(world,
entity);
497 const Clock&
clock()
const {
return *m_ctx->clock; }
545 return m_ctx->net->simulates(*m_ctx->scene, m_entity);
557 return m_ctx->net->isMine(m_entity);
580 return m_ctx->net->commandFor(m_entity);
592 return m_ctx->scene->createEntity();
603 m_ctx->scene->add(
entity, makeName(name));
618 HierarchyOperations::setParent(*m_ctx->scene,
entity, parent);
646 void loadScene(
const std::string& scenePath) { *m_ctx->pendingSceneLoad = scenePath; }
670 template<
typename EventT =
void,
typename Fn>
672 using Event =
typename detail::SubscribedEvent<EventT, std::decay_t<Fn>>::type;
673 std::function<void(
const Event&)> listener = std::forward<Fn>(callback);
677 "A behavior subscribed before it started; "
678 "subscribe() belongs in onStart(), not the constructor"
684 const ListenerId
id =
events->subscribe<Event>(
685 [
this, listener = std::move(listener)](
const Event& event) {
686 runGuarded(
"event listener", [&] { listener(event); });
689 m_subscriptions.push_back([
events,
id]() {
events->unsubscribe<Event>(id); });
702 template<
typename Fn>
703 bool runGuarded(
const char* hookName, Fn&& fn) {
707 }
catch (
const std::exception& e) {
708 reportError(
"Behavior", hookLabel(hookName), e.what());
710 reportError(
"Behavior", hookLabel(hookName),
"non-std exception");
721 std::string hookLabel(
const char* hookName)
const {
722 return std::string(
typeName()) +
" / " + hookName;
731 void bindContext(EntityId
entity, BehaviorContext& context) {
742 void clearSubscriptions() {
743 for (
auto& unsubscribe : m_subscriptions) unsubscribe();
744 m_subscriptions.clear();
749 BehaviorContext* m_ctx =
nullptr;
751 std::vector<std::function<void()>> m_subscriptions;
752 bool m_started =
false;
753 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:30
Base class for native C++ gameplay behaviors.
Definition behavior.h:192
const T & get() const
This entity's T, which it must have.
Definition behavior.h:356
InputMap & input()
Named input actions, for the frame queries and to define bindings.
Definition behavior.h:479
void restartScene()
Start the scene being played over: the file loadScene last loaded, else the scene the host began play...
Definition behavior.h:655
WindowManager * window()
The window, or null on a host that draws nothing, such as a dedicated server.
Definition behavior.h:504
virtual void onTriggerStay(EntityId other)
Called on each later tick other is still inside, on onCollisionStay's terms.
Definition behavior.h:282
const InputCommand & command() const
The input driving this entity on the tick now running.
Definition behavior.h:579
T * findInScene()
The scene's behavior of type T, as findBehavior(Scene&) finds it.
Definition behavior.h:395
std::optional< Math::Ray > pointerRay() const
The ray from the camera through the cursor.
T * findBehavior(EntityId other)
other's behavior of type T, or null when it has none.
Definition behavior.h:411
T * findBehavior()
This entity's behavior of type T, or null when it has none.
Definition behavior.h:386
virtual void onUpdate(float dt)
Called every variable-step frame on which simulation time advanced.
Definition behavior.h:219
const T * tryGet() const
This entity's T, or null when it has none.
Definition behavior.h:343
NetSession & net()
The wire, or an offline session that answers as though local.
Definition behavior.h:486
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:635
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:496
bool isMine() const
Whether this entity belongs to the player at this end.
Definition behavior.h:556
static T * findBehavior(Scene &world, EntityId entity)
The free findBehavior, which the member overloads would otherwise hide.
Definition behavior.h:422
static const T * findBehavior(const Scene &world, EntityId entity)
The free findBehavior, which the member overloads would otherwise hide.
Definition behavior.h:428
virtual void onCollisionEnter(const Collision &hit)
Called on the first tick this entity touches another, in a resolved (non-trigger) contact.
Definition behavior.h:249
virtual void onDestroy()
Called on entity removal, and when the session ends (see BehaviorSystem::endSession).
Definition behavior.h:294
virtual void onFixedUpdate(float dt)
Called on each fixed-step tick; fed from simulation time, so pause and time-scale apply.
Definition behavior.h:236
bool isReplaying() const
Whether this tick already happened and is being run again.
Definition behavior.h:568
EntityId spawn(const char *name, EntityId parent)
Create an entity carrying name, parented under parent.
Definition behavior.h:616
virtual void onRealtimeUpdate(float dt)
Called every frame on real time, paused or not.
Definition behavior.h:229
virtual void onTriggerExit(EntityId other)
Called on the first tick other no longer overlaps, on onCollisionExit's terms.
Definition behavior.h:289
void loadScene(const std::string &scenePath)
Load scenePath, replacing everything currently in the scene.
Definition behavior.h:646
bool isSimulated() const
Whether this end decides what happens to this entity.
Definition behavior.h:544
virtual void onCollisionStay(const Collision &hit)
Called on each later tick the contact lasts.
Definition behavior.h:258
ResourceManager & resources()
The assets this session holds, to look one up or add one.
Definition behavior.h:444
virtual void onCollisionExit(const Collision &hit)
Called on the first tick the contact is over.
Definition behavior.h:268
void subscribe(Fn &&callback)
Listen for an event until the behavior is destroyed or the session ends.
Definition behavior.h:671
const OceanData * ocean() const
The scene's sea at this moment: the tick's in onFixedUpdate, the frame's in onUpdate (OceanSystem).
bool has() const
Whether this entity carries a T.
Definition behavior.h:365
virtual void onStart()
Called on the first simulation tick this instance runs in play mode.
Definition behavior.h:209
EventSender events()
The event bus's sending half, to emit or enqueue on.
Definition behavior.h:453
T & get()
This entity's T, which it must have.
Definition behavior.h:352
const RenderSettings & projectRender() const
The project's render settings, which render() started from.
Definition behavior.h:470
virtual void onTriggerEnter(EntityId other)
Called on the first tick other overlaps this entity's trigger.
Definition behavior.h:275
EntityId entity() const
The entity this behavior is attached to.
Definition behavior.h:324
T * tryGet()
This entity's T, or null when it has none.
Definition behavior.h:339
ScreenView screen() const
The 3D view through the scene's active camera: the pointer's ray into the world, a point of the world...
auto & add(T &&component)
Give this entity a T.
Definition behavior.h:375
EntityId spawn(const char *name)
Create an entity carrying name.
Definition behavior.h:601
RenderSettings & render()
The quality settings the frame is drawn at; a write lands on the next frame.
Definition behavior.h:460
void destroy(EntityId target)
Destroy entity and its subtree, firing onDestroy.
Definition behavior.h:630
Scene & scene()
The scene this behavior's entity lives in.
Definition behavior.h:437
virtual void visitFields(BehaviorFieldVisitor &visitor)
Visit the behavior's reflected authoring fields; ReflectedBehavior generates it.
Definition behavior.h:308
EntityId spawn()
Create a new, empty entity; add components to it through scene().
Definition behavior.h:591
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
What an authoring host tells the engine about the frame it draws over.
Definition host_chrome.h:18
One end of a game, whether it is the authority or a guest.
Definition net_session.h:118
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
The 3D view a game draws through: where it sits in the window, and the scene's active camera filling ...
Definition screen_view.h:26
Owns the application's window, its WindowSurface, input, and frame limiting.
Definition window_manager.h:69
Everything gameplay code may reach, bundled behind one pointer.
Definition behavior.h:46
const HostChrome * chrome
Where in the window the scene is drawn.
Definition behavior.h:59
const OceanData * ocean
OceanSystem's, this tick's or frame's.
Definition behavior.h:63
WindowManager * window
The only nullable field: a host that draws nothing has none.
Definition behavior.h:51
One entity's view of a CollisionEvent, as its behaviors' collision hooks receive it.
Definition physics_events.h:47
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
The scene's sea at the moment being computed (OceanSystem): its waves, its ripples and its look - wha...
Definition ocean_data.h:13
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:101