dash_system.cpp 4.12 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	{
		_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>()) {
			if((dash.dash || dash.was_move) && move.step_time_left > 0) {
				//dash
				glm::vec2 position(transform.position.x, transform.position.z);
				auto      movement = position - dash.last_position;
				auto      angle    = std::atan2(movement.y, movement.x);
				auto      width    = glm::length(movement);
				auto      height   = dash.attack_width;

				auto center = rotate_point(
				        {dash.last_position.x + movement.x / 2, dash.last_position.y + movement.y / 2},
				        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
94
95
96
					if (kill.only_stationary) {
						continue;
					}
Kevin Balz's avatar
Dash    
Kevin Balz committed
97
					auto circle = rotate_point({k_transform.position.x, k_transform.position.z}, angle);
Kevin Balz's avatar
Kevin Balz committed
98
					if(rect_circle_intersects(topleft.x, topleft.y, width, height, circle.x, circle.y, kill.radius)) {
Florian Oetke's avatar
Florian Oetke committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
						// calc attack angle
						auto target_dir = glm::vec2(k_transform.position.x, k_transform.position.z)
						                  - dash.last_position;
						auto attacker_angle = std::atan2(move.last_step.y, move.last_step.x);
						auto attack_angle   = normalize_angle(std::atan2(target_dir.y, target_dir.x)
                                                            - std::atan2(move.last_step.y, move.last_step.x));

						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);

115
						_bus.send<Enemy_killed_msg>(k_transform.position,
Florian Oetke's avatar
Florian Oetke committed
116
						                            glm::vec3(attack_direction.x, 0.f, attack_direction.y));
Kevin Balz's avatar
Dash    
Kevin Balz committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
						_ecs.erase(entity);
					}
				}

				dash.last_position = position;
				dash.dash          = true;
			} else {
				dash.dash = false;
			}

			if(move.move) {
				if(!dash.was_move || move.step_time_left <= 0) {
					dash.was_move      = true;
					dash.last_position = {transform.position.x, transform.position.z};
				}
			} else {
				dash.was_move = false;
			}
		}
	}
Florian Oetke's avatar
Florian Oetke committed
137
} // namespace phase_shifter::gameplay