summaryrefslogtreecommitdiff
path: root/battles/src/TeamManager.cpp
blob: 9dc90e3cca0631e3316a4dca3fc5e564a2be3cff (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
#include "TeamManager.h"

#include <algorithm>
#include <random>
#include <utility>
#include <vector>

#include <entt/entity/registry.hpp>


namespace kurator
{
namespace battles
{


TeamManager::TeamManager() :
	teams {},
	generator {std::random_device{}()}
{
}


void
TeamManager::add(int team, entt::entity entity)
{
	for (int i = teams.size(); i < team + 1; ++i)
		teams.emplace_back();
	teams.at(team).push_back(std::move(entity));
}


TeamManager::Team
TeamManager::get(int team) const
{
	return teams.at(team);
}


entt::entity
TeamManager::random(int team)
{
	auto& members = teams.at(team);
	if (members.size() == 0)
		return entt::null;
	std::uniform_int_distribution<Team::size_type> uniform{0, members.size() - 1};
	return members.at(uniform(generator));
}


void
TeamManager::clear(entt::registry& registry)
{
	for (auto& members : teams) {
		auto is_valid = [&registry](entt::entity entity){ return !registry.valid(entity); };
		members.erase(std::remove_if(members.begin(), members.end(), is_valid), members.end());
	}
}


}  // namespace battles
}  // namespace kurator