Switched from UnityEvent to System.Action

This commit is contained in:
Syntriax 2022-11-13 18:07:54 +03:00
parent 8a1fdc6f9e
commit d76675b6f1
22 changed files with 44 additions and 57 deletions

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 2c348589c341f3d4f8db2e34198ec32e guid: 8747cc51a59e7a14fae4687c7136d15b
folderAsset: yes folderAsset: yes
DefaultImporter: DefaultImporter:
externalObjects: {} externalObjects: {}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 91b4c4b7439cfcb448a15d0f2ce12ff9 guid: 8dc876763de82b74cb9a05685d57e841
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 1213a467d17883844b2dc54d266cf211 guid: df412a9878b7d5b43ab34aef7261ec3a
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 8a31fdb05e206b44cbe615353e924344 guid: d1b41d4e3ee5ce24eaef42d5c8fc8c3a
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,5 +1,5 @@
using System;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.ColliderTrigger namespace Syntriax.Modules.Movement.ColliderTrigger
{ {
@ -7,6 +7,6 @@ namespace Syntriax.Modules.Movement.ColliderTrigger
{ {
LayerMask ColliderMask { get; set; } LayerMask ColliderMask { get; set; }
bool IsTrigerred { get; } bool IsTrigerred { get; }
UnityEvent<bool> OnTriggered { get; } Action<bool> OnTriggered { get; set; }
} }
} }

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 8ab2264f0e385af4d8decc8750297a9e guid: e199ccade2250b646abfd859e4aa1e7a
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,5 +1,5 @@
using System;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.ColliderTrigger namespace Syntriax.Modules.Movement.ColliderTrigger
{ {
@ -11,7 +11,7 @@ namespace Syntriax.Modules.Movement.ColliderTrigger
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; } public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public UnityEvent<bool> OnTriggered { get; protected set; } = null; public Action<bool> OnTriggered { get; set; } = null;
public bool IsTrigerred public bool IsTrigerred
{ {
get => _isTrigerred; get => _isTrigerred;
@ -26,9 +26,6 @@ namespace Syntriax.Modules.Movement.ColliderTrigger
} }
} }
protected void Awake()
=> OnTriggered = new UnityEvent<bool>();
protected void FixedUpdate() protected void FixedUpdate()
=> IsTrigerred = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, results, ColliderMask) > 0; => IsTrigerred = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, results, ColliderMask) > 0;

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 090650329160e7b42ba89ccfad00375e guid: cbd3633509c80ca49b6ae013c990bdbe
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,4 +1,4 @@
using UnityEngine.Events; using System;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
{ {
@ -9,7 +9,7 @@ namespace Syntriax.Modules.Movement
bool CanTakeOver { get; } bool CanTakeOver { get; }
UnityEvent<bool> OnTakeOverStateChanged { get; } Action<bool> OnTakeOverStateChanged { get; set; }
void Move(float x = 0f, float y = 0f, float z = 0f); void Move(float x = 0f, float y = 0f, float z = 0f);
void ApplyMovement(); void ApplyMovement();

View File

@ -1,5 +1,5 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
{ {
@ -8,8 +8,8 @@ namespace Syntriax.Modules.Movement
IMovement ActiveMovement { get; } IMovement ActiveMovement { get; }
List<IMovement> Movements { get; } List<IMovement> Movements { get; }
UnityEvent<IMovement> OnMovementDeactivated { get; } Action<IMovement> OnMovementDeactivated { get; set; }
UnityEvent<IMovement> OnMovementActivated { get; } Action<IMovement> OnMovementActivated { get; set; }
void RecacheMovements(); void RecacheMovements();
} }

View File

@ -12,7 +12,7 @@ namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{ {
base.Start(); base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>(); groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered.AddListener(OnGroundTrigger); groundTrigger.OnTriggered += OnGroundTrigger;
} }
private void OnGroundTrigger(bool isGrounded) private void OnGroundTrigger(bool isGrounded)

View File

@ -12,7 +12,7 @@ namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{ {
base.Start(); base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>(); groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered.AddListener(OnGroundTrigger); groundTrigger.OnTriggered += OnGroundTrigger;
} }
private void OnGroundTrigger(bool isGrounded) private void OnGroundTrigger(bool isGrounded)

View File

@ -12,7 +12,7 @@ namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{ {
base.Start(); base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>(); groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered.AddListener(OnGroundTrigger); groundTrigger.OnTriggered += OnGroundTrigger;
} }
private void OnGroundTrigger(bool isGrounded) private void OnGroundTrigger(bool isGrounded)

View File

@ -1,6 +1,6 @@
using System;
using Syntriax.Modules.Movement.State; using Syntriax.Modules.Movement.State;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.Implementations.TwoDimensional namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{ {
@ -15,7 +15,7 @@ namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
public float BaseSpeed { get; set; } = 1f; public float BaseSpeed { get; set; } = 1f;
public float MovementMultiplier { get; set; } = 1f; public float MovementMultiplier { get; set; } = 1f;
public UnityEvent<bool> OnTakeOverStateChanged { get; protected set; } = null; public Action<bool> OnTakeOverStateChanged { get; set; } = null;
public bool CanTakeOver public bool CanTakeOver
{ {
get => _canTakeOver; get => _canTakeOver;
@ -32,19 +32,14 @@ namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
public IToggleState ToggleState { get; protected set; } = null; public IToggleState ToggleState { get; protected set; } = null;
protected virtual void Awake()
{
OnTakeOverStateChanged = new UnityEvent<bool>();
}
protected virtual void Start() protected virtual void Start()
{ {
rigid = GetComponent<Rigidbody2D>(); rigid = GetComponent<Rigidbody2D>();
toggleState = GetComponent<ToggleState>(); toggleState = GetComponent<ToggleState>();
movementController = GetComponent<IMovementController>(); movementController = GetComponent<IMovementController>();
movementController.OnMovementActivated.AddListener(OnActivated); movementController.OnMovementActivated += OnActivated;
movementController.OnMovementDeactivated.AddListener(OnDeactivated); movementController.OnMovementDeactivated += OnDeactivated;
} }
protected abstract void OnDeactivated(); protected abstract void OnDeactivated();

View File

@ -1,14 +1,14 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Syntriax.Modules.Movement.State; using Syntriax.Modules.Movement.State;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
{ {
public class MovementController : MonoBehaviour, IMovementController public class MovementController : MonoBehaviour, IMovementController
{ {
public UnityEvent<IMovement> OnMovementDeactivated { get; protected set; } = null; public Action<IMovement> OnMovementDeactivated { get; set; } = null;
public UnityEvent<IMovement> OnMovementActivated { get; protected set; } = null; public Action<IMovement> OnMovementActivated { get; set; } = null;
private IMovement _activeMovement = null; private IMovement _activeMovement = null;
public IMovement ActiveMovement public IMovement ActiveMovement
@ -35,8 +35,6 @@ namespace Syntriax.Modules.Movement
protected virtual void Awake() protected virtual void Awake()
{ {
OnMovementDeactivated = new UnityEvent<IMovement>();
OnMovementActivated = new UnityEvent<IMovement>();
Movements = new List<IMovement>(32); Movements = new List<IMovement>(32);
} }
@ -57,14 +55,14 @@ namespace Syntriax.Modules.Movement
public virtual void RecacheMovements() public virtual void RecacheMovements()
{ {
foreach (IMovement movement in Movements) foreach (IMovement movement in Movements)
movement.OnTakeOverStateChanged.RemoveListener(OnTakeOverListener); movement.OnTakeOverStateChanged -= OnTakeOverListener;
Movements.Clear(); Movements.Clear();
GetComponents<IMovement>(Movements); GetComponents<IMovement>(Movements);
UpdateActiveMovement(); UpdateActiveMovement();
foreach (IMovement movement in Movements) foreach (IMovement movement in Movements)
movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener); movement.OnTakeOverStateChanged += OnTakeOverListener;
} }
private void OnTakeOverListener(bool arg0) => UpdateActiveMovement(); private void OnTakeOverListener(bool arg0) => UpdateActiveMovement();

View File

@ -1,10 +1,10 @@
using UnityEngine.Events; using System;
namespace Syntriax.Modules.Movement.State namespace Syntriax.Modules.Movement.State
{ {
public interface IToggleState public interface IToggleState
{ {
bool Toggled { get; set; } bool Toggled { get; set; }
UnityEvent<bool> OnToggleStateChanged { get; } Action<bool> OnToggleStateChanged { get; set; }
} }
} }

View File

@ -1,4 +1,4 @@
using UnityEngine.Events; using System;
namespace Syntriax.Modules.Movement.State namespace Syntriax.Modules.Movement.State
{ {
@ -20,6 +20,6 @@ namespace Syntriax.Modules.Movement.State
} }
} }
public UnityEvent<bool> OnToggleStateChanged { get; protected set; } = new UnityEvent<bool>(); public Action<bool> OnToggleStateChanged { get; set; } = null;
} }
} }

View File

@ -1,5 +1,5 @@
using System;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.State namespace Syntriax.Modules.Movement.State
{ {
@ -20,6 +20,6 @@ namespace Syntriax.Modules.Movement.State
} }
} }
public UnityEvent<bool> OnToggleStateChanged { get; protected set; } = new UnityEvent<bool>(); public Action<bool> OnToggleStateChanged { get; set; } = null;
} }
} }

