]> git.localhorst.tv Git - blank.git/blobdiff - src/world/Entity.hpp
fix return type for Entity::Transform()
[blank.git] / src / world / Entity.hpp
index ef5e4e32eb92ce4667e233d85f3b485a3aeffcf6..e38e0b449e79bf41eae31c4800a7bc10ad04d6ea 100644 (file)
@@ -2,9 +2,13 @@
 #define BLANK_WORLD_ENTITY_HPP_
 
 #include "Chunk.hpp"
-#include "../model/CompositeModel.hpp"
-#include "../model/geometry.hpp"
+#include "EntityDerivative.hpp"
+#include "EntityState.hpp"
+#include "Steering.hpp"
+#include "../geometry/primitive.hpp"
+#include "../model/Instance.hpp"
 
+#include <cstdint>
 #include <string>
 #include <glm/glm.hpp>
 #include <glm/gtc/quaternion.hpp>
 namespace blank {
 
 class DirectionalLighting;
+class EntityController;
 class Shape;
+class World;
 
 class Entity {
 
 public:
        Entity() noexcept;
+       ~Entity() noexcept;
 
-       CompositeModel &GetModel() noexcept { return model; }
-       const CompositeModel &GetModel() const noexcept { return model; }
+       // note that when copying an entity which owns its controller, the
+       // original must outlive the copy, otherwise the copy ends up with
+       // an invalid controller pointer
+       Entity(const Entity &) noexcept;
+       Entity &operator =(const Entity &) = delete;
+
+       Steering &GetSteering() noexcept { return steering; }
+       const Steering &GetSteering() const noexcept { return steering; }
+
+       bool HasController() const noexcept { return ctrl; }
+       // entity takes over ownership of controller
+       void SetController(EntityController *c) noexcept;
+       // entity uses shared controller
+       void SetController(EntityController &c) noexcept;
+       void UnsetController() noexcept;
+       EntityController &GetController() noexcept { return *ctrl; }
+       const EntityController &GetController() const noexcept { return *ctrl; }
+
+       Instance &GetModel() noexcept { return model; }
+       const Instance &GetModel() const noexcept { return model; }
+
+       std::uint32_t ID() const noexcept { return id; }
+       void ID(std::uint32_t i) noexcept { id = i; }
 
        const std::string &Name() const noexcept { return name; }
        void Name(const std::string &n) { name = n; }
 
        const AABB &Bounds() const noexcept { return bounds; }
-       void Bounds(const AABB &b) noexcept { bounds = b; }
+       // get distance between local origin and farthest vertex
+       float Radius() const noexcept { return radius; }
+       void Bounds(const AABB &b) noexcept { bounds = b; radius = b.OriginRadius(); }
 
        bool WorldCollidable() const noexcept { return world_collision; }
        void WorldCollidable(bool b) noexcept { world_collision = b; }
 
-       const glm::vec3 &Velocity() const noexcept { return velocity; }
-       void Velocity(const glm::vec3 &v) noexcept { velocity = v; }
+       float MaxVelocity() const noexcept { return max_vel; }
+       void MaxVelocity(float v) noexcept { max_vel = v; }
+       float MaxControlForce() const noexcept { return max_force; }
+       void MaxControlForce(float f) noexcept { max_force = f; }
+
+       glm::vec3 ControlForce(const EntityState &) const noexcept;
 
-       const glm::vec3 &Position() const noexcept { return model.Position(); }
-       void Position(const Chunk::Pos &, const glm::vec3 &) noexcept;
-       void Position(const glm::vec3 &) noexcept;
-       void Move(const glm::vec3 &delta) noexcept;
+       const glm::vec3 &Velocity() const noexcept { return state.velocity; }
 
-       const Chunk::Pos ChunkCoords() const noexcept { return chunk; }
+       const ExactLocation::Fine &Position() const noexcept { return state.pos.block; }
+       void Position(const ExactLocation::Coarse &, const ExactLocation::Fine &) noexcept;
+       void Position(const ExactLocation::Fine &) noexcept;
+
+       const glm::ivec3 ChunkCoords() const noexcept { return state.pos.chunk; }
 
        glm::vec3 AbsolutePosition() const noexcept {
-               return glm::vec3(chunk * Chunk::Extent()) + Position();
+               return state.AbsolutePosition();
        }
        glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
-               return glm::vec3((chunk - other.chunk) * Chunk::Extent()) + Position() - other.Position();
+               return state.Diff(other.state);
        }
 
-       /// direction is rotation axis, magnitude is speed in rad/ms
-       const glm::vec3 &AngularVelocity() const noexcept { return angular_velocity; }
-       void AngularVelocity(const glm::vec3 &v) noexcept { angular_velocity = v; }
-
-       const glm::quat &Orientation() const noexcept { return model.Orientation(); }
-       void Orientation(const glm::quat &o) noexcept { model.Orientation(o); }
-       void Rotate(const glm::quat &delta) noexcept;
-
-       glm::mat4 ChunkTransform(const Chunk::Pos &chunk_offset) const noexcept;
-       glm::mat4 Transform(const Chunk::Pos &chunk_offset) const noexcept;
-       Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
+       /// orientation of local coordinate system
+       void Orientation(const glm::quat &o) noexcept { state.orient = o; }
+       const glm::quat &Orientation() const noexcept { return state.orient; }
+
+       /// orientation of head within local coordinate system, in radians
+       float Pitch() const noexcept { return state.pitch; }
+       float Yaw() const noexcept { return state.yaw; }
+       void TurnHead(float delta_pitch, float delta_yaw) noexcept;
+       void SetHead(float pitch, float yaw) noexcept;
+
+       /// get a transform for this entity's coordinate space
+       const glm::mat4 &Transform() const noexcept { return model_transform; }
+       /// get a transform for this entity's coordinate space relative to reference chunk
+       glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
+       /// get a transform for this entity's view space relative to reference chunk
+       glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
+       /// get a ray in entity's face direction originating from center of vision
+       Ray Aim(const ExactLocation::Coarse &chunk_offset) const noexcept;
+
+       /// true if this entity's position will change (significantly) the next update
+       bool Moving() const noexcept { return speed > 0.0f; }
+       /// magnitude of velocity
+       float Speed() const noexcept { return speed; }
+       /// normalized velocity or heading if standing still
+       const glm::vec3 &Heading() const noexcept { return heading; }
+
+       void SetState(const EntityState &s) noexcept { state = s; }
+       const EntityState &GetState() const noexcept { return state; }
 
        void Ref() noexcept { ++ref_count; }
        void UnRef() noexcept { --ref_count; }
@@ -68,29 +120,66 @@ public:
        bool Dead() const noexcept { return dead; }
        bool CanRemove() const noexcept { return dead && ref_count <= 0; }
 
-       void Update(int dt) noexcept;
+       void Update(World &, float dt);
 
        void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
-               model.Render(M, prog);
+               if (model) model.Render(M, prog);
        }
 
 private:
