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

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Destructible
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokDestructible
==============================================================
*/

class TurokDestructible : ScriptObject
{
	kActor @self;

	TurokDestructible(kActor @actor)
	{
		@self = actor;
	}

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

	void OnTick(void)
	{
	}

	/*
	==============================================================
	MeleeBluntHeavy
	==============================================================
	*/

	void MeleeBluntHeavy(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.MeleeObject("Damage_Blunt_20", kVec3(x, y, z), w);
	}

	/*
	==============================================================
	OnDamage
	==============================================================
	*/

	void OnDamage(kActor @instigator, kDictMem @damageDef, const int damage)
	{
		bool bValue = false;

		if(self.AnimState().PlayingID() == anim_destructibleDeath)
		{
			return;
		}

		if(damageDef is null)
		{
			return;
		}

		if(damageDef.GetBool("bExplode", bValue) && bValue)
		{
			self.AnimState().Blend(anim_destructibleDeath, 4.0f, 4.0f, 0);
			self.Flags() &= ~AF_SOLID;
			self.MarkPersistentBit(false);
		}
	}

	// Smoke39 - AF_DEAD causes non-solid actors to become shootable, so clear it
	void OnDeath( kActor@ instigator, kDictMem@ damageDef )
	{
		self.Flags() &= ~AF_DEAD;
	}

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

	void OnSpawn(void)
	{
		self.AnimState().Set(anim_destructibleIdle, 4.0f, ANF_LOOP);
	}

	/*
	==============================================================
	OnRestore
	==============================================================
	*/

	void OnRestore(void)
	{
		// Smoke39 - AF_DEAD causes non-solid actors to become shootable, so don't set it
		self.Flags() |= AF_IGNORESOUNDEVENTS; // |AF_DEAD;
		self.Flags() &= ~AF_SOLID;

		self.AnimState().Set(anim_destructibleDeath, 4.0f, 0);
		self.AnimState().SetLastFrame(true);
	}
};
