// *******************************************************************************
// **       Funcion: Activa.sqf
// **   Descripcion: Inicializa trampa explosiva
// *******************************************************************************
// **         Autor: RAVEN
// *******************************************************************************

_carga = _this select 0;
_tipo  = _this select 1;

private ["_objetos","_kaboom","_radar","_dir","_dist","_tipCarga","_prox","_altura","_d1","_detect","_max","_posCarga",
		"_px","_py"];

_posCarga = _carga modelToWorld [0,0,0];
_altura = _posCarga select 2;

_detect = 1.50; _d1 = 0.8;
//_tipCarga = "B_30mm_HE";
//_tipCarga = "R_Hydra_HE";
_tipCarga = "G_40mm_HE";

switch (_tipo) do
{
	case 1: {_dist = 1.50};
	case 2: {_dist = 3.00};
	case 3: {_dist = 5.00};
};

if (IsServer) then
{
	if (local player) then {_max = 1} else {_max = 0};
} else {_max = 1};
_dir = direction _carga;

_radar = createTrigger ["EmptyDetector", (position _carga)];
_radar setTriggerActivation ["ANY", "present", true];
_radar setTriggerArea [(_d1 / 2), (_dist / 2), _dir, true];
_radar setTriggerStatements ["this", "", ""];
_radar setTriggerText "Area de detección de carga";
_radar setTriggerTimeout [0, 0, 0, false];
_radar setTriggerType "SWITCH";

while {alive (_carga)} do
{
	_objetos = list _radar;
	sleep 0.10;

	if ((count _objetos) > _max) then
	{
		_obj = _objetos select 0;
		_prox = abs(((_obj modeltoworld [0,0,0]) select 2) - _altura);
		
		if (_prox < _detect) then
		{
			playsound "Trap_beep";
			sleep 0.10;
			_px = (_poscarga select 0) - (_dist / 2) * sin((direction _carga) - 0);
			_py = (_poscarga select 1) - (_dist / 2) * cos((direction _carga) - 0);
			_kaboom = _TipCarga createvehicle [_px,_py, _posCarga select 2];
			deletevehicle _carga;
		};
	};
};
deletevehicle _radar;