dofile("Scripts/Objects/Materials/tree.lua");
dofile("Scripts/Objects/Types/tree.lua");

wave_touch = "Effects/trees/green_tree_touch.dsc";
leaves_splash = "Effects/trees/green_tree_splash.dsc";
destr_splash = "Effects/trees/tree.dsc";

-- INIT
crown = nil;
trunk = nil;
damage = -1;
health = 400;
state = nil;

function Initialize()
	crown = object.GetFrameGroupId("Crown");
	if crown < 0 then crown = nil end;
	trunk = object.GetFrameGroupId("Trunk");
	if trunk < 0 then trunk = nil end;
end

function CheckHealth(CrownLimit)

	object.SetHealth(health);

	if health <= CrownLimit then
		if crown then object.SetGroupVisible(false, crown) end;
		crown = nil;
		state = true;
	end

	if health <= 0 then
		if trunk then object.SetGroupVisible(false, trunk) end;
		trunk = nil;
		CreateDecal("broken_tree.dcd", object.GetPosition(), -4);
--		CreateObject(destr_splash, object.GetPosition());
		object.Die();
	end
end

function CheckDirectDamage(WeaponDamage, Caliber, Point, Distance, ZoneId)
	if WeaponDamage > 200 then
		health = health - WeaponDamage / 3;
		CheckHealth(75);
	end

	if object.IsVisibleByCamera() then -- place visuals
		if state then 
			CreateObject(wave_touch, Point);
		end
	end
end

function CheckSplashDamageZones(DetonationDamage, Caliber, Zones)
	if state then 
		CreateObject(destr_splash, object.GetPosition());
	else
		CreateObject(leaves_splash, object.GetPosition());	
	end

	minus = DetonationDamage / 33;
	health = health - minus;
	CheckHealth(65);
end

function CheckCollision(Object)
--[[
	object.SetGroupVisible(false, trunk);
	object.SetGroupVisible(false, crown);
	if object.IsVisibleByCamera() and (crown ~= -1) then -- place visuals
		CreateObject(leaves_splash, object.GetPosition());
	end
	health = 0;
	PlaySound("derevo_slomali",object.GetPosition());
	CheckHealth(50);
--]]
end
