]> git.localhorst.tv Git - blobs.git/blobdiff - src/app/states.cpp
allow clicking celestial bodies
[blobs.git] / src / app / states.cpp
index cbb2309581f05d35f0babf00374b0b97729ccaf0..98add9653a5af3e0571b82c6e6fd52435eb135c4 100644 (file)
@@ -1,5 +1,6 @@
 #include "MasterState.hpp"
 
+#include "Application.hpp"
 #include "../creature/Creature.hpp"
 #include "../graphics/Viewport.hpp"
 #include "../math/const.hpp"
@@ -10,6 +11,9 @@
 
 #include <glm/gtx/transform.hpp>
 
+#include <iostream>
+#include <glm/gtx/io.hpp>
+
 
 namespace blobs {
 namespace app {
@@ -23,12 +27,17 @@ MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
 , 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 {
@@ -88,6 +97,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();
        }
 }
 
@@ -99,7 +110,44 @@ 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_creature = nullptr;
+               double closest_dist = std::numeric_limits<double>::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;
        }
@@ -119,7 +167,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(cp.GetCreature().Size() * 2.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) {
@@ -194,6 +246,7 @@ void MasterState::OnRender(graphics::Viewport &viewport) {
        }
 
        viewport.ClearDepth();
+       bp.Draw(viewport);
        cp.Draw(viewport);
        rp.Draw(viewport);
        tp.Draw(viewport);