101 lines
3.7 KiB
C#
101 lines
3.7 KiB
C#
using System;
|
|
using RebootKit.Engine.Foundation;
|
|
using RebootKit.Engine.Main;
|
|
using RebootKit.Engine.Simulation.Sensors;
|
|
using RebootReality.jelycho.Main;
|
|
using Unity.Cinemachine;
|
|
using UnityEngine;
|
|
using UnityEngine.Assertions;
|
|
|
|
namespace RebootReality.jelycho.Player {
|
|
[AddComponentMenu(GameConsts.k_AddComponentMenu + "Player/First Person Camera")]
|
|
public class FPPCamera : MonoBehaviour {
|
|
[ConfigVar("fpp.camera.fov", 60.0f, "Field of view for the first person camera.")]
|
|
static ConfigVar s_CameraFOV;
|
|
|
|
[Header("Base")]
|
|
[field: SerializeField]
|
|
public float Sensitivity { get; set; }
|
|
|
|
[SerializeField] float m_PitchMin = -80f;
|
|
[SerializeField] float m_PitchMax = 80f;
|
|
|
|
[field: SerializeField]
|
|
public CinemachineCamera Camera { get; private set; }
|
|
|
|
CinemachineBasicMultiChannelPerlin m_NoiseComponent;
|
|
|
|
[SerializeField] SignalSourceAsset m_BobbingNoiseSettings;
|
|
[SerializeField] float m_BobbingFrequency = 0.5f;
|
|
[SerializeField] float m_BobbingAmplitude = 0.75f;
|
|
|
|
float m_BobbingIntensity = 0.0f;
|
|
|
|
[SerializeField] SignalSourceAsset m_ShakeNoiseSettings;
|
|
[SerializeField] float m_ShakeFrequency = 1.0f;
|
|
[SerializeField] float m_ShakeAmplitude = 1.0f;
|
|
|
|
[Header("Picking")]
|
|
[SerializeField] float m_PickDistance = 5.0f;
|
|
[SerializeField] LayerMask m_PickLayer;
|
|
|
|
readonly RaycastSensor m_RaycastSensor = new RaycastSensor();
|
|
|
|
[NonSerialized] public float Pitch;
|
|
[NonSerialized] public float Yaw;
|
|
|
|
public ISensor Sensor {
|
|
get {
|
|
m_RaycastSensor.ray = new Ray(Camera.transform.position, Camera.transform.forward);
|
|
m_RaycastSensor.maxDistance = m_PickDistance;
|
|
m_RaycastSensor.layerMask = m_PickLayer;
|
|
return m_RaycastSensor;
|
|
}
|
|
}
|
|
|
|
void Awake() {
|
|
m_NoiseComponent =
|
|
Camera.GetCinemachineComponent(CinemachineCore.Stage.Noise) as CinemachineBasicMultiChannelPerlin;
|
|
Assert.IsNotNull(m_NoiseComponent);
|
|
|
|
SetBobbing(0.0f);
|
|
}
|
|
|
|
public void Tick(float deltaTime) {
|
|
Camera.Lens.FieldOfView = s_CameraFOV.FloatValue;
|
|
// Camera.transform.localRotation = Quaternion.Euler(Pitch, 0f, 0f);
|
|
|
|
if (RR.World.Context is WorldContext worldContext) {
|
|
float shakeIntensity = worldContext.FeedbacksManager.GetShakeIntensityForPosition(transform.position);
|
|
|
|
if (shakeIntensity > 0.0f) {
|
|
m_NoiseComponent.NoiseProfile = m_ShakeNoiseSettings as NoiseSettings;
|
|
m_NoiseComponent.AmplitudeGain = m_ShakeAmplitude * shakeIntensity;
|
|
m_NoiseComponent.FrequencyGain = m_ShakeFrequency * shakeIntensity;
|
|
} else {
|
|
m_NoiseComponent.NoiseProfile = m_BobbingNoiseSettings as NoiseSettings;
|
|
m_NoiseComponent.AmplitudeGain = m_BobbingAmplitude * m_BobbingIntensity;
|
|
m_NoiseComponent.FrequencyGain = m_BobbingFrequency * m_BobbingIntensity;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Rotate(float x, float y) {
|
|
float sens = Sensitivity;
|
|
|
|
Pitch -= y * sens;
|
|
Pitch = Mathf.Clamp(Pitch, m_PitchMin, m_PitchMax);
|
|
|
|
Yaw += x * sens;
|
|
}
|
|
|
|
public void SetLookDirection(Vector3 forward) {
|
|
Pitch = Mathf.Asin(-forward.y) * Mathf.Rad2Deg;
|
|
Yaw = Mathf.Atan2(forward.x, forward.z) * Mathf.Rad2Deg;
|
|
}
|
|
|
|
public void SetBobbing(float t) {
|
|
m_BobbingIntensity = t;
|
|
}
|
|
}
|
|
} |