summaryrefslogtreecommitdiff
path: root/sim/src/weapons.cpp
blob: 28fc6fa245186f31425a90c9ec863c1cc2ae6f5a (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
#include <kurator/sim/weapons.h>

#include <entt/entt.hpp>

#include <kurator/sim/events.h>
#include <kurator/sim/FloatingMovement.h>
#include <kurator/sim/HitPoints.h>
#include <kurator/sim/State.h>
#include <kurator/universe/TurretType.h>


namespace kurator
{
namespace sim
{


static bool consume(float& dt, double& target);
static bool reload_(float& dt, Turret& turret);


void
Turret::shoot_at(State& ctx, const entt::entity& target, const double distance)
{
	while (reload_(action, *this)) {
		auto& target_points = ctx.registry.get<HitPoints>(target);
		const auto& movement = ctx.registry.get<FloatingMovement>(target);
		auto damage = type.effective_damage(distance, movement.speed.magnitude());
		if (damage > 0.0) {
			damage = target_points.deal(damage);
			ctx.dispatcher.trigger(Hit{damage, owner, target});
		}
		delay = type.rate_of_fire;
		if (--rounds < 1)
			reload = type.reload;
	}
}


void
update_turrets(State& ctx)
{
	auto turrets = ctx.registry.view<Turret>();
	for (auto&& [entity, turret] : turrets.each()) {
		if (!ctx.registry.valid(turret.owner)) {
			ctx.registry.destroy(entity);
			continue;
		}
		while (turret.action > 0.0) {
			if (reload_(turret.action, turret))
				break;
		}
		turret.action = ctx.clock.dt;
	}
}


bool
consume(float& dt, double& target)
{
	if (target <= 0.0)
		return true;
	const auto _dt = dt;
	dt -= target;
	target -= _dt;
	return target <= 0.0;
}


bool
reload_(float& dt, Turret& turret)  // at this point move it to the class?
{
	if (turret.rounds < 1 && consume(dt, turret.reload))
		turret.rounds = turret.type.rounds;
	return turret.rounds > 0 && consume(dt, turret.delay);
}


}  // namespace sim
}  // namespace kurator