//
// Copyright(C) 2014-2015 Samuel Villarreal
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// DESCRIPTION:
//      Quake Source Object Class
//

#include "scripts/common.txt"

/*
==============================================================
TurokQuakeSource
==============================================================
*/

shared class TurokQuakeSource : ScriptObject
{
    kActor @self;
    float duration;
    float currentTime;
    float velocity;
    float angle;
    
    TurokQuakeSource(kActor @actor)
    {
        @self = actor;
        duration = 0;
        currentTime = 0;
        velocity = 0;
        angle = 0;
    }
    
    /*
    ==============================================================
    SetupShake
    ==============================================================
    */
    
    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;
    }
    
    /*
    ==============================================================
    SetupThump
    ==============================================================
    */
    
    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);
    }
    
    /*
    ==============================================================
    OnTick
    ==============================================================
    */
    
    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;
            }
            
            /*if(target.OnGround())
            {
                float vel = ((Math::RandFloat() * (velocity * 0.5f)) + (velocity * 0.5f)) * power;
                target.Velocity().z = ((dist * vel * 0.0009765625f) * 15.0f) * GAME_DELTA_TIME;
            }*/
        
            target.RecoilPitch() = (dist * av * 0.00390625f);
            target.Roll() = (dist * rv * 0.00390625f);
        }
        
        currentTime -= 0.02f;
    }
    
    /*
    ==============================================================
    OnSpawn
    ==============================================================
    */
    
    void OnSpawn(void)
    {
    }
};
