blob: b7922978d77c5d59877580acf48642f3151aff86 (
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
|
#include "TeamManager.h"
#include <algorithm>
#include <numeric>
#include <random>
#include <utility>
#include <entt/entity/registry.hpp>
namespace kurator
{
namespace sim
{
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(const int exclude)
{
std::vector<int> open;
open.resize(teams.size());
std::iota(open.begin(), open.end(), 0);
auto is_closed = [&](const int team){ return team == exclude || teams.at(team).empty(); };
open.erase(std::remove_if(open.begin(), open.end(), is_closed), open.end());
if (open.empty())
return entt::null;
std::uniform_int_distribution<Team::size_type> team{0, open.size() - 1};
const auto& members = teams.at(open.at(team(generator)));
std::uniform_int_distribution<Team::size_type> member{0, members.size() - 1};
return members.at(member(generator));
}
void
TeamManager::clear(entt::registry& registry)
{
for (auto& members : teams) {
auto is_valid = [®istry](entt::entity entity){ return !registry.valid(entity); };
members.erase(std::remove_if(members.begin(), members.end(), is_valid), members.end());
}
}
} // namespace sim
} // namespace kurator
|