camera_system.cpp 1.65 KB
Newer Older
1
2
3
4
5
6
#include "camera_system.hpp"

#include <mirrage/ecs/components/transform_comp.hpp>

namespace phase_shifter::gameplay {

Florian Oetke's avatar
Florian Oetke committed
7
8
	using mirrage::ecs::components::Transform_comp;

9
10
	Camera_system::Camera_system(mirrage::ecs::Entity_manager& entity_manager)
	  : _entity_manager(entity_manager)
11
12
13
14
15
16
	{
		_entity_manager.register_component_type<Viewtarget_comp>();
		_entity_manager.register_component_type<Spring_comp>();
	}

	void Camera_system::update(mirrage::util::Time dt)
17
	{
Florian Oetke's avatar
Florian Oetke committed
18
19
		for(auto&& [transform, viewtarget, spring] :
		    _entity_manager.list<Transform_comp, Viewtarget_comp, Spring_comp>()) {
20
			auto target_facet = _entity_manager.get(viewtarget.target);
21
22

			if(target_facet.is_some()) {
23
24
				auto target_transform =
				        target_facet.get_or_throw().get<mirrage::ecs::components::Transform_comp>();
25
26

				if(target_transform.is_some()) {
27
					auto      target_position  = target_transform.get_or_throw().position;
28
29
30
31
32
33
34
					glm::vec3 offset           = viewtarget.offset;
					glm::vec3 dest_pos         = {target_position.x + offset.x,
                                          target_position.y + offset.y,
                                          target_position.z + offset.z};
					float     damping_constant = 2 * sqrtf(spring.spring_constant);
					glm::vec3 damping_force    = -damping_constant * spring.velocity;

Florian Oetke's avatar
Florian Oetke committed
35
36
37
38
39
					glm::vec3 spring_force = spring.spring_constant * (dest_pos - transform.position);
					glm::vec3 acceleration = (spring_force + damping_force) / spring.mass;
					spring.velocity += dt.value() * acceleration;
					transform.position += dt.value() * spring.velocity;
					transform.direction(-offset);
40
41
42
43
				}
			}
		}
	}
44
} // namespace phase_shifter::gameplay