From: Daniel Karbach Date: Thu, 12 Mar 2015 23:02:40 +0000 (+0100) Subject: allow face/turn selection of placed blocks X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=f924498d348c33d0c27f129c599a7466312253b2;p=blank.git allow face/turn selection of placed blocks 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. --- diff --git a/src/block.hpp b/src/block.hpp index 23c3979..a142a96 100644 --- a/src/block.hpp +++ b/src/block.hpp @@ -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; } + }; diff --git a/src/hud.cpp b/src/hud.cpp index c43d784..266b80a 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -1,6 +1,8 @@ #include "hud.hpp" +#include "block.hpp" #include "init.hpp" +#include "shader.hpp" #include "shape.hpp" #include @@ -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({ { -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; } diff --git a/src/hud.hpp b/src/hud.hpp index a3dc02e..4bfd5fb 100644 --- a/src/hud.hpp +++ b/src/hud.hpp @@ -2,7 +2,6 @@ #define BLANK_HUD_H_ #include "model.hpp" -#include "shader.hpp" #include "world.hpp" #include @@ -10,10 +9,13 @@ 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; diff --git a/src/interface.cpp b/src/interface.cpp index f20903b..1f66b73 100644 --- a/src/interface.cpp +++ b/src/interface.cpp @@ -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) { diff --git a/src/interface.hpp b/src/interface.hpp index ed41708..a4b7a92 100644 --- a/src/interface.hpp +++ b/src/interface.hpp @@ -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();