PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
component_storage.h
1#pragma once
2
3#include "pixelbullet/core/containers/sparse_set.h"
4#include "pixelbullet/serialization/node.h"
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 Clear() = 0;
18 virtual void RemoveComponent(uint32_t entity) = 0;
19};
20
21template <typename T>
23{
24 SparseSet<T> set;
25
26 void Serialize(Node& node) const override
27 {
28 node.SetType(NodeType::Array);
29 auto& properties = node.GetProperties();
30 properties.clear();
31 for (const auto& entry : set)
32 {
33 Node entryNode;
34 entryNode["entity"] << entry.id;
35 Node compNode;
36 compNode << entry.data;
37 entryNode["component"] = compNode;
38 properties.push_back({ "", entryNode });
39 }
40 }
41
42 void Deserialize(const Node& node) override
43 {
44 ASSERT(node.GetType() == NodeType::Array || node.GetType() == NodeType::Null, "Expected component storage data to be an array");
45 for (const auto& [key, entryNode] : node.GetProperties())
46 {
47 uint32_t entity = 0;
48 entryNode["entity"] >> entity;
49 T component{};
50 entryNode["component"] >> component;
51 set.Insert(entity, component);
52 }
53 }
54
55 void Clear() override
56 {
57 set.Clear();
58 }
59
60 void RemoveComponent(uint32_t entity) override
61 {
62 set.Erase(entity);
63 }
64};
65} // namespace pixelbullet
#define ASSERT(condition,...)
Asserts that a condition is true.
Definition assert.h:163
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition sparse_set.h:13
Definition component_storage.h:13
Definition component_storage.h:23