X-Git-Url: http://git.localhorst.tv/?p=blobs.git;a=blobdiff_plain;f=src%2Fapp%2Fstates.cpp;h=cfac2fa8450565d4ccf5b4403f3c3ce4e9cb385d;hp=b1a20db92a50740934dfac6849065940c84e4ab6;hb=dd8b3145a03ed676b0ae6311c29fc3d68f666b15;hpb=6c1097479fd1ea41f0f76b91e67613822acf2e90 diff --git a/src/app/states.cpp b/src/app/states.cpp index b1a20db..cfac2fa 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -2,6 +2,7 @@ #include "../creature/Creature.hpp" #include "../graphics/Viewport.hpp" +#include "../math/const.hpp" #include "../world/Body.hpp" #include "../world/Planet.hpp" #include "../world/Simulation.hpp" @@ -18,6 +19,10 @@ MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept , assets(assets) , sim(sim) , cam(sim.Root()) +, cam_dist(10.0) +, cam_tgt_dist(10.0) +, cam_orient(PI * 0.125, 0.0, 0.0) +, cam_dragging(false) , cp(assets) , remain(0) , thirds(0) @@ -51,11 +56,19 @@ void MasterState::OnUpdate(int dt) { } void MasterState::Tick() { + constexpr double dt = 0.01666666666666666666666666666666; if (!paused) { - sim.Tick(); + sim.Tick(dt); } remain -= FrameMS(); thirds = (thirds + 1) % 3; + + double cam_diff = cam_tgt_dist - cam_dist; + if (std::abs(cam_diff) > 0.001) { + cam_dist += cam_diff * 0.25; + } else { + cam_dist = cam_tgt_dist; + } } int MasterState::FrameMS() const noexcept { @@ -69,9 +82,40 @@ void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) { } } +void MasterState::OnMouseDown(const SDL_MouseButtonEvent &e) { + if (e.button == SDL_BUTTON_RIGHT) { + SDL_SetRelativeMouseMode(SDL_TRUE); + cam_dragging = true; + } +} + +void MasterState::OnMouseUp(const SDL_MouseButtonEvent &e) { + if (e.button == SDL_BUTTON_RIGHT) { + SDL_SetRelativeMouseMode(SDL_FALSE); + cam_dragging = false; + } +} + +void MasterState::OnMouseMotion(const SDL_MouseMotionEvent &e) { + constexpr double pitch_scale = PI * 0.001; + constexpr double yaw_scale = PI * 0.002; + if (cam_dragging) { + cam_orient.x = glm::clamp(cam_orient.x + double(e.yrel) * pitch_scale, 0.0, PI * 0.5); + cam_orient.y = std::fmod(cam_orient.y + double(e.xrel) * yaw_scale, PI * 2.0); + } +} + +void MasterState::OnMouseWheel(const SDL_MouseWheelEvent &e) { + constexpr double roll_scale = PI * 0.0625; + constexpr double zoom_scale = -1.0; + constexpr double zoom_base = 1.125; + cam_orient.z = glm::clamp(cam_orient.z + double(e.x) * roll_scale, PI * -0.5, PI * 0.5); + cam_tgt_dist = std::max(1.0, cam_tgt_dist * std::pow(zoom_base, double(e.y) * zoom_scale)); +} + void MasterState::OnRender(graphics::Viewport &viewport) { if (cp.Shown()) { - cam.TopDown(cp.GetCreature(), 10.0f); + cam.Radial(cp.GetCreature(), cam_dist, cam_orient); assets.shaders.planet_surface.Activate(); assets.shaders.planet_surface.SetV(cam.View()); assets.shaders.sun_surface.Activate();