# SpatialFear.SFNPCCommunication

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 |
    //=============================================================================
    // Spatial Fear
    // Class: SFNPCCommunication
    // Description: Communication subclass for NPC communication.
    //				List of additional tags:
    //				<changeViewPoint:number> Changes the viewpoint of the communication, according to the specified number:
    //								number = 0:	VP_Rotating,
    //								number = 1:	VP_BehindPlayer,
    //								number = 2:	VP_InFrontPlayer,
    //								number = 3:	VP_RightOfPlayer,
    //								number = 4:	VP_LeftOfPlayer,
    //								number = 5:	VP_ThisPosition,
    //								number = 6: VP_FirstPerson,
    //				<playanimsequence:string>	- plays a chain of animations, specified in the string parameter
    //										string = 1;3;2;4;4;3;5 . The sting is a comma separated string of numbers, that define the chain of animations.
    //										You can use SFSTARTAGAIN inside the comma separated string, e.g. 1;3;2;4;4;3;5;SFSTARTAGAIN to start the chain again, from the beginning, after the last sequence has been played,
    //										otherwise the actor stops playing an animation after having reached the end of the animation chain.
    //										You can use SFLOOPEND inside the comma separated string, e.g. 1;3;2;4;4;3;5;SFLOOPEND to make the actor play the last animation of the sequence as a looping one,
    //										in cases, where you don't want to let the deco actor in an unanimated state.
    //				<endanimsequence>	- ends any currently playing animation sequence
    //
    // Communication samples
    //
    // Author: Markus Nuebel
    //=============================================================================
    class SFNPCCommunication extends SFCommunication;

    enum EViewPoint
    {
    	VP_Rotating,
    	VP_BehindPlayer,
    	VP_InFrontPlayer,
    	VP_RightOfPlayer,
    	VP_LeftOfPlayer,
    	VP_ThisPosition,
    	VP_FirstPerson,
    } ;

    var (SpatialFear)	EViewPoint		ViewPoint;	// Type of viewpoint for this conversation. Default: VP_BehindPlayer
    var (SpatialFear)	float			fViewDist; // Distance of viewpoint from conversation. Default: 100UUs
    var (SpatialFear)	float			fViewHeight; // Height from ground. Default: 40UUs

    var float			fBobStep;
    var float			fBobHeight;
    var bool			bBobDown;
    var float			fHorizontalBobDir;
    var vector	m_vectViewLocation; // Private: Location, where to look at
    var rotator	m_rotDirection;	// Private: Used for rotating camera
    var vector	m_vectStartLocation; // Private: location at start

    function setupView()
    {
    	if(m_oEventInstigator.IsA('PlayerPawn'))
    			PlayerPawn(m_oEventInstigator).ViewTarget = self;

    }

    function resetView()
    {
    	PlayerPawn(m_oEventInstigator).ViewTarget = None;
    	// Restore the mesh
    	if(ViewPoint == VP_FirstPerson )
    		m_oEventInstigator.bHidden=false;
    }

    function calcLocation()
    {
    	local vector X,Y,Z;
    	local vector	vecLocation;

    	// No need to calculate viewpoint for
    	// first person view

    	switch(ViewPoint)
    	{
    		case VP_FirstPerson :
    			// Remove mesh for communication
    			m_oEventInstigator.bHidden=true;
    			// Set the new location
    			setLocation(PlayerPawn(m_oEventInstigator).Location + vect(0.0,0.0,1.0)*PlayerPawn(m_oEventInstigator).BaseEyeHeight*1.2);
    		break;
    		case VP_ThisPosition:
    			setLocation(m_vectStartLocation);
    		break;
    		case VP_BehindPlayer:
    			GetAxes(m_oEventInstigator.Rotation, X,Y,Z);
    			vecLocation = m_vectViewLocation - Normal(X)*fViewDist + vect(0,0,1)*fViewHeight;
    			setLocation(checkLocation(vecLocation));
    		break;
    		case VP_InFrontPlayer:
    			GetAxes(m_oEventInstigator.Rotation, X,Y,Z);
    			vecLocation = m_vectViewLocation + Normal(X)*fViewDist + vect(0,0,1)*fViewHeight;
    			setLocation(checkLocation(vecLocation));
    		break;
    		case VP_RightOfPlayer:
    			GetAxes(m_oEventInstigator.Rotation, X,Y,Z);
    			vecLocation = m_vectViewLocation + Normal(Y)*fViewDist + vect(0,0,1)*fViewHeight;
    			setLocation(checkLocation(vecLocation));
    		break;
    		case VP_LeftOfPlayer:
    			GetAxes(m_oEventInstigator.Rotation, X,Y,Z);
    			vecLocation = m_vectViewLocation - Normal(Y)*fViewDist + vect(0,0,1)*fViewHeight;
    			setLocation(checkLocation(vecLocation));
    		break;
    		case VP_Rotating:
    			if((0 == m_rotDirection.Yaw) && (0 == m_rotDirection.Roll) && (0 == m_rotDirection.Pitch))
    			{
    				// Init the rotator
    				GetAxes(m_oEventInstigator.Rotation, X,Y,Z);
    				vecLocation = m_vectViewLocation - Normal(X)*fViewDist + vect(0,0,1)*fViewHeight;
    				m_rotDirection	= rotator(vecLocation-m_vectViewLocation);
    			}
    			else
    			{
    				m_rotDirection.Yaw	= (m_rotDirection.Yaw + 60.0) % 65536; // Will leave rounding error, because of addition instead of absolut calculation.
    				vecLocation = m_vectViewLocation + Normal(vector(m_rotDirection)) * fViewDist;
    				setLocation(checkLocation(vecLocation));
    			}

    			break;
    	}
    }

    function vector checkLocation(vector vecOrg)
    {
    	local vector	vecViewDir;
    	local vector HitLocation, HitNormal;
    	local Actor	HitActor;

    	if(ViewPoint == VP_FirstPerson)
    		return vecOrg;

    	HitActor = Trace(HitLocation, HitNormal, vecOrg, m_vectViewLocation);
    	if ( HitActor == None )
    		return vecOrg;
    	else
    	{
    		vecViewDir	= m_vectViewLocation - HitLocation;
    		return HitLocation + Normal(vecViewDir)*5;
    	}

    }

    function startTracking()
    {
    	local rotator rotStartDir;
    	local rotator rotPlayerRot, rotDir;

    	// Save the start loc
    	m_vectStartLocation=Location;

    	// Calculate the viewing destination
    	m_vectViewLocation = m_oEventActor.Location + 0.5*(m_oEventInstigator.Location - m_oEventActor.Location);

    	// Calculates the viewpoint location
    	calcLocation();

    	// Change the view
    	setupView();

    	// Set initial rotation
        rotStartDir = rotator(m_vectViewLocation - Location);
        //rotStartDir.Roll = Rotation.Roll;
    	SetRotation(rotStartDir);
    	DesiredRotation = rotStartDir;

    	SetTimer(fBobStep, false);

    	// Calcualte the rotation
    	rotPlayerRot	= PlayerPawn(m_oEventInstigator).Rotation;
    	rotDir			= rotator(Normal(m_oEventActor.Location - m_oEventInstigator.Location));
    	rotPlayerRot.Yaw	= rotDir.Yaw;

    	// Pause level action
    	//PlayerPawn(m_oEventInstigator).PlayersOnly();
    	PlayerPawn(m_oEventInstigator).GotoState('PlayerChat');

    	PlayerPawn(m_oEventInstigator).SetRotation(rotDir);
    	PlayerPawn(m_oEventInstigator).DesiredRotation = rotator(m_vectViewLocation - Location);

    	// Start tracking
    	GotoState('Tracking');
    }

    function stopTracking()
    {
    	SetTimer(0.0f, false);

    	resetView();

    	// Resume level action
    //	PlayerPawn(m_oEventInstigator).PlayersOnly();
    	PlayerPawn(m_oEventInstigator).GotoState('PlayerWalking');

    	GotoState('Idle');
    }

    state Tracking
    {
    	function Tick( float DeltaTime )
    	{
    		local vector X,Y,Z;

    		if(ViewPoint != VP_FirstPerson)
    			m_oEventInstigator.bHidden = false;
    		else
    			m_oEventInstigator.bHidden = true;

    		GetAxes(Rotation, X,Y,Z);

    //		if(ViewPoint == VP_Rotating )
    			calcLocation();

    		setLocation(Location+(vect(0.0,0.0,-1.0)*fBobHeight)+(Normal(Y)*fBobHeight*fHorizontalBobDir));

    		if(ViewPoint != VP_FirstPerson)
    		{
    			DesiredRotation = rotator(m_vectViewLocation - Location);
    			SetRotation(rotator(m_vectViewLocation - Location));
    		}
    		else
    		{
    			DesiredRotation = rotator(m_vectViewLocation+vect(0.0,0.0,1.0)*PlayerPawn(m_oEventInstigator).BaseEyeHeight - Location);
    			SetRotation(rotator(m_vectViewLocation+vect(0.0,0.0,1.0)*PlayerPawn(m_oEventInstigator).BaseEyeHeight - Location));
    		}
    		//DesiredRotation.Roll = Rotation.Roll;
    	}

    	function Timer()
    	{
    		if(bBobDown)
    		{
    			fBobHeight -= 0.09 + (fBobHeight-6.0)*0.01;
    			if(fBobHeight <= 0)
    			{
    				bBobDown=false;
    				// Switch direction
    				fHorizontalBobDir *= -1.0f;
    			}
    		}
    		else
    		{
    			fBobHeight += 0.09 + (6.0f-fBobHeight)*0.01;
    			if(fBobHeight >= 6)
    				bBobDown=true;

    		}
    		SetTimer(fBobStep, false);
    	}
    Begin:
    SetPhysics(PHYS_Rotating);
    }

    // The idle state has nothing to do
    auto state Idle
    {
    }

    defaultproperties
    {
         ViewPoint=VP_ThisPosition
         fViewDist=100.000000
         fViewHeight=40.000000
         fBobStep=0.100000
         fHorizontalBobDir=1.000000
         CommunicationProtocol(0)="<animRate:0.8><loopanim:5>Hi guys, this is a NPC communication sample.<wait:5>\n<cls><branch:1>"
         CommunicationProtocol(1)="Let's do some viewpoint changes.<wait:2>\n<cls>ActorPosition view:<changeViewPoint:5><wait:3><cls>Rotating view:<changeViewPoint:0><wait:5><cls>Behind view:<changeViewPoint:1><wait:3><cls>InFront view:<changeViewPoint:2><wait:3><cls>Right view:<changeViewPoint:3><wait:3><cls>Left view:<changeViewPoint:4><wait:3><branch:2>"
         CommunicationProtocol(2)="<playanim:0>Now we are playing a single animation chain.<wait:3><playanimsequence:0;1;2;3;4><wait:1><cls>Ok, done.\nNow we are playing a looping animation chain using the param SFSTARTAGAIN.<wait:3><playanimsequence:0;1;2;3;4;SFSTARTAGAIN><wait:13><cls>\nNow let's stop this.<wait:2><endanimsequence><wait:2><cls>Now we are playing a chain that loops on the last chain element using the param SFLOOPEND.<wait:3><playanimsequence:0;1;2;3;4;SFLOOPEND><wait:13><cls>Ok, that's it for today.<wait:2><endanimsequence><playanim:0>"
         Animations(0)=GSitBase
         Animations(1)=GSitBreath
         Animations(2)=GSitLookUp
         Animations(3)=GSitLookRight
         Animations(4)=GSitGive
         Animations(5)=ChairSitTalk
         Animations(6)=AirVent
         Animations(7)=AirVentIn
         Animations(8)=Body4
         Type=CT_NPC
         bHidePlayerHUD=True
         RotationRate=(Pitch=6000,Yaw=6000,Roll=6000)
    }

class file time: 12/7/2003 3:58:24 PM - creation time: 12/7/2003 4:03:45 PM
Created with UnCodeX