Skip to content

Instantly share code, notes, and snippets.

@AustinBrunkhorst
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save AustinBrunkhorst/96cf4c4c3ab1e280c79d to your computer and use it in GitHub Desktop.

Select an option

Save AustinBrunkhorst/96cf4c4c3ab1e280c79d to your computer and use it in GitHub Desktop.
Entity Filter System with a hint of magic
#pragma once
#include "ComponentsConfig.h"
namespace Ursine
{
// forward declarations
class Entity;
class EntityFilter
{
ComponentTypeMask _mask_one;
ComponentTypeMask _mask_exclude;
ComponentTypeMask _mask_contains;
public:
EntityFilter(void);
inline ComponentTypeMask OneMask(void) const;
inline ComponentTypeMask ExcludeMask(void) const;
inline ComponentTypeMask ContainsMask(void) const;
bool Matches(Entity *entity) const;
template<class... Types>
EntityFilter &All(void);
template<class... Types>
EntityFilter &Exclude(void);
template<class... Types>
EntityFilter &One(void);
};
}
#include "EntityFilter.hpp"
#include "ComponentRegistrar.h"
namespace Ursine
{
namespace
{
template<typename... Types>
struct MaskUnpack { };
template<>
struct MaskUnpack<>
{
static ComponentTypeMask Apply(void)
{
return 0;
}
};
template<typename First, typename... Types>
struct MaskUnpack<First, Types...>
{
static ComponentTypeMask Apply(void)
{
return ComponentRegistrar::GetMask<First>() | MaskUnpack<Types...>::Apply();
}
};
}
inline ComponentTypeMask EntityFilter::OneMask(void) const
{
return _mask_one;
}
inline ComponentTypeMask EntityFilter::ExcludeMask(void) const
{
return _mask_exclude;
}
inline ComponentTypeMask EntityFilter::ContainsMask(void) const
{
return _mask_contains;
}
template<class... Types>
EntityFilter &EntityFilter::All(void)
{
_mask_contains |= MaskUnpack<Types...>::Apply();
return *this;
}
template<class... Types>
EntityFilter &EntityFilter::Exclude(void)
{
_mask_exclude |= MaskUnpack<Types...>::Apply();
return *this;
}
template<class... Types>
EntityFilter &EntityFilter::One(void)
{
_mask_one |= MaskUnpack<Types...>::Apply();
return *this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment