aboutsummaryrefslogtreecommitdiff
path: root/src/shared/entity
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/entity')
-rw-r--r--src/shared/entity/animate.cc5
-rw-r--r--src/shared/entity/animate.hh89
-rw-r--r--src/shared/entity/entity.cc1
-rw-r--r--src/shared/entity/entity.hh69
-rw-r--r--src/shared/entity/moveable.cc3
-rw-r--r--src/shared/entity/moveable.hh17
-rw-r--r--src/shared/entity/player.cc1
-rw-r--r--src/shared/entity/player.hh63
8 files changed, 248 insertions, 0 deletions
diff --git a/src/shared/entity/animate.cc b/src/shared/entity/animate.cc
new file mode 100644
index 0000000..4e6f891
--- /dev/null
+++ b/src/shared/entity/animate.cc
@@ -0,0 +1,5 @@
+#include "shared/entity/animate.hh"
+
+namespace shared {
+
+} // namespace shared
diff --git a/src/shared/entity/animate.hh b/src/shared/entity/animate.hh
new file mode 100644
index 0000000..9a5c910
--- /dev/null
+++ b/src/shared/entity/animate.hh
@@ -0,0 +1,89 @@
+#ifndef SHARED_ENTITY_ANIMATE_HH_
+#define SHARED_ENTITY_ANIMATE_HH_
+
+#include "shared/entity/entity.hh"
+
+namespace shared {
+
+// An animate is a thing with viewangles, commands and velocity, ie mobs,
+// players etc.
+class animate : virtual public entity {
+public:
+ using commands_t = std::uint32_t;
+ enum mask : commands_t {
+ forward = 1 << 0,
+ left = 1 << 1,
+ backward = 1 << 2,
+ right = 1 << 3,
+ jump = 1 << 4,
+ crouch = 1 << 5,
+ sprint = 1 << 6,
+ attack = 1 << 7,
+
+ flying = 1 << 30
+ };
+
+protected:
+ commands_t commands;
+ shared::math::angles viewangles;
+ glm::vec3 velocity;
+ std::uint32_t active_item;
+
+public:
+ template <typename... Args>
+ animate(const commands_t& commands, const shared::math::angles& viewangles,
+ const glm::vec3& velocity, const std::uint32_t& active_item,
+ Args&&... args) noexcept
+ : entity(std::forward<Args>(args)...), commands(commands),
+ viewangles(viewangles), velocity(velocity), active_item(active_item) {
+ }
+
+ animate(const proto::animate& proto) noexcept
+ : entity(proto.entity()), commands(proto.commands()),
+ viewangles(shared::net::get_angles(proto.viewangles())),
+ velocity(shared::net::get_vec3(proto.velocity())),
+ active_item(proto.active_item()) {}
+
+public:
+ void pack(proto::animate* const proto) const noexcept {
+ this->entity::pack(proto->mutable_entity());
+
+ proto->set_active_item(this->active_item);
+ proto->set_commands(this->commands);
+ shared::net::set_angles(proto->mutable_viewangles(), this->viewangles);
+ shared::net::set_vec3(proto->mutable_velocity(), this->velocity);
+ }
+
+public:
+ const decltype(commands)& get_commands() const noexcept {
+ return this->commands;
+ }
+ decltype(commands)& get_mutable_commands() noexcept {
+ return this->commands;
+ }
+ const decltype(viewangles)& get_angles() const noexcept {
+ return this->viewangles;
+ }
+ decltype(viewangles)& get_mutable_angles() noexcept {
+ return this->viewangles;
+ }
+ const decltype(velocity)& get_velocity() const noexcept {
+ return this->velocity;
+ }
+ decltype(velocity)& get_mutable_velocity() noexcept {
+ return this->velocity;
+ }
+ const decltype(active_item)& get_active_item() const noexcept {
+ return this->active_item;
+ }
+ decltype(active_item)& get_mutable_active_item() noexcept {
+ return this->active_item;
+ }
+
+public:
+ bool operator==(const animate&) const noexcept = default;
+};
+
+} // namespace shared
+
+#endif
diff --git a/src/shared/entity/entity.cc b/src/shared/entity/entity.cc
new file mode 100644
index 0000000..8cc618b
--- /dev/null
+++ b/src/shared/entity/entity.cc
@@ -0,0 +1 @@
+#include "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 <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
diff --git a/src/shared/entity/moveable.cc b/src/shared/entity/moveable.cc
new file mode 100644
index 0000000..8d9261a
--- /dev/null
+++ b/src/shared/entity/moveable.cc
@@ -0,0 +1,3 @@
+#include "shared/entity/moveable.hh"
+
+namespace shared {}
diff --git a/src/shared/entity/moveable.hh b/src/shared/entity/moveable.hh
new file mode 100644
index 0000000..502bb39
--- /dev/null
+++ b/src/shared/entity/moveable.hh
@@ -0,0 +1,17 @@
+#ifndef SHARED_ENTITY_MOVEABLE_HH_
+#define SHARED_ENTITY_MOVEABLE_HH_
+
+#include "shared/entity/animate.hh"
+#include "shared/movement/struct.hh"
+
+namespace shared {
+
+// Moveable is an animate that provides an AABB.
+class moveable : virtual public shared::animate {
+public:
+ virtual const movement::aabb& get_aabb() const noexcept = 0;
+};
+
+} // namespace shared
+
+#endif
diff --git a/src/shared/entity/player.cc b/src/shared/entity/player.cc
new file mode 100644
index 0000000..c78a7ae
--- /dev/null
+++ b/src/shared/entity/player.cc
@@ -0,0 +1 @@
+#include "shared/entity/player.hh"
diff --git a/src/shared/entity/player.hh b/src/shared/entity/player.hh
new file mode 100644
index 0000000..a2fcd81
--- /dev/null
+++ b/src/shared/entity/player.hh
@@ -0,0 +1,63 @@
+#ifndef SHARED_ENTITY_PLAYER_HH_
+#define SHARED_ENTITY_PLAYER_HH_
+
+#include <algorithm>
+
+#include "shared/entity/moveable.hh"
+#include "shared/entity/animate.hh"
+#include "shared/item/items.hh"
+#include "shared/movement/struct.hh"
+
+namespace shared {
+
+class player : virtual public shared::moveable {
+public:
+ static constexpr float HEIGHT = 1.9f;
+ static constexpr float EYE_HEIGHT = HEIGHT * 0.8f;
+ static constexpr float HALFWIDTH = 0.25f;
+
+ static constexpr int INVENTORY_COLS = 10;
+ static constexpr int INVENTORY_ROWS = 5;
+
+public:
+ shared::item::items inventory;
+
+public:
+ template <typename... Args>
+ player(shared::item::items&& inventory, const commands_t& commands,
+ const shared::math::angles& viewangles, const glm::vec3& velocity,
+ const std::uint32_t& active_item, Args&&... args) noexcept
+ : shared::entity(args...), shared::animate(commands, viewangles,
+ velocity, active_item,
+ args...),
+ inventory(std::forward<shared::item::items>(inventory)) {}
+
+ player(const proto::player& proto) noexcept
+ : entity(proto.animate().entity()), animate(proto.animate()),
+ inventory(proto.inventory()) {}
+
+ // constructor for no inventory
+ player(const proto::animate& proto) noexcept
+ : entity(proto.entity()), animate(proto), inventory() {}
+
+ virtual ~player() noexcept {}
+
+public:
+ void pack(proto::player* const proto) const noexcept {
+ this->animate::pack(proto->mutable_animate());
+ this->inventory.pack(proto->mutable_inventory());
+ }
+
+ virtual const movement::aabb& get_aabb() const noexcept override {
+ static constexpr movement::aabb aabb = {
+ .min = {-HALFWIDTH + movement::EPSILON, 0.0f + movement::EPSILON,
+ -HALFWIDTH + movement::EPSILON},
+ .max = {HALFWIDTH - movement::EPSILON, HEIGHT - movement::EPSILON,
+ HALFWIDTH - movement::EPSILON}};
+ return aabb;
+ }
+};
+
+} // namespace shared
+
+#endif