|
| 1 | +#include <game.hpp> |
| 2 | +#include <glm/gtx/norm.hpp> |
| 3 | +#include <le2d/context.hpp> |
| 4 | +#include <cmath> |
| 5 | + |
| 6 | +namespace miracle { |
| 7 | +Game::Game(gsl::not_null<le::ServiceLocator const*> services) : m_services(services) { |
| 8 | + m_triangle.vertices = { |
| 9 | + le::Vertex{.position = {-50.0f, -50.0f}}, |
| 10 | + le::Vertex{.position = {+50.0f, -50.0f}}, |
| 11 | + le::Vertex{.position = {+0.0f, +75.0f}}, |
| 12 | + }; |
| 13 | + m_circle.create(50.0f); |
| 14 | +} |
| 15 | + |
| 16 | +void Game::on_cursor_pos(le::event::CursorPos const& cursor_pos) { |
| 17 | + auto const framebuffer_size = m_services->get<le::Context>().framebuffer_size(); |
| 18 | + m_cursor_pos = cursor_pos.normalized.to_target(framebuffer_size); |
| 19 | +} |
| 20 | + |
| 21 | +void Game::tick([[maybe_unused]] kvf::Seconds const dt) { |
| 22 | + m_circle.transform.position = m_cursor_pos; |
| 23 | + |
| 24 | + auto const dist_sq = glm::length2(m_cursor_pos); |
| 25 | + if (dist_sq > 0.1f) { |
| 26 | + auto const dist = std::sqrt(dist_sq); |
| 27 | + auto const normalized = m_cursor_pos / dist; |
| 28 | + static constexpr auto up_v = glm::vec2{0.0f, 1.0f}; |
| 29 | + auto const dot = glm::dot(normalized, up_v); |
| 30 | + auto const angle = glm::degrees(std::acos(dot)); |
| 31 | + m_triangle.transform.orientation = m_cursor_pos.x > 0.0f ? -angle : angle; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void Game::render(le::Renderer& renderer) const { |
| 36 | + m_triangle.draw(renderer); |
| 37 | + m_circle.draw(renderer); |
| 38 | +} |
| 39 | +} // namespace miracle |
0 commit comments