#include "collisionparticle.h"
#include <windows.h>
#include <iostream>
#include "hud.h"
#include "cl_util.h"
#include "cvardef.h"
#include "usercmd.h"
#include "const.h"

#include "entity_state.h"
#include "cl_entity.h"
#include "ref_params.h"
#include "in_defs.h" // PITCH YAW ROLL
#include "pm_movevars.h"
#include "pm_shared.h"
#include "pm_defs.h"
#include "event_api.h"
#include "pmtrace.h"
#include "pm_shared.h"
#include "pm_defs.h"
#include "event_api.h"
extern "C"
{

// HLDM
bool UTIL_GetWaterSurface( Vector vecStart, Vector vecEnd, float* vecReturn);

}
CollisionParticle::CollisionParticle(HSPRITE sprite):VelocityParticle(sprite)
{	VelocityParticle::VelocityParticle(sprite);
	acceleration = vec3_t(0,0,0);
	m_fCollision = false;
	SetThink(&CollisionParticle::collisionThink);
}

CollisionParticle::CollisionParticle(vec3_t _position,vec3_t _velocity,vec3_t _acceleration,HSPRITE sprite):VelocityParticle(_position,_velocity,sprite)
{
	VelocityParticle::VelocityParticle(_position,_velocity,sprite);
	acceleration = _acceleration;
	m_fCollision = false;
	SetThink(&CollisionParticle::collisionThink);	
}
//-------------------------------------------------------------------------
CollisionParticle::~CollisionParticle()
{	
}
//-------------------------------------------------------------------------
void CollisionParticle::collisionThink(float time)
{
	if(m_fCollision)return;
	vec3_t vecNewPos = startPosition + velocity * time + acceleration * time * time * 0.5;
	pmtrace_t *tr = gEngfuncs.PM_TraceLine( position, vecNewPos, PM_TRACELINE_PHYSENTSONLY, 2, -1 );	
	if ( tr->fraction != 1.0 )
	{
		m_fCollision = true;
		m_fEnabled = false;
		position = tr->endpos;
	}
	else
	{
		position = vecNewPos;
	}
	Particle::defaultThink(time);
	/*
	vec3_t vecEnd;	
	UTIL_GetWaterSurface( position, vecNewPos, vecEnd);
	if(vecEnd!=vecNewPos)
	{
		m_fCollision = true;
		position = vecEnd;
	}
	else
	{
		position = vecNewPos;
	}
	*/
	//velocity = velocity + acceleration * timeDiff;
	//VelocityParticle::velocityThink(timeDiff);
}