UnrealShare
Class ThingFactory

source: e:\games\UnrealTournament\UnrealShare\Classes\ThingFactory.uc
Core.Object
   |
   +--Engine.Actor
      |
      +--Engine.Keypoint
         |
         +--UnrealShare.ThingFactory
Direct Known Subclasses:CreatureFactory

class ThingFactory
extends Engine.Keypoint

//============================================================================= // ThingFactory. //=============================================================================
Variables
 enum EDistribution
           non-pawn items spawned should be set to falling
 bool bCovert
           only do hidden spawns
 bool bFalling
           non-pawn items spawned should be set to falling
 bool bOnlyPlayerTouched
           only player can trigger it
 bool bStoppable
           stops producing when untouched
 int capacity
           max number of items ever buildable (-1 = no limit)
 float interval
           average time interval between spawnings
 name itemtag
           tag given to items produced at this factory
 int maxitems
           max number of items from this factory at any time
 int numspots
           number of spawnspots
 class prototype
           the template class
 SpawnPoint spawnspot[16]
           possible start locations

States
Finished, Spawning, Waiting

Function Summary
 void PostBeginPlay()
 void StartBuilding()


State Finished Function Summary


State Spawning Function Summary
 void BeginState()
 void StartBuilding()
 void Timer()
 bool trySpawn(int start, int end)
 void Trigger(Actor Other, Pawn EventInstigator)
 void UnTouch(Actor Other)


State Waiting Function Summary
 void Touch(Actor Other)
 void Trigger(Actor Other, Pawn EventInstigator)



Source Code


00001	//=============================================================================
00002	// ThingFactory.
00003	//=============================================================================
00004	class ThingFactory extends Keypoint;
00005	
00006	var() class<actor> prototype; 	// the template class
00007	var() int maxitems;	// max number of items from this factory at any time
00008	var	  int	numitems;	// current number of items from this factory
00009	var int numspots;		// number of spawnspots
00010	var() int capacity;		// max number of items ever buildable (-1 = no limit)
00011	var() float interval;	// average time interval between spawnings
00012	var() name	itemtag;	// tag given to items produced at this factory
00013	var() bool	bFalling;	// non-pawn items spawned should be set to falling
00014	
00015	var() enum EDistribution
00016	{
00017		DIST_Constant,
00018		DIST_Uniform,
00019		DIST_Gaussian
00020	}
00021	timeDistribution;
00022	
00023	var() bool bOnlyPlayerTouched; //only player can trigger it
00024	var() bool bCovert;		//only do hidden spawns
00025	var() bool bStoppable;	//stops producing when untouched
00026	var Spawnpoint spawnspot[16]; //possible start locations
00027	
00028	function PostBeginPlay()
00029	{
00030		local Spawnpoint newspot;
00031		
00032		Super.PostBeginPlay();
00033		numspots = 0;
00034		numitems = 0;
00035		foreach AllActors( class 'Spawnpoint', newspot, tag )
00036		{
00037			if (numspots < 16)
00038			{
00039				spawnspot[numspots] = newspot;
00040				newspot.factory = self;
00041				numspots += 1;
00042			}
00043		}
00044		if (itemtag == '')
00045			itemtag = 'MadeInUSA';
00046	}	
00047	
00048	
00049	function StartBuilding()
00050	{
00051	}
00052	
00053	Auto State Waiting
00054	{
00055		function Trigger( actor Other, pawn EventInstigator )
00056		{
00057			local Actor A;
00058	
00059			if ( Event != '' )
00060				ForEach AllActors( class 'Actor', A, Event)
00061					A.Trigger(self, EventInstigator);
00062			GotoState('Spawning');
00063		}
00064			
00065		function Touch(Actor Other)
00066		{
00067			local pawn otherpawn;
00068		
00069			otherpawn = Pawn(Other);
00070			if ( (otherpawn != None) && (!bOnlyPlayerTouched || otherpawn.bIsPlayer) )
00071				Trigger(other, otherPawn);
00072		}
00073	}
00074	
00075	State Spawning
00076	{
00077		function UnTouch(Actor Other)
00078		{
00079			local int i;
00080			if (bStoppable)
00081			{
00082				//check if some other pawn still touching
00083				for (i=0;i<4;i++)
00084					if ( (pawn(Touching[i]) != None) && (!bOnlyPlayerTouched || pawn(Touching[i]).bIsPlayer) )
00085						return;
00086				GotoState('Waiting');
00087			}
00088		}
00089	
00090		function Trigger(actor Other, pawn EventInstigator)
00091		{
00092			//only if Other is from this factory
00093			if ( Other.class != prototype )
00094				return;
00095				
00096			numitems--;
00097			if (numitems < maxitems)
00098				StartBuilding();
00099		}
00100	
00101		function bool trySpawn(int start, int end)
00102		{
00103			local int i;
00104			local bool done;
00105	
00106			done = false;
00107			i = start;
00108			while (i < end)
00109			{
00110				if (spawnspot[i].Create())
00111				{
00112					done = true;
00113					i = end;
00114					capacity--;
00115					numitems++;
00116					if (capacity == 0)
00117						GotoState('Finished');
00118				}
00119				i++;
00120			}
00121			
00122			return done;
00123		}
00124			
00125		function Timer()
00126		{
00127			local int start;
00128			
00129			if (numitems < maxitems)
00130			{
00131				//pick a spawn point
00132				start = Rand(numspots);
00133				if ( !trySpawn(start, numspots) )
00134					trySpawn(0, start);
00135			}
00136				
00137			if (numitems < maxitems)
00138				StartBuilding();
00139		}
00140	
00141		Function StartBuilding()
00142		{
00143			local float nextTime;
00144			if (timeDistribution == DIST_Constant)
00145				nextTime = interval;
00146			else if (timeDistribution == DIST_Uniform)
00147				nextTime = 2 * FRand() * interval;
00148			else //timeDistribution is gaussian
00149				nextTime = 0.5 * (FRand() + FRand() + FRand() + FRand()) * interval;
00150				
00151			if (capacity > 0)
00152				SetTimer(nextTime, false);
00153		}
00154	
00155		function BeginState()
00156		{
00157			if ( !bStoppable )
00158				Disable('UnTouch');
00159		}
00160	
00161	Begin:
00162		Timer();
00163	}
00164	
00165	state Finished
00166	{
00167	}	
00168	
00169	defaultproperties
00170	{
00171	     MaxItems=1
00172	     capacity=1000000
00173	     interval=1.000000
00174	     bFalling=True
00175	     bStatic=False
00176	     bCollideActors=True
00177	}

End Source Code