summaryrefslogtreecommitdiff
path: root/sim/src/BaseBattle.cpp
blob: 888379e97602b00e31ed96642ddf8e7569ee110a (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
81
82
83
84
85
86
87
88
89
90
91
92
#include "BaseBattle.h"

#include <algorithm>

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

#include <kurator/engine/Context.h>
#include <kurator/campaign/Scenario.h>
#include <kurator/sim/components.h>
#include <kurator/sim/FloatingMovement.h>
#include <kurator/sim/systems.h>
#include <kurator/sim/TurretControl.h>
#include <kurator/universe/UniqueIdentifier.h>

#include "Builder.h"
#include "RandomSpawner.h"


namespace kurator
{
namespace sim
{


static void setup_scenario(entt::registry& registry, const campaign::Scenario& scenario);


BaseBattle::BaseBattle(const campaign::Scenario& scenario) :
	_registry {}
{
	setup_scenario(_registry, scenario);
	manager.extend(_registry);
}


void
setup_scenario(entt::registry& registry, const campaign::Scenario& scenario)
{
	RandomSpawner spawner {scenario.last_team(), scenario.radius, 0.1};
	Builder build {registry, spawner};
	for (const auto& ship : scenario.ships) {
		const auto entity = build(ship.loadout.type, ship.team);
		registry.emplace<universe::UniqueIdentifier>(entity, ship.identifier);
		auto& state = registry.get<AIState>(entity);
		for (const auto& turret_type : ship.loadout.turrets) {
			build(turret_type, entity);
			state.keep_at_range = std::min(state.keep_at_range, turret_type.optimal_range);
		}
	}
}


entt::registry&
BaseBattle::registry()
{
	return _registry;
}


entt::dispatcher&
BaseBattle::dispatcher()
{
	return _dispatcher;
}


void
BaseBattle::update(engine::Context& ctx)
{
	pick_random_targets();
	keep_at_range(ctx);
	FloatingMovement::update(ctx);
	TurretControl::update(ctx);
	kill_off_dead(ctx);
	manager.update(ctx);
}


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


}  // namespace sim
}  // namespace kurator