aboutsummaryrefslogtreecommitdiff
path: root/src/shared/entity/entity.hh
blob: 2031fbf0c92cea4111f7abcfdb26cca9ade25b13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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