summaryrefslogtreecommitdiff
path: root/sim/src/FloatingMovement.cpp
blob: e1794d580c41e85ec9b1cb0bc65502a00ce3dfd2 (plain)
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
#include <kurator/sim/FloatingMovement.h>

#include <entt/entity/registry.hpp>
#include <entt/signal/dispatcher.hpp>

#include <kurator/sim/components.h>


namespace kurator
{
namespace sim
{


void
FloatingMovement::update(entt::registry& registry, entt::dispatcher&, const float dt)
{
	auto view = registry.view<Transform, FloatingMovement, AIState>();
	for (auto&& [entity, transform, movement, ai] : view.each()) {
		const auto offset = ai.destination - transform.position;
		const auto at_destination = offset.magnitude() > movement.destination_boundary;
		const auto acceleration =
			at_destination ?
			offset.normalized().scale(movement.acceleration * dt) :
			offset.normalized().scale(-1 * movement.deceleration * dt);
		movement.speed.x += acceleration.x;
		movement.speed.y += acceleration.y;
		if (movement.speed.magnitude() > movement.max_speed)
			movement.speed = movement.speed.normalized().scale(movement.max_speed);
		const auto speed = movement.speed.scale(dt);
		transform.position.x += speed.x;
		transform.position.y += speed.y;
		transform.angle = speed.angle();
	}
}


}  // namespace sim
}  // namespace kurator