blob: cbc74d9abe2a3260c6be5d9ee94f771802b7c348 (
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
|
/**
* Copyright (C) 2012 Alec Thomas <alec@swapoff.org>
* All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* Author: Alec Thomas <alec@swapoff.org>
*/
#pragma once
#include <string>
#include <boost/unordered_set.hpp>
#include "entityx/Entity.h"
namespace entityx {
/**
* Allow entities to be tagged with strings.
*/
class TagsComponent : public Component<TagsComponent> {
struct TagsPredicate {
TagsPredicate(const std::string &tag) : tag(tag) {}
bool operator () (EntityManager &manager, Entity id) {
auto tags = manager.component<TagsComponent>(id);
return tags != nullptr && tags->tags.find(tag) != tags->tags.end();
}
std::string tag;
};
public:
/**
* Construct a new TagsComponent with the given tags.
*
* eg. TagsComponent tags("a", "b", "c");
*/
template <typename ... Args>
TagsComponent(const std::string &tag, const Args & ... tags) {
set_tags(tag, tags ...);
}
/**
* Filter the provided view to only those entities with the given tag.
*/
static EntityManager::View view(const EntityManager::View &view, const std::string &tag) {
return EntityManager::View(view, TagsPredicate(tag));
}
boost::unordered_set<std::string> tags;
private:
template <typename ... Args>
void set_tags(const std::string &tag1, const std::string &tag2, const Args & ... tags) {
this->tags.insert(tag1);
set_tags(tag2, tags ...);
}
void set_tags(const std::string &tag) {
tags.insert(tag);
}
};
}
|