Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
TrailSystem.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#include "TrailSystem.hpp"
4
5namespace esengine::trail {
6
8 if (!destroyConn_.isConnected()) {
9 destroyConn_ = registry.onDestroy([this](Entity entity) {
10 states_.erase(entity);
11 });
12 }
13
14 time_ += dt;
15
16 auto view = registry.activeView<ecs::Transform, ecs::TrailRenderer>();
17 for (auto entity : view) {
18 const auto& trail = view.get<ecs::TrailRenderer>(entity);
19 // Frozen (disabled) trails keep their history so re-enabling resumes the
20 // streak instead of restarting it.
21 if (!trail.enabled) continue;
22
23 auto& transform = view.get<ecs::Transform>(entity);
24 transform.ensureDecomposed();
25 const glm::vec3& pos = transform.worldPosition;
26
27 auto it = states_.find(entity);
28 if (it == states_.end()) {
29 if (!trail.emitting) continue;
30 it = states_.try_emplace(entity).first;
31 }
32 auto& state = it->second;
33
34 // Record a new anchor when the head has moved far enough (or the trail is
35 // empty). The live head between anchors is added by the plugin from the
36 // current transform, so a stationary emitter never accretes points.
37 if (trail.emitting) {
38 bool record = state.points.empty();
39 if (!record) {
40 glm::vec3 d = pos - state.points.back().position;
41 f32 md = trail.minVertexDistance;
42 record = glm::dot(d, d) >= md * md;
43 }
44 if (record) {
45 state.points.push_back({pos, time_});
46 }
47 }
48
49 // Age out points older than `time` from the tail (front = oldest).
50 f32 cutoff = time_ - trail.time;
51 while (!state.points.empty() && state.points.front().birth_time < cutoff) {
52 state.points.pop_front();
53 }
54
55 if (state.points.empty()) {
56 states_.erase(it);
57 }
58 }
59}
60
62 states_.erase(entity);
63}
64
66 auto it = states_.find(entity);
67 return it != states_.end() ? &it->second : nullptr;
68}
69
70} // namespace esengine::trail
Per-App motion-trail simulation: records each TrailRenderer entity's world position over time into a ...
Central container for all entities and components.
Definition Registry.hpp:73
Connection onDestroy(DestroyCallback callback)
Subscribe to entity destruction.
Definition Registry.hpp:509
void update(ecs::Registry &registry, f32 dt)
const TrailState * getState(Entity entity) const
void clear(Entity entity)
float f32
32-bit floating point
Definition Types.hpp:59
Entity identifier with packed index + generation.
Definition Types.hpp:135
Unified transform component.
Definition Transform.hpp:45
Per-entity point history. Front = oldest (tail), back = newest (near head).