VariableMovement Added

This commit is contained in:
Syntriax 2022-03-09 21:46:49 +03:00
parent a047be629b
commit e531bdf77e
23 changed files with 393 additions and 2 deletions

View File

@ -1,3 +1,16 @@
{ {
"name": "Movement" "name": "Movement",
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
} }

8
VariableMovement.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 726dbb7aaeb452e42b1faf1318513cdb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,7 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public interface IVariableMovement
{
VMAsset Asset { get; set; }
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6cdf8a548c241e0419fcf7e95c760cae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

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

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8626f2dd751462548adce79ccb84d6d3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.VariableMovement
{
public interface IVariableMovementWithState : IVariableMovement
{
bool Enabled { get; set; }
UnityEvent<bool> OnToggleStateChanged { get; }
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c4e5bc603a4ef1342abf1e987cfced7f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 63a9b9d234afa1d4d81a6f018393c057
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine;
namespace Syntriax.Modules.Movement.VariableMovement
{
[CreateAssetMenu(fileName = "VMAsset", menuName = "Syntriax/Modules/Movement/Variable Movement/Asset", order = 0)]
public class VMAssetSO : ScriptableObject
{
public VMAsset asset;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 675d01134857a5b4aa8278985b7d2c09
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine;
namespace Syntriax.Modules.Movement.VariableMovement
{
[CreateAssetMenu(fileName = "VMCollection", menuName = "Syntriax/Modules/Movement/Variable Movement/Collection", order = 0)]
public class VMCollectionSO : ScriptableObject
{
public VMCollection collection;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b6d52a12481917c4b877c026fa81dfef
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,19 @@
using System;
namespace Syntriax.Modules.Movement.VariableMovement
{
[Serializable]
public struct VMAsset
{
public string Name;
public float Multiplier;
public string ActionReference;
public VMAsset(string name, float multiplier, string actionReference)
{
Name = name;
Multiplier = multiplier;
ActionReference = actionReference;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 35f90754856e0cf43a33a1a6bc5e0423
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,17 @@
using System;
namespace Syntriax.Modules.Movement.VariableMovement
{
[Serializable]
public struct VMCollection
{
public VMAsset DefaultAsset;
public VMAsset[] VMAssets;
public VMCollection(VMAsset defaultAsset, VMAsset[] vMAssets)
{
DefaultAsset = defaultAsset;
VMAssets = vMAssets;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6c9addcc3980a2948b5c2e493f8f1642
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,11 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovement : IVariableMovement
{
public VMAsset Asset { get; set; }
public VariableMovement() { }
public VariableMovement(VMAsset asset)
=> Asset = asset;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9fa3a5e4edf40ce42a96a32da76de626
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovementController : MonoBehaviour, IVariableMovementController
{
public UnityEvent<IVariableMovement> OnVariableMovementChanged { get; protected set; } = null;
public IInputActionCollection InputActionCollection { get; set; } = null;
private IVariableMovement _activeVariableMovement = null;
public IVariableMovement ActiveVariableMovement
{
get => _activeVariableMovement;
protected set
{
_activeVariableMovement = value;
OnVariableMovementChanged.Invoke(value);
if (movementController != null)
SetMultiplierToActiveMovement(movementController.ActiveMovement);
}
}
protected IMovementController movementController = null;
protected IVariableMovement defaultVariableMovement = null;
protected List<IVariableMovementWithState> variableMovementsWithState = null;
protected VMCollection collection;
protected virtual void Awake()
{
OnVariableMovementChanged = new UnityEvent<IVariableMovement>();
defaultVariableMovement = new VariableMovement();
variableMovementsWithState = new List<IVariableMovementWithState>(16);
}
protected virtual void Start()
{
movementController = GetComponent<IMovementController>();
movementController.OnMovementChanged.AddListener(SetMultiplierToActiveMovement);
}
protected void SetMultiplierToActiveMovement(IMovement currentMovement)
=> currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier;
public void LoadVariableMovementCollection(VMCollection collection)
{
if (InputActionCollection == null)
throw new System.NullReferenceException("InputActionCollection must be set on the VariableMovementController to load a collection!");
this.collection = collection;
UnsubscribeFromMovements();
variableMovementsWithState.Clear();
foreach (VMAsset asset in collection.VMAssets)
variableMovementsWithState.Add(new VariableMovementWithState(asset, InputActionCollection));
defaultVariableMovement.Asset = collection.DefaultAsset;
SubscribeToMovements();
UpdateActiveVariableMovement();
}
protected virtual void SubscribeToMovements()
{
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
variableMovementWithState.OnToggleStateChanged.AddListener(VariableMovementStateListener);
}
protected virtual void UnsubscribeFromMovements()
{
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
variableMovementWithState.OnToggleStateChanged.RemoveListener(VariableMovementStateListener);
}
private void VariableMovementStateListener(bool arg0) => UpdateActiveVariableMovement();
protected virtual void UpdateActiveVariableMovement()
{
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
if (variableMovementWithState.Enabled)
{
ActiveVariableMovement = variableMovementWithState;
return;
}
ActiveVariableMovement = defaultVariableMovement;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c54e2868362dbe4eab92c16ee11fc42
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,63 @@
using UnityEngine.Events;
using UnityEngine.InputSystem;
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovementWithState : IVariableMovementWithState
{
public UnityEvent<bool> OnToggleStateChanged { get; protected set; } = null;
private bool _enabled = false;
public bool Enabled
{
get => _enabled;
set
{
bool isNewValue = _enabled != value;
_enabled = value;
if (isNewValue)
OnToggleStateChanged.Invoke(value);
}
}
private VMAsset _asset;
public VMAsset Asset
{
get => _asset;
set
{
if (actionReference != null)
{
actionReference.performed -= Performed;
actionReference.canceled -= Cancelled;
}
foreach (InputAction action in inputClass)
if (action.name == value.Name)
{
actionReference = action;
actionReference.performed += Performed;
actionReference.canceled += Cancelled;
break;
}
_asset = value;
Enabled = false;
}
}
protected readonly IInputActionCollection inputClass;
protected InputAction actionReference = null;
public VariableMovementWithState(VMAsset asset, IInputActionCollection inputClass)
{
this.inputClass = inputClass;
OnToggleStateChanged = new UnityEvent<bool>();
Asset = asset;
}
protected void Cancelled(InputAction.CallbackContext obj) => Enabled = false;
protected void Performed(InputAction.CallbackContext obj) => Enabled = true;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 95216561eb395f54596b439538c1c691
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: