dash_system.cpp 3.92 KB
Newer Older
Kevin Balz's avatar
Dash    
Kevin Balz committed
1
2
3
4
5
#include "dash_system.hpp"
#include "dash_comp.hpp"
#include "killable_comp.hpp"
#include "movement_comp.hpp"

6
7
#include "../messages.hpp"

Kevin Balz's avatar
Dash    
Kevin Balz committed
8
9
10
11
12
13
#include <mirrage/ecs/components/transform_comp.hpp>
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/ecs/entity_set_view.hpp>

namespace phase_shifter::gameplay {

Florian Oetke's avatar
Florian Oetke committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	using namespace mirrage::util::unit_literals;

	namespace {
		[[maybe_unused]] float normalize_angle(float a)
		{
			if(a > mirrage::util::PI) {
				return a - 2 * mirrage::util::PI;
			} else if(a <= -mirrage::util::PI) {
				return a + 2 * mirrage::util::PI;
			} else {
				return a;
			}
		}
	} // namespace

Kevin Balz's avatar
Dash    
Kevin Balz committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	bool rect_circle_intersects(float rect_x,
	                            float rect_y,
	                            float rect_w,
	                            float rect_h,
	                            float circle_x,
	                            float circle_y,
	                            float radius)
	{
		float dist_x = std::abs(circle_x - rect_x - rect_w / 2);
		float dist_y = std::abs(circle_y - rect_y - rect_h / 2);

		if(dist_x > rect_w / 2 + radius) {
			return false;
		}
		if(dist_y > rect_h / 2 + radius) {
			return false;
		}

		if(dist_x <= rect_w / 2) {
			return true;
		}
		if(dist_y <= rect_h / 2) {
			return true;
		}

		float dx = dist_x - rect_w / 2;
		float dy = dist_y - rect_h / 2;
		return dx * dx + dy * dy <= radius * radius;
	}

	glm::vec2 rotate_point(glm::vec2 p, float angle)
	{
		return {p.x * std::cos(angle) - p.y * std::sin(angle), p.y * std::cos(angle) + p.x * std::sin(angle)};
	}


	using mirrage::ecs::Entity_handle;
	using mirrage::ecs::components::Transform_comp;

68
69
	Dash_system::Dash_system(mirrage::util::Message_bus& bus, mirrage::ecs::Entity_manager& ecs)
	  : _bus(bus), _ecs(ecs)
Kevin Balz's avatar
Dash    
Kevin Balz committed
70
71
72
73
74
75
76
77
78
	{
		_ecs.register_component_type<Dash_comp>();
		_ecs.register_component_type<Killable_comp>();
	}

	void Dash_system::update(mirrage::util::Time dt)
	{

		for(auto&& [transform, move, dash] : _ecs.list<Transform_comp, Movement_comp, Dash_comp>()) {
Kevin Balz's avatar
Kevin Balz committed
79
			if(dash.dash || move.step_time_left > 0) {
Kevin Balz's avatar
Dash    
Kevin Balz committed
80
				//dash
Kevin Balz's avatar
Kevin Balz committed
81
				std::cout << "dash" << std::endl;
Kevin Balz's avatar
Dash    
Kevin Balz committed
82
				glm::vec2 position(transform.position.x, transform.position.z);
Kevin Balz's avatar
Kevin Balz committed
83
				auto      movement = (position - move.last_position) * 1.1f;
Kevin Balz's avatar
Dash    
Kevin Balz committed
84
85
86
87
88
				auto      angle    = std::atan2(movement.y, movement.x);
				auto      width    = glm::length(movement);
				auto      height   = dash.attack_width;

				auto center = rotate_point(
Kevin Balz's avatar
Kevin Balz committed
89
				        {move.last_position.x + movement.x / 2, move.last_position.y + movement.y / 2},
Kevin Balz's avatar
Dash    
Kevin Balz committed
90
91
92
93
94
				        angle);
				glm::vec2 topleft(center.x - width / 2, center.y + height / 2);

				for(auto&& [entity, k_transform, kill] :
				    _ecs.list<Entity_handle, Transform_comp, Killable_comp>()) {
Kevin Balz's avatar
Kevin Balz committed
95
96
97
					if (kill.only_stationary) {
						continue;
					}
Kevin Balz's avatar
Dash    
Kevin Balz committed
98
					auto circle = rotate_point({k_transform.position.x, k_transform.position.z}, angle);
Kevin Balz's avatar
Kevin Balz committed
99
					if(rect_circle_intersects(topleft.x, topleft.y, width, height, circle.x, circle.y, kill.radius)) {
Florian Oetke's avatar
Florian Oetke committed
100
101
						// calc attack angle
						auto target_dir = glm::vec2(k_transform.position.x, k_transform.position.z)
Kevin Balz's avatar
Kevin Balz committed
102
103
						                  - move.last_position;
						auto attacker_angle = std::atan2(move.last_position.y, move.last_position.x);
Florian Oetke's avatar
Florian Oetke committed
104
						auto attack_angle   = normalize_angle(std::atan2(target_dir.y, target_dir.x)
Kevin Balz's avatar
Kevin Balz committed
105
                                                            - std::atan2(move.last_position.y, move.last_position.x));
Florian Oetke's avatar
Florian Oetke committed
106
107
108
109
110
111
112
113
114
115

						if(attack_angle < (-15_deg).value())
							attack_angle = (-45_deg).value();
						else if(attack_angle > (15_deg).value())
							attack_angle = (45_deg).value();
						else
							attack_angle = 0;

						auto attack_direction = rotate_point({1, 0}, attack_angle + attacker_angle);

116
						_bus.send<Enemy_killed_msg>(k_transform.position,
Florian Oetke's avatar
Florian Oetke committed
117
						                            glm::vec3(attack_direction.x, 0.f, attack_direction.y));
Kevin Balz's avatar
Dash    
Kevin Balz committed
118
119
120
121
						_ecs.erase(entity);
					}
				}

Kevin Balz's avatar
Kevin Balz committed
122
				dash.dash          = move.step_time_left > 0;
Kevin Balz's avatar
Dash    
Kevin Balz committed
123
124
125
126
127
			} else {
				dash.dash = false;
			}
		}
	}
Florian Oetke's avatar
Florian Oetke committed
128
} // namespace phase_shifter::gameplay