#include "scripts/common.txt"
#include "scripts/BP_common.txt"

//(7) Params 1 -128..127 (flags 1 = squish enemy, 2)
enum TunnelDebrisFlags
{
	TDF_ISENEMY = 1 << 0,
	TDF_REMOVE = 1 << 1, //nodraw on on collide
}

class BP_Tunnel_Debris : ScriptObject
{
    kActor@ self;
	kActor@ hummer;
	kActor@ tank;
	bool collided;
	int lifeTime;
	int flags;
	//------------------------------------------------------------------------------------------------------------------------
    BP_Tunnel_Debris(kActor @actor)
    {
        @self = actor;
		collided = false;
		
		self.Flags() |= AF_ALWAYSACTIVE;
        self.ClipFlags() = (CF_DROPOFF|CF_CLIPEDGES|CF_NOCLIPACTORS|CF_COLLIDEFLOORS|CF_COLLIDEHEIGHT|CF_NOCOLLIDEFUNC);
        //self.ClipFlags() = (CF_DROPOFF|CF_CLIPEDGES|CF_COLLIDEFLOORS|CF_COLLIDEHEIGHT);

    }
	//------------------------------------------------------------------------------------------------------------------------
	void OnPostBeginLevel()
	{
		@hummer = World.GetActorByTID(1001);
		@tank = World.GetActorByTID(1002);
		flags = self.SpawnParams(7);
		if ((flags & TDF_ISENEMY) != 0)
		{
			self.Yaw() = Math::Deg2Rad(Math::RandRange(0.0f, 359.0f));
		}
	}
	//------------------------------------------------------------------------------------------------------------------------
    void OnSpawn()
	{
	}
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick(void)
    {
		if (!collided)
		{
			if ((flags & TDF_ISENEMY) != 0 && self.AnimState().PlayingID() != anim_aiWalking)
			{
				self.AnimState().Set(anim_aiWalking, 4.0f, ANF_LOOP|ANF_ROOTMOTION);
			}
			//check for collision with hummer and tank
			if (hummer !is null && hummer.Origin().z <= 1.0f && IsInActorRadius2(self, hummer))
				OnCustomCollide(hummer);
			else if (tank !is null && tank.Origin().z <= 1.0f && IsInActorRadius2(self, tank))
				OnCustomCollide(tank);
		}
		else
		{
			if ((flags & TDF_REMOVE) != 0)
			{
				self.Remove();
				return;
			}
			if (!Math::Approximately(self.Velocity().z, 0.0f))
			{
				self.Yaw() += Math::Deg2Rad(3.0f);
				self.Pitch() += Math::Deg2Rad(3.0f);
				if ((flags & TDF_ISENEMY) != 0 && self.GameTicks() % 3 == 0)
				{
					self.SpawnFx("fx/spurt_blood.kfx", Math::vecZero);
				}
			}
		}
    }
	//------------------------------------------------------------------------------------------------------------------------
	void OnCustomCollide(kActor@ actor)
	{
		collided = true;
		self.Velocity() = kVec3(0.0f, 15.0f, 9.0f);
		self.BounceDamp() = Math::RandRange(0.2f, 0.5f);
		self.Gravity() = 0.25f;
		if ((flags & TDF_ISENEMY) != 0)
		{
			if (@actor == @hummer && Game.GetCurrentMapID() == 128)
			{
				Game.CallDelayedMapScript(4, self, 0.0f); //hummer hit enemy debris
			}

			Game.PlaySound("sounds/shaders/carsquish.ksnd");
			//Game.SpawnFx("fx/spurt_blood.kfx", self.Origin() + kVec3(0.0f, 0.0f, 150.0f), self.SectorIndex());
			self.AnimState().Set(anim_aiDeathRunning, 4.0f, ANF_ROOTMOTION);
			//self.Remove();
		}
		else
		{
			if (@actor == @hummer && Game.GetCurrentMapID() == 128)
			{
				Game.CallDelayedMapScript(3, self, 0.0f); //hummer hit debris
			}
			
			if (Math::RandMax(2) == 0)
				self.PlaySound("sounds/shaders/explosion_1.ksnd");
			else
				self.PlaySound("sounds/shaders/explosion_2.ksnd");
			self.SpawnFx("fx/explosion.kfx", Math::vecZero);
		}
	}
	//------------------------------------------------------------------------------------------------------------------------
	// Anim Actions
	//------------------------------------------------------------------------------------------------------------------------
    void GunFire(kActor @instigator, const float w, const float x, const float y, const float z) {}
    void DeathSound(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        Game.PlaySound("sounds/shaders/alien_death.ksnd");
    }
    void ViolentSound(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        Game.PlaySound("sounds/shaders/alien_violent_death.ksnd");
    }
	void DropItem(kActor @instigator, const float w, const float x, const float y, const float z) {}
	void FootStepPuff(kActor @instigator, const float w, const float x, const float y, const float z) {}
	void Unused60(kActor @instigator, const float w, const float x, const float y, const float z) {}
	//------------------------------------------------------------------------------------------------------------------------
};
