Class VehicleFactory extends Actor;

var() class<Vehicle> VehicleClass;
var() byte TeamNum;
var() bool bStartTeamLocked,bInitialActive;
var() float VehicleRespawnTime,VehicleResetTime;
var Vehicle MyVehicle;
var float NextRespawnTimer;
var bool bInitAct;
var byte InitialTeamNum;
var() bool bDisableTeamSpawn;

function PostBeginPlay()
{
	bInitAct = bInitialActive;
	InitialTeamNum = TeamNum;
	if( bInitialActive )
		SetTimer(1,False);
}
function Timer()
{
	if( !bInitialActive )
		Return;
	if( VehicleClass==None )
	{
		Error("Missing vehicle class!");
		Return;
	}
	if( MyVehicle==None || MyVehicle.bDeleteMe )
	{
		MyVehicle = Spawn(VehicleClass);
		if( MyVehicle==None )
		{
			SetTimer(1,False);
			Return;
		}
		MyVehicle.MyFactory = Self;
		MyVehicle.CurrentTeam = TeamNum;
		MyVehicle.bTeamLocked = bStartTeamLocked;

		if (!MyVehicle.bDisableTeamSpawn && !bDisableTeamSpawn)
			MyVehicle.SetOverlayMat(MyVehicle.TeamOverlays[TeamNum],0.5);

		MyVehicle.bDisableTeamSpawn = (MyVehicle.bDisableTeamSpawn || bDisableTeamSpawn);
	}
}
function NotifyVehicleDestroyed()
{
	NextRespawnTimer = Level.TimeSeconds+VehicleRespawnTime;
	if( !bInitialActive )
		Return;
	MyVehicle = None;
	SetTimer(VehicleRespawnTime,False);
}
function Reset()
{
	bInitialActive = bInitAct;
	TeamNum = InitialTeamNum;
	if( MyVehicle!=None )
	{
		MyVehicle.MyFactory = None;
		MyVehicle.Destroy();
	}
	NextRespawnTimer = 0;
	SetTimer(1,False);
}
event Trigger( Actor Other, Pawn EventInstigator )
{
	if( !bInitialActive )
	{
		bInitialActive = True;
		if( MyVehicle==None || MyVehicle.bDeleteMe )
		{
			if( NextRespawnTimer<=Level.TimeSeconds )
				SetTimer(0.5,False);
			else SetTimer((NextRespawnTimer-Level.TimeSeconds),False);
		}
	}
}
event UnTrigger( Actor Other, Pawn EventInstigator )
{
	if( bInitialActive )
	{
		bInitialActive = False;
		if( MyVehicle!=None && !MyVehicle.bDeleteMe && !MyVehicle.bHadADriver )
		{
			MyVehicle.MyFactory = None;
			MyVehicle.Destroy();
			MyVehicle = None;
		}
		SetTimer(0,False);
	}
}
function Destroyed()
{
	if( MyVehicle!=None && MyVehicle.bDeleteMe )
	{
		if( !MyVehicle.bHadADriver )
		{
			MyVehicle.MyFactory = None;
			MyVehicle.Destroy();
			MyVehicle = None;
		}
		else MyVehicle.MyFactory = None;
	}
}

defaultproperties
{
	VehicleResetTime=60
	VehicleRespawnTime=5
	Mesh=Mesh'WoodenBoxM'
	DrawType=DT_Mesh
	bHidden=True
	CollisionHeight=22
	CollisionRadius=22
	bCollideActors=False
	bCollideWorld=True
	bCollideWhenPlacing=True
	bInitialActive=True
	RemoteRole=ROLE_None
	bStartTeamLocked=True
	bDisableTeamSpawn=True
}
