summaryrefslogtreecommitdiff
path: root/sim/src/systems.cpp
blob: c6f997abf06a4d89a0f178b0eefd785a04950b5d (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
#include "systems.h"

#include <kurator/sim/components.h>
#include <kurator/sim/events.h>
#include <kurator/sim/HitPoints.h>
#include <kurator/sim/State.h>
#include <kurator/stats/events.h>
#include <kurator/universe/UniqueIdentifier.h>


namespace kurator
{
namespace sim
{


void
pick_random_targets(State& ctx, TeamManager& manager)
{
	auto view = ctx.registry.view<Team, AIState>();
	for (auto&& [entity, team, ai] : view.each()) {
		if (!ctx.registry.valid(ai.target))
			ai.target = manager.random(team.id);
	}
}


void
keep_at_range(State& ctx)
{
	auto view = ctx.registry.view<Transform, AIState>();
	for (auto&& [entity, self, ai] : view.each()) {
		if (!ctx.registry.valid(ai.target))
			continue;
		const auto target = ctx.registry.get<Transform>(ai.target);
		const auto offset = target.position - self.position;
		ai.destination = target.position - offset.normalized().scale(ai.keep_at_range);
	}
}


void
kill_off_dead(State& ctx)
{
	auto view = ctx.registry.view<HitPoints>();
	for (auto&& [entity, points] : view.each()) {
		if (points.is_alive())
			continue;
		if (ctx.registry.all_of<universe::UniqueIdentifier, Team>(entity)) {
			const auto& [identifier, team] = ctx.registry.get<universe::UniqueIdentifier, Team>(entity);
			ctx.dispatcher.trigger(stats::ShipLeft{ctx.clock.game, identifier, team.id, true});
			ctx.dispatcher.trigger(Destroyed{entity});
		}
		ctx.registry.destroy(entity);
	}
}


}  // namespace sim
}  // namespace kurator