//
//
// DESCRIPTION:
//      LifeForce
//
//
//-------------------
//
//



#include "scripts/common.txt"




///////////////////////////////////////start of namespace LifeForce------------------------

	namespace LifeForce
	{


//---------------------------------------------

	bool getSpawnLifeForce;

	bool takeAwayOnce;

	bool printAfterDeath;

    	float m_timeDelay100LifeForce = 0.0;

	const int LifeForce1Amount = 1;

	const int LifeForce10Amount = 10;

//---------------------------------------------


	void LifeForceTrackerIncrease(const int LifeForceAmount)
		{

			int LFTracker = 0;
			GameVariables.GetInt("LifeForceTracker", LFTracker);
			LFTracker += LifeForceAmount;
			GameVariables.SetValue("LifeForceTracker", "" + LFTracker);

		}


	int GetLifeForceTracker()
		{
			int LFTracker = 0;
			GameVariables.GetInt("LifeForceTracker", LFTracker);
			return LFTracker;

		}


	void PlayerLosesLife()
		{

			int LFTracker = 0;
			GameVariables.GetInt("LifeForceTracker", LFTracker);
			if (LFTracker < 100)
			{
				LFTracker = 0;				
			} else
			{
				LFTracker -= 100;
			}
			GameVariables.SetValue("LifeForceTracker", "" + LFTracker);

		}


	void LifeForceTrackerSetTo999()
		{

			int LFTracker = 999;
			GameVariables.SetValue("LifeForceTracker", "" + LFTracker);

		}



///-----------------



	void SpareLifeForceTotalIncrease(const int SpareLifeForceAmount)
		{

			int SLFTotal = 0;
			GameVariables.GetInt("SpareLifeForceTotal", SLFTotal);
			SLFTotal += SpareLifeForceAmount;
			Game.PrintLine("" + SLFTotal + " Spare LifeForces", 5, 120);
			GameVariables.SetValue("SpareLifeForceTotal", "" + SLFTotal);

		}

	void SpareLifeForceTotalDecrease(const int SpareLifeForceAmount)
		{

			int SLFTotal = 0;
			GameVariables.GetInt("SpareLifeForceTotal", SLFTotal);
			SLFTotal -= SpareLifeForceAmount;
			Game.PrintLine(" ", 5, 120);
			GameVariables.SetValue("SpareLifeForceTotal", "" + SLFTotal);

		}


	int GetSpareLifeForceTotal()
		{
			int SLFTotal = 0;
			GameVariables.GetInt("SpareLifeForceTotal", SLFTotal);
			return SLFTotal;
		}

	void ShowSpareLifeForceTotal()
		{
			int SLFTotal = 0;
			GameVariables.GetInt("SpareLifeForceTotal", SLFTotal);
			Game.PrintLine("" + SLFTotal + " Spare LifeForces", 5, 180);
		}


	void Spawn10LifeForce()
		{


       			float x, y, z;
        
       			x = Player.Actor().Origin().x;
       			y = Player.Actor().Origin().y;
       			z = Player.Actor().Origin().z;

			int SLFTotal = 0;
			GameVariables.GetInt("SpareLifeForceTotal", SLFTotal);
			SLFTotal -= 10;
			LifeForceTrackerIncrease(10);
			ActorFactory.Spawn(AT_REAL_COIN10, x, y, z, 0, Player.Actor().SectorIndex());
			getSpawnLifeForce = true;
			GameVariables.SetValue("SpareLifeForceTotal", "" + SLFTotal);

		}

	void Spawn1LifeForce()
		{


       			float x, y, z;
        
       			x = Player.Actor().Origin().x;
       			y = Player.Actor().Origin().y;
       			z = Player.Actor().Origin().z;

			int SLFTotal = 0;
			GameVariables.GetInt("SpareLifeForceTotal", SLFTotal);
			SLFTotal -= 1;
			LifeForceTrackerIncrease(1);
			ActorFactory.Spawn(AT_REAL_COIN1, x, y, z, 0, Player.Actor().SectorIndex());
			getSpawnLifeForce = true;
			GameVariables.SetValue("SpareLifeForceTotal", "" + SLFTotal);

		}


///-----------------


	bool Use10LifeForce()
	{
		if(!(GetSpareLifeForceTotal() > 9))
		{
			return false;
		}

		if(!(GetLifeForceTracker() < 990))
		{
			return false;
		}

		if(Player.Locked())
		{
			return false;
		}

	return true;

	}

	bool Use1LifeForce()
	{
		if(!(GetSpareLifeForceTotal() > 0))
		{
			return false;
		}

		if(!(GetLifeForceTracker() < 999))
		{
			return false;
		}

		if(Player.Locked())
		{
			return false;
		}

	return true;

	}

	bool StopPushToLifeForce()
	{
		if(GetSpareLifeForceTotal() <= 0)
		{
			return true;
		}

		if(GetLifeForceTracker() >= 999)
		{
			return true;
		}

	return false;

	}


	void PlayerNotFalling()
	{
		if (!Player.Locked())
		{
			takeAwayOnce = true;
		}

	}


	void OnlyPrintIfHasSpareLifeForceAfterDeath()
	{
		if (GetSpareLifeForceTotal() > 0)
		{
			if (!Player.Locked())
			{
				printAfterDeath = true;
			}
		}
	}



	void PrintSpareLifeForceAfterDeath()
	{
		Game.PrintLine("" + GetSpareLifeForceTotal() + " Spare LifeForces", 5, 180);
		printAfterDeath = false;
	}



	void MovePlayerSlightly()
	{

		Player.Actor().MoveToPosition(Player.Actor().Origin().x + 0.1f * (1 - 2*Math::RandMax(2)), Player.Actor().Origin().y );

	}



///-----------------


	void PushPlayerIntoSpawnLifeForce() 
	{

		if (getSpawnLifeForce)
		{
			MovePlayerSlightly();
			if (StopPushToLifeForce())
			{
				getSpawnLifeForce = false;
				m_timeDelay100LifeForce = 0.0;
				ShowSpareLifeForceTotal();
			}

		}

	}



	void LifeForceMain()
	{

		if (!StopPushToLifeForce())
		{
			if (Use10LifeForce())
			{
				m_timeDelay100LifeForce += GAME_DELTA_TIME;
				if (m_timeDelay100LifeForce > 0.5)
				{
					Spawn10LifeForce();
	
				}

				if (m_timeDelay100LifeForce > 0.1 && printAfterDeath == true)
				{
					PrintSpareLifeForceAfterDeath();
	
				}

			} else if (Use1LifeForce())
			{
				m_timeDelay100LifeForce += GAME_DELTA_TIME;
				if (m_timeDelay100LifeForce > 0.5)
				{
					Spawn1LifeForce();

				}

				if (m_timeDelay100LifeForce > 0.1 && printAfterDeath == true)
				{
					PrintSpareLifeForceAfterDeath();
	
				}
			}
		}

	}



	void TakeAwayOnlyOnceFalling()
	{

		if (!(Player.Actor().AnimState().IsPlaying(36)))
		{
			return;
		}



		if(!takeAwayOnce)
		{
			return;
		}


		PlayerLosesLife();
		takeAwayOnce = false;


	}



///-----------------

	void PrintOutSpareLifeForces()
	{
		if (!Player.Locked())
		{
			Game.PrintLine("" + GetSpareLifeForceTotal() + " Spare LifeForces", 5, 180);
		}
	}

//////////////////////////////////////////end of namespace LifeForce------------------------
	}



	void SpareLifeForces()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void SpareLifeForce()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void sparelifeforces()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void sparelifeforce()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void LifeForceStorage()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void lifeforcestorage()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void slf()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void SLF()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void LFS()
	{
		LifeForce::PrintOutSpareLifeForces();
	}

	void lfs()
	{
		LifeForce::PrintOutSpareLifeForces();
	}


