/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "core/trigger.h"

		using namespace GS;
		using namespace GS::Core;


//------------------------------------------------------------------------------
void Trigger::DropItem(Item *item) {
	ListForeachPtr(ItemInTrigger *, ti, items_in_trigger) 
		if (ti->item == item) {
			items_in_trigger.Remove(ti);
			delete ti;
		}
}
void				Trigger::MarkItem(Item *item)
{
	ListForeachPtr(ItemInTrigger *, ti, items_in_trigger)
		if	(ti->item == item)
		{
			ti->inside = true;
			return;
		}

	items_in_trigger.Add(new ItemInTrigger(item));
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void				Trigger::ReadyItemList()
{
	ListForeachPtr(ItemInTrigger *, ti, items_in_trigger)
		ti->inside = false;
}
void				Trigger::PurgeItemList()
{
	ListForeachPtr(ItemInTrigger *, ti, items_in_trigger)
		if	(!ti->inside)
			DropItem(ti->item);
}
//------------------------------------------------------------------------------

bool				Trigger::IsInside(const Vector4 &p)
{
	Vector4 local = p * GetInverseMatrix();
	return (local.x > -0.5) && (local.x < 0.5) && (local.y > -0.5) && (local.y < 0.5) && (local.z > -0.5) && (local.z < 0.5);
}

Trigger::~Trigger()
{
	ListForeachPtr(ItemInTrigger *, ti, items_in_trigger)
		delete ti;
}
