VoxelEngine
 
Loading...
Searching...
No Matches
BlockState.h
1#pragma once
2
3#include <glm/glm.hpp>
4
5#include <iosfwd>
6#include <optional>
7#include <string>
8#include <type_traits>
9#include <unordered_map>
10#include <variant>
11
12#include "../serialize/ISerializable.h"
13
14
15namespace engine {
16
17 class BlockState : public ISerializable {
18 public:
19 using Value = std::variant<bool, int, unsigned int, float, glm::vec3, glm::ivec3>;
20
21 private:
22 // Helper to check if type T is one of the types in the variant (100% AI code :) )
23 template <typename T, typename Variant>
24 struct is_variant_member;
25
26 template <typename T, typename... Types>
27 struct is_variant_member<T, std::variant<Types...>>
28 : std::disjunction<std::is_same<T, Types>...> {};
29
30 template <typename T>
31 static constexpr bool is_value_type_v = is_variant_member<T, Value>::value;
32
33 public:
34 static BlockState makeRotation(glm::vec3 facing);
35 // static BlockState makeVariant(uint8_t variantIndex);
36
37 const glm::vec3& facing() const { return m_facing; }
38
39 BlockState() = default;
40 ~BlockState() = default;
41
42 template <typename T>
43 requires is_value_type_v<T>
44 void set(const std::string& name, const T& value) {
45 m_properties[name] = value;
46 }
47
48 template <typename T>
49 requires is_value_type_v<T>
50 std::optional<T> get(const std::string& name) const {
51 auto it = m_properties.find(name);
52 if (it == m_properties.end()) {
53 return std::nullopt;
54 }
55 return std::get<T>(it->second);
56 }
57
58 void serialize(std::ostream& out) const override;
59 void deserialize(std::istream& in) override;
60
61 protected:
62 glm::vec3 m_facing;
64 float m_rotation = 0.0f;
65
66 std::unordered_map<std::string, Value> m_properties;
67 };
68} // namespace engine
Definition BlockState.h:17
float m_rotation
Definition BlockState.h:64
Definition ISerializable.h:6