Skip to content

Instantly share code, notes, and snippets.

@alecthomas
Last active March 15, 2017 03:08
Show Gist options
  • Save alecthomas/6a45f05a08fdc1b4d74f to your computer and use it in GitHub Desktop.
Save alecthomas/6a45f05a08fdc1b4d74f to your computer and use it in GitHub Desktop.
#pragma once
#include <string>
#include <unordered_set>
#include "entityx/Entity.h"
#include "entityx/Event.h"
class Groups : public entityx::Receiver<Groups> {
public:
Groups(entityx::EventManager &events) {
events.subscribe<entityx::EntityDestroyedEvent>(*this);
}
void add(const std::string &group, entityx::Entity entity) {
groups_[group].insert(entity);
}
void remove(const std::string &group, entityx::Entity entity) {
groups_[group].erase(entity);
}
const std::unordered_set<entityx::Entity> &group(const std::string &name) {
return groups_[name];
}
void receive(const entityx::EntityDestroyedEvent &event) {
for (auto it : groups_) {
auto f = it.second.find(event.entity);
if (f != it.second.end()) {
it.second.erase(f);
}
}
}
private:
std::unordered_map<std::string, std::unordered_set<entityx::Entity>> groups_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment