Commit 4048dcbd authored by Tim Scheiber's avatar Tim Scheiber
Browse files

slight optimization

parent 6df9bb59
Pipeline #3375 failed with stage
in 2 minutes and 27 seconds
......@@ -30,6 +30,7 @@ namespace phase_shifter::gameplay {
{
auto beat = _beat_system.beat_state();
despawn_distant_bullets();
do_movement(beat);
do_shooting(beat);
do_bullet_hit_detection();
......@@ -110,86 +111,98 @@ namespace phase_shifter::gameplay {
{
for(auto&& [entity, shooting, transform] :
_entity_manager.list<Entity_facet, Shooting_comp, components::Transform_comp>()) {
auto my_position = transform.position;
glm::vec3 closest_target_pos(0.f);
float closest_dist = 999999999.f;
for(auto&& [target, target_comp, target_transform] :
_entity_manager.list<Entity_facet, Target_comp, components::Transform_comp>()) {
auto target_position = target_transform.position;
auto dist = glm::length(glm::vec2(target_position.x, target_position.z)
- glm::vec2(my_position.x, my_position.z));
if(dist < closest_dist) {
closest_dist = dist;
closest_target_pos = target_position;
}
}
auto my_position = transform.position;
for(auto&& [player, player_transform] :
_entity_manager.list<Player_comp, components::Transform_comp>()) {
if(glm::length(glm::vec2{player_transform.position.x, player_transform.position.z}
- glm::vec2{my_position.x, my_position.z}) <= trigger_distance_shooting) {
glm::vec3 closest_target_pos(0.f);
float closest_dist = 999999999.f;
for(auto&& [target, target_comp, target_transform] :
_entity_manager.list<Entity_facet, Target_comp, components::Transform_comp>()) {
auto target_position = target_transform.position;
auto dist = glm::length(glm::vec2(target_position.x, target_position.z)
- glm::vec2(my_position.x, my_position.z));
if(dist < closest_dist) {
closest_dist = dist;
closest_target_pos = target_position;
}
}
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 *= -1;
}
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 *= -1;
}
if(closest_dist <= shooting.attack_radius) {
shooting.idle = false;
} else {
shooting.idle = true;
}
if(closest_dist <= shooting.attack_radius) {
shooting.idle = false;
} else {
shooting.idle = true;
}
auto orientation = shooting.target_direction;
if(shooting.idle) {
orientation = shooting.default_orientation + shooting.rotation;
}
auto rad_orientation = orientation * mirrage::util::PI / 180.f;
auto orientation = shooting.target_direction;
if(shooting.idle) {
orientation = shooting.default_orientation + shooting.rotation;
}
auto rad_orientation = orientation * mirrage::util::PI / 180.f;
if(beat.beat) {
if(shooting.idle && shooting.rotate) {
shooting.do_rotation();
shooting.rotate = false;
}
if(shooting.wait_beats == 0) {
auto bullet_pattern = shooting.next_pattern();
auto spawn_position =
transform.position
+ shooting.spawn_offset
* glm::vec3(std::sin(rad_orientation), 0, -std::cos(rad_orientation));
for(auto bullet : bullet_pattern.bullets) {
auto bullet_direction = orientation + bullet.direction;
auto rad_bullet_direction = bullet_direction * mirrage::util::PI / 180.f;
_entity_manager.entity_builder("bullet")
.position(spawn_position)
.rotation(glm::rotation({0, 0, 1},
glm::vec3(std::sin(rad_bullet_direction),
0,
-std::cos(rad_bullet_direction))))
.post_create([=](auto entity) {
entity.process([=](Continuous_path_comp& cont_path) {
cont_path.direction = bullet_direction;
cont_path.curvature = bullet.curvature;
});
entity.process([=](Movement_comp& move, Continuous_path_comp& cont_path) {
float rad_direction = cont_path.direction * mirrage::util::PI / 180.f;
move.aim.x = std::sin(rad_direction);
move.aim.y = -std::cos(rad_direction);
});
})
.create();
if(beat.beat) {
if(shooting.idle && shooting.rotate) {
shooting.do_rotation();
shooting.rotate = false;
}
if(shooting.wait_beats == 0) {
auto bullet_pattern = shooting.next_pattern();
auto spawn_position = transform.position
+ shooting.spawn_offset
* glm::vec3(std::sin(rad_orientation),
0,
-std::cos(rad_orientation));
for(auto bullet : bullet_pattern.bullets) {
auto bullet_direction = orientation + bullet.direction;
auto rad_bullet_direction = bullet_direction * mirrage::util::PI / 180.f;
_entity_manager.entity_builder("bullet")
.position(spawn_position)
.rotation(glm::rotation({0, 0, 1},
glm::vec3(std::sin(rad_bullet_direction),
0,
-std::cos(rad_bullet_direction))))
.post_create([=](auto entity) {
entity.process([=](Continuous_path_comp& cont_path) {
cont_path.direction = bullet_direction;
cont_path.curvature = bullet.curvature;
});
entity.process([=](Movement_comp& move,
Continuous_path_comp& cont_path) {
float rad_direction =
cont_path.direction * mirrage::util::PI / 180.f;
move.aim.x = std::sin(rad_direction);
move.aim.y = -std::cos(rad_direction);
});
})
.create();
}
shooting.wait_beats = shooting.pause_between_shots;
if(shooting.rotation_per_step < 0.0001f) {
shooting.rotate = true;
}
} else {
shooting.wait_beats--;
}
}
shooting.wait_beats = shooting.pause_between_shots;
shooting.rotate = true;
} else {
shooting.wait_beats--;
transform.orientation = glm::rotation(
{0, 0, 1}, glm::vec3(std::sin(rad_orientation), 0, -std::cos(rad_orientation)));
entity.process([&](Movement_comp& movement) { movement.want_orientation = false; });
}
}
transform.orientation = glm::rotation(
{0, 0, 1}, glm::vec3(std::sin(rad_orientation), 0, -std::cos(rad_orientation)));
entity.process([&](Movement_comp& movement) { movement.want_orientation = false; });
}
}
......@@ -226,6 +239,25 @@ namespace phase_shifter::gameplay {
}
}
void Enemy_system::despawn_distant_bullets()
{
for(auto&& [bullet_handle, bullet, bullet_transform] :
_entity_manager.list<Entity_handle, Bullet_comp, components::Transform_comp>()) {
bool despawn_bullet = true;
for (auto&& [player, player_transform] :
_entity_manager.list<Player_comp, components::Transform_comp>()) {
if(glm::length(glm::vec2{bullet_transform.position.x, bullet_transform.position.z}
- glm::vec2{player_transform.position.x, player_transform.position.z})
< trigger_distance_despawn_bullets) {
despawn_bullet = false;
}
}
if (despawn_bullet) {
_entity_manager.erase(bullet_handle);
}
}
}
bool Enemy_system::intersect(const Stadium& stad1, const Stadium& stad2)
{
glm::vec2 direction1 = stad1.point2 - stad1.point1;
......
......@@ -32,12 +32,16 @@ namespace phase_shifter::gameplay {
mirrage::ecs::Entity_manager& _entity_manager;
const Beat_system& _beat_system;
const float trigger_distance_shooting = 20.f; // radius around players the turrets have to be in in order to shoot
const float trigger_distance_despawn_bullets = 30.f; // radius around players the bullets have to leave in order to despawn
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();
void despawn_distant_bullets();
bool intersect(const Stadium& stad1, const Stadium& stad2);
float dot(const glm::vec2& a, const glm::vec2& b);
......
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