//
// 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; // Smoke39 - repurposed as lastBloodTick
	int soundTimer;
	float spinSpeed;

	TurokGiblet(kActor @actor)
	{
		@self = actor;
		timer = 0;
		soundTimer = 0;
		spinSpeed = 0;
	}

	/*
	==============================================================
	Spin
	==============================================================
	*/

	void Spin(void)
	{
		kVec3 normal, cp, dir;
		float div;

		self.CheckPosition(kVec3(self.Origin().x, self.Origin().y, self.Origin().z));
		normal = CModel.ContactNormal();

		dir = self.Velocity();
		dir.Normalize();

		cp = normal.Cross(dir);
		cp.Normalize();

		switch(self.Type())
		{
		case AT_GIB_ALIEN3:   // alien head
		case AT_GIB_ALIEN1:   // alien body
		case AT_GIB_STALKER3: // leaper head
		case AT_GIB_STALKER1: // leaper body
			div = 25.0f;
			break;

		default:
			div = 10.0f;
			break;
		}

		spinSpeed = self.Velocity().Unit() / div;
	}

	/*
	==============================================================
	OnTick
	==============================================================
	*/

	void OnTick(void)
	{
/*		if(timer >= 50)
		{
			self.Remove();
			return;
		}
*/
		// Smoke39 - scale spin speed with game speed
		self.Yaw()   += spinSpeed * 0.125f * Game.GetGameSpeed();
		self.Pitch() += spinSpeed * 0.125f * Game.GetGameSpeed();
		if(soundTimer > 0)
		{
			soundTimer--;
		}

        if(self.OnGround())
        {
            float vz = Math::Fabs(self.Velocity().z);

            if(vz > 1.0f)
            {
                if(soundTimer == 0)
                {
                    soundTimer = 6;
                    if(vz > 3.413f)
                    {
                        self.PlaySound("sounds/shaders/generic_238.ksnd");
                    }
                    else if(vz > 1.7f)
                    {
                        self.PlaySound("sounds/shaders/generic_239.ksnd");
                    }
                    else
                    {
                        self.PlaySound("sounds/shaders/generic_240.ksnd");
                    }
                }

                Spin();
            }
            else
            {
                spinSpeed = 0;
            }
        }

        // Smoke39 - scale spawn rate with game speed
		if ( (PlayLoop.Ticks() & 7) == 0 && PlayLoop.Ticks() != timer )
		{
			timer = PlayLoop.Ticks();
			// Smoke39 - let all gibs spawn blood
			if(self.Velocity().UnitSq() > 0.5f)
			{
				switch(self.Type())
				{
				case AT_GIB_ALIEN3:
				case AT_GIB_STALKER4: // actually alien torso
				case AT_GIB_ALIEN2:
				case AT_GIB_ALIEN1:
					self.SpawnFx("fx/spurt_greenblood.kfx", Math::vecZero);
					break;
				default:
					self.SpawnFx("fx/spurt_blood.kfx", Math::vecZero);
					break;
				}
			}

//			timer++;
		}
	}

	// Smoke39 - despawn here instead of on a timer
	void OnSleep()
	{
		self.Remove();
	}

	/*
	==============================================================
	OnSpawn
	==============================================================
	*/

	void OnSpawn(void)
	{
	}
};
