camera_system.cpp 1.66 KB
Newer Older
1
2
3
4
5
6
7
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
#include "camera_system.hpp"

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

namespace phase_shifter::gameplay {

	Camera_system::Camera_system(mirrage::ecs::Entity_manager& entity_manager) 
		: _entity_manager(entity_manager)
	{
		_entity_manager.register_component_type<Viewtarget_comp>();
		_entity_manager.register_component_type<Spring_comp>();
	}

	void Camera_system::update(mirrage::util::Time dt)
	{ 
		for (auto&& [camera, viewtarget, spring] : _entity_manager.list<mirrage::ecs::Entity_facet, Viewtarget_comp, Spring_comp>()) {
			auto& target_facet = _entity_manager.get(viewtarget.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;
					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;

					camera.process([&](mirrage::ecs::components::Transform_comp& transform) {
						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);
					});
				}
			}
		}
	}
}