#include "scripts/common.txt"

//Wall Width/Radius (float) (Delay to restart)
//Dead HeightStep Height (float) (Anim Speed. 0 = 4.0f)

class BP_FallingRocks : ScriptObject
{
    kActor @self;
	float delay;
	bool animCycleCompleted;
	//------------------------------------------------------------------------------------------------------------------------
    BP_FallingRocks(kActor @actor)
    {
        @self = actor;
    }
	//------------------------------------------------------------------------------------------------------------------------
	float GetAnimSpeed()
	{
		if (self.StepHeight() > 0.0f)
		{
			return self.StepHeight();
		}
		
		return 4.0f;
	}
	//------------------------------------------------------------------------------------------------------------------------
    void MeleeBluntWimpy(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.MeleeObject("Damage_Blunt_2", kVec3(x, y, z), w);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void MeleeBluntMedium(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.MeleeObject("Damage_Blunt_5", kVec3(x, y, z), w);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void MeleeBluntStrong(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.MeleeObject("Damage_Blunt_10", kVec3(x, y, z), w);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void MeleeBluntVeryHeavy(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.MeleeObject("Damage_Blunt_30", kVec3(x, y, z), w);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void ScreenShake(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        // Player.Actor().RecoilPitch() = -Math::Deg2Rad(w);
        // Player.Actor().Velocity().z = ((w * 0.512f) * 15.0f) * (4 * GAME_DELTA_TIME);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void ThumpAndHop(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        Player.Actor().Velocity().z = (-255.0f / (w * 15.0f)) * (4 * GAME_DELTA_TIME);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void WeakKnockBack(kActor @instigator, const float w, const float x, const float y, const float z)
    {
		//BP: Also take heavy damage
		MeleeBluntStrong(instigator, w, x, y, z);
        self.InteractActorsAtPosition(self.GetTransformedVector(kVec3(x, y, z)), "KnockbackTarget", x, y, z, w);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void KnockbackTarget(kActor @actor, const float arg1, const float arg2, const float arg3, const float arg4)
    {
        kVec3 org = self.GetTransformedVector(kVec3(arg1, arg2, arg3));
        kVec3 targOrg;
        kVec3 dir;
        float dist;
        
        if(actor is null)
        {
            return;
        }
        
        targOrg = actor.Origin();
        targOrg.z += (actor.Height() * 0.5f);
        
        dir = targOrg - org;
        dist = dir.Unit();
        
        if(dist <= (arg4 * 10.24f) + actor.Radius())
        {
            actor.Velocity() += (dir * 13.824f) * GAME_DELTA_TIME;
            actor.Velocity().z = 10.24f;
            
            actor.PlaySound("sounds/shaders/generic_81_kick_impact.ksnd");
            
            if(actor is Player.Actor().CastToActor())
            {
                Player.Actor().PlayerFlags() |= PF_NOAIRFRICTION;
            }
        }
    }
	//------------------------------------------------------------------------------------------------------------------------
	void Unused398(kActor @instigator, const float w, const float x, const float y, const float z)
	{
	}
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick(void)
    {
		if (!animCycleCompleted)
		{
			if (self.AnimState().CycleCompleted())
			{
				animCycleCompleted = true;
				self.RunFxEvent("FallingRockFade");
			}
			return;
		}

		delay -= GAME_DELTA_TIME;
		if (delay <= 0.0f)
		{
			delay = self.WallRadius();
			PlayTrapAnimation();
		}
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnBeginLevel()
    {
		PlayTrapAnimation();
		delay = self.WallRadius();
	}
	//------------------------------------------------------------------------------------------------------------------------
	void PlayTrapAnimation()
	{
        if (self.AnimState().CheckAnimID(anim_destructibleIdle))
        {
            self.AnimState().Set(anim_trapIdle, GetAnimSpeed(), 0);
        }
        else
        {
            self.AnimState().Set(anim_trapActivate, GetAnimSpeed(), 0);
        }
		animCycleCompleted = false;
		self.RunFxEvent("FallingRockStart");
	}
	//------------------------------------------------------------------------------------------------------------------------
    void OnSpawn(void)
    {
    }
	//------------------------------------------------------------------------------------------------------------------------
};
