rj-action-library-godot-dev.../Scripts/GameJam/Player/PlayerCamera.cs

114 lines
2.3 KiB
C#

using Godot;
using Rokojori;
namespace GameJam
{
[Tool]
[GlobalClass]
public partial class PlayerCamera:RJVirtualCamera3D
{
[Export]
public Node3D player;
[Export]
public RJSensor yawLeft;
[Export]
public RJSensor yawRight;
[Export]
public float yawSpeed;
[Export]
public float yawSmoothing = 0.1f;
[Export]
public float yaw;
[Export]
public float pitchSpeed;
[Export]
public float pitchSmoothing = 0.1f;
[Export]
public RJSensor pitchUp;
[Export]
public RJSensor pitchDown;
[Export]
public float minPitch;
[Export]
public float maxPitch;
[Export]
public float pitch;
[Export]
public float distance = 5;
[Export]
public Vector3 cameraOffset;
float smoothYaw = 0;
float smoothPitch = 0;
[Export]
public float positionSmoothing = 0.1f;
[Export]
public Vector3 position = new Vector3(0,0,0);
public float DeadZone( float value, float treshold )
{
return Mathf.Max( 0, ( value - treshold ) ) / ( 1 - treshold );
}
public float DeadZone( float value )
{
return DeadZone( value, 0.4f );
}
public override void _Process( double delta )
{
var yawValue = DeadZone( yawLeft.GetValue() ) - DeadZone( yawRight.GetValue() );
var pitchValue = DeadZone( pitchUp.GetValue() ) - DeadZone( pitchDown.GetValue() );
yaw += yawSpeed * yawValue;
pitch += pitchSpeed * pitchValue;
pitch = Mathf.Clamp( pitch, minPitch, maxPitch );
smoothYaw += yawSmoothing * ( yaw - smoothYaw );
smoothPitch += pitchSmoothing * ( pitch - smoothPitch );
GlobalRotation = new Vector3( Mathf.DegToRad( smoothPitch ), Mathf.DegToRad( smoothYaw ), 0 );
var offset = GlobalBasis.Z * distance;
position = position.Lerp( player.GlobalPosition, positionSmoothing );
GlobalPosition = position + offset + cameraOffset;
}
[Export]
public float fov = 60;
public override Vector3 GetCameraPosition()
{
return GlobalPosition;
}
public override Quaternion GetCameraRotation()
{
return GlobalBasis.GetRotationQuaternion();
}
public override float GetCameraFOV()
{
return fov;
}
}
}