PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
component_storage.h
1#pragma once
2
4#include "pixelbullet/core/containers/sparse_set.h"
5#include "pixelbullet/serialization/node.h"
6
7#include <cstdint>
8#include <string>
9#include <typeinfo>
10#include <utility>
11
12namespace pixelbullet
13{
15{
16 virtual ~ComponentStorageBase() = default;
17 virtual void Serialize(Node& node) const = 0;
18 virtual void Deserialize(const Node& node) = 0;
19 virtual void Clear() = 0;
20 virtual void RemoveComponent(uint32_t entity) = 0;
21};
22
23template <typename T>
25{
26 SparseSet<T> components;
27
28 void Serialize(Node& node) const override
29 {
30 node.set_type(NodeType::Array);
31 auto& properties = node.properties();
32 properties.clear();
33 for (const auto& entry : components)
34 {
35 Node entry_node;
36 entry_node["entity"] << entry.id;
37 Node component_node;
38 component_node << entry.value;
39 entry_node["component"] = component_node;
40 properties.push_back({ "", entry_node });
41 }
42 }
43
44 void Deserialize(const Node& node) override
45 {
46 ASSERT(node.type() == NodeType::Array || node.type() == NodeType::Null, "Expected component storage data to be an array");
47 for (const auto& [key, entry_node] : node.properties())
48 {
49 (void)key;
50 uint32_t entity = 0;
51 entry_node["entity"] >> entity;
52 T component{};
53 entry_node["component"] >> component;
54 components.insert_or_assign(entity, std::move(component));
55 }
56 }
57
58 void Clear() override
59 {
60 components.clear();
61 }
62
63 void RemoveComponent(uint32_t entity) override
64 {
65 components.erase(entity);
66 }
67};
68} // namespace pixelbullet
Provides assertion and panic mechanisms with optional custom formatting.
#define ASSERT(condition,...)
Asserts that a condition is true.
Definition assert.h:142
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:49
Definition sparse_set.h:16
Definition component_storage.h:15
Definition component_storage.h:25