-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathGroupManager.cpp
executable file
·89 lines (70 loc) · 1.93 KB
/
GroupManager.cpp
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
#include "GroupManager.h"
#include "Entity.h"
namespace artemis {
GroupManager::GroupManager() : empty_bag(1) {
empty_string.clear();
}
ImmutableBag<Entity*> * GroupManager::getEntities(std::string group) {
Bag<Entity*> * bag = entitiesByGroup[group];
if(bag == NULL) {
// create a new empty group
bag = new Bag<Entity*>(32);
entitiesByGroup[group] = bag;
}
return bag;
}
std::string GroupManager::getGroupOf(Entity& e) {
if(e.getId() < groupByEntity.getCapacity()){
std::string * group = groupByEntity.get(e.getId());
if(group == NULL)
return empty_string;
return *group;
}
return empty_string;
}
bool GroupManager::isGrouped(Entity& e) {
return !getGroupOf(e).empty();
}
bool GroupManager::isInGroup(std::string group, Entity& e) {
//TODO ignore case
//return strncasecmp(group, getGroupOf(e)) == 0;
return group.compare(getGroupOf(e)) == 0;
}
void GroupManager::remove(Entity& e) {
if(e.getId() < groupByEntity.getCapacity()){
std::string * groupId = groupByEntity.get(e.getId());
if(groupId != NULL){
groupByEntity.set(e.getId(), NULL);
Bag<Entity*> * entities = entitiesByGroup[*groupId];
if(entities != NULL){
entities->remove(&e);
}
entities = NULL;
delete groupId;
groupId = NULL;
}
groupId = NULL;
}
}
void GroupManager::set(std::string group, Entity& e) {
remove(e);
Bag<Entity*> * entities = entitiesByGroup[group];
if(entities == NULL){
entities = new Bag<Entity*>(32);
entitiesByGroup[group] = entities;
}
entities->add(&e);
entities = NULL;
groupByEntity.set(e.getId(), new std::string(group));
}
GroupManager::~GroupManager(){
groupByEntity.deleteData();
//groupByEntity.clear();
std::map<std::string, Bag<Entity*>*>::iterator it;
for(it = entitiesByGroup.begin(); it != entitiesByGroup.end(); it++)
{
delete it->second;
}
entitiesByGroup.clear();
}
};