Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Aggregator proposal #424

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UOP1_Project/Assets/Prefabs/Characters/PigChef.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_inputReader: {fileID: 11400000, guid: 945ec0365077176418488737deed54be, type: 2}
_eventAggregator: {fileID: 11400000, guid: c92bfb91ff27b83459c86d7413b0081a, type: 2}
gameplayCameraTransform: {fileID: 11400000, guid: bc205269957643647a8b5f98f028f9fb,
type: 2}
_openInventoryChannel: {fileID: 11400000, guid: 30f6db2122a30480b996908173e1c7d7,
Expand Down
2 changes: 1 addition & 1 deletion UOP1_Project/Assets/Prefabs/Gameplay/CameraSystem.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 085156cba0e34b540aeddafe12d1e2f1, type: 3}
m_Name:
m_EditorClassIdentifier:
inputReader: {fileID: 11400000, guid: 945ec0365077176418488737deed54be, type: 2}
eventBus: {fileID: 11400000, guid: b4774447b4f72b8408dbd98248f139ca, type: 2}
mainCamera: {fileID: 8745341642014614481}
freeLookVCam: {fileID: 8745341641394998849}
_speedMultiplier: 0.5
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%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: c3683d4b74c443d4a48ca7b36086f246, type: 3}
m_Name: EventAggregatorSO
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions UOP1_Project/Assets/ScriptableObjects/Events/UnityEventBusSO.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%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: c44145970255443c86929d77c7ac2d2a, type: 3}
m_Name: UnityEventBusSO
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions UOP1_Project/Assets/Scripts/Camera/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

