aboutsummaryrefslogtreecommitdiff
path: root/src/shared/entity/entity.hh
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
commite4483eca01b48b943cd0461e24a74ae1a3139ed4 (patch)
treeed58c3c246e3af1af337697695d780aa31f6ad9a /src/shared/entity/entity.hh
parent1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (diff)
Update to most recent version (old initial commit)
Diffstat (limited to 'src/shared/entity/entity.hh')
-rw-r--r--src/shared/entity/entity.hh69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/shared/entity/entity.hh b/src/shared/entity/entity.hh
new file mode 100644
index 0000000..2031fbf
--- /dev/null
+++ b/src/shared/entity/entity.hh
@@ -0,0 +1,69 @@
+#ifndef SHARED_ENTITY_ENTITY_HH_
+#define SHARED_ENTITY_ENTITY_HH_
+
+#include <cstdint>
+
+#include "shared/math/math.hh"
+#include "shared/movement/struct.hh"
+#include "shared/net/proto.hh"
+
+namespace shared {
+
+// Abstract base class all entities derive from.
+
+// A protobuf object may be provided as a constructor, and this functionality
+// should be implemented for all derived classes. Call pack with the correct
+// mutable_class* for storage as a protobuf object.
+class entity {
+public:
+ using index_t = std::uint32_t;
+
+protected:
+ index_t index;
+ math::coords chunk_pos;
+ glm::vec3 local_pos;
+
+public:
+ entity(const index_t& index, const shared::math::coords& chunk_pos,
+ const glm::vec3& local_pos) noexcept
+ : index(index), chunk_pos(chunk_pos), local_pos(local_pos) {}
+ entity(const proto::entity& proto) noexcept
+ : index(proto.index()),
+ chunk_pos(shared::net::get_coords(proto.chunk_pos())),
+ local_pos(shared::net::get_vec3(proto.local_pos())) {}
+ virtual ~entity() noexcept {}
+
+public:
+ const decltype(index)& get_index() const noexcept {
+ return this->index;
+ }
+ decltype(index)& get_mutable_index() noexcept {
+ return this->index;
+ }
+ const decltype(chunk_pos)& get_chunk_pos() const noexcept {
+ return this->chunk_pos;
+ }
+ decltype(chunk_pos)& get_mutable_chunk_pos() noexcept {
+ return this->chunk_pos;
+ }
+ const decltype(local_pos)& get_local_pos() const noexcept {
+ return this->local_pos;
+ }
+ decltype(local_pos)& get_mutable_local_pos() noexcept {
+ return this->local_pos;
+ }
+
+protected:
+ void pack(proto::entity* const proto) const noexcept {
+ proto->set_index(index);
+ shared::net::set_coords(proto->mutable_chunk_pos(), this->chunk_pos);
+ shared::net::set_vec3(proto->mutable_local_pos(), this->local_pos);
+ }
+
+public:
+ bool operator==(const entity&) const noexcept = default;
+};
+
+} // namespace shared
+
+#endif