浏览代码

More code refactoring around Anchors

/main
Ciro Continisio 3 年前
当前提交
202b38ba
共有 5 个文件被更改,包括 42 次插入37 次删除
  1. 16
      UOP1_Project/Assets/Prefabs/Gameplay/CameraSystem.prefab
  2. 41
      UOP1_Project/Assets/Scripts/Camera/CameraManager.cs
  3. 8
      UOP1_Project/Assets/Scripts/Characters/Protagonist.cs
  4. 4
      UOP1_Project/Assets/Scripts/Gameplay/SpawnSystem.cs
  5. 10
      UOP1_Project/Assets/Scripts/RuntimeAnchors/RuntimeAnchorBase.cs

16
UOP1_Project/Assets/Prefabs/Gameplay/CameraSystem.prefab


ModeOverride: 0
LensShift: {x: 0, y: 0}
GateFit: 2
m_SensorSize: {x: 1.7773722, y: 1}
m_SensorSize: {x: 1.7777778, y: 1}
m_Transitions:
m_BlendHint: 0
m_InheritPosition: 0

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5457523176376150323}
m_LocalRotation: {x: 0.029291233, y: 0.955957, z: -0.22923526, w: 0.1809406}
m_LocalRotation: {x: 0.15980278, y: -0.00000012420624, z: 0.00000002459104, w: 0.98714894}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:

ModeOverride: 0
LensShift: {x: 0, y: 0}
GateFit: 2
m_SensorSize: {x: 1.7773722, y: 1}
m_SensorSize: {x: 1.7777778, y: 1}
m_Transitions:
m_BlendHint: 0
m_InheritPosition: 0

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6333144650067166509}
m_LocalRotation: {x: 0.02468605, y: 0.96147096, z: -0.20488085, w: 0.18162619}
m_LocalRotation: {x: 0.10067279, y: -0.000000012171338, z: 0.0000000020894737, w: 0.9949196}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:

ModeOverride: 0
LensShift: {x: 0, y: 0}
GateFit: 2
m_SensorSize: {x: 1.7773722, y: 1}
m_SensorSize: {x: 1.7777778, y: 1}
m_Transitions:
m_BlendHint: 0
m_InheritPosition: 0

_speedMultiplier: 1
_cameraTransformAnchor: {fileID: 11400000, guid: bc205269957643647a8b5f98f028f9fb,
type: 2}
_frameObjectChannel: {fileID: 11400000, guid: 2723b3f59f7ede3498fe7e385d2bb6ee,
_protagonistTransformAnchor: {fileID: 11400000, guid: 35fc4039342b6ba458d0d4429e89ee74,
type: 2}
_camShakeEvent: {fileID: 11400000, guid: 3d8e5223e1e74274cbb375151106b21e, type: 2}
--- !u!1 &8745341641394998848

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8745341641394998848}
m_LocalRotation: {x: 0.00007046065, y: -0.98033255, z: 0.07314793, w: 0.18329611}
m_LocalRotation: {x: 0.034622885, y: 0.00000001234202, z: -4.2647538e-10, w: 0.99940044}
m_LocalPosition: {x: 0, y: 4, z: -9}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:

ModeOverride: 0
LensShift: {x: 0, y: 0}
GateFit: 2
m_SensorSize: {x: 1.7773722, y: 1}
m_SensorSize: {x: 1.7777778, y: 1}
m_Transitions:
m_BlendHint: 0
m_InheritPosition: 1

41
UOP1_Project/Assets/Scripts/Camera/CameraManager.cs


public CinemachineImpulseSource impulseSource;
private bool _isRMBPressed;
[SerializeField, Range(.5f, 3f)]
private float _speedMultiplier = 1f; //TODO: make this modifiable in the game settings
[SerializeField][Range(.5f, 3f)] private float _speedMultiplier = 1f; //TODO: make this modifiable in the game settings
[SerializeField] private TransformAnchor _protagonistTransformAnchor = default;
[Tooltip("The CameraManager listens to this event, fired by objects in any scene, to adapt camera position")]
[SerializeField] private TransformEventChannelSO _frameObjectChannel = default;
public void SetupProtagonistVirtualCamera(Transform target)
{
freeLookVCam.Follow = target;
freeLookVCam.LookAt = target;
freeLookVCam.OnTargetObjectWarped(target, target.position - freeLookVCam.transform.position - Vector3.forward);
}
private void OnEnable()
{
inputReader.cameraMoveEvent += OnCameraMove;

_frameObjectChannel.OnEventRaised += OnFrameObjectEvent;
_protagonistTransformAnchor.OnAnchorProvided += SetupProtagonistVirtualCamera;
_cameraTransformAnchor.Value = mainCamera.transform;
}
private void OnDisable()

