X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fentity.cpp;h=275c8467345dbd0f72eba5718b8456e1cfdcaa4d;hb=e53a0e2e711a7d8bd9b0ddacd1360aa14370643f;hp=d72bd1cc20a2772019a9ab85fef51e6776579062;hpb=eca1fdcc8e34a4918418b2de122c6200aeb7ceaf;p=blank.git diff --git a/src/entity.cpp b/src/entity.cpp index d72bd1c..275c846 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,77 +1,114 @@ #include "entity.hpp" -#include "chunk.hpp" +#include "geometry.hpp" +#include "shape.hpp" #include #include +namespace { + +blank::Model::Buffer model_buffer; + +} namespace blank { -Entity::Entity() -: velocity() -, position() +Entity::Entity() noexcept +: shape(nullptr) +, model() +, velocity(0, 0, 0) +, position(0, 0, 0) +, chunk(0, 0, 0) +, angular_velocity(1.0f, 0.0f, 0.0f, 0.0f) , rotation(1.0f) { } -void Entity::Velocity(const glm::vec3 &vel) { +void Entity::SetShape(const Shape *s, const glm::vec3 &color) { + shape = s; + model_buffer.Clear(); + shape->Vertices(model_buffer.vertices, model_buffer.normals, model_buffer.indices); + model_buffer.colors.resize(shape->VertexCount(), color); + model.Update(model_buffer); +} + +void Entity::SetShapeless() noexcept { + shape = nullptr; +} + + +void Entity::Velocity(const glm::vec3 &vel) noexcept { velocity = vel; } -void Entity::Position(const Block::Pos &pos) { +void Entity::Position(const Block::Pos &pos) noexcept { position = pos; - while (position.x >= Chunk::Width()) { - position.x -= Chunk::Width(); + while (position.x >= Chunk::width) { + position.x -= Chunk::width; ++chunk.x; } while (position.x < 0) { - position.x += Chunk::Width(); + position.x += Chunk::width; --chunk.x; } - while (position.y >= Chunk::Height()) { - position.y -= Chunk::Height(); + while (position.y >= Chunk::height) { + position.y -= Chunk::height; ++chunk.y; } while (position.y < 0) { - position.y += Chunk::Height(); + position.y += Chunk::height; --chunk.y; } - while (position.z >= Chunk::Depth()) { - position.z -= Chunk::Depth(); + while (position.z >= Chunk::depth) { + position.z -= Chunk::depth; ++chunk.z; } while (position.z < 0) { - position.z += Chunk::Depth(); + position.z += Chunk::depth; --chunk.z; } } -void Entity::Move(const glm::vec3 &delta) { +void Entity::Move(const glm::vec3 &delta) noexcept { Position(position + delta); } -void Entity::Rotation(const glm::mat4 &rot) { +void Entity::AngularVelocity(const glm::quat &v) noexcept { + angular_velocity = v; +} + +void Entity::Rotation(const glm::mat4 &rot) noexcept { rotation = rot; } -glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const { +void Entity::Rotate(const glm::quat &delta) noexcept { + Rotation(rotation * glm::mat4_cast(delta)); +} + +glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept { const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent(); return glm::translate(position + chunk_pos) * rotation; } -Ray Entity::Aim(const Chunk::Pos &chunk_offset) const { +Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept { glm::mat4 transform = Transform(chunk_offset); - glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f); + glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); from /= from.w; glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f); to /= to.w; return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) }; } -void Entity::Update(int dt) { +void Entity::Update(int dt) noexcept { Move(velocity * float(dt)); + Rotate(angular_velocity * float(dt)); +} + + +void Entity::Draw() noexcept { + model.Draw(); } }