浏览代码

Initial decoupling of CameraManager and SpawnSystem. Spawn system still needs reference to CameraManager, because it must provide it to Protagonist.

/main
Jan Urbanec 4 年前
当前提交
347a978e
共有 6 个文件被更改,包括 71 次插入8 次删除
  1. 15
      UOP1_Project/Assets/Scripts/CameraManager.cs
  2. 14
      UOP1_Project/Assets/Scripts/SpawnSystem.cs
  3. 14
      UOP1_Project/Assets/ScriptableObjects/EventChannels/FrameObjectEvent_Channel.asset
  4. 8
      UOP1_Project/Assets/ScriptableObjects/EventChannels/FrameObjectEvent_Channel.asset.meta
  5. 17
      UOP1_Project/Assets/Scripts/Events/ScriptableObjects/TransformEventChannelSO.cs
  6. 11
      UOP1_Project/Assets/Scripts/Events/ScriptableObjects/TransformEventChannelSO.cs.meta

15
UOP1_Project/Assets/Scripts/CameraManager.cs


[SerializeField, Range(1f, 5f)]
private float speed = default;
[Header("Listening on channels")]
[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;

inputReader.cameraMoveEvent += OnCameraMove;
inputReader.enableMouseControlCameraEvent += OnEnableMouseControlCamera;
inputReader.disableMouseControlCameraEvent += OnDisableMouseControlCamera;
if(_frameObjectChannel != null)
_frameObjectChannel.OnEventRaised += OnFrameObjectEvent;
}
private void OnDisable()

inputReader.disableMouseControlCameraEvent -= OnDisableMouseControlCamera;
if (_frameObjectChannel != null)
_frameObjectChannel.OnEventRaised -= OnFrameObjectEvent;
}
private void OnEnableMouseControlCamera()

freeLookVCam.m_XAxis.m_InputAxisValue = cameraMovement.x * Time.smoothDeltaTime * speed;
freeLookVCam.m_YAxis.m_InputAxisValue = cameraMovement.y * Time.smoothDeltaTime * speed;
}
private void OnFrameObjectEvent(Transform value)
{
SetupProtagonistVirtualCamera(value);
}
}

14
UOP1_Project/Assets/Scripts/SpawnSystem.cs


[SerializeField] private Protagonist _playerPrefab = null;
[Header("Scene References")]
[SerializeField] private CameraManager _cameraManager;
[SerializeField] private TransformEventChannelSO _frameObjectChannel;
void Start()
{

[ContextMenu("Attempt Auto Fill")]
private void AutoFill()
{
if (_cameraManager == null)
_cameraManager = FindObjectOfType<CameraManager>();
if (_spawnLocations == null || _spawnLocations.Length == 0)
_spawnLocations = transform.GetComponentsInChildren<Transform>(true)
.Where(t => t != this.transform)

private void Spawn(int spawnIndex)
{
Transform spawnLocation = GetSpawnLocation(spawnIndex, _spawnLocations);
Protagonist playerInstance = InstantiatePlayer(_playerPrefab, spawnLocation, _cameraManager);
Protagonist playerInstance = InstantiatePlayer(_playerPrefab, spawnLocation);
SetupCameras(playerInstance);
}

return spawnLocations[index];
}
private Protagonist InstantiatePlayer(Protagonist playerPrefab, Transform spawnLocation, CameraManager _cameraManager)
private Protagonist InstantiatePlayer(Protagonist playerPrefab, Transform spawnLocation)
{
if (playerPrefab == null)
throw new Exception("Player Prefab can't be null.");

private void SetupCameras(Protagonist player)
{
player.gameplayCamera = _cameraManager.mainCamera.transform;
_cameraManager.SetupProtagonistVirtualCamera(player.transform);
//TODO: update this hacky assignment of mainCamera
player.gameplayCamera = UnityEngine.Object.FindObjectOfType<CameraManager>().mainCamera.transform;
_frameObjectChannel.RaiseEvent(player.transform);
}
}

14
UOP1_Project/Assets/ScriptableObjects/EventChannels/FrameObjectEvent_Channel.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e5d62472420234949aa61408039f315b, type: 3}
m_Name: FrameObjectEvent_Channel
m_EditorClassIdentifier:

8
UOP1_Project/Assets/ScriptableObjects/EventChannels/FrameObjectEvent_Channel.asset.meta


fileFormatVersion: 2
guid: 2723b3f59f7ede3498fe7e385d2bb6ee
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

17
UOP1_Project/Assets/Scripts/Events/ScriptableObjects/TransformEventChannelSO.cs


using UnityEngine.Events;
using UnityEngine;
/// <summary>
/// This class is used for Events that have one transform argument.
/// Example: Spawn system initializes player and fire event, where the transform is the position of player.
/// </summary>
[CreateAssetMenu(menuName = "Events/Transform Event Channel")]
public class TransformEventChannelSO : ScriptableObject
{
public UnityAction<Transform> OnEventRaised;
public void RaiseEvent(Transform value)
{
OnEventRaised.Invoke(value);
}
}

11
UOP1_Project/Assets/Scripts/Events/ScriptableObjects/TransformEventChannelSO.cs.meta


fileFormatVersion: 2
guid: e5d62472420234949aa61408039f315b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存