dash_system.cpp 3.03 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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <mirrage/ecs/components/transform_comp.hpp>
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/ecs/entity_set_view.hpp>

namespace phase_shifter::gameplay {

	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;

53
54
	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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
	{
		_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>()) {
					auto circle = rotate_point({k_transform.position.x, k_transform.position.z}, angle);
Florian Oetke's avatar
Florian Oetke committed
80
					if(rect_circle_intersects(topleft.x, topleft.y, width, height, circle.x, circle.y, 1)) {
81
82
						_bus.send<Enemy_killed_msg>(k_transform.position,
						                            k_transform.position - transform.position);
Kevin Balz's avatar
Dash    
Kevin Balz committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
						_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
103
} // namespace phase_shifter::gameplay