Skip to content

Commit 7f8d075

Browse files
committed
Add class Game
1 parent e179a9d commit 7f8d075

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/game.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

src/game.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <kvf/time.hpp>
3+
#include <le2d/drawable/shape.hpp>
4+
#include <le2d/event.hpp>
5+
#include <le2d/renderer.hpp>
6+
#include <le2d/service_locator.hpp>
7+
8+
namespace miracle {
9+
class Game {
10+
public:
11+
explicit Game(gsl::not_null<le::ServiceLocator const*> services);
12+
13+
void on_cursor_pos(le::event::CursorPos const& cursor_pos);
14+
15+
void tick(kvf::Seconds dt);
16+
void render(le::Renderer& renderer) const;
17+
18+
private:
19+
gsl::not_null<le::ServiceLocator const*> m_services;
20+
21+
le::drawable::Triangle m_triangle{};
22+
le::drawable::Circle m_circle{};
23+
24+
glm::vec2 m_cursor_pos{};
25+
};
26+
} // namespace miracle

0 commit comments

Comments
 (0)