summaryrefslogtreecommitdiff
path: root/battles/src/BaseBattle.cpp
blob: 40889362daf3a01db2a55394efbd43f78aa988d7 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "BaseBattle.h"

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

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

#include "Builder.h"


namespace kurator
{
namespace battles
{


BaseBattle::BaseBattle(const Scenario& scenario) :
	_registry {},
	spawner {scenario.total_teams(), 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;
}


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


void
BaseBattle::update(const float dt)
{
	pick_random_targets();
	keep_at_range();
	floating_movement(dt);
	turrets(dt);
	kill_off_dead();
	manager.clear(_registry);  // registry supports on destructions events
}


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 + 1) % 2);  // FIXME
	}
}


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


void
BaseBattle::floating_movement(const float dt)
{
	auto view = _registry.view<Transform, FloatingMovement, AIState>();
	for (auto&& [entity, self, movement, ai] : view.each()) {
		const double speed = movement.speed * dt;
		const Point offset = ai.destination - self.position;
		if (offset.magnitude() > speed) {
			const Point move = offset.normalized().scale(speed);
			self.position.x += move.x;
			self.position.y += move.y;
		}
	}
}


void
BaseBattle::turrets(const float dt)
{
	auto view = _registry.view<TurretControl, universe::TurretType>();
	for (auto&& [entity, control, def] : view.each()) {
		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;
				_dispatcher.trigger(Hit{def.base_damage, entity, state.target});
			}
			control.reload = def.rate_of_fire;
		}
	}
}


void
BaseBattle::kill_off_dead()
{
	auto view = _registry.view<HitPoints>();
	for (auto&& [entity, points] : view.each()) {
		if (points.health <= 0.0)
			_registry.destroy(entity);
	}
}


}  // namespace battles
}  // namespace kurator