PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
ComponentStorage.hpp
1#pragma once
2
3#include "PixelBullet/Math/SparseSet.hpp"
4#include "PixelBullet/Serialization/Node.hpp"
5
6#include <cstdint>
7#include <string>
8#include <typeinfo>
9
10namespace PixelBullet
11{
13 {
14 virtual ~ComponentStorageBase() = default;
15 virtual void Serialize(Node& node) const = 0;
16 virtual void Deserialize(const Node& node) = 0;
17 virtual void RemoveComponent(uint32_t entity) = 0;
18 virtual std::string GetTypeName() const = 0;
19 };
20
21 template <typename T>
23 {
24 SparseSet<T> Set;
25
26 void Serialize(Node& node) const override
27 {
28 node.SetType(NodeType::Array);
29 for (const auto& entry : Set)
30 {
31 Node entryNode;
32 entryNode["entity"] << entry.id;
33 Node compNode;
34 compNode << entry.data;
35 entryNode["component"] = compNode;
36 node.GetProperties().push_back({ "", entryNode });
37 }
38 }
39
40 void Deserialize(const Node& node) override
41 {
42 for (const auto& [key, entryNode] : node.GetProperties())
43 {
44 uint32_t entity = 0;
45 entryNode["entity"] >> entity;
46 T component{};
47 entryNode["component"] >> component;
48 Set.Insert(entity, component);
49 }
50 }
51
52 void RemoveComponent(uint32_t entity) override
53 {
54 Set.Erase(entity);
55 }
56
57 [[nodiscard]] std::string GetTypeName() const override
58 {
59 return typeid(T).name();
60 }
61 };
62} // namespace PixelBullet
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition Node.hpp:51
Definition SparseSet.hpp:12
Definition ComponentStorage.hpp:13
Definition ComponentStorage.hpp:23