public class CameraManager : MonoBehaviour
{
public InputReader inputReader;
[SerializeField] private UnityEventBusSO eventBus;

public Camera mainCamera;
public CinemachineFreeLook freeLookVCam;
private bool _isRMBPressed;

[SerializeField, Range(.5f, 3f)]
private float _speedMultiplier = 1f; //TODO: make this modifiable in the game settings
private float _speedMultiplier = 1f; //TODO: make this modifiable in the game settings
[SerializeField] private TransformAnchor _cameraTransformAnchor = default;

[Header("Listening on channels")]
Expand All @@ -30,9 +31,9 @@ public void SetupProtagonistVirtualCamera(Transform target)

private void OnEnable()
{
inputReader.cameraMoveEvent += OnCameraMove;
inputReader.enableMouseControlCameraEvent += OnEnableMouseControlCamera;
inputReader.disableMouseControlCameraEvent += OnDisableMouseControlCamera;
eventBus.Subscribe<CameraMoveEvent>(OnCameraMove);
eventBus.Subscribe<EnableMouseControlEvent>(OnEnableMouseControlCamera);
eventBus.Subscribe<DisableMouseControlEvent>(OnDisableMouseControlCamera);

if (_frameObjectChannel != null)
_frameObjectChannel.OnEventRaised += OnFrameObjectEvent;
Expand All @@ -42,15 +43,15 @@ private void OnEnable()

private void OnDisable()
{
inputReader.cameraMoveEvent -= OnCameraMove;
inputReader.enableMouseControlCameraEvent -= OnEnableMouseControlCamera;
inputReader.disableMouseControlCameraEvent -= OnDisableMouseControlCamera;
eventBus.Unsubscribe<CameraMoveEvent>(OnCameraMove);
eventBus.Unsubscribe<EnableMouseControlEvent>(OnEnableMouseControlCamera);
eventBus.Unsubscribe<DisableMouseControlEvent>(OnDisableMouseControlCamera);

if (_frameObjectChannel != null)
_frameObjectChannel.OnEventRaised -= OnFrameObjectEvent;
}

private void OnEnableMouseControlCamera()
private void OnEnableMouseControlCamera(EnableMouseControlEvent evt)
{
_isRMBPressed = true;

Expand All @@ -67,7 +68,7 @@ IEnumerator DisableMouseControlForFrame()
_cameraMovementLock = false;
}

private void OnDisableMouseControlCamera()
private void OnDisableMouseControlCamera(DisableMouseControlEvent evt)
{
_isRMBPressed = false;

Expand All @@ -80,16 +81,16 @@ private void OnDisableMouseControlCamera()
freeLookVCam.m_YAxis.m_InputAxisValue = 0;
}

private void OnCameraMove(Vector2 cameraMovement, bool isDeviceMouse)
private void OnCameraMove(CameraMoveEvent moveEvent)
{
if (_cameraMovementLock)
return;

if (isDeviceMouse && !_isRMBPressed)
if (moveEvent.IsDeviceMouse && !_isRMBPressed)
return;

freeLookVCam.m_XAxis.m_InputAxisValue = cameraMovement.x * Time.deltaTime * _speedMultiplier;
freeLookVCam.m_YAxis.m_InputAxisValue = cameraMovement.y * Time.deltaTime * _speedMultiplier;
freeLookVCam.m_XAxis.m_InputAxisValue = moveEvent.Movement.x * Time.deltaTime * _speedMultiplier;
freeLookVCam.m_YAxis.m_InputAxisValue = moveEvent.Movement.y * Time.deltaTime * _speedMultiplier;
}

private void OnFrameObjectEvent(Transform value)
Expand Down
68 changes: 34 additions & 34 deletions UOP1_Project/Assets/Scripts/Characters/Protagonist.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using System;
using UnityEngine;
using static EventAggregator;

/// <summary>
/// <para>This component consumes input on the InputReader and stores its values. The input is then read, and manipulated, by the StateMachines's Actions.</para>
/// </summary>
public class Protagonist : MonoBehaviour
public class Protagonist : MonoBehaviour,
IHandle<AttackEvent>,
IHandle<JumpEvent>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming here is gorgeous!

IHandle<JumpCancelledEvent>,
IHandle<MoveEvent>,
IHandle<StartedRunningEvent>,
IHandle<StoppedRunningEvent>,
IHandle<OpenInventoryEvent>
{
[SerializeField] private InputReader _inputReader = default;
[SerializeField] private EventAggregatorSO _eventAggregator;

public TransformAnchor gameplayCameraTransform;

[SerializeField] private VoidEventChannelSO _openInventoryChannel = default;
Expand Down Expand Up @@ -38,27 +47,13 @@ private void OnControllerColliderHit(ControllerColliderHit hit)
//Adds listeners for events being triggered in the InputReader script
private void OnEnable()
{
_inputReader.jumpEvent += OnJumpInitiated;
_inputReader.jumpCanceledEvent += OnJumpCanceled;
_inputReader.moveEvent += OnMove;
_inputReader.openInventoryEvent += OnOpenInventory;
_inputReader.startedRunning += OnStartedRunning;
_inputReader.stoppedRunning += OnStoppedRunning;
_inputReader.attackEvent += OnStartedAttack;
//...
_eventAggregator.Subscribe(this);
}

//Removes all listeners to the events coming from the InputReader script
private void OnDisable()
{
_inputReader.jumpEvent -= OnJumpInitiated;
_inputReader.jumpCanceledEvent -= OnJumpCanceled;
_inputReader.moveEvent -= OnMove;
_inputReader.openInventoryEvent -= OnOpenInventory;
_inputReader.startedRunning -= OnStartedRunning;
_inputReader.stoppedRunning -= OnStoppedRunning;
_inputReader.attackEvent -= OnStartedAttack;
//...
_eventAggregator.Unsubscribe(this);
}

private void Update()
Expand Down Expand Up @@ -107,37 +102,42 @@ private void RecalculateMovement()
_previousSpeed = targetSpeed;
}

//---- EVENT LISTENERS ----
// Triggered from Animation Event
public void ConsumeAttackInput() => attackInput = false;

private void OnMove(Vector2 movement)
public void Handle(AttackEvent msg)
{
_inputVector = movement;
attackInput = true;
}

private void OnJumpInitiated()
public void Handle(JumpEvent msg)
{
jumpInput = true;
}

private void OnJumpCanceled()
public void Handle(JumpCancelledEvent msg)
{
jumpInput = false;
}

private void OnStoppedRunning() => isRunning = false;

private void OnStartedRunning() => isRunning = true;

private void OnOpenInventory()
public void Handle(MoveEvent msg)
{
_openInventoryChannel.RaiseEvent();


_inputVector = msg.Movement;
}

public void Handle(StartedRunningEvent msg)
{
isRunning = true;
}

private void OnStartedAttack() => attackInput = true;
public void Handle(StoppedRunningEvent msg)
{
isRunning = false;
}

// Triggered from Animation Event
public void ConsumeAttackInput() => attackInput = false;
public void Handle(OpenInventoryEvent msg)
{ // Clearly this event should be handled elsewhere and not on the
// protagonist but for now limited changes.
_openInventoryChannel.RaiseEvent();
}
}
Loading