]> git.localhorst.tv Git - blank.git/commitdiff
allow face/turn selection of placed blocks
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 12 Mar 2015 23:02:40 +0000 (00:02 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 12 Mar 2015 23:02:40 +0000 (00:02 +0100)
Also display oriented block in HUD.

Use Q for face, E for turn.
Told you I'd reassign them ;)

And I'll do it again, no worries.

src/block.hpp
src/hud.cpp
src/hud.hpp
src/interface.cpp
src/interface.hpp

index 23c3979feba4ecd36d9867512901b81229a10fef..a142a96256e285e3acfca5b6848e04df00e4ebc0 100644 (file)
@@ -42,6 +42,11 @@ struct Block {
 
        const glm::mat4 &Transform() const;
 
+       Face GetFace() const { return Face(orient / 4); }
+       void SetFace(Face face) { orient = face * TURN_COUNT + GetTurn(); }
+       Turn GetTurn() const { return Turn(orient % 4); }
+       void SetTurn(Turn turn) { orient = GetFace() * TURN_COUNT + turn; }
+
 };
 
 
index c43d7849fb3083acb73d44dc52ef4e2265e61483..266b80ae9dcbdc4f7aa29984817152501813c621 100644 (file)
@@ -1,6 +1,8 @@
 #include "hud.hpp"
 
+#include "block.hpp"
 #include "init.hpp"
+#include "shader.hpp"
 #include "shape.hpp"
 
 #include <glm/gtc/matrix_transform.hpp>
@@ -8,8 +10,9 @@
 
 namespace blank {
 
-HUD::HUD()
-: block()
+HUD::HUD(const BlockTypeRegistry &types)
+: types(types)
+, block()
 , block_buf()
 , block_transform(1.0f)
 , block_visible(false)
@@ -22,7 +25,7 @@ HUD::HUD()
        block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
        block_transform = glm::scale(block_transform, glm::vec3(50.0f));
        block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
-       block_transform = glm::rotate(block_transform, 0.85f, glm::vec3(0.0f, 1.0f, 0.0f));
+       block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
 
        crosshair.vertices = std::vector<glm::vec3>({
                { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
@@ -46,9 +49,11 @@ void HUD::Viewport(float x, float y, float width, float height) {
 }
 
 
-void HUD::Display(const BlockType &type) {
+void HUD::Display(const Block &b) {
+       const BlockType &type = *types.Get(b.type);
+
        block_buf.Clear();
-       type.FillModel(block_buf);
+       type.FillModel(block_buf, b.Transform());
        block.Update(block_buf);
        block_visible = type.visible;
 }
index a3dc02e880d2d0c4013e4eaf5234a6972cb1a2c3..4bfd5fb9ae5903a0861287b5791b90b1c7eb9911 100644 (file)
@@ -2,7 +2,6 @@
 #define BLANK_HUD_H_
 
 #include "model.hpp"
-#include "shader.hpp"
 #include "world.hpp"
 
 #include <glm/glm.hpp>
 
 namespace blank {
 
+class BlockTypeRegistry;
+class DirectionalLighting;
+
 class HUD {
 
 public:
-       HUD();
+       explicit HUD(const BlockTypeRegistry &);
 
        HUD(const HUD &) = delete;
        HUD &operator =(const HUD &) = delete;
@@ -21,11 +23,13 @@ public:
        void Viewport(float width, float height);
        void Viewport(float x, float y, float width, float height);
 
-       void Display(const BlockType &);
+       void Display(const Block &);
 
        void Render(DirectionalLighting &);
 
 private:
+       const BlockTypeRegistry &types;
+
        Model block;
        Model::Buffer block_buf;
        glm::mat4 block_transform;
index f20903bd0a2c5acec54f7d2bd5904c66e160c02f..1f66b73e06ffc56afd1da35b1b502cafbdb0d193 100644 (file)
@@ -12,7 +12,7 @@ namespace blank {
 Interface::Interface(World &world)
 : world(world)
 , ctrl(world.Player())
-, hud()
+, hud(world.BlockTypes())
 , aim_chunk(nullptr)
 , aim_block(0)
 , aim_normal()
@@ -30,7 +30,7 @@ Interface::Interface(World &world)
 , up(false)
 , down(false) {
        hud.Viewport(960, 600);
-       hud.Display(*world.BlockTypes()[selection.type]);
+       hud.Display(selection);
 }
 
 
@@ -48,17 +48,37 @@ void Interface::Handle(const SDL_KeyboardEvent &event) {
                case SDLK_d:
                        right = event.state == SDL_PRESSED;
                        break;
-               case SDLK_q:
                case SDLK_SPACE:
                        up = event.state == SDL_PRESSED;
                        break;
-               case SDLK_e:
                case SDLK_LSHIFT:
                        down = event.state == SDL_PRESSED;
                        break;
+
+               case SDLK_q:
+                       if (event.state == SDL_PRESSED) {
+                               FaceBlock();
+                       }
+                       break;
+               case SDLK_e:
+                       if (event.state == SDL_PRESSED) {
+                               TurnBlock();
+                       }
+                       break;
        }
 }
 
+void Interface::FaceBlock() {
+       selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
+       hud.Display(selection);
+}
+
+void Interface::TurnBlock() {
+       selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
+       hud.Display(selection);
+}
+
+
 void Interface::Handle(const SDL_MouseMotionEvent &event) {
        ctrl.RotateYaw(event.xrel * yaw_sensitivity);
        ctrl.RotatePitch(event.yrel * pitch_sensitivity);
@@ -79,7 +99,7 @@ void Interface::Handle(const SDL_MouseButtonEvent &event) {
 void Interface::PickBlock() {
        if (!aim_chunk) return;
        selection = aim_chunk->BlockAt(aim_block);
-       hud.Display(*world.BlockTypes()[selection.type]);
+       hud.Display(selection);
 }
 
 void Interface::PlaceBlock() {
@@ -114,7 +134,7 @@ void Interface::SelectNext() {
        if (size_t(selection.type) >= world.BlockTypes().Size()) {
                selection.type = 1;
        }
-       hud.Display(*world.BlockTypes()[selection.type]);
+       hud.Display(selection);
 }
 
 void Interface::SelectPrevious() {
@@ -122,7 +142,7 @@ void Interface::SelectPrevious() {
        if (selection.type <= 0) {
                selection.type = world.BlockTypes().Size() - 1;
        }
-       hud.Display(*world.BlockTypes()[selection.type]);
+       hud.Display(selection);
 }
 
 void Interface::Handle(const SDL_WindowEvent &event) {
index ed417086504a8f8b86d97f648abe4349d3692e83..a4b7a92cef1cf10c1d9c79d30ab0ff72e9cf818f 100644 (file)
@@ -26,6 +26,9 @@ public:
        void Handle(const SDL_MouseWheelEvent &);
        void Handle(const SDL_WindowEvent &);
 
+       void FaceBlock();
+       void TurnBlock();
+
        void PickBlock();
        void PlaceBlock();
        void RemoveBlock();