]> git.localhorst.tv Git - blank.git/blobdiff - src/entity.cpp
don't add obstructed blocks to meshes
[blank.git] / src / entity.cpp
index ef9275f41f5f3a05ec975f88d6bdbf9afb7ca46b..d72bd1cc20a2772019a9ab85fef51e6776579062 100644 (file)
@@ -1,5 +1,8 @@
 #include "entity.hpp"
 
+#include "chunk.hpp"
+
+#include <cmath>
 #include <glm/gtx/transform.hpp>
 
 
@@ -8,9 +11,7 @@ namespace blank {
 Entity::Entity()
 : velocity()
 , position()
-, rotation(1.0f)
-, transform(1.0f)
-, dirty(false) {
+, rotation(1.0f) {
 
 }
 
@@ -19,30 +20,49 @@ 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::Rotation(const glm::mat4 &rot) {
        rotation = rot;
 }
 
-const glm::mat4 &Entity::Transform() const {
-       if (dirty) {
-               transform = glm::translate(position) * rotation;
-               dirty = false;
-       }
-       return 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 {
-       Transform();
+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);