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

Rotating turrets

parent dbd9f338
Pipeline #3291 passed with stage
in 5 minutes and 3 seconds
......@@ -20,7 +20,10 @@
"FixedPath": {
},
"Shooting": {
"orientation": 270
"default_orientation": 270,
"rotation_per_step": 5,
"max_rotation": 10,
"spawn_offset": 1
},
"Killable": {
}
......
......@@ -56,16 +56,50 @@ namespace phase_shifter::gameplay {
}
for (auto&& [shooting, transform] : _entity_manager.list<Shooting_comp, mirrage::ecs::components::Transform_comp>()) {
auto target_facet = _entity_manager.get(shooting.target);
if(target_facet.is_some()) {
auto target_transform =
target_facet.get_or_throw().get<mirrage::ecs::components::Transform_comp>();
if(target_transform.is_some()) {
auto& target_position = target_transform.get_or_throw().position;
auto& my_position = transform.position;
auto dist = glm::length(glm::vec2(target_position.x, target_position.z)
- glm::vec2(my_position.x, my_position.z));
shooting.target_direction = std::asin((target_position.x - my_position.x) / dist);
if (dist <= shooting.attack_radius) {
shooting.idle = false;
} else {
shooting.idle = true;
}
}
}
if (beat.beat) {
auto bullet_pattern = shooting.next_pattern();
auto orientation = shooting.orientation;
auto bullet_pattern = shooting.next_pattern();
auto orientation = shooting.target_direction;
if (shooting.idle) {
orientation = shooting.default_orientation + shooting.rotation;
shooting.rotate();
}
auto rad_orientation = orientation * mirrage::util::PI / 180.f;
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(transform.position)
.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 = orientation + bullet.direction;
cont_path.direction = bullet_direction;
cont_path.curvature = bullet.curvature;
});
})
......
......@@ -10,4 +10,12 @@ namespace phase_shifter::gameplay {
i_next_pattern %= patterns.size();
return pattern;
}
void Shooting_comp::rotate() {
rotation += rotation_per_step;
if (glm::abs(rotation) > max_rotation) {
rotation_per_step *= -1;
rotation += 2 * rotation_per_step;
}
}
}
\ No newline at end of file
......@@ -10,7 +10,6 @@ namespace phase_shifter::gameplay {
struct Bullet {
Bullet() {}
Bullet(float direction, float curvature) : direction(direction), curvature(curvature) {}
Bullet(float attributes[2]) : direction(attributes[0]), curvature(attributes[1]) {}
float direction = 0;
float curvature = 0;
......@@ -28,15 +27,24 @@ namespace phase_shifter::gameplay {
using Component::Component;
Bulletpattern next_pattern();
void rotate();
float orientation = 0; // orientation of "forward" in degrees with 0 beeing North (-Z)
float default_orientation = 0; // default orientation of "forward" in degrees with 0° beeing North (-Z)
float rotation_per_step = 0; // rotation the turret does per beat in degrees during idle state
float max_rotation = 0; // max rotation the turret does in degrees during idle before reversing rotation direction
float spawn_offset = 0; // offset of the bullet spawn point in meters
float attack_radius = 0; // radius in which the turret targets the target
mirrage::ecs::Entity_handle target; // the target the turret targets while not idle
float target_direction = 0; // angular direction towards the target in degrees with 0° beeing North (-Z)
std::vector<Bulletpattern> patterns; // sequence of bullet patterns that are looped through
float rotation = 0; // current rotation from default_orientation in degrees
bool idle = true; // if false, the turret targets the target
private:
unsigned int i_next_pattern = 0;
};
sf2_structDef(Shooting_comp, orientation);
sf2_structDef(Shooting_comp, default_orientation, rotation_per_step, max_rotation, spawn_offset, attack_radius);
}
\ No newline at end of file
......@@ -106,8 +106,7 @@ namespace phase_shifter {
.post_create([=](auto entity) {
entity.process([&](gameplay::Fixed_path_comp& fixed_path) { fixed_path.update_path({}); });
entity.process([&](gameplay::Shooting_comp& shooting) {
shooting.patterns.push_back(gameplay::Bulletpattern({{0, 0}, {30, 0}, {-30, 0}}));
shooting.patterns.push_back(gameplay::Bulletpattern({{15, 0}, {-15, 0}}));
shooting.patterns.push_back(gameplay::Bulletpattern({{0, 0}}));
});
})
.create();
......
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