X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fstates.cpp;h=98add9653a5af3e0571b82c6e6fd52435eb135c4;hb=3989da924c4e33c52f500aead5ae62bb40294781;hp=b1a20db92a50740934dfac6849065940c84e4ab6;hpb=2ab70a92ae39cebc6166ef15545ebcbd31a31c38;p=blobs.git diff --git a/src/app/states.cpp b/src/app/states.cpp index b1a20db..98add96 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -1,7 +1,9 @@ #include "MasterState.hpp" +#include "Application.hpp" #include "../creature/Creature.hpp" #include "../graphics/Viewport.hpp" +#include "../math/const.hpp" #include "../world/Body.hpp" #include "../world/Planet.hpp" #include "../world/Simulation.hpp" @@ -9,6 +11,9 @@ #include +#include +#include + namespace blobs { namespace app { @@ -18,10 +23,21 @@ MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept , assets(assets) , sim(sim) , cam(sim.Root()) +, cam_dist(5.0) +, cam_tgt_dist(5.0) +, cam_orient(PI * 0.375, PI * 0.25, 0.0) +, cam_dragging(false) +, bp(assets) , cp(assets) +, rp(sim) +, tp(sim) , remain(0) , thirds(0) , paused(false) { + bp.ZIndex(10.0f); + cp.ZIndex(20.0f); + rp.ZIndex(30.0f); + tp.ZIndex(40.0f); } MasterState::~MasterState() noexcept { @@ -45,17 +61,32 @@ void MasterState::OnResize(int w, int h) { void MasterState::OnUpdate(int dt) { remain += dt; - while (remain >= FrameMS()) { +#ifdef NDEBUG + int max_tick = 10; +#else + // one tick per frame when debugging so pausing execution doesn't result in more ticks + int max_tick = 1; +#endif + while (remain >= FrameMS() && max_tick > 0) { Tick(); + --max_tick; } } 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 { @@ -66,12 +97,86 @@ int MasterState::FrameMS() const noexcept { void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) { if (e.keysym.sym == SDLK_p) { paused = !paused; + } else if (e.keysym.sym == SDLK_F1) { + rp.Toggle(); + } +} + +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_LEFT) { + glm::dmat4 inverse(glm::inverse(cam.Projection() * cam.View())); + math::Ray ray(inverse * App().GetViewport().ShootPixel(e.x, e.y)); + + creature::Creature *closest_creature = nullptr; + double closest_dist = std::numeric_limits::infinity(); + for (creature::Creature *c : sim.LiveCreatures()) { + glm::dvec3 normal(0.0); + double dist = 0.0; + if (Intersect(ray, c->CollisionBounds(), glm::dmat4(cam.Model(c->GetSituation().GetPlanet())) * c->CollisionTransform(), normal, dist) + && dist < closest_dist) { + closest_creature = c; + closest_dist = dist; + } + } + + world::Body *closest_body = nullptr; + for (world::Body *b : sim.Bodies()) { + glm::dvec3 normal(0.0); + double dist = 0.0; + if (Intersect(ray, glm::dmat4(cam.Model(*b)) * b->CollisionBounds(), normal, dist) && dist < closest_dist) { + closest_creature = nullptr; + closest_body = b; + closest_dist = dist; + } + } + + if (closest_creature) { + cp.Show(*closest_creature); + bp.Hide(); + } else if (closest_body) { + bp.Show(*closest_body); + cp.Hide(); + } else { + cp.Hide(); + bp.Hide(); + } + } else 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.499); + 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); + if (cp.Shown()) { + cam_tgt_dist = std::max(cp.GetCreature().Size() * 2.0, cam_tgt_dist * std::pow(zoom_base, double(e.y) * zoom_scale)); + } else { + 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(); @@ -135,13 +240,16 @@ void MasterState::OnRender(graphics::Viewport &viewport) { // TODO: extend to nearby bodies as well for (auto c : cam.Reference().Creatures()) { assets.shaders.creature_skin.SetM(cam.Model(cam.Reference()) * glm::mat4(c->LocalTransform())); - assets.shaders.creature_skin.SetBaseColor(c->BaseColor()); - assets.shaders.creature_skin.SetHighlightColor(c->HighlightColor()); + assets.shaders.creature_skin.SetBaseColor(glm::vec3(c->BaseColor())); + assets.shaders.creature_skin.SetHighlightColor(glm::vec4(c->HighlightColor())); c->Draw(viewport); } viewport.ClearDepth(); - cp.Draw(assets, viewport); + bp.Draw(viewport); + cp.Draw(viewport); + rp.Draw(viewport); + tp.Draw(viewport); } }