X-Git-Url: http://git.localhorst.tv/?p=blobs.git;a=blobdiff_plain;f=src%2Fapp%2Fstates.cpp;h=eff36da0f4621c3225571216507dae99e630cfa3;hp=cbb2309581f05d35f0babf00374b0b97729ccaf0;hb=200f9da6241613a1c19431896b09d1715c262c6d;hpb=76c09039792065ca1c259fb4b681c84c29a9dbd8 diff --git a/src/app/states.cpp b/src/app/states.cpp index cbb2309..eff36da 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -1,5 +1,6 @@ #include "MasterState.hpp" +#include "Application.hpp" #include "../creature/Creature.hpp" #include "../graphics/Viewport.hpp" #include "../math/const.hpp" @@ -92,14 +93,33 @@ void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) { } void MasterState::OnMouseDown(const SDL_MouseButtonEvent &e) { - if (e.button == SDL_BUTTON_RIGHT) { + if (e.button == SDL_BUTTON_RIGHT && cp.Shown()) { SDL_SetRelativeMouseMode(SDL_TRUE); cam_dragging = true; } } 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; }