//
// 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:
//      Giblet Object Classes
//

#include "scripts/common.txt"

/*
==============================================================
TurokGiblet
==============================================================
*/

shared class TurokGiblet : ScriptObject
{
    kActor @self;
    int timer;
    float spinSpeed;
    
    TurokGiblet(kActor @actor)
    {
        @self = actor;
        timer = 0;
        spinSpeed = 0;
    }
    
    /*
    ==============================================================
    Spin
    ==============================================================
    */
    
    void Spin(void)
    {
        kVec3 normal, cp, dir;
        float div;
        
        self.CheckPosition(self.Origin().x, self.Origin().y);
        normal = CModel.ContactNormal();
        
        dir = self.Velocity();
        dir.Normalize();
        
        cp = normal.Cross(dir);
        cp.Normalize();
        
        switch(self.Type())
        {
        case AT_GIB_ALIEN3:
        case AT_GIB_ALIEN1:
        case AT_GIB_STALKER3:
        case AT_GIB_STALKER1:
            div = 25.0f;
            break;
            
        default:
            div = 10.0f;
            break;
        }
        
        spinSpeed = self.Velocity().Unit() / div;
    }
    
    /*
    ==============================================================
    OnTick
    ==============================================================
    */
    
    void OnTick(void)
    {
        if(timer >= 50)
        {
            self.Remove();
            return;
        }
        
        self.Yaw() += spinSpeed * 0.125f;
        self.Pitch() += spinSpeed * 0.125f;
        
        if(self.OnGround())
        {
            float vz = Math::Fabs(self.Velocity().z);
            
            if(vz > 1.0f)
            {
                // if(vz > 3.413f)
                // {
                    // self.PlaySound("sounds/shaders/generic_238.ksnd");
                // }
                // else if(vz <= 3.413f)
                // {
                    // self.PlaySound("sounds/shaders/generic_239.ksnd");
                // }
                // else if(vz <= 1.7f)
                // {
                    // self.PlaySound("sounds/shaders/generic_240.ksnd");
                // }
                
                Spin();
            }
            else
            {
                spinSpeed = 0;
            }
        }
        
        if((PlayLoop.Ticks() & 7) == 0)
        {
            if(self.Velocity().UnitSq() > 0.5f)
            {
                switch(self.Type())
                {
                case AT_GIB_ALIEN3:
                case AT_GIB_ALIEN1:
                case AT_GIB_STALKER3:
                case AT_GIB_STALKER1:
                    self.SpawnFx("fx/spurt_blood.kfx", Math::vecZero);
                    break;
                }
            }
            
            timer++;
        }
    }
    
	// void OnCollide(kCModel @model) {
		// Sys.Print("OnCollide");
	// }
	
    /*
    ==============================================================
    OnSpawn
    ==============================================================
    */
    
    void OnSpawn(void)
    {
    }
};
