enemy_system.cpp 4.12 KB
Newer Older
1
2
#include "enemy_system.hpp"

3
#include "beat_system.hpp"
4
#include "movement_comp.hpp"
5

6
#include <mirrage/ecs/components/transform_comp.hpp>
7
8
9

namespace phase_shifter::gameplay {
	
10
11
	Enemy_system::Enemy_system(mirrage::ecs::Entity_manager& entity_manager, const Beat_system& beat_system)
	  : _entity_manager(entity_manager), _beat_system(beat_system)
12
13
	{
		_entity_manager.register_component_type<Fixed_path_comp>();
14
		_entity_manager.register_component_type<Follow_target_comp>();
15
		_entity_manager.register_component_type<Continuous_path_comp>();
16
		_entity_manager.register_component_type<Shooting_comp>();
17
18
19
20
	}

	void Enemy_system::update(mirrage::util::Time dt)
	{
21
22
		auto beat = _beat_system.beat_state();

23
24
25
26
27
28
29
30
31
32
		for (auto&& [movement, fixed_path] : _entity_manager.list<Movement_comp, Fixed_path_comp>()) {
			if (!movement.move) {
				float rad_direction = fixed_path.next_direction() * mirrage::util::PI / 180.f;
				if (rad_direction >= 0) {
					movement.aim.x      = std::sin(rad_direction);
					movement.aim.y      = -std::cos(rad_direction);
					movement.move       = true;
				}
			}
		}
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		
		for (auto&&[movement, follow_target, transform] : _entity_manager.list<Movement_comp, Follow_target_comp, mirrage::ecs::components::Transform_comp>()) {
			auto target_facet = _entity_manager.get(follow_target.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;
					movement.aim = glm::vec2({target_position.x - my_position.x, target_position.z - my_position.z});
					movement.move = true;
				}
			}
		}
48
49
50
51
52
53
54
55
56

		for(auto&& [movement, cont_path] : _entity_manager.list<Movement_comp, Continuous_path_comp>()) {
			if(!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;
			}
		}
57
58

		for (auto&& [shooting, transform] : _entity_manager.list<Shooting_comp, mirrage::ecs::components::Transform_comp>()) {
Tim Scheiber's avatar
Tim Scheiber committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
			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;
					}
				}
			}

79
			if (beat.beat) {
Tim Scheiber's avatar
Tim Scheiber committed
80
81
82
83
84
85
86
87
88
89
90
				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));
91
92

				for (auto bullet : bullet_pattern.bullets) {
Tim Scheiber's avatar
Tim Scheiber committed
93
94
					auto bullet_direction = orientation + bullet.direction;
					auto rad_bullet_direction = bullet_direction * mirrage::util::PI / 180.f;
95
					_entity_manager.entity_builder("bullet")
Tim Scheiber's avatar
Tim Scheiber committed
96
97
98
99
					        .position(spawn_position)
					        .rotation(glm::rotation(
					                {0, 0, 1},
					                glm::vec3(std::sin(rad_bullet_direction), 0, -std::cos(rad_bullet_direction))))
100
101
					        .post_create([=](auto entity) {
						        entity.process([=](Continuous_path_comp& cont_path) {
Tim Scheiber's avatar
Tim Scheiber committed
102
							        cont_path.direction = bullet_direction;
103
104
105
106
107
108
109
									cont_path.curvature = bullet.curvature;
						        });
							})
					        .create();
				}
			}
		}
110
111
	}
}