]> git.localhorst.tv Git - blank.git/blobdiff - src/ui/ui.cpp
chat state
[blank.git] / src / ui / ui.cpp
index c249ed47148a3a67d5f0e38cc85a91064e64634a..0f82139b2a7bd5350db9965e51d89ea1ea3c0843 100644 (file)
 #include "../app/FrameCounter.hpp"
 #include "../app/init.hpp"
 #include "../audio/Audio.hpp"
+#include "../audio/SoundBank.hpp"
 #include "../graphics/Font.hpp"
 #include "../graphics/Viewport.hpp"
 #include "../io/TokenStreamReader.hpp"
-#include "../model/shapes.hpp"
+#include "../model/bounds.hpp"
 #include "../world/BlockLookup.hpp"
 #include "../world/World.hpp"
 #include "../world/WorldManipulator.hpp"
@@ -82,10 +83,11 @@ void PlayerController::Invalidate() noexcept {
 }
 
 void PlayerController::UpdatePlayer() noexcept {
-       constexpr float max_vel = 0.005f;
+       constexpr float max_vel = 5.0f; // in m/s
        if (dirty) {
-               player.GetEntity().Orientation(glm::quat(glm::vec3(pitch, yaw, 0.0f)));
-               player.GetEntity().Velocity(glm::rotateY(move_dir * max_vel, yaw));
+               player.GetEntity().Orientation(glm::quat(glm::vec3(0.0f, yaw, 0.0f)));
+               player.GetEntity().GetModel().EyesState().orientation = glm::quat(glm::vec3(pitch, 0.0f, 0.0f));
+               player.GetEntity().TargetVelocity(glm::rotateY(move_dir * max_vel, yaw));
 
                Ray aim = player.Aim();
                if (!world.Intersection(aim, glm::mat4(1.0f), player.GetEntity().ChunkCoords(), aim_world)) {
@@ -210,8 +212,11 @@ HUD::HUD(Environment &env, Config &config, const Player &player)
 // message box
 , messages(env.assets.small_ui_font)
 , msg_timer(5000)
+, msg_keep(false)
 // crosshair
 , crosshair() {
+       const float ls = env.assets.small_ui_font.LineSkip();
+
        // "inventory"
        block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
        block_transform = glm::scale(block_transform, glm::vec3(50.0f));
@@ -229,42 +234,42 @@ HUD::HUD(Environment &env, Config &config, const Player &player)
        counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
        counter_text.Foreground(glm::vec4(1.0f));
        counter_text.Background(glm::vec4(0.5f));
-       position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
+       position_text.Position(glm::vec3(-25.0f, 25.0f + ls, 0.0f), Gravity::NORTH_EAST);
        position_text.Foreground(glm::vec4(1.0f));
        position_text.Background(glm::vec4(0.5f));
-       orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
+       orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * ls, 0.0f), Gravity::NORTH_EAST);
        orientation_text.Foreground(glm::vec4(1.0f));
        orientation_text.Background(glm::vec4(0.5f));
-       block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
+       block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * ls, 0.0f), Gravity::NORTH_EAST);
        block_text.Foreground(glm::vec4(1.0f));
        block_text.Background(glm::vec4(0.5f));
        block_text.Set(env.assets.small_ui_font, "Block: none");
-       entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
+       entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * ls, 0.0f), Gravity::NORTH_EAST);
        entity_text.Foreground(glm::vec4(1.0f));
        entity_text.Background(glm::vec4(0.5f));
        entity_text.Set(env.assets.small_ui_font, "Entity: none");
 
        // message box
-       messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
+       messages.Position(glm::vec3(25.0f, -25.0f - 2 * ls, 0.0f), Gravity::SOUTH_WEST);
        messages.Foreground(glm::vec4(1.0f));
        messages.Background(glm::vec4(0.5f));
 
        // crosshair
-       OutlineMesh::Buffer buf;
+       PrimitiveMesh::Buffer buf;
        buf.vertices = std::vector<glm::vec3>({
                { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
                {   0.0f, -10.0f, 0.0f }, {  0.0f, 10.0f, 0.0f },
        });
-       buf.indices = std::vector<OutlineMesh::Index>({
+       buf.indices = std::vector<PrimitiveMesh::Index>({
                0, 1, 2, 3
        });
-       buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
+       buf.colors.resize(4, { 10.0f, 10.0f, 10.0f, 1.0f });
        crosshair.Update(buf);
 }
 
 namespace {
 
-OutlineMesh::Buffer outl_buf;
+PrimitiveMesh::Buffer outl_buf;
 
 }
 
@@ -272,7 +277,7 @@ void HUD::FocusBlock(const Chunk &chunk, int index) {
        const Block &block = chunk.BlockAt(index);
        const BlockType &type = chunk.Type(index);
        outl_buf.Clear();
-       type.FillOutlineMesh(outl_buf);
+       type.OutlinePrimitiveMesh(outl_buf);
        outline.Update(outl_buf);
        outline_transform = chunk.Transform(player.GetEntity().ChunkCoords());
        outline_transform *= chunk.ToTransform(Chunk::ToPos(index), index);
@@ -375,9 +380,9 @@ void HUD::Update(int dt) {
 void HUD::Render(Viewport &viewport) noexcept {
        // block focus
        if (outline_visible && config.video.world) {
-               PlainColor &outline_prog = viewport.WorldOutlineProgram();
+               PlainColor &outline_prog = viewport.WorldColorProgram();
                outline_prog.SetM(outline_transform);
-               outline.Draw();
+               outline.DrawLines();
        }
 
        // clear depth buffer so everything renders above the world
@@ -398,16 +403,16 @@ void HUD::Render(Viewport &viewport) noexcept {
                }
 
                // message box
-               if (msg_timer.Running()) {
+               if (msg_keep || msg_timer.Running()) {
                        messages.Render(viewport);
                }
 
                // crosshair
-               PlainColor &outline_prog = viewport.HUDOutlineProgram();
+               PlainColor &outline_prog = viewport.HUDColorProgram();
                viewport.EnableInvertBlending();
                viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
                outline_prog.SetM(viewport.Cursor());
-               crosshair.Draw();
+               crosshair.DrawLines();
        }
 
        // debug overlay
@@ -424,22 +429,26 @@ void HUD::Render(Viewport &viewport) noexcept {
 }
 
 
-InteractiveManipulator::InteractiveManipulator(Environment &env, Entity &player)
+InteractiveManipulator::InteractiveManipulator(Audio &audio, const SoundBank &sounds, Entity &player)
 : player(player)
-, audio(env.audio)
-, place_sound(env.loader.LoadSound("thump"))
-, remove_sound(env.loader.LoadSound("plop")) {
+, audio(audio)
+, sounds(sounds) {
 
 }
 
 void InteractiveManipulator::SetBlock(Chunk &chunk, int index, const Block &block) {
+       const BlockType &old_type = chunk.Type(index);
        chunk.SetBlock(index, block);
+       const BlockType &new_type = chunk.Type(index);
        glm::vec3 coords = chunk.ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(index));
-       // TODO: get sound effect from block type
-       if (block.type == 0) {
-               audio.Play(remove_sound, coords);
+       if (new_type.id == 0) {
+               if (old_type.remove_sound >= 0) {
+                       audio.Play(sounds[old_type.remove_sound], coords);
+               }
        } else {
-               audio.Play(place_sound, coords);
+               if (new_type.place_sound >= 0) {
+                       audio.Play(sounds[new_type.place_sound], coords);
+               }
        }
 }