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

#include <kurator/engine/Context.h>
#include <kurator/sim/components.h>


namespace kurator
{
namespace sim
{


void
FloatingMovement::update(engine::Context& ctx)
{
	auto view = ctx.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 * ctx.clock.dt) :
			offset.normalized().scale(-1 * movement.deceleration * ctx.clock.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(ctx.clock.dt);
		transform.position.x += speed.x;
		transform.position.y += speed.y;
		transform.angle = speed.angle();
	}
}


}  // namespace sim
}  // namespace kurator