Commit c2a0f704 authored by Kevin Balz's avatar Kevin Balz
Browse files

Merge branch 'feature/4-player-attack-directional' into 'develop'

Resolve "Player Attack: Directional"

Closes #4

See merge request !11
parents b51b5eb8 53aa2b65
Pipeline #3281 passed with stage
in 8 minutes and 7 seconds
......@@ -17,6 +17,8 @@
"step_time_percentage": 0.2,
"off_beat_threshold": 0.2
},
"Killable": {
},
"FixedPath": {
}
}
......@@ -18,5 +18,7 @@
"off_beat_threshold": 0.2
},
"FollowTarget": {
},
"Killable": {
}
}
......@@ -19,6 +19,9 @@
},
"Input_controller": {},
"Player": {},
"Dash": {
"attack_width": 0.8
},
"Rigid_body": {
"radius": 1.0
}
......
#include "dash_comp.hpp"
\ No newline at end of file
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
#include <mirrage/utils/units.hpp>
namespace phase_shifter::gameplay {
struct Dash_comp : public mirrage::ecs::Component<Dash_comp> {
static constexpr const char* name() { return "Dash"; }
using Component::Component;
float attack_width = 1;
bool was_move = false;
bool dash = false;
glm::vec2 last_position;
};
sf2_structDef(Dash_comp, attack_width, was_move, dash, last_position);
}
\ No newline at end of file
#include "dash_system.hpp"
#include "dash_comp.hpp"
#include "killable_comp.hpp"
#include "movement_comp.hpp"
#include <mirrage/ecs/components/transform_comp.hpp>
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/ecs/entity_set_view.hpp>
namespace phase_shifter::gameplay {
bool rect_circle_intersects(float rect_x,
float rect_y,
float rect_w,
float rect_h,
float circle_x,
float circle_y,
float radius)
{
float dist_x = std::abs(circle_x - rect_x - rect_w / 2);
float dist_y = std::abs(circle_y - rect_y - rect_h / 2);
if(dist_x > rect_w / 2 + radius) {
return false;
}
if(dist_y > rect_h / 2 + radius) {
return false;
}
if(dist_x <= rect_w / 2) {
return true;
}
if(dist_y <= rect_h / 2) {
return true;
}
float dx = dist_x - rect_w / 2;
float dy = dist_y - rect_h / 2;
return dx * dx + dy * dy <= radius * radius;
}
glm::vec2 rotate_point(glm::vec2 p, float angle)
{
return {p.x * std::cos(angle) - p.y * std::sin(angle), p.y * std::cos(angle) + p.x * std::sin(angle)};
}
using mirrage::ecs::Entity_handle;
using mirrage::ecs::components::Transform_comp;
Dash_system::Dash_system(mirrage::ecs::Entity_manager& ecs) : _ecs(ecs)
{
_ecs.register_component_type<Dash_comp>();
_ecs.register_component_type<Killable_comp>();
}
void Dash_system::update(mirrage::util::Time dt)
{
for(auto&& [transform, move, dash] : _ecs.list<Transform_comp, Movement_comp, Dash_comp>()) {
if((dash.dash || dash.was_move) && move.step_time_left > 0) {
//dash
glm::vec2 position(transform.position.x, transform.position.z);
auto movement = position - dash.last_position;
auto angle = std::atan2(movement.y, movement.x);
auto width = glm::length(movement);
auto height = dash.attack_width;
auto center = rotate_point(
{dash.last_position.x + movement.x / 2, dash.last_position.y + movement.y / 2},
angle);
glm::vec2 topleft(center.x - width / 2, center.y + height / 2);
std::cout << angle * 180 / 3.14 << std::endl;
for(auto&& [entity, k_transform, kill] :
_ecs.list<Entity_handle, Transform_comp, Killable_comp>()) {
auto circle = rotate_point({k_transform.position.x, k_transform.position.z}, angle);
if(rect_circle_intersects(
topleft.x,
topleft.y,
width,
height,
circle.x,
circle.y,
1))
{
_ecs.erase(entity);
}
}
dash.last_position = position;
dash.dash = true;
} else {
dash.dash = false;
}
if(move.move) {
if(!dash.was_move || move.step_time_left <= 0) {
dash.was_move = true;
dash.last_position = {transform.position.x, transform.position.z};
}
} else {
dash.was_move = false;
}
}
}
} // namespace phase_shifter::gameplay
\ No newline at end of file
#pragma once
#include <mirrage/utils/units.hpp>
namespace mirrage::ecs {
class Entity_manager;
}
namespace phase_shifter::gameplay {
class Dash_system {
public:
Dash_system(mirrage::ecs::Entity_manager&);
void update(mirrage::util::Time);
private:
mirrage::ecs::Entity_manager& _ecs;
bool _was_move = false;
glm::vec2 _start_position;
};
}
\ No newline at end of file
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
#include <mirrage/utils/units.hpp>
namespace phase_shifter::gameplay {
struct Killable_comp : public mirrage::ecs::Stateless_tag_component<Killable_comp> {
static constexpr const char* name() { return "Killable"; }
using Stateless_tag_component::Stateless_tag_component;
};
} // namespace phase_shifter::gameplay
......@@ -20,9 +20,9 @@ namespace phase_shifter::gameplay {
auto o = [](auto t, auto s) { return t * t * ((s + 1) * t + s); };
if(t < 0.5)
return 0.5 * a(t * 2.0, tension);
return 0.5f * a(t * 2.0f, tension);
else
return 0.5 * (o(t * 2.0 - 2.0, tension) + 2.0);
return 0.5f * (o(t * 2.0f - 2.0f, tension) + 2.0f);
}
} // namespace
......
......@@ -10,6 +10,8 @@
#include "helper/attachment_system.hpp"
#include "input/input_system.hpp"
#include "level/level_system.hpp"
#include "gameplay/killable_comp.hpp"
#include "gameplay/dash_system.hpp"
#include "ui/hud_system.hpp"
#include <context.hpp>
......@@ -39,6 +41,7 @@ namespace phase_shifter {
, _level_system(std::make_unique<level::Level_system>(_entities, engine.assets()))
, _camera_system(std::make_unique<gameplay::Camera_system>(_entities))
, _enemy_system(std::make_unique<gameplay::Enemy_system>(_entities))
, _dash_system(std::make_unique<gameplay::Dash_system>(_entities))
{
_entities.register_component_type<ecs::components::Transform_comp>();
......@@ -111,6 +114,7 @@ namespace phase_shifter {
_beat_system->update(dt);
_input_system->update(dt);
_level_system->update(dt);
_dash_system->update(dt);
_movement_system->update(dt);
_combat_system->update(dt);
_camera_system->update(dt);
......
......@@ -25,6 +25,7 @@ namespace phase_shifter {
class Camera_system;
class Enemy_system;
class Combat_system;
class Dash_system;
} // namespace gameplay
namespace helper {
class Attachment_system;
......@@ -71,6 +72,7 @@ namespace phase_shifter {
std::unique_ptr<level::Level_system> _level_system;
std::unique_ptr<gameplay::Camera_system> _camera_system;
std::unique_ptr<gameplay::Enemy_system> _enemy_system;
std::unique_ptr<gameplay::Dash_system> _dash_system;
// TODO: add systems here
mirrage::util::Console_command_container _commands;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment