Commit f10aebf5 authored by Tim Scheiber's avatar Tim Scheiber
Browse files

Added simple collision detection for bullets with player

parent 9245a765
Pipeline #3317 failed with stage
in 3 minutes and 58 seconds
...@@ -22,5 +22,6 @@ ...@@ -22,5 +22,6 @@
}, },
"Rigid_body": { "Rigid_body": {
"radius": 0.1 "radius": 0.1
} },
"Bullet": {}
} }
...@@ -41,7 +41,7 @@ namespace phase_shifter::gameplay { ...@@ -41,7 +41,7 @@ namespace phase_shifter::gameplay {
_beat_index++; _beat_index++;
LOG(plog::debug) << "beat"; LOG(plog::debug) << "beat";
beats_left = size - 1 - _beat_index - _beat_offset; beats_left = size - 1 - _beat_index + _beat_offset;
if(beats_left <= 0) { if(beats_left <= 0) {
_bus.send<Lose_msg>(); _bus.send<Lose_msg>();
} }
......
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
#include <mirrage/utils/units.hpp>
namespace phase_shifter::gameplay {
struct Bullet_comp : public mirrage::ecs::Stateless_tag_component<Bullet_comp> {
static constexpr const char* name() { return "Bullet"; }
using Stateless_tag_component::Stateless_tag_component;
};
} // namespace phase_shifter::gameplay
#include "enemy_system.hpp" #include "enemy_system.hpp"
#include "beat_system.hpp" #include "beat_system.hpp"
#include "combat_system.hpp"
#include "movement_comp.hpp" #include "movement_comp.hpp"
#include "rigid_body_comp.hpp"
#include "player_comp.hpp"
#include <mirrage/ecs/components/transform_comp.hpp> #include <mirrage/ecs/components/transform_comp.hpp>
...@@ -9,29 +13,44 @@ namespace phase_shifter::gameplay { ...@@ -9,29 +13,44 @@ namespace phase_shifter::gameplay {
using namespace mirrage::ecs; using namespace mirrage::ecs;
Enemy_system::Enemy_system(Entity_manager& entity_manager, const Beat_system& beat_system) Enemy_system::Enemy_system(mirrage::util::Message_bus& bus,
: _entity_manager(entity_manager), _beat_system(beat_system) Entity_manager& entity_manager,
const Beat_system& beat_system)
: _bus(bus), _entity_manager(entity_manager), _beat_system(beat_system)
{ {
_entity_manager.register_component_type<Fixed_path_comp>(); _entity_manager.register_component_type<Fixed_path_comp>();
_entity_manager.register_component_type<Follow_target_comp>(); _entity_manager.register_component_type<Follow_target_comp>();
_entity_manager.register_component_type<Continuous_path_comp>(); _entity_manager.register_component_type<Continuous_path_comp>();
_entity_manager.register_component_type<Shooting_comp>(); _entity_manager.register_component_type<Shooting_comp>();
_entity_manager.register_component_type<Target_comp>(); _entity_manager.register_component_type<Target_comp>();
_entity_manager.register_component_type<Bullet_comp>();
} }
void Enemy_system::update(mirrage::util::Time dt) void Enemy_system::update(mirrage::util::Time dt)
{ {
auto beat = _beat_system.beat_state(); auto beat = _beat_system.beat_state();
for (auto&& [movement, fixed_path] : _entity_manager.list<Movement_comp, Fixed_path_comp>()) { do_movement(beat);
do_shooting(beat);
do_bullet_hit_detection();
}
void Enemy_system::do_movement(Beat_state& beat) {
do_fixed_path_movement(beat);
do_cont_path_movement(beat);
do_hunting_movement(beat);
}
void Enemy_system::do_fixed_path_movement(Beat_state& beat) {
for(auto&& [movement, fixed_path] : _entity_manager.list<Movement_comp, Fixed_path_comp>()) {
if(beat.beat) { if(beat.beat) {
if(fixed_path.wait_beats == 0 && !movement.move) { if(fixed_path.wait_beats == 0 && !movement.move) {
float direction = fixed_path.next_direction(); float direction = fixed_path.next_direction();
if(direction < 360) { if(direction < 360) {
float rad_direction = direction * mirrage::util::PI / 180.f; float rad_direction = direction * mirrage::util::PI / 180.f;
movement.aim.x = std::sin(rad_direction); movement.aim.x = std::sin(rad_direction);
movement.aim.y = -std::cos(rad_direction); movement.aim.y = -std::cos(rad_direction);
movement.move = true; movement.move = true;
} }
fixed_path.wait_beats = fixed_path.pause_between_steps; fixed_path.wait_beats = fixed_path.pause_between_steps;
} else { } else {
...@@ -39,15 +58,33 @@ namespace phase_shifter::gameplay { ...@@ -39,15 +58,33 @@ namespace phase_shifter::gameplay {
} }
} }
} }
}
for (auto&&[movement, follow_target, transform] : _entity_manager.list<Movement_comp, Follow_target_comp, components::Transform_comp>()) {
void Enemy_system::do_cont_path_movement(Beat_state& beat) {
for(auto&& [movement, cont_path] : _entity_manager.list<Movement_comp, Continuous_path_comp>()) {
if(beat.beat) {
if(cont_path.wait_beats == 0 && !movement.move) {
float rad_direction = cont_path.next_direction() * mirrage::util::PI / 180.f;
movement.aim.x = std::sin(rad_direction);
movement.aim.y = -std::cos(rad_direction);
movement.move = true;
cont_path.wait_beats = cont_path.pause_between_steps;
} else {
cont_path.wait_beats--;
}
}
}
}
void Enemy_system::do_hunting_movement(Beat_state& beat) {
for(auto&& [movement, follow_target, transform] :
_entity_manager.list<Movement_comp, Follow_target_comp, components::Transform_comp>()) {
if(beat.beat) { if(beat.beat) {
if(follow_target.wait_beats == 0 && !movement.move) { if(follow_target.wait_beats == 0 && !movement.move) {
auto target_facet = _entity_manager.get(follow_target.target); auto target_facet = _entity_manager.get(follow_target.target);
if(target_facet.is_some()) { if(target_facet.is_some()) {
auto target_transform = auto target_transform = target_facet.get_or_throw().get<components::Transform_comp>();
target_facet.get_or_throw().get<components::Transform_comp>();
if(target_transform.is_some()) { if(target_transform.is_some()) {
auto& target_position = target_transform.get_or_throw().position; auto& target_position = target_transform.get_or_throw().position;
...@@ -63,45 +100,35 @@ namespace phase_shifter::gameplay { ...@@ -63,45 +100,35 @@ namespace phase_shifter::gameplay {
} }
} }
} }
}
for(auto&& [movement, cont_path] : _entity_manager.list<Movement_comp, Continuous_path_comp>()) { void Enemy_system::do_shooting(Beat_state& beat) {
if(beat.beat) { for(auto&& [shooting, transform] :
if(cont_path.wait_beats == 0 && !movement.move) { _entity_manager.list<Shooting_comp, components::Transform_comp>()) {
float rad_direction = cont_path.next_direction() * mirrage::util::PI / 180.f; auto my_position = transform.position;
movement.aim.x = std::sin(rad_direction);
movement.aim.y = -std::cos(rad_direction);
movement.move = true;
cont_path.wait_beats = cont_path.pause_between_steps;
} else {
cont_path.wait_beats--;
}
}
}
for (auto&& [shooting, transform] : _entity_manager.list<Shooting_comp, components::Transform_comp>()) {
auto my_position = transform.position;
glm::vec3 closest_target_pos; glm::vec3 closest_target_pos;
float closest_dist = 999999999.f; float closest_dist = 999999999.f;
for(auto&& [target, target_comp, target_transform] : for(auto&& [target, target_comp, target_transform] :
_entity_manager.list<Entity_facet, Target_comp, components::Transform_comp>()) { _entity_manager.list<Entity_facet, Target_comp, components::Transform_comp>()) {
auto target_position = target_transform.position; auto target_position = target_transform.position;
auto dist = glm::length(glm::vec2(target_position.x, target_position.z) auto dist = glm::length(glm::vec2(target_position.x, target_position.z)
- glm::vec2(my_position.x, my_position.z)); - glm::vec2(my_position.x, my_position.z));
if(dist < closest_dist) { if(dist < closest_dist) {
closest_dist = dist; closest_dist = dist;
closest_target_pos = target_position; closest_target_pos = target_position;
} }
} }
glm::vec2 t_direction(closest_target_pos.x - my_position.x, closest_target_pos.z - my_position.z); glm::vec2 t_direction(closest_target_pos.x - my_position.x, closest_target_pos.z - my_position.z);
shooting.target_direction = std::acos(-t_direction.y / glm::length(t_direction)) * 180.f / mirrage::util::PI; shooting.target_direction =
if (t_direction.x < 0) { std::acos(-t_direction.y / glm::length(t_direction)) * 180.f / mirrage::util::PI;
if(t_direction.x < 0) {
shooting.target_direction *= -1; shooting.target_direction *= -1;
} }
if (closest_dist <= shooting.attack_radius) { if(closest_dist <= shooting.attack_radius) {
shooting.idle = false; shooting.idle = false;
} else { } else {
shooting.idle = true; shooting.idle = true;
...@@ -113,7 +140,7 @@ namespace phase_shifter::gameplay { ...@@ -113,7 +140,7 @@ namespace phase_shifter::gameplay {
} }
auto rad_orientation = orientation * mirrage::util::PI / 180.f; auto rad_orientation = orientation * mirrage::util::PI / 180.f;
if (beat.beat) { if(beat.beat) {
if(shooting.idle && shooting.rotate) { if(shooting.idle && shooting.rotate) {
shooting.do_rotation(); shooting.do_rotation();
shooting.rotate = false; shooting.rotate = false;
...@@ -144,7 +171,7 @@ namespace phase_shifter::gameplay { ...@@ -144,7 +171,7 @@ namespace phase_shifter::gameplay {
} }
shooting.wait_beats = shooting.pause_between_shots; shooting.wait_beats = shooting.pause_between_shots;
shooting.rotate = true; shooting.rotate = true;
} else { } else {
shooting.wait_beats--; shooting.wait_beats--;
} }
...@@ -154,4 +181,20 @@ namespace phase_shifter::gameplay { ...@@ -154,4 +181,20 @@ namespace phase_shifter::gameplay {
{0, 0, 1}, glm::vec3(std::sin(rad_orientation), 0, -std::cos(rad_orientation))); {0, 0, 1}, glm::vec3(std::sin(rad_orientation), 0, -std::cos(rad_orientation)));
} }
} }
void Enemy_system::do_bullet_hit_detection()
{
for(auto&& [bullet_handle, bullet, bullet_transform, bullet_body] :
_entity_manager.list<Entity_handle, Bullet_comp, components::Transform_comp, Rigid_body_comp>()) {
for(auto&& [player_handle, player, player_transform, player_body] :
_entity_manager.list<Entity_handle, Player_comp, components::Transform_comp, Rigid_body_comp>()) {
glm::vec2 bullet_pos{bullet_transform.position.x, bullet_transform.position.z};
glm::vec2 player_pos{player_transform.position.x, player_transform.position.z};
if (glm::length(player_pos - bullet_pos) < bullet_body.radius + player_body.radius) {
_entity_manager.erase(bullet_handle);
_bus.send<Damaged_msg>(player_handle);
}
}
}
}
} }
\ No newline at end of file
...@@ -8,19 +8,29 @@ ...@@ -8,19 +8,29 @@
#include "continuous_path_comp.hpp" #include "continuous_path_comp.hpp"
#include "shooting_comp.hpp" #include "shooting_comp.hpp"
#include "target_comp.hpp" #include "target_comp.hpp"
#include "bullet_comp.hpp"
#include "beat_system.hpp"
namespace phase_shifter::gameplay { namespace phase_shifter::gameplay {
class Beat_system;
class Enemy_system { class Enemy_system {
public: public:
Enemy_system(mirrage::ecs::Entity_manager& entity_manager, const Beat_system& beat_system); Enemy_system(mirrage::util::Message_bus& bus,
mirrage::ecs::Entity_manager& entity_manager,
const Beat_system& beat_system);
void update(mirrage::util::Time dt); void update(mirrage::util::Time dt);
private: private:
mirrage::util::Message_bus& _bus;
mirrage::ecs::Entity_manager& _entity_manager; mirrage::ecs::Entity_manager& _entity_manager;
const Beat_system& _beat_system; const Beat_system& _beat_system;
void do_movement(Beat_state& beat);
void do_fixed_path_movement(Beat_state& beat);
void do_cont_path_movement(Beat_state& beat);
void do_hunting_movement(Beat_state& beat);
void do_shooting(Beat_state& beat);
void do_bullet_hit_detection();
}; };
} }
\ No newline at end of file
...@@ -42,7 +42,7 @@ namespace phase_shifter { ...@@ -42,7 +42,7 @@ namespace phase_shifter {
, _attachment_system(std::make_unique<helper::Attachment_system>(_entities)) , _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
, _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities, *_beat_system)) , _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities, *_beat_system))
, _camera_system(std::make_unique<gameplay::Camera_system>(_entities)) , _camera_system(std::make_unique<gameplay::Camera_system>(_entities))
, _enemy_system(std::make_unique<gameplay::Enemy_system>(_entities, *_beat_system)) , _enemy_system(std::make_unique<gameplay::Enemy_system>(engine.bus(), _entities, *_beat_system))
, _dash_system(std::make_unique<gameplay::Dash_system>(engine.bus(), _entities)) , _dash_system(std::make_unique<gameplay::Dash_system>(engine.bus(), _entities))
, _effect_system(std::make_unique<ui::Effect_system>(engine.bus(), _entities)) , _effect_system(std::make_unique<ui::Effect_system>(engine.bus(), _entities))
{ {
...@@ -115,10 +115,10 @@ namespace phase_shifter { ...@@ -115,10 +115,10 @@ namespace phase_shifter {
_input_system->update(dt); _input_system->update(dt);
_level_system->update(dt); _level_system->update(dt);
_dash_system->update(dt); _dash_system->update(dt);
_enemy_system->update(dt);
_movement_system->update(dt); _movement_system->update(dt);
_combat_system->update(dt); _combat_system->update(dt);
_camera_system->update(dt); _camera_system->update(dt);
_enemy_system->update(dt);
_attachment_system->update(dt); _attachment_system->update(dt);
......
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