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

class BP_QuakeSource : ScriptObject
{
    kActor @self;
    float duration;
    float currentTime;
    float velocity;
    float angle;
    
	//------------------------------------------------------------------------------------------------------------------------
    BP_QuakeSource(kActor @actor)
    {
        @self = actor;
        duration = 0;
        currentTime = 0;
        velocity = 0;
        angle = 0;
    }
	//------------------------------------------------------------------------------------------------------------------------
    void SetupShake(const kVec3 &in location, const float v, const float a, const float d)
    {
        self.Origin() = location;
        velocity = v;
        angle = a * 0.3333333432674408f;
        duration = Math::Fabs(d);
        currentTime = duration;
    }
	//------------------------------------------------------------------------------------------------------------------------
    void SetupThump(const kVec3 &in location, const float d)
    {
        float ang, vel;
        
        ang = -(Math::pi / (60.0f / d));
        vel = d * 1.706666666666667f;
        
        SetupShake(location, vel, ang, d);
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick(void)
    {
        kPuppet @target = Player.Actor();
        
        if (currentTime <= 0.0f)
        {
            self.Remove();
            return;
        }
        
        if ((PlayLoop.Ticks() & 3) == 0)
        {
            float power;
            float dist;
            float av, rv;
            float dx, dy;
        
            power = currentTime / duration;
            av = (Math::RandCFloat() * (angle * 0.5f)) * power;
            rv = (Math::RandCFloat() * (angle * 0.5f)) * power;
            
            dx = self.Origin().x - target.Origin().x;
            dy = self.Origin().y - target.Origin().y;
            
            dist = 1024.0f - Math::Sqrt(dx * dx + dy * dy);
            
            if(dist < 0.0f)
            {
                dist = 0.0f;
            }
            
            target.RecoilPitch() = (dist * av * 0.00390625f);
            target.Roll() = (dist * rv * 0.00390625f);
        }
        
        currentTime -= GAME_DELTA_TIME;
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnSpawn(void)
    {
    }
	//------------------------------------------------------------------------------------------------------------------------
};