View File

@ -1,4 +1,4 @@
using UnityEngine.Events; using System;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
namespace Syntriax.Modules.Movement.VariableMovement namespace Syntriax.Modules.Movement.VariableMovement
@ -6,7 +6,7 @@ namespace Syntriax.Modules.Movement.VariableMovement
public interface IVariableMovementController public interface IVariableMovementController
{ {
IVariableMovement ActiveVariableMovement { get; } IVariableMovement ActiveVariableMovement { get; }
UnityEvent<IVariableMovement> OnVariableMovementChanged { get; } Action<IVariableMovement> OnVariableMovementChanged { get; set; }
IInputActionCollection InputActionCollection { get; set; } IInputActionCollection InputActionCollection { get; set; }
void LoadVariableMovementCollection(VMCollection collection); void LoadVariableMovementCollection(VMCollection collection);

View File

@ -1,4 +1,4 @@
using UnityEngine.Events; using System;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
namespace Syntriax.Modules.Movement.VariableMovement namespace Syntriax.Modules.Movement.VariableMovement
@ -6,7 +6,7 @@ namespace Syntriax.Modules.Movement.VariableMovement
public interface IVariableMovementWithState : IVariableMovement public interface IVariableMovementWithState : IVariableMovement
{ {
bool Enabled { get; set; } bool Enabled { get; set; }
UnityEvent<bool> OnToggleStateChanged { get; } Action<bool> OnToggleStateChanged { get; set; }
IInputActionCollection InputCollection { set; } IInputActionCollection InputCollection { set; }
} }
} }

