From e4483eca01b48b943cd0461e24a74ae1a3139ed4 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 21:57:46 +1100 Subject: Update to most recent version (old initial commit) --- src/shared/entity/entity.hh | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/shared/entity/entity.hh (limited to 'src/shared/entity/entity.hh') 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 + +#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 -- cgit v1.2.3