Repository navigation
Expand file tree
/
Copy pathLevel.hpp
More file actions
118 lines (92 loc) · 4 KB
/
Copy pathLevel.hpp
File metadata and controls
118 lines (92 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include "flecs.h"
#include <memory>
#include <vector>
#include <cstring>
#include "Persistent.hpp"
namespace Lca {
class Level {
public:
virtual ~Level() = default;
// 1. Called on background thread. Load textures, meshes, materials, etc.
virtual void loadAssets() {}
// 2. Called on main thread. Create entities, setup scene.
virtual void setupScene(flecs::world& world) {}
// 3. Called on main thread before switching levels.
// Default: deletes all non-persistent user entities.
virtual void cleanupScene(flecs::world& world) {
deleteNonPersistentEntities(world);
}
// Safely delete all user entities without Persistent/PersistentSystem tags.
// Skips flecs built-in entities, module entities, component definitions,
// and module-owned systems (stats, REST, etc.).
static void deleteNonPersistentEntities(flecs::world& world) {
auto isUserEntity = [](flecs::entity e) {
ecs_world_t* raw = e.world().c_ptr();
ecs_entity_t id = e.id();
if (id < EcsFirstUserEntityId) return false;
char* path = ecs_get_path_w_sep(raw, 0, id, ".", nullptr);
const bool isFlecsEntity = path && std::strncmp(path, "flecs", 5) == 0;
if (path) {
ecs_os_free(path);
}
if (isFlecsEntity) return false;
if (ecs_has_id(raw, id, ecs_pair(EcsOnDelete, EcsPanic))) return false;
// Skip module entities — their children (observers/systems) would
// be cascade-deleted even if tagged PersistentSystem.
if (ecs_has_id(raw, id, EcsModule)) return false;
ecs_table_t* table = ecs_get_table(raw, id);
if (table && ecs_table_has_flags(table, EcsTableHasBuiltins)) return false;
return true;
};
std::vector<flecs::entity> toDelete;
auto q = world.query_builder<>()
.without<Lca::Component::Persistent>()
.without<Lca::Component::PersistentSystem>()
.build();
q.each([&toDelete, &isUserEntity](flecs::entity e) {
if (!isUserEntity(e)) return;
toDelete.push_back(e);
});
// Delete in immediate mode (NOT deferred) to avoid bulk constraint violations
for (auto& ent : toDelete) {
if (ent.is_alive()) {
ent.destruct();
}
}
}
};
// ---------------------------------------------------------------------------
// StreamingLevel — a world chunk that can be streamed in/out without a
// loading screen. Heavy work (file I/O, decompression) goes in loadAssets()
// which runs on a background thread. setupScene() / cleanupScene() run on
// the main thread and should be lightweight (entity creation / deletion).
// ---------------------------------------------------------------------------
class StreamingLevel {
public:
virtual ~StreamingLevel() = default;
// Unique identifier for this chunk / region (e.g. "zone_A3").
virtual std::string getId() const = 0;
// 1. Called on background thread. Load textures, meshes, materials, etc.
// Put ALL heavy work here — file I/O, decompression, parsing.
virtual void loadAssets() {}
// 2. Called on main thread once loadAssets() completes.
// Create entities, set up scene state. Keep this lightweight.
virtual void setupScene(flecs::world& world) {}
// 3. Called on main thread when this chunk is streamed out.
// Destroy entities owned by this streaming level.
virtual void cleanupScene(flecs::world& world) {}
};
// Component to request a level load from within a Flecs system
struct LoadLevelRequest {
std::shared_ptr<Level> nextLevel;
};
// Component to request streaming level loads from within a Flecs system.
// Set on the world singleton; processed and removed each frame.
struct StreamInRequest {
std::vector<std::shared_ptr<StreamingLevel>> levels;
};
struct StreamOutRequest {
std::vector<std::string> levelIds;
};
} // namespace Lca