-       CompositeModel model;
+       void UpdatePhysics(World &, float dt);
+       void UpdateTransforms() noexcept;
+       void UpdateHeading() noexcept;
+       void UpdateModel(float dt) noexcept;
+public:
+       // temporarily made public so AI can use it until it's smoothed out to be suitable for players, too
+       void OrientBody(float dt) noexcept;
+private:
+       void OrientHead(float dt) noexcept;
 
-       std::string name;
+       EntityDerivative CalculateStep(
+               World &,
+               const EntityState &cur,
+               float dt,
+               const EntityDerivative &prev
+       ) const;
 
-       AABB bounds;
 
-       glm::vec3 velocity;
-       Chunk::Pos chunk;
+private:
+       Steering steering;
+       EntityController *ctrl;
+       Instance model;
+
+       std::uint32_t id;
+       std::string name;
 
-       glm::vec3 angular_velocity;
+       AABB bounds;
+       float radius;
+       EntityState state;
+
+       /// chunk to model space
+       glm::mat4 model_transform;
+       /// model to view space
+       /// if this entity has no model, the eyes are assumed to
+       /// be at origin and oriented towards pitch of model space
+       glm::mat4 view_transform;
+       float speed;
+       glm::vec3 heading;
+
+       // TODO: I'd prefer a drag solution
+       float max_vel;
+       // TODO: split max_force into (local) axes?
+       //       e.g. players may want to disable vertical thrust under certain
+       //       conditions (e.g. "walking" ^^)
+       float max_force;
 
        int ref_count;
 
        bool world_collision;
        bool dead;
 
+       bool owns_controller;
+
 };
 
 }