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

#include "movement_comp.hpp"
4
#include <mirrage/ecs/components/transform_comp.hpp>
5
6
7
8
9
10

namespace phase_shifter::gameplay {
	
	Enemy_system::Enemy_system(mirrage::ecs::Entity_manager& entity_manager) : _entity_manager(entity_manager)
	{
		_entity_manager.register_component_type<Fixed_path_comp>();
11
		_entity_manager.register_component_type<Follow_target_comp>();
12
		_entity_manager.register_component_type<Continuous_path_comp>();
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	}

	void Enemy_system::update(mirrage::util::Time dt)
	{
		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;
				}
			}
		}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
		
		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;
				}
			}
		}
42
43
44
45
46
47
48
49
50

		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;
			}
		}
51
52
	}
}