-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor lighthouse #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7cef3ab
Refactor lighthouse
NikolaJelic d87ec64
Refacotr PR requests
NikolaJelic e4360a0
Add very basic enemy spawn and render
NikolaJelic 972e408
Add util/random, demo in app (#5)
karnkaul c7ab5f6
Merge branch 'main' into nikola/enemy
NikolaJelic 30c3153
Refactor Enemy
NikolaJelic c1b20c7
Include <numbers> to fix compilation on Win
NikolaJelic 3e86574
Refactor small issues
NikolaJelic 8ec1158
Add missing namespace
NikolaJelic 5862bef
Change function signature
NikolaJelic acef7e4
Add enemy waves
NikolaJelic d76611d
Fix iterator type
NikolaJelic 26b9323
Fix type `int` -> `size_t`
NikolaJelic 19bc668
Add simple collision detection
NikolaJelic e241f8a
Minor formatting and include changes
NikolaJelic d378bf9
Extract enemy starter movement values to dedicated struct
NikolaJelic 1c2b973
Fix vec2 include
NikolaJelic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <enemy.hpp> | ||
#include <algorithm> | ||
#include "enemy_params.hpp" | ||
#include "glm/geometric.hpp" | ||
#include "glm/vec2.hpp" | ||
#include "kvf/color.hpp" | ||
#include "util/random.hpp" | ||
|
||
namespace miracle { | ||
Enemy::Enemy(gsl::not_null<le::ServiceLocator const*> services, EnemyParams const& params) | ||
: m_services(services), m_target_pos(params.target_pos), m_move_speed(params.move_speed), m_diameter(util::random_range(40.0f, 60.0f)) { | ||
m_sprite.create(m_diameter, kvf::red_v); | ||
auto const framebuffer_size = m_services->get<le::Context>().framebuffer_size(); | ||
auto const radius = static_cast<float>(std::max(framebuffer_size.x, framebuffer_size.y)) / 2.0f; | ||
|
||
m_sprite.transform.position = util::get_random_location_on_radius(radius); | ||
// TODO: add proper textures | ||
} | ||
|
||
void Enemy::render(le::Renderer& renderer) const { m_sprite.draw(renderer); } | ||
|
||
void Enemy::translate(kvf::Seconds const dt) { | ||
glm::vec2 const direction = glm::normalize(m_target_pos - m_sprite.transform.position); | ||
glm::vec2 const movement = direction * m_move_speed * dt.count(); | ||
m_sprite.transform.position += movement; | ||
} | ||
|
||
void Enemy::check_collision(glm::vec2 pos, float radius) { | ||
if (glm::distance(pos, m_sprite.transform.position) < radius + m_diameter / 2) { m_health = 0; } | ||
} | ||
|
||
} // namespace miracle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
#include <glm/gtx/norm.hpp> | ||
#include <kvf/time.hpp> | ||
#include <le2d/context.hpp> | ||
#include <le2d/drawable/shape.hpp> | ||
#include <le2d/event.hpp> | ||
#include <le2d/renderer.hpp> | ||
#include <le2d/service_locator.hpp> | ||
#include <optional> | ||
#include "enemy_params.hpp" | ||
#include "glm/vec2.hpp" | ||
#include "le2d/texture.hpp" | ||
|
||
namespace miracle { | ||
class Enemy { | ||
public: | ||
explicit Enemy(gsl::not_null<le::ServiceLocator const*> services, EnemyParams const& params); | ||
|
||
void render(le::Renderer& renderer) const; | ||
void translate(kvf::Seconds dt); | ||
// There are temporary parameters, will take the light beam object once it is created | ||
void check_collision(glm::vec2 pos, float radius); | ||
[[nodiscard]] std::size_t get_health() const { return m_health; } | ||
|
||
private: | ||
gsl::not_null<le::ServiceLocator const*> m_services; | ||
std::optional<le::Texture> m_texture; | ||
le::drawable::Circle m_sprite{}; | ||
glm::vec2 m_target_pos{}; | ||
float m_move_speed{}; | ||
float m_diameter{}; | ||
std::size_t m_health{100}; | ||
}; | ||
} // namespace miracle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
#include "glm/vec2.hpp" | ||
|
||
struct EnemyParams { | ||
glm::vec2 target_pos{}; | ||
float move_speed{}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
#include <glm/gtx/norm.hpp> | ||
#include <kvf/time.hpp> | ||
#include <le2d/context.hpp> | ||
#include <le2d/drawable/shape.hpp> | ||
#include <le2d/event.hpp> | ||
#include <le2d/renderer.hpp> | ||
#include <le2d/service_locator.hpp> | ||
#include <optional> | ||
#include "le2d/texture.hpp" | ||
|
||
namespace miracle { | ||
NikolaJelic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Lighthouse { | ||
public: | ||
explicit Lighthouse(gsl::not_null<le::ServiceLocator const*> services); | ||
|
||
void rotate_towards_cursor(glm::vec2 cursor_pos); | ||
void render(le::Renderer& renderer) const; | ||
|
||
private: | ||
gsl::not_null<le::ServiceLocator const*> m_services; | ||
std::optional<le::Texture> m_texture; | ||
le::drawable::Circle m_sprite{}; | ||
}; | ||
} // namespace miracle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <lighhouse.hpp> | ||
#include "le2d/asset_loader.hpp" | ||
#include "le2d/data_loader.hpp" | ||
|
||
namespace miracle { | ||
Lighthouse::Lighthouse(gsl::not_null<le::ServiceLocator const*> services) : m_services(services) { | ||
m_sprite.create(200.0f); | ||
auto const& data_loader = services->get<le::IDataLoader>(); | ||
auto const& context = services->get<le::Context>(); | ||
auto const asset_loader = le::AssetLoader{&data_loader, &context}; | ||
m_texture = asset_loader.load_texture("images/lighthouse.png"); | ||
m_sprite.texture = &m_texture.value(); | ||
} | ||
|
||
void Lighthouse::rotate_towards_cursor(glm::vec2 cursor_pos) { | ||
auto const dist_sq = glm::length2((cursor_pos)); | ||
if (dist_sq > 0.1f) { | ||
auto const dist = std::sqrt(dist_sq); | ||
auto const normalized = cursor_pos / dist; | ||
static constexpr auto up_v = glm::vec2(0.0f, 1.0f); | ||
auto const dot = glm::dot(normalized, up_v); | ||
auto const angle = glm::degrees(std::acos(dot)); | ||
m_sprite.transform.orientation = cursor_pos.x > 0.0f ? -angle : angle; | ||
} | ||
} | ||
|
||
void Lighthouse::render(le::Renderer& renderer) const { m_sprite.draw(renderer); } | ||
} // namespace miracle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
#include <glm/ext/vector_float2.hpp> | ||
NikolaJelic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <klib/assert.hpp> | ||
#include <klib/concepts.hpp> | ||
#include <cmath> | ||
#include <numbers> | ||
#include <random> | ||
namespace miracle::util { | ||
/// \brief Wrapper that reuses the same random engine for all calls. | ||
class Random { | ||
public: | ||
template <klib::NumberT Type> | ||
[[nodiscard]] static auto in_range(Type const min, Type const max) -> Type { | ||
if constexpr (std::integral<Type>) { | ||
auto dist = std::uniform_int_distribution<Type>{min, max}; | ||
return dist(m_engine); | ||
} else { | ||
auto dist = std::uniform_real_distribution<Type>{min, max}; | ||
return dist(m_engine); | ||
} | ||
} | ||
|
||
private: | ||
inline static std::default_random_engine m_engine{std::random_device{}()}; | ||
}; | ||
|
||
/// \returns Random value in the range [min, max]. | ||
template <klib::NumberT Type> | ||
[[nodiscard]] auto random_range(Type const min, Type const max) -> Type { | ||
return Random::in_range(min, max); | ||
} | ||
|
||
/// \returns Random index in the range [0, size - 1]. | ||
/// \pre size must be greater than 0. | ||
[[nodiscard]] inline auto random_index(std::size_t const size) -> std::size_t { | ||
KLIB_ASSERT(size > 0); | ||
return Random::in_range(0uz, size - 1); | ||
} | ||
// Returns a random coordinate on the specified radius | ||
[[nodiscard]] inline auto get_random_location_on_radius(float radius) -> glm::vec2 { | ||
float const angle = random_range(0.0f, 2.0f * std::numbers::pi_v<float>); | ||
return {radius * std::cos(angle), radius * std::sin(angle)}; | ||
NikolaJelic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} // namespace miracle::util |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.