#pragma once

#include "SpatialPartitioningTree.h"

class QuadTree;

class QuadTreeNode : public SpatialPartitioningTreeNode
{
protected:
	friend QuadTree;
	//WORD x,y;
	//WORD size;
	//WORD level;

	//AABB m_Bound;
	//DXVector3 m_Points[8];

public:
	QuadTreeNode(void) : SpatialPartitioningTreeNode(4)
	{
	}
};

class QuadTree : public SpatialPartitioningTree
{
	unsigned m_Levels;

	//QuadTreeNode* m_pRoot;
	QuadTreeNode **m_Leaves;

public:
	QuadTree(const unsigned levels);
	~QuadTree(void);

	//void UpdateBound(void) { if (m_pRoot) ((QuadTreeNode*)m_pRoot)->UpdateBound(); }

	//returns the leaf with the specific ind
	QuadTreeNode* GetNode(const unsigned level,const unsigned x,const unsigned y)
	{
		if(level < m_Levels && x < (unsigned)(1<<(level)) && y < (unsigned)(1<<(level)))
			return m_Leaves[level]+x+y*(1<<level);
		else
			return NULL;
	}

	QuadTreeNode* GetLeaf(const unsigned x, const unsigned y)
	{
		if (x < (unsigned)(1<<(m_Levels-1)) && y < (unsigned)(1<<(m_Levels-1)))
			return m_Leaves[m_Levels-1]+x+y*(1<<(m_Levels-1));
		else 
			return NULL;
	}

	void AddObject(SceneObject* const obj);
};
