Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
PhysicsLayer.hpp
1#pragma once
2
3#include <stdint.h>
4#include "Assert.hpp"
5
6namespace Grindstone::Physics {
7 constexpr uint32_t MaxLayerCount = 32;
8
9 struct LayerMask;
10
11 struct Layer {
12 Layer() = default;
13 Layer(const Layer& other) = default;
14 Layer(Layer&& other) noexcept = default;
15 Layer& operator=(const Layer& other) = default;
16 Layer& operator=(Layer&& other) noexcept = default;
17
18 Layer(uint8_t value) : layer(value) {
19 GS_ASSERT(value < MaxLayerCount);
20 }
21
22 Layer& operator=(uint8_t value) {
23 GS_ASSERT(value < MaxLayerCount);
24 layer = value;
25 return *this;
26 }
27
28 explicit operator uint8_t() const {
29 return layer;
30 }
31
32 LayerMask GetLayerMask() const;
33
34 uint8_t AsUint8() const {
35 return layer;
36 }
37
38 uint8_t layer;
39 };
40
41 struct LayerMask {
42 LayerMask() = default;
43 LayerMask(const LayerMask& other) = default;
44 LayerMask(LayerMask&& other) noexcept = default;
45 LayerMask& operator=(const LayerMask& other) = default;
46 LayerMask& operator=(LayerMask&& other) noexcept = default;
47
48 LayerMask(uint32_t value) : mask(value) {}
49 LayerMask& operator=(uint32_t value) {
50 mask = value;
51 return *this;
52 }
53
54 LayerMask(Layer layer) : mask(layer.GetLayerMask().AsUint32()) {}
55 LayerMask& operator=(Layer layer) {
56 mask = layer.GetLayerMask().AsUint32();
57 return *this;
58 }
59
60 LayerMask& operator&=(const LayerMask& other) {
61 mask &= other.mask;
62 return *this;
63 }
64
65 LayerMask& operator|=(const LayerMask& other) {
66 mask |= other.mask;
67 return *this;
68 }
69
70 LayerMask& operator^=(const LayerMask& other) {
71 mask ^= other.mask;
72 return *this;
73 }
74
75 LayerMask operator&(const LayerMask& other) const {
76 return mask & other.mask;
77 }
78
79 LayerMask operator|(const LayerMask& other) const {
80 return mask | other.mask;
81 }
82
83 LayerMask operator^(const LayerMask& other) const {
84 return mask ^ other.mask;
85 }
86
87 friend LayerMask operator~(const LayerMask& obj) {
88 return ~obj.mask;
89 }
90
91 explicit operator uint32_t() const {
92 return mask;
93 }
94
95 explicit operator bool() const {
96 return mask != 0;
97 }
98
99 uint32_t AsUint32() const {
100 return mask;
101 }
102
103 bool Matches(Layer layer) const {
104 return mask & layer.GetLayerMask().mask;
105 }
106
107 bool Matches(LayerMask otherMask) const {
108 return mask & otherMask.mask;
109 }
110
111 bool HasValue() const {
112 return mask != 0;
113 }
114
115 uint32_t mask;
116 };
117
118 inline LayerMask Layer::GetLayerMask() const {
119 return LayerMask(1 << layer);
120 }
121}
Definition PhysicsLayer.hpp:41
Definition PhysicsLayer.hpp:11