]> git.localhorst.tv Git - blobs.git/blobdiff - src/app/states.cpp
track top ten for each record
[blobs.git] / src / app / states.cpp
index cfac2fa8450565d4ccf5b4403f3c3ce4e9cb385d..bec6da1387221b903ff2897e88a31d7ad4a1a8e6 100644 (file)
@@ -1,5 +1,6 @@
 #include "MasterState.hpp"
 
+#include "Application.hpp"
 #include "../creature/Creature.hpp"
 #include "../graphics/Viewport.hpp"
 #include "../math/const.hpp"
@@ -19,14 +20,19 @@ 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_dist(5.0)
+, cam_tgt_dist(5.0)
+, cam_orient(PI * 0.375, PI * 0.25, 0.0)
 , cam_dragging(false)
 , cp(assets)
+, rp(sim)
+, tp(sim)
 , remain(0)
 , thirds(0)
 , paused(false) {
+       cp.ZIndex(10.0f);
+       rp.ZIndex(20.0f);
+       tp.ZIndex(30.0f);
 }
 
 MasterState::~MasterState() noexcept {
@@ -50,8 +56,15 @@ 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;
        }
 }
 
@@ -79,6 +92,8 @@ 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();
        }
 }
 
@@ -90,7 +105,26 @@ void MasterState::OnMouseDown(const SDL_MouseButtonEvent &e) {
 }
 
 void MasterState::OnMouseUp(const SDL_MouseButtonEvent &e) {
-       if (e.button == SDL_BUTTON_RIGHT) {
+       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 = nullptr;
+               double closest_dist = 1.0e24;
+               for (creature::Creature *c : sim.LiveCreatures()) {
+                       glm::dvec3 normal(0.0);
+                       double dist = 0.0;
+                       if (Intersect(ray, c->CollisionBox(), glm::dmat4(cam.Model(c->GetSituation().GetPlanet())) * c->CollisionTransform(), normal, dist)
+                               && dist < closest_dist) {
+                               closest = c;
+                               closest_dist = dist;
+                       }
+               }
+               if (closest) {
+                       cp.Show(*closest);
+               } else {
+                       cp.Hide();
+               }
+       } else if (e.button == SDL_BUTTON_RIGHT) {
                SDL_SetRelativeMouseMode(SDL_FALSE);
                cam_dragging = false;
        }
@@ -100,7 +134,7 @@ 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.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);
        }
 }
@@ -110,7 +144,11 @@ void MasterState::OnMouseWheel(const SDL_MouseWheelEvent &e) {
        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));
+       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) {
@@ -179,13 +217,15 @@ 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);
+       cp.Draw(viewport);
+       rp.Draw(viewport);
+       tp.Draw(viewport);
 }
 
 }