Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T>
- class SpacePartitioning
- {
- public:
- virtual void insert(T * entity) = 0;
- virtual bool remove(T * entity) = 0;
- virtual T * instanceCollision(T * entity, float dx, float dy) = 0;
- virtual std::vector<T*> instancesCollision(T * entity, float x, float y) = 0;
- virtual bool collision(T * entity, float dx, float dy) = 0;
- virtual std::vector<T*> retrieve(float x1, float y1, float x2, float y2, int mode) const = 0;
- };
- template<class T>
- class InstanceContext
- {
- vector< vector<T*> > instances;
- vector< SpacePartitioning<T> > partitionning;
- void add(T*);
- void remove(T*);
- static void setGlobal();
- static InstanceContext & getDefault();
- static InstanceContext & global();
- };
- template<class T>
- class ObjectBase
- {
- InstanceContext<T> * my_context;
- int index;
- ObjectBase(int index) :
- my_context(InstanceContext<T>::global()),
- index(index)
- {
- my_context->add(index, this);
- }
- virtual ~ObjectBase()
- {
- my_context->remove(index, this);
- }
- virtual void step(float dt) {}
- virtual void draw() const {}
- InstanceContext<T> & context();
- };
- class Object2d : public ObjectBase<Object2d>
- {
- Vec2 pos;
- Object2d(float x, float y, int index): ObjectBase(index), pos(x, y) {}
- };
- // Auto index
- template<class T>
- class Object : public Object2d
- {
- static int current_index;
- Object(float x, float y): Object2d(x, y, current_index) {}
- int instanceNumber() const
- {
- return context().number(objectIndex());
- }
- T * find(int n)
- {
- return reinterpret_cast<T*>(context().get(n));
- }
- };
- int Object<T>::current_index = hidden::global_index++;
- //--------------------------------------------
- // Usage
- //--------------------------------------------
- class Player : public Object<Player>
- {
- };
- void test()
- {
- InstanceContext<Object2d> ic;
- ic.setGlobal();
- new Player(32, 48);
- new Player(16, 16);
- Player * first = Player::find(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment