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 @@
},
"Rigid_body": {
"radius": 0.1
}
},
"Bullet": {}
}
......@@ -41,7 +41,7 @@ namespace phase_shifter::gameplay {
_beat_index++;
LOG(plog::debug) << "beat";
beats_left = size - 1 - _beat_index - _beat_offset;
beats_left = size - 1 - _beat_index + _beat_offset;
if(beats_left <= 0) {
_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 "beat_system.hpp"
#include "combat_system.hpp"
#include "movement_comp.hpp"
#include "rigid_body_comp.hpp"
#include "player_comp.hpp"
#include <mirrage/ecs/components/transform_comp.hpp>
......@@ -9,21 +13,36 @@ namespace phase_shifter::gameplay {
using namespace mirrage::ecs;
Enemy_system::Enemy_system(Entity_manager& entity_manager, const Beat_system& beat_system)
: _entity_manager(entity_manager), _beat_system(beat_system)
Enemy_system::Enemy_system(mirrage::util::Message_bus& bus,
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<Follow_target_comp>();
_entity_manager.register_component_type<Continuous_path_comp>();
_entity_manager.register_component_type<Shooting_comp>();
_entity_manager.register_component_type<Target_comp>();
_entity_manager.register_component_type<Bullet_comp>();
}
void Enemy_system::update(mirrage::util::Time dt)
{
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(fixed_path.wait_beats == 0 && !movement.move) {
float direction = fixed_path.next_direction();
......@@ -39,15 +58,33 @@ namespace phase_shifter::gameplay {
}
}
}
}
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--;
}
}
}
}
for (auto&&[movement, follow_target, transform] : _entity_manager.list<Movement_comp, Follow_target_comp, components::Transform_comp>()) {
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(follow_target.wait_beats == 0 && !movement.move) {
auto target_facet = _entity_manager.get(follow_target.target);
if(target_facet.is_some()) {
auto target_transform =
target_facet.get_or_throw().get<components::Transform_comp>();
auto target_transform = target_facet.get_or_throw().get<components::Transform_comp>();
if(target_transform.is_some()) {
auto& target_position = target_transform.get_or_throw().position;
......@@ -63,22 +100,11 @@ namespace phase_shifter::gameplay {
}
}
}
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--;
}
}
}
for (auto&& [shooting, transform] : _entity_manager.list<Shooting_comp, components::Transform_comp>()) {
void Enemy_system::do_shooting(Beat_state& beat) {
for(auto&& [shooting, transform] :
_entity_manager.list<Shooting_comp, components::Transform_comp>()) {
auto my_position = transform.position;
glm::vec3 closest_target_pos;
float closest_dist = 999999999.f;
......@@ -96,12 +122,13 @@ namespace phase_shifter::gameplay {
}
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;
if (t_direction.x < 0) {
shooting.target_direction =
std::acos(-t_direction.y / glm::length(t_direction)) * 180.f / mirrage::util::PI;
if(t_direction.x < 0) {
shooting.target_direction *= -1;
}
if (closest_dist <= shooting.attack_radius) {
if(closest_dist <= shooting.attack_radius) {
shooting.idle = false;
} else {
shooting.idle = true;
......@@ -113,7 +140,7 @@ namespace phase_shifter::gameplay {
}
auto rad_orientation = orientation * mirrage::util::PI / 180.f;
if (beat.beat) {
if(beat.beat) {
if(shooting.idle && shooting.rotate) {
shooting.do_rotation();
shooting.rotate = false;
......@@ -154,4 +181,20 @@ namespace phase_shifter::gameplay {
{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 @@
#include "continuous_path_comp.hpp"
#include "shooting_comp.hpp"
#include "target_comp.hpp"
#include "bullet_comp.hpp"
namespace phase_shifter::gameplay {
#include "beat_system.hpp"
class Beat_system;
namespace phase_shifter::gameplay {
class Enemy_system {
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);
private:
mirrage::util::Message_bus& _bus;
mirrage::ecs::Entity_manager& _entity_manager;
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 {
, _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
, _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities, *_beat_system))
, _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))
, _effect_system(std::make_unique<ui::Effect_system>(engine.bus(), _entities))
{
......@@ -115,10 +115,10 @@ namespace phase_shifter {
_input_system->update(dt);
_level_system->update(dt);
_dash_system->update(dt);
_enemy_system->update(dt);
_movement_system->update(dt);
_combat_system->update(dt);
_camera_system->update(dt);
_enemy_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