summaryrefslogtreecommitdiff
path: root/battles/src/Battle.cpp
blob: 99ed07605a93b65bed2db899e86a8233a342da8d (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
#include <kurator/battles/Battle.h>

#include <memory>
#include <random>

#include <entt/entity/registry.hpp>

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


namespace kurator
{
namespace battles
{


class BaseBattle : public Battle
{
public:
	BaseBattle(const Scenario& scenario);
	entt::registry& registry() override;
	void update(float dt) override;
private:
	entt::registry _registry;
};


BaseBattle::BaseBattle(const Scenario& scenario)
{
	std::random_device dev;
	std::uniform_real_distribution<> pos{-2.5, 2.5};
	for (const auto& ship : scenario.ships) {
		const auto entity = _registry.create();
		_registry.emplace<universe::ShipType>(entity, ship.type);
		_registry.emplace<Team>(entity, ship.team);
		_registry.emplace<Transform>(entity, Point{pos(dev), pos(dev)}, Point{0.0, 0.0});
	}
}


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


void
BaseBattle::update(const float dt)
{
	auto view = _registry.view<Transform, Team>();
	for (auto&& [entity, transform, team] : view.each())
		transform.position.x += 0.1 * dt * (team.id * 2 - 1);
}


std::unique_ptr<Battle>
prepare(const Scenario& scenario)
{
	return std::make_unique<BaseBattle>(scenario);
}


}  // namespace battles
}  // namespace kurator