//---------------------------
// Includes
//---------------------------
#include "StdAfx.h"
#include "Food.h"
#include "ItemList.h"
#include "Painter.h"
#include "ResourceLoader.h"
#include "InputState.h"
#include "CollisionMesh.h"
#include "Image.h"

//---------------------------
// Defines
//---------------------------

//---------------------------
// Constructor & Destructor
//---------------------------
ItemList::ItemList()
{
	for(int i = 0; i < MAX_NR_OF_ITEMS;++i)
	{
		m_ItemPtrArr[i] = 0;
	}
}

ItemList::~ItemList()
{
	// Deletes all the elements in the array, 
	// Uses the RemoveAll method
	this->RemoveAll();
}

//---------------------------
// Own methods
//---------------------------

bool ItemList::Add(Item* itemPtr)
{
	// When there is still a free place in the array , the methods puts the given item at the free place and returns true
	// When no free place was found, the method returns the value false

	for(int i = 0; i < MAX_NR_OF_ITEMS;++i)
	{
		if(m_ItemPtrArr[i] == 0)
		{
			m_ItemPtrArr[i] = itemPtr;
			return true;
		}
	}

	return false;
}

bool ItemList::Remove(Item* itemPtr)
{
	// Searches the position of the item in the array.

	// When the item is found, it is deleted and the corresponding pointer in the array is set to 0
	// and the method returns the value true.

	// When the item is not found in the array, it returns the value false

	for(int i = 0; i < MAX_NR_OF_ITEMS;++i)
	{
		if(m_ItemPtrArr[i] == itemPtr)
		{
			delete m_ItemPtrArr[i];
			m_ItemPtrArr[i] = 0;
			return true;
		}
	}

	return false;
}

void ItemList::RemoveAll()
{
	for(int i = 0; i < MAX_NR_OF_ITEMS;++i)
	{
		if(m_ItemPtrArr[i] != 0)
		{
			delete m_ItemPtrArr[i];
			m_ItemPtrArr[i] = 0;
		}
	}
}

int ItemList::GetSize() const
{
	int size = 0;
	for(int i = 0; i < MAX_NR_OF_ITEMS;++i)
	{
		if(m_ItemPtrArr[i] != 0)
		++size;
	}
	return size;
}

void ItemList::Tick(InputState *inputStatePtr)
{
	for(int i = 0; i < MAX_NR_OF_ITEMS;++i)
	{
		if(m_ItemPtrArr[i] != 0)
		m_ItemPtrArr[i]->Tick(inputStatePtr);
	}
}

void ItemList::Paint(Painter *painterPtr, Matrix2D matTransform)
{
	for(int i = 0; i < MAX_NR_OF_ITEMS;++i)
	{
		if(m_ItemPtrArr[i] != 0)
		m_ItemPtrArr[i]->Paint(painterPtr, matTransform);
	}
}