diff --git a/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseInputModule.cs b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseInputModule.cs new file mode 100644 index 0000000..e2c77d4 --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseInputModule.cs @@ -0,0 +1,13 @@ +using System; +using UnityEngine; + +namespace Goldenwere.GWSU.CTRL.CHAR.Core +{ + public abstract class BaseInputModule : MonoBehaviour + { + public abstract void RegisterModuleAction(BaseModuleAction _action); + public abstract void RegisterModuleAction(BaseModuleAction _action); + public abstract void UnregisterModuleAction(BaseModuleAction _action); + public abstract void UnregisterModuleAction(BaseModuleAction _action); + } +} diff --git a/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseInputModule.cs.meta b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseInputModule.cs.meta new file mode 100644 index 0000000..c8528b6 --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseInputModule.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2563e6c15d3cc124bb77cf676545f879 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseModuleAction.cs b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseModuleAction.cs new file mode 100644 index 0000000..e9cd62c --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseModuleAction.cs @@ -0,0 +1,64 @@ +using System; +using UnityEngine; + +namespace Goldenwere.GWSU.CTRL.CHAR.Core +{ + public enum ModuleActionType : uint + { + Held = 0, + Pressed = 1, + HeldOrPressed = 2, + } + + public class BaseModuleAction + { + [SerializeField] private ScriptableObject emitter; + [SerializeField] private Action listener; + [SerializeField] private ModuleActionType type; + + private Guid guid; + + public Guid GUID => guid; + public Action Listener => listener; + public ScriptableObject Emitter => emitter; + public ModuleActionType Type => type; + + public BaseModuleAction( + Action _listener, + ScriptableObject _emitter, + ModuleActionType _type + ) + { + guid = Guid.NewGuid(); + listener = _listener; + emitter = _emitter; + type = _type; + } + } + + public class BaseModuleAction + { + [SerializeField] private ScriptableObject emitter; + [SerializeField] private Action listener; + [SerializeField] private ModuleActionType type; + + private Guid guid; + + public Guid GUID => guid; + public Action Listener => listener; + public ScriptableObject Emitter => emitter; + public ModuleActionType Type => type; + + public BaseModuleAction( + Action _listener, + ScriptableObject _emitter, + ModuleActionType _type + ) + { + guid = Guid.NewGuid(); + listener = _listener; + emitter = _emitter; + type = _type; + } + } +} diff --git a/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseModuleAction.cs.meta b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseModuleAction.cs.meta new file mode 100644 index 0000000..cc9c4c9 --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/Core/Runtime/BaseModuleAction.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 670fd3287f776fd4abd946ae86e91e06 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.asmdef b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.asmdef new file mode 100644 index 0000000..a79db50 --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.asmdef @@ -0,0 +1,17 @@ +{ + "name": "Goldenwere.GWSU.CTRL.CHAR.InputSystemModule", + "rootNamespace": "", + "references": [ + "GUID:75469ad4d38634e559750d17036d5f7c", + "GUID:7118095471e21fa4782c349e8d42f7a3" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.asmdef.meta b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.asmdef.meta new file mode 100644 index 0000000..56397d4 --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4d883c6428850a645aecf33a1ee4c832 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/InputModule.cs b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/InputModule.cs new file mode 100644 index 0000000..e39c10f --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/InputModule.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; +using Goldenwere.GWSU.CTRL.CHAR.Core; + +namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule +{ + public class InputModule : BaseInputModule + { + internal struct Callbacks + { + public Action canceled; + public Action performed; + public Action started; + } + + private Dictionary registeredCallbacks; + + private Dictionary RegisteredCallbacks + { + get + { + if (registeredCallbacks == null) + { + registeredCallbacks = new Dictionary(); + } + return registeredCallbacks; + } + } + + public override void RegisterModuleAction(BaseModuleAction _action) + { + if (TryGetReference(_action.Emitter, out InputActionReference _reference)) + { + Action performed = (InputAction.CallbackContext ctx) => + { + _action.Listener.Invoke(); + }; + _reference.action.performed += performed; + RegisteredCallbacks.Add + ( + _action.GUID, + new Callbacks + { + performed = performed, + } + ); + } + } + + public override void RegisterModuleAction(BaseModuleAction _action) + { + if (TryGetReference(_action.Emitter, out InputActionReference _reference)) + { + + } + } + + public override void UnregisterModuleAction(BaseModuleAction _action) + { + if (TryGetReference(_action.Emitter, out InputActionReference _reference)) + { + Unregister(_reference, _action.GUID); + } + } + + public override void UnregisterModuleAction(BaseModuleAction _action) + { + if (TryGetReference(_action.Emitter, out InputActionReference _reference)) + { + Unregister(_reference, _action.GUID); + } + } + + private bool CheckForCallback(Guid id) + { + if (RegisteredCallbacks.ContainsKey(id)) + { + return true; + } + Debug.LogError("Attempted to unregister a module action that was not yet registered!"); + return false; + } + + private bool TryGetReference(ScriptableObject _emitter, out InputActionReference _reference) + { + if (_emitter is InputActionReference reference) + { + _reference = reference; + return true; + } + else + { + Debug.LogError($"Emitter was not an InputActionReference!"); + } + _reference = null; + return false; + } + + private void Unregister(InputActionReference _reference, Guid _id) + { + if (CheckForCallback(_id)) + { + _reference.action.performed -= RegisteredCallbacks[_id].performed; + RegisteredCallbacks.Remove(_id); + } + } + } +} diff --git a/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/InputModule.cs.meta b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/InputModule.cs.meta new file mode 100644 index 0000000..32eff02 --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Runtime/InputModule.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2ae55851646730b41a9efe1ad3aa3b5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Samples/SampleProject/Runtime/FakeControllerModule.cs b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Samples/SampleProject/Runtime/FakeControllerModule.cs new file mode 100644 index 0000000..81e239b --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Samples/SampleProject/Runtime/FakeControllerModule.cs @@ -0,0 +1,53 @@ +using UnityEngine; +using Goldenwere.GWSU.CTRL.CHAR.Core; + +namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.SampleProject +{ + public class FakeControllerModule : BaseControllerModule + { + [SerializeField] private BaseInputModule module; + [SerializeField] private ScriptableObject emitterOnCrouch; + [SerializeField] private ScriptableObject emitterOnJump; + [SerializeField] private ScriptableObject emitterOnMovement; + + private BaseModuleAction actionOnCrouch; + private BaseModuleAction actionOnJump; + private BaseModuleAction actionOnMovement; + + private void Awake() + { + actionOnCrouch = new BaseModuleAction(OnCrouch, emitterOnCrouch, ModuleActionType.HeldOrPressed); + actionOnJump = new BaseModuleAction(OnJump, emitterOnJump, ModuleActionType.Pressed); + actionOnMovement = new BaseModuleAction(OnMovement, emitterOnJump, ModuleActionType.Held); + } + + private void OnEnable() + { + //module.RegisterModuleAction(actionOnCrouch); + module.RegisterModuleAction(actionOnJump); + //module.RegisterModuleAction(actionOnMovement); + } + + private void OnDisable() + { + //module.UnregisterModuleAction(actionOnCrouch); + module.UnregisterModuleAction(actionOnJump); + //module.UnregisterModuleAction(actionOnMovement); + } + + private void OnCrouch(bool pressed) + { + Debug.Log($"Crouch was {(pressed ? "pressed" : "released")}"); + } + + private void OnJump() + { + Debug.Log("Jumped"); + } + + private void OnMovement(Vector2 value) + { + Debug.Log($"Value was: {value}"); + } + } +} diff --git a/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Samples/SampleProject/Runtime/FakeControllerModule.cs.meta b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Samples/SampleProject/Runtime/FakeControllerModule.cs.meta new file mode 100644 index 0000000..d8189ba --- /dev/null +++ b/Assets/Goldenwere/GWSU/Controller/Character/InputSystemModule/Samples/SampleProject/Runtime/FakeControllerModule.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 838b4ff14e50cc2449fd122b9daf073b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: