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


		#include "scene3d/scene_physic_world_interface.h"
		#include "scene3d/mitem_event_interface.h"
		#include "scene3d/scene.h"
		#include "physic/physic_world.h"

		using namespace GS::S3D;


//------------------------------------------------------------------------------
void	IScenePhysicWorld::PhysicStep(float timestep, bool pre_tick)
{
	if	(pre_tick)
	{
		scene->SynchronizePhysics();

		// Pre-tick, step physics.
		scene->scene_event->OnPhysicStep(scene, true);

		ArrayListForeachPtr(MItem *, item, scene->GetActiveList())
			switch (item->GetItemType())
			{
				default:
					if	(!item->physic_item || (item->physic_item_desc.physic_mode == PhysicItemDesc::Mode_None))
						break;

				case Type_Instance:
					item->item_event->OnPhysicStep(item, true);
					break;
			}
		

	}
	else
	{
		// End of tick, dispatch collision events.
		for	(uint n = 0; n < scene->physic_world->GetCollisionPairCount(); ++n)
		{
			CollisionPair pair;
			if	(!scene->physic_world->GetCollisionPair(n, pair))
				continue;

			MItem	*item_a = (MItem *)pair.a->GetUserPointer(),
					*item_b = (MItem *)pair.b->GetUserPointer();
			if (item_a && item_a->item_event.IsValid() && item_b && item_b->item_event.IsValid()) {
				item_a->item_event->OnCollision(item_a, item_b, pair);
				item_b->item_event->OnCollision(item_b, item_a, pair);
			}
		}
	}
}
//------------------------------------------------------------------------------
