Skip to content
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

Lana/attractor #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/bave
Submodule bave updated from 80bcd8 to 5002fc
42 changes: 42 additions & 0 deletions src/spaced/spaced/game/attractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <glm/gtx/norm.hpp>
#include <spaced/game/Attractor.hpp>

namespace spaced {

using bave::RoundedQuad;

Attractor::Attractor(Services const& services) {
auto rounded_quad = RoundedQuad{};
rounded_quad.size = glm::vec2{20.0f};
rounded_quad.corner_radius = 5.0f;
shape.set_shape(rounded_quad);
}
void Attractor::tick(bave::Seconds dt) {
dt += m_residue;
if (dt > max_dt) { return; }
for (; dt > 0s; dt -= time_slice) { integrate(); }
m_residue = dt;
}

void Attractor::integrate() {

auto const gx = position.x;
auto const gy = position.y;
auto const mx = target.x;
auto const my = target.y;

auto force_x = mx - gx;
auto force_y = my - gy;
auto const mag = sqrt((force_x * force_x) + (force_y * force_y));
auto strength = force / mag * mag;
force_x *= strength;
force_y *= strength;
auto const acceleration = glm::vec2{force_x, force_y};

auto const dv = acceleration * time_slice.count();
m_velocity += dv;
m_velocity *= (1.0f - dampen);
position += m_velocity * time_slice.count();
shape.transform.position = position;
}
} // namespace spaced
31 changes: 31 additions & 0 deletions src/spaced/spaced/game/attractor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <bave/core/time.hpp>
#include <bave/graphics/shape.hpp>
#include <glm/vec2.hpp>
#include <spaced/game/weapon.hpp>

namespace spaced {
class Attractor {
public:
Attractor() = default;
Attractor(Services const& services);

glm::vec2 target{};
glm::vec2 position{};

bave::Seconds time_slice{1.0f / 250.0f};
bave::Seconds max_dt{0.8s};
float dampen{0.01f};
float force{4.1f};
float min_distance{0.1f};

void tick(bave::Seconds dt);
bave::RoundedQuadShape shape{};

private:
void integrate();

glm::vec2 m_velocity{};
bave::Seconds m_residue{};
};
} // namespace spaced
6 changes: 5 additions & 1 deletion src/spaced/spaced/game/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Player::Player(Services const& services, std::unique_ptr<IController> controller
rounded_quad.size = glm::vec2{100.0f};
rounded_quad.corner_radius = 20.0f;
ship.set_shape(rounded_quad);
m_highlight = Attractor(services);

debug_switch_weapon();
}
Expand All @@ -37,6 +38,9 @@ void Player::tick(std::span<NotNull<IDamageable*> const> targets, Seconds const
auto const y_position = m_controller->tick(dt);
set_y(y_position);

m_highlight.target = ship.transform.position;
m_highlight.tick(dt);

auto const muzzle_position = ship.transform.position + 0.5f * glm::vec2{ship.get_shape().size.x, 0.0f};
if (m_controller->is_firing() && m_debug.shots_remaining > 0) {
if (auto round = m_weapon->fire(muzzle_position)) {
Expand All @@ -56,7 +60,7 @@ void Player::tick(std::span<NotNull<IDamageable*> const> targets, Seconds const

void Player::draw(Shader& shader) const {
ship.draw(shader);

m_highlight.shape.draw(shader);
for (auto const& round : m_weapon_rounds) { round->draw(shader); }
}

Expand Down
2 changes: 2 additions & 0 deletions src/spaced/spaced/game/player.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <bave/graphics/shape.hpp>
#include <bave/logger.hpp>
#include <spaced/game/attractor.hpp>
#include <spaced/game/controller.hpp>
#include <spaced/game/health.hpp>
#include <spaced/game/weapon.hpp>
Expand Down Expand Up @@ -39,6 +40,7 @@ class Player : public bave::IDrawable {
std::unique_ptr<IController> m_controller;
std::unique_ptr<Weapon> m_weapon{};
std::vector<std::unique_ptr<Weapon::Round>> m_weapon_rounds{};
Attractor m_highlight{};

struct {
int shots_remaining{};
Expand Down
Loading