#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