]> git.localhorst.tv Git - blank.git/blobdiff - src/ui/ui.cpp
give unique IDs to entities
[blank.git] / src / ui / ui.cpp
index 1de6237e32cee5899b9d6ab0e647270f98461382..2ec4d883d3e6f963dc047e85694a5bfb9bcec00d 100644 (file)
@@ -59,6 +59,10 @@ HUD::HUD(const BlockTypeRegistry &types, const Font &font)
 }
 
 
+void HUD::DisplayNone() {
+       block_visible = false;
+}
+
 void HUD::Display(const Block &b) {
        const BlockType &type = types.Get(b.type);
 
@@ -98,10 +102,12 @@ void HUD::Render(Viewport &viewport) noexcept {
 Interface::Interface(
        const Config &config,
        Environment &env,
-       World &world)
+       World &world,
+       Entity &player)
 : env(env)
 , world(world)
-, ctrl(world.Player())
+// let's assume this succeeds and hope for the best for now
+, ctrl(player)
 , hud(world.BlockTypes(), env.assets.small_ui_font)
 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
 , aim_world()
@@ -112,16 +118,17 @@ Interface::Interface(
 , position_text()
 , orientation_text()
 , block_text()
-, last_displayed()
+, last_block()
+, last_entity(nullptr)
 , messages(env.assets.small_ui_font)
 , msg_timer(5000)
 , config(config)
 , place_timer(256)
 , remove_timer(256)
 , remove(0)
-, selection(1)
-, place_sound(env.assets.LoadSound("thump"))
-, remove_sound(env.assets.LoadSound("plop"))
+, selection(0)
+, place_sound(env.loader.LoadSound("thump"))
+, remove_sound(env.loader.LoadSound("plop"))
 , fwd(0)
 , rev(0)
 , debug(false) {
@@ -138,10 +145,14 @@ Interface::Interface(
        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.Foreground(glm::vec4(1.0f));
+       entity_text.Background(glm::vec4(0.5f));
+       entity_text.Set(env.assets.small_ui_font, "Entity: none");
        messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
        messages.Foreground(glm::vec4(1.0f));
        messages.Background(glm::vec4(0.5f));
-       hud.Display(selection);
+       hud.DisplayNone();
 }
 
 
@@ -195,19 +206,6 @@ void Interface::HandlePress(const SDL_KeyboardEvent &event) {
                        ToggleCollision();
                        break;
 
-               case Keymap::PRINT_BLOCK:
-                       PrintBlockInfo();
-                       break;
-               case Keymap::PRINT_CHUNK:
-                       PrintChunkInfo();
-                       break;
-               case Keymap::PRINT_LIGHT:
-                       PrintLightInfo();
-                       break;
-               case Keymap::PRINT_SELECTION:
-                       PrintSelectionInfo();
-                       break;
-
                case Keymap::TOGGLE_VISUAL:
                        ToggleVisual();
                        break;
@@ -274,92 +272,6 @@ void Interface::ToggleCollision() {
        }
 }
 
-void Interface::PrintBlockInfo() {
-       std::cout << std::endl;
-       if (!aim_world) {
-               PostMessage("not looking at any block");
-               Ray aim = ctrl.Aim();
-               std::stringstream s;
-               s << "aim ray: " << aim.orig << ", " << aim.dir;
-               PostMessage(s.str());
-               return;
-       }
-       std::stringstream s;
-       s << "looking at block " << aim_world.block
-               << " " << aim_world.BlockCoords()
-               << " of chunk " << aim_world.GetChunk().Position()
-       ;
-       PostMessage(s.str());
-       Print(aim_world.GetBlock());
-}
-
-void Interface::PrintChunkInfo() {
-       std::cout << std::endl;
-       if (!aim_world) {
-               PostMessage("not looking at any block");
-               return;
-       }
-       std::stringstream s;
-       s << "looking at chunk " << aim_world.GetChunk().Position();
-       PostMessage(s.str());
-
-       PostMessage("  neighbors:");
-       if (aim_world.GetChunk().HasNeighbor(Block::FACE_LEFT)) {
-               s.str("");
-               s << " left  " << aim_world.GetChunk().GetNeighbor(Block::FACE_LEFT).Position();
-               PostMessage(s.str());
-       }
-       if (aim_world.GetChunk().HasNeighbor(Block::FACE_RIGHT)) {
-               s.str("");
-               s << " right " << aim_world.GetChunk().GetNeighbor(Block::FACE_RIGHT).Position();
-               PostMessage(s.str());
-       }
-       if (aim_world.GetChunk().HasNeighbor(Block::FACE_UP)) {
-               s.str("");
-               s << " up    " << aim_world.GetChunk().GetNeighbor(Block::FACE_UP).Position();
-               PostMessage(s.str());
-       }
-       if (aim_world.GetChunk().HasNeighbor(Block::FACE_DOWN)) {
-               s.str("");
-               s << " down  " << aim_world.GetChunk().GetNeighbor(Block::FACE_DOWN).Position();
-               PostMessage(s.str());
-       }
-       if (aim_world.GetChunk().HasNeighbor(Block::FACE_FRONT)) {
-               s.str("");
-               s << " front " << aim_world.GetChunk().GetNeighbor(Block::FACE_FRONT).Position();
-               PostMessage(s.str());
-       }
-       if (aim_world.GetChunk().HasNeighbor(Block::FACE_BACK)) {
-               s.str("");
-               s << " back  " << aim_world.GetChunk().GetNeighbor(Block::FACE_BACK).Position();
-               PostMessage(s.str());
-       }
-       std::cout << std::endl;
-}
-
-void Interface::PrintLightInfo() {
-       std::stringstream s;
-       s
-               << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
-               << " at position " << world.Player().Position()
-       ;
-       PostMessage(s.str());
-}
-
-void Interface::PrintSelectionInfo() {
-       std::cout << std::endl;
-       Print(selection);
-}
-
-void Interface::Print(const Block &block) {
-       std::stringstream s;
-       s << "type: " << block.type
-               << ", face: " << block.GetFace()
-               << ", turn: " << block.GetTurn()
-       ;
-       PostMessage(s.str());
-}
-
 void Interface::ToggleAudio() {
        config.audio_disabled = !config.audio_disabled;
        if (config.audio_disabled) {
@@ -385,6 +297,7 @@ void Interface::ToggleDebug() {
                UpdatePosition();
                UpdateOrientation();
                UpdateBlockInfo();
+               UpdateEntityInfo();
        }
 }
 
@@ -413,21 +326,32 @@ void Interface::UpdateOrientation() {
 void Interface::UpdateBlockInfo() {
        if (aim_world) {
                const Block &block = aim_world.GetBlock();
-               if (last_displayed != block) {
+               if (last_block != block) {
                        std::stringstream s;
                        s << "Block: "
                                << aim_world.GetType().label
                                << ", face: " << block.GetFace()
                                << ", turn: " << block.GetTurn();
                        block_text.Set(env.assets.small_ui_font, s.str());
-                       last_displayed = block;
+                       last_block = block;
                }
        } else {
-               if (last_displayed != Block()) {
+               if (last_block != Block()) {
                        std::stringstream s;
                        s << "Block: none";
                        block_text.Set(env.assets.small_ui_font, s.str());
-                       last_displayed = Block();
+                       last_block = Block();
+               }
+       }
+}
+
+void Interface::UpdateEntityInfo() {
+       if (aim_entity) {
+               if (last_entity != aim_entity.entity) {
+                       std::stringstream s;
+                       s << "Entity: " << aim_entity.entity->Name();
+                       entity_text.Set(env.assets.small_ui_font, s.str());
+                       last_entity = aim_entity.entity;
                }
        }
 }
@@ -575,10 +499,10 @@ OutlineModel::Buffer outl_buf;
 }
 
 void Interface::CheckAim() {
-       if (!world.Intersection(aim, glm::mat4(1.0f), aim_world)) {
+       if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled().ChunkCoords(), aim_world)) {
                aim_world = WorldCollision();
        }
-       if (!world.Intersection(aim, glm::mat4(1.0f), aim_entity)) {
+       if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled(), aim_entity)) {
                aim_entity = EntityCollision();
        }
        if (aim_world && aim_entity) {
@@ -594,6 +518,7 @@ void Interface::CheckAim() {
        }
        if (debug) {
                UpdateBlockInfo();
+               UpdateEntityInfo();
        }
 }
 
@@ -601,7 +526,7 @@ void Interface::UpdateOutline() {
        outl_buf.Clear();
        aim_world.GetType().FillOutlineModel(outl_buf);
        outline.Update(outl_buf);
-       outline_transform = aim_world.GetChunk().Transform(world.Player().ChunkCoords());
+       outline_transform = aim_world.GetChunk().Transform(Player().ChunkCoords());
        outline_transform *= aim_world.BlockTransform();
        outline_transform *= glm::scale(glm::vec3(1.005f));
 }
@@ -620,7 +545,11 @@ void Interface::Render(Viewport &viewport) noexcept {
                counter_text.Render(viewport);
                position_text.Render(viewport);
                orientation_text.Render(viewport);
-               block_text.Render(viewport);
+               if (aim_world) {
+                       block_text.Render(viewport);
+               } else if (aim_entity) {
+                       entity_text.Render(viewport);
+               }
        }
 
        if (msg_timer.Running()) {
@@ -684,11 +613,6 @@ void Keymap::LoadDefault() {
        Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
        Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
 
-       Map(SDL_SCANCODE_B, PRINT_BLOCK);
-       Map(SDL_SCANCODE_C, PRINT_CHUNK);
-       Map(SDL_SCANCODE_L, PRINT_LIGHT);
-       Map(SDL_SCANCODE_P, PRINT_SELECTION);
-
        Map(SDL_SCANCODE_ESCAPE, EXIT);
 }
 
@@ -780,14 +704,6 @@ const char *Keymap::ActionToString(Action action) {
                        return "toggle_visual";
                case TOGGLE_DEBUG:
                        return "toggle_debug";
-               case PRINT_BLOCK:
-                       return "print_block";
-               case PRINT_CHUNK:
-                       return "print_chunk";
-               case PRINT_LIGHT:
-                       return "print_light";
-               case PRINT_SELECTION:
-                       return "print_selection";
                case EXIT:
                        return "exit";
        }
@@ -828,14 +744,6 @@ Keymap::Action Keymap::StringToAction(const std::string &str) {
                return TOGGLE_VISUAL;
        } else if (str == "toggle_debug") {
                return TOGGLE_DEBUG;
-       } else if (str == "print_block") {
-               return PRINT_BLOCK;
-       } else if (str == "print_chunk") {
-               return PRINT_CHUNK;
-       } else if (str == "print_light") {
-               return PRINT_LIGHT;
-       } else if (str == "print_selection") {
-               return PRINT_SELECTION;
        } else if (str == "exit") {
                return EXIT;
        } else {