inputReader.disableMouseControlCameraEvent -= OnDisableMouseControlCamera;
_frameObjectChannel.OnEventRaised -= OnFrameObjectEvent;
_protagonistTransformAnchor.OnAnchorProvided -= SetupProtagonistVirtualCamera;
}
private void Start()
{
//Setup the camera target if the protagonist is already available
if(_protagonistTransformAnchor.isSet)
SetupProtagonistVirtualCamera();
_cameraTransformAnchor.Value = mainCamera.transform;
}
private void OnEnableMouseControlCamera()

freeLookVCam.m_YAxis.m_InputAxisValue = cameraMovement.y * deviceMultiplier * _speedMultiplier;
}
private void OnFrameObjectEvent(Transform value)
/// <summary>
/// Provides Cinemachine with its target, taken from the TransformAnchor SO containing a reference to the player's Transform component.
/// This method is called every time the player is reinstantiated.
/// </summary>
public void SetupProtagonistVirtualCamera()
SetupProtagonistVirtualCamera(value);
Transform target = _protagonistTransformAnchor.Value;
freeLookVCam.Follow = target;
freeLookVCam.LookAt = target;
freeLookVCam.OnTargetObjectWarped(target, target.position - freeLookVCam.transform.position - Vector3.forward);
}
}

8
UOP1_Project/Assets/Scripts/Characters/Protagonist.cs


public class Protagonist : MonoBehaviour
{
[SerializeField] private InputReader _inputReader = default;
public TransformAnchor gameplayCameraTransform;
[SerializeField] private TransformAnchor _gameplayCameraTransform = default;
private Vector2 _inputVector;
private float _previousSpeed;

float targetSpeed;
Vector3 adjustedMovement;
if (gameplayCameraTransform.isSet)
if (_gameplayCameraTransform.isSet)
Vector3 cameraForward = gameplayCameraTransform.Value.forward;
Vector3 cameraForward = _gameplayCameraTransform.Value.forward;
Vector3 cameraRight = gameplayCameraTransform.Value.right;
Vector3 cameraRight = _gameplayCameraTransform.Value.right;
cameraRight.y = 0f;
//Use the two axes, modulated by the corresponding inputs, and construct the final vector

4
UOP1_Project/Assets/Scripts/Gameplay/SpawnSystem.cs


{
Protagonist playerInstance = InstantiatePlayer(_playerPrefab, GetSpawnLocation());
_playerInstantiatedChannel.RaiseEvent(playerInstance.transform); // The CameraSystem will pick this up to frame the player
_playerTransformAnchor.Value = playerInstance.transform;
_playerInstantiatedChannel.RaiseEvent(playerInstance.transform);
_playerTransformAnchor.Value = playerInstance.transform; //the CameraSystem will pick this up to frame the player
//TODO: Probably move this to the GameManager once it's up and running
_inputReader.EnableGameplayInput();

10
UOP1_Project/Assets/Scripts/RuntimeAnchors/RuntimeAnchorBase.cs


{
[HideInInspector] public bool isSet = false; // Any script can check if the transform is null before using it, by just checking this bool
public UnityAction AnchorProvided;
public UnityAction OnAnchorProvided;
private T _value;
public T Value

isSet = _value != null;
//Notify whoever is waiting for this anchor
if(AnchorProvided != null)
if(OnAnchorProvided != null)
AnchorProvided.Invoke();
Debug.Log(AnchorProvided.GetInvocationList()[0].Method.Name);
OnAnchorProvided.Invoke();
Debug.Log(OnAnchorProvided.GetInvocationList()[0].Method.Name);
public void OnDisable()
private void OnDisable()
{
_value = null;
isSet = false;
正在加载...
取消
保存