View File

@ -1,14 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
namespace Syntriax.Modules.Movement.VariableMovement namespace Syntriax.Modules.Movement.VariableMovement
{ {
public class VariableMovementController : MonoBehaviour, IVariableMovementController public class VariableMovementController : MonoBehaviour, IVariableMovementController
{ {
public UnityEvent<IVariableMovement> OnVariableMovementChanged { get; protected set; } = null; public Action<IVariableMovement> OnVariableMovementChanged { get; set; } = null;
private IInputActionCollection _inputActionCollection = null; private IInputActionCollection _inputActionCollection = null;
public IInputActionCollection InputActionCollection public IInputActionCollection InputActionCollection
{ {
@ -43,7 +42,6 @@ namespace Syntriax.Modules.Movement.VariableMovement
protected virtual void Awake() protected virtual void Awake()
{ {
OnVariableMovementChanged = new UnityEvent<IVariableMovement>();
defaultVariableMovement = new VariableMovement(); defaultVariableMovement = new VariableMovement();
variableMovementsWithState = new List<IVariableMovementWithState>(16); variableMovementsWithState = new List<IVariableMovementWithState>(16);
} }
@ -51,8 +49,8 @@ namespace Syntriax.Modules.Movement.VariableMovement
protected virtual void Start() protected virtual void Start()
{ {
movementController = GetComponent<IMovementController>(); movementController = GetComponent<IMovementController>();
movementController.OnMovementActivated.AddListener(SetMultiplierToActiveMovement); movementController.OnMovementActivated += SetMultiplierToActiveMovement;
movementController.OnMovementDeactivated.AddListener(ResetMultiplierToDefault); movementController.OnMovementDeactivated += ResetMultiplierToDefault;
} }
private void ResetMultiplierToDefault(IMovement currentMovement) private void ResetMultiplierToDefault(IMovement currentMovement)
@ -91,13 +89,13 @@ namespace Syntriax.Modules.Movement.VariableMovement
protected virtual void SubscribeToMovements() protected virtual void SubscribeToMovements()
{ {
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState) foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
variableMovementWithState.OnToggleStateChanged.AddListener(VariableMovementStateListener); variableMovementWithState.OnToggleStateChanged += VariableMovementStateListener;
} }
protected virtual void UnsubscribeFromMovements() protected virtual void UnsubscribeFromMovements()
{ {
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState) foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
variableMovementWithState.OnToggleStateChanged.RemoveListener(VariableMovementStateListener); variableMovementWithState.OnToggleStateChanged -= VariableMovementStateListener;
} }
private void VariableMovementStateListener(bool arg0) => UpdateActiveVariableMovement(); private void VariableMovementStateListener(bool arg0) => UpdateActiveVariableMovement();

View File

@ -1,11 +1,11 @@
using UnityEngine.Events; using System;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
namespace Syntriax.Modules.Movement.VariableMovement namespace Syntriax.Modules.Movement.VariableMovement
{ {
public class VariableMovementWithState : IVariableMovementWithState public class VariableMovementWithState : IVariableMovementWithState
{ {
public UnityEvent<bool> OnToggleStateChanged { get; protected set; } = null; public Action<bool> OnToggleStateChanged { get; set; } = null;
private bool _enabled = false; private bool _enabled = false;
public bool Enabled public bool Enabled
{ {
@ -52,7 +52,6 @@ namespace Syntriax.Modules.Movement.VariableMovement
public VariableMovementWithState(VMAsset asset, IInputActionCollection inputCollection) public VariableMovementWithState(VMAsset asset, IInputActionCollection inputCollection)
{ {
InputCollection = inputCollection; InputCollection = inputCollection;
OnToggleStateChanged = new UnityEvent<bool>();
Asset = asset; Asset = asset;
} }