X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fui%2Fui.cpp;h=02f2781c5e8f9cdb2326c1933092fd9105569ff1;hb=1bc2f230105ad6e1ee8d999ddc079cd85d244bf9;hp=e7e6b0c7725a36264222a08633119d4f443ba6d2;hpb=75172fd735e34082c34b47ae7c194445b53038d9;p=blank.git diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index e7e6b0c..02f2781 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -13,10 +13,12 @@ #include "../app/init.hpp" #include "../audio/Audio.hpp" #include "../audio/SoundBank.hpp" +#include "../geometry/distance.hpp" #include "../graphics/Font.hpp" #include "../graphics/Viewport.hpp" #include "../io/TokenStreamReader.hpp" #include "../model/bounds.hpp" +#include "../net/CongestionControl.hpp" #include "../world/BlockLookup.hpp" #include "../world/World.hpp" #include "../world/WorldManipulator.hpp" @@ -37,12 +39,16 @@ PlayerController::PlayerController(World &world, Player &player) : world(world) , player(player) , move_dir(0.0f) -, pitch(0.0f) -, yaw(0.0f) , dirty(true) , aim_world() , aim_entity() { + player.GetEntity().SetController(*this); +} +PlayerController::~PlayerController() { + if (&player.GetEntity().GetController() == this) { + player.GetEntity().UnsetController(); + } } void PlayerController::SetMovement(const glm::vec3 &m) noexcept { @@ -54,20 +60,26 @@ void PlayerController::SetMovement(const glm::vec3 &m) noexcept { Invalidate(); } -void PlayerController::TurnHead(float dp, float dy) noexcept { - pitch += dp; - if (pitch > PI / 2) { - pitch = PI / 2; - } else if (pitch < -PI / 2) { - pitch = -PI / 2; - } - yaw += dy; - if (yaw > PI) { - yaw -= PI * 2; - } else if (yaw < -PI) { - yaw += PI * 2; +glm::vec3 PlayerController::ControlForce(const Entity &e, const EntityState &s) const { + if (!iszero(move_dir)) { + // scale input by max velocity, apply yaw, and transform to world space + return TargetVelocity(glm::vec3(glm::vec4(rotateY(move_dir * e.MaxVelocity(), s.yaw), 0.0f) * transpose(e.Transform())), s, 5.0f); + } else { + // target velocity of 0 is the same as halt + return Halt(s, 5.0f); } - Invalidate(); +} + +void PlayerController::TurnHead(float dp, float dy) noexcept { + player.GetEntity().TurnHead(dp, dy); +} + +float PlayerController::GetPitch() const noexcept { + return player.GetEntity().Pitch(); +} + +float PlayerController::GetYaw() const noexcept { + return player.GetEntity().Yaw(); } void PlayerController::SelectInventory(int i) noexcept { @@ -83,17 +95,12 @@ void PlayerController::Invalidate() noexcept { } void PlayerController::UpdatePlayer() noexcept { - constexpr float max_vel = 5.0f; // in m/s if (dirty) { - 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)) { + if (!world.Intersection(aim, player.GetEntity().ChunkCoords(), aim_world)) { aim_world = WorldCollision(); } - if (!world.Intersection(aim, glm::mat4(1.0f), player.GetEntity(), aim_entity)) { + if (!world.Intersection(aim, player.GetEntity(), aim_entity)) { aim_entity = EntityCollision(); } if (aim_world && aim_entity) { @@ -112,12 +119,12 @@ void PlayerController::UpdatePlayer() noexcept { DirectInput::DirectInput(World &world, Player &player, WorldManipulator &manip) : PlayerController(world, player) , manip(manip) -, place_timer(256) -, remove_timer(256) { +, place_timer(0.25f) +, remove_timer(0.25f) { } -void DirectInput::Update(int dt) { +void DirectInput::Update(Entity &, float dt) { Invalidate(); // world has changed in the meantime UpdatePlayer(); @@ -209,11 +216,19 @@ HUD::HUD(Environment &env, Config &config, const Player &player) , block_text() , show_block(false) , show_entity(false) +// net stats +, bandwidth_text() +, rtt_text() +, packet_loss_text() +, show_net(false) // 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)); @@ -231,23 +246,37 @@ 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"); + // net stats + bandwidth_text.Position(glm::vec3(-25.0f, 25.0f + 6 * ls, 0.0f), Gravity::NORTH_EAST); + bandwidth_text.Foreground(glm::vec4(1.0f)); + bandwidth_text.Background(glm::vec4(0.5f)); + bandwidth_text.Set(env.assets.small_ui_font, "TX: 0.0KB/s RX: 0.0KB/s"); + rtt_text.Position(glm::vec3(-25.0f, 25.0f + 7 * ls, 0.0f), Gravity::NORTH_EAST); + rtt_text.Foreground(glm::vec4(1.0f)); + rtt_text.Background(glm::vec4(0.5f)); + rtt_text.Set(env.assets.small_ui_font, "RTT: unavailable"); + packet_loss_text.Position(glm::vec3(-25.0f, 25.0f + 8 * ls, 0.0f), Gravity::NORTH_EAST); + packet_loss_text.Foreground(glm::vec4(1.0f)); + packet_loss_text.Background(glm::vec4(0.5f)); + packet_loss_text.Set(env.assets.small_ui_font, "Packet loss: 0.0%"); + // 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)); @@ -345,10 +374,10 @@ void HUD::UpdatePosition() { } void HUD::UpdateOrientation() { - //std::stringstream s; - //s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch()) - // << ", yaw: " << rad2deg(ctrl.Yaw()); - //orientation_text.Set(env.assets.small_ui_font, s.str()); + std::stringstream s; + s << std::setprecision(3) << "pitch: " << glm::degrees(player.GetEntity().Pitch()) + << ", yaw: " << glm::degrees(player.GetEntity().Yaw()); + orientation_text.Set(env.assets.small_ui_font, s.str()); } void HUD::PostMessage(const char *msg) { @@ -359,6 +388,27 @@ void HUD::PostMessage(const char *msg) { } +void HUD::UpdateNetStats(const CongestionControl &stat) { + if (!config.video.debug) return; + + std::stringstream s; + s << std::fixed << std::setprecision(1) + << "TX: " << stat.Upstream() + << "KB/s, RX: " << stat.Downstream() << "KB/s"; + bandwidth_text.Set(env.assets.small_ui_font, s.str()); + + s.str(""); + s << "RTT: " << stat.RoundTripTime() << "ms"; + rtt_text.Set(env.assets.small_ui_font, s.str()); + + s.str(""); + s << "Packet loss: " << (stat.PacketLoss() * 100.0f) << "%"; + packet_loss_text.Set(env.assets.small_ui_font, s.str()); + + show_net = true; +} + + void HUD::Update(int dt) { msg_timer.Update(dt); if (msg_timer.HitOnce()) { @@ -390,6 +440,8 @@ void HUD::Render(Viewport &viewport) noexcept { if (block_visible) { DirectionalLighting &world_prog = viewport.HUDProgram(); world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f }); + world_prog.SetLightColor({ 1.0f, 1.0f, 1.0f }); + world_prog.SetAmbientColor({ 0.1f, 0.1f, 0.1f }); // disable distance fog world_prog.SetFogDensity(0.0f); @@ -400,7 +452,7 @@ void HUD::Render(Viewport &viewport) noexcept { } // message box - if (msg_timer.Running()) { + if (msg_keep || msg_timer.Running()) { messages.Render(viewport); } @@ -422,6 +474,11 @@ void HUD::Render(Viewport &viewport) noexcept { } else if (show_entity) { entity_text.Render(viewport); } + if (show_net) { + bandwidth_text.Render(viewport); + rtt_text.Render(viewport); + packet_loss_text.Render(viewport); + } } } @@ -461,11 +518,20 @@ Interface::Interface( , client_ctrl(cc) , fwd(0) , rev(0) -, slot(0) -, num_slots(10) { +, num_slots(10) +, locked(false) { } +void Interface::Lock() { + fwd = glm::ivec3(0); + rev = glm::ivec3(0); + locked = true; +} + +void Interface::Unlock() { + locked = false; +} void Interface::HandlePress(const SDL_KeyboardEvent &event) { if (!config.input.keyboard) return; @@ -597,7 +663,7 @@ void Interface::HandleRelease(const SDL_KeyboardEvent &event) { } void Interface::Handle(const SDL_MouseMotionEvent &event) { - if (!config.input.mouse) return; + if (locked || !config.input.mouse) return; player_ctrl.TurnHead( event.yrel * config.input.pitch_sensitivity, event.xrel * config.input.yaw_sensitivity); @@ -651,7 +717,7 @@ void Interface::UpdateMovement() { } void Interface::InvAbs(int s) { - slot = s % num_slots; + int slot = s % num_slots; while (slot < 0) { slot += num_slots; } @@ -659,7 +725,7 @@ void Interface::InvAbs(int s) { } void Interface::InvRel(int delta) { - InvAbs(slot + delta); + InvAbs(player_ctrl.GetPlayer().GetInventorySlot() + delta); }