summaryrefslogtreecommitdiff
path: root/battles/src/BaseBattle.cpp
blob: fc4aa69a645b31caac722db62aa5734df44b8db6 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "BaseBattle.h"

#include <memory>
#include <type_traits>

#include <entt/entity/registry.hpp>

#include <kurator/battles/components.h>
#include <kurator/battles/Scenario.h>
#include <kurator/universe.h>

#include "Builder.h"


namespace kurator
{
namespace battles
{


int total_teams_in(const Scenario& scenario);


BaseBattle::BaseBattle(const Scenario& scenario) :
	_registry {},
	spawner {total_teams_in(scenario), 2.5, 0.1}
{
	const auto repo = universe::load_sample();
	Builder build {_registry, spawner};
	for (const auto& ship : scenario.ships) {
		const auto entity = build(repo->ship_type(ship.type), ship.team);
		for (const auto& turret_type : ship.turrets)
			build(repo->turret_type(turret_type), entity);
		manager.add(ship.team, entity);  // registry supports on construction events
	}
}


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


void
BaseBattle::update(const float dt)
{
	auto view = _registry.view<Team, Transform, AIState, FloatingMovement>();
	for (auto&& [entity, team, transform, state, movement] : view.each()) {
		if (!_registry.valid(state.target))
			state.target = manager.random((team.id + 1) % 2);
		if (!_registry.valid(state.target))
			continue;
		const auto target = _registry.get<Transform>(state.target);
		const double speed = movement.speed * dt;
		const Point diff = target.position - transform.position;
		const Point dest = target.position - diff.normalized();
		const Point move = dest - transform.position;
		if (move.magnitude() > speed) {
			const Point eff = move.normalized().scale(speed);
			transform.position.x += eff.x;
			transform.position.y += eff.y;
		}
	}
	auto view2 = _registry.view<HitPoints>();
	for (auto&& [entity, points] : view2.each()) {
		if (points.health <= 0.0)
			_registry.destroy(entity);
	}
	auto view3 = _registry.view<TurretControl, universe::TurretType>();
	for (auto&& [entity, control, def] : view3.each()) {  // split into systems!
		if (!_registry.valid(control.owner)) {
			_registry.destroy(entity);
			continue;
		}
		const auto& [state, transform] = _registry.get<AIState, Transform>(control.owner);  // no checks
		if (!_registry.valid(state.target))
			continue;
		if (control.reload > 0.0)
			control.reload -= dt;
		if (control.reload <= 0.0) {
			auto& target_points = _registry.get<HitPoints>(state.target);
			const auto& target = _registry.get<Transform>(state.target);
			const auto distance = transform.position - target.position;
			if (def.range > distance.magnitude())
				target_points.health -= def.base_damage;
			control.reload = def.rate_of_fire;
		}
	}
	manager.clear(_registry);  // registry supports on destructions events
}


int
total_teams_in(const Scenario& scenario)
{
	int last_team = 0;
	for (const auto& ship : scenario.ships) {
		if (ship.team > last_team)
			last_team = ship.team;
	}
	return last_team + 1;
}


}  // namespace battles
}  // namespace kurator