enemy_system.cpp 2.71 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

		for (auto&& [shooting, transform] : _entity_manager.list<Shooting_comp, mirrage::ecs::components::Transform_comp>()) {
			if (beat.beat) {
				auto bullet_pattern = shooting.next_pattern();
				auto orientation    = shooting.orientation;

				for (auto bullet : bullet_pattern.bullets) {
					_entity_manager.entity_builder("bullet")
					        .position(transform.position)
					        .post_create([=](auto entity) {
						        entity.process([=](Continuous_path_comp& cont_path) {
							        cont_path.direction = orientation + bullet.direction;
									cont_path.curvature = bullet.curvature;
						        });
							})
					        .create();
				}
			}
		}
76
77
	}
}