X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fentity.cpp;h=52502c26f256aceebcd7b2a30958cf98ec84c9ff;hb=46509f82dcea114b004c53a7f3a9608f2518077f;hp=ef9275f41f5f3a05ec975f88d6bdbf9afb7ca46b;hpb=cb959294a8271969ddfe411471d7f04e82c4788a;p=blank.git diff --git a/src/entity.cpp b/src/entity.cpp index ef9275f..52502c2 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,17 +1,41 @@ #include "entity.hpp" +#include "geometry.hpp" +#include "shape.hpp" + +#include #include +namespace { + +blank::Model::Buffer model_buffer; + +} namespace blank { Entity::Entity() -: velocity() -, position() -, rotation(1.0f) -, transform(1.0f) -, dirty(false) { +: 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::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() { + shape = nullptr; } @@ -19,30 +43,57 @@ void Entity::Velocity(const glm::vec3 &vel) { velocity = vel; } -void Entity::Position(const glm::vec3 &pos) { +void Entity::Position(const Block::Pos &pos) { position = pos; - dirty = true; + while (position.x >= Chunk::Width()) { + position.x -= Chunk::Width(); + ++chunk.x; + } + while (position.x < 0) { + position.x += Chunk::Width(); + --chunk.x; + } + while (position.y >= Chunk::Height()) { + position.y -= Chunk::Height(); + ++chunk.y; + } + while (position.y < 0) { + position.y += Chunk::Height(); + --chunk.y; + } + while (position.z >= Chunk::Depth()) { + position.z -= Chunk::Depth(); + ++chunk.z; + } + while (position.z < 0) { + position.z += Chunk::Depth(); + --chunk.z; + } } void Entity::Move(const glm::vec3 &delta) { - position += delta; - dirty = true; + Position(position + delta); +} + +void Entity::AngularVelocity(const glm::quat &v) { + angular_velocity = v; } void Entity::Rotation(const glm::mat4 &rot) { rotation = rot; } -const glm::mat4 &Entity::Transform() const { - if (dirty) { - transform = glm::translate(position) * rotation; - dirty = false; - } - return transform; +void Entity::Rotate(const glm::quat &delta) { + Rotation(rotation * glm::mat4_cast(delta)); } -Ray Entity::Aim() const { - Transform(); +glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const { + 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 { + glm::mat4 transform = Transform(chunk_offset); glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f); from /= from.w; glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f); @@ -52,6 +103,12 @@ Ray Entity::Aim() const { void Entity::Update(int dt) { Move(velocity * float(dt)); + Rotate(angular_velocity * float(dt)); +} + + +void Entity::Draw() { + model.Draw(); } }