From 2a20131e001fc58ebf62118cd6e6b8f61571fb2d Mon Sep 17 00:00:00 2001 From: "ALIENJACK\\alien" Date: Thu, 18 Jul 2024 15:26:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=B8=80=E4=B8=8B,=E8=AE=A9K?= =?UTF-8?q?eyMapper=E6=94=AF=E6=8C=81=E5=90=84=E7=A7=8D=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E8=AE=BE=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Script/Emu/InputManager/InputManager.cs | 23 +++--- .../Script/Emu/InputManager/KeyMapper.cs | 50 ++----------- .../Script/Emu/InputManager/LocalKeyMapper.cs | 72 +++++++++++++++++++ .../Emu/InputManager/LocalKeyMapper.cs.meta | 11 +++ .../Emu/InputManager/NesJoyController.cs | 11 ++- .../Script/Emu/InputManager/NetKeyMapper.cs | 24 +++++++ .../Emu/InputManager/NetKeyMapper.cs.meta | 11 +++ 7 files changed, 140 insertions(+), 62 deletions(-) create mode 100644 AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs.meta create mode 100644 AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs.meta diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/InputManager.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/InputManager.cs index 1b099b4..ff1f63f 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/InputManager.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/InputManager.cs @@ -7,24 +7,17 @@ namespace AxibugEmuOnline.Client.Input { public class InputManager : MonoBehaviour { - private KeyMapper m_p1Mapper = new KeyMapper(); - private KeyMapper m_p2Mapper = new KeyMapper(); - private KeyMapper m_p3Mapper = new KeyMapper(); - private KeyMapper m_p4Mapper = new KeyMapper(); + private KeyMapper m_p1Mapper = new LocalKeyMapper(); + private KeyMapper m_p2Mapper = new NetKeyMapper(); + private KeyMapper m_p3Mapper = new NetKeyMapper(); + private KeyMapper m_p4Mapper = new NetKeyMapper(); private void Awake() { - m_p1Mapper.SetKeyMapper(KeyCode.W, EnumKeyKind.Up); - m_p1Mapper.SetKeyMapper(KeyCode.S, EnumKeyKind.Down); - m_p1Mapper.SetKeyMapper(KeyCode.A, EnumKeyKind.Left); - m_p1Mapper.SetKeyMapper(KeyCode.D, EnumKeyKind.Right); - m_p1Mapper.SetKeyMapper(KeyCode.V, EnumKeyKind.Select); - m_p1Mapper.SetKeyMapper(KeyCode.B, EnumKeyKind.Start); - m_p1Mapper.SetKeyMapper(KeyCode.J, EnumKeyKind.B); - m_p1Mapper.SetKeyMapper(KeyCode.K, EnumKeyKind.A); - m_p1Mapper.SetKeyMapper(KeyCode.U, EnumKeyKind.TurboB); - m_p1Mapper.SetKeyMapper(KeyCode.I, EnumKeyKind.TurboA); - m_p1Mapper.SetComplete(); + m_p1Mapper.Init(); + m_p2Mapper.Init(); + m_p3Mapper.Init(); + m_p4Mapper.Init(); } private void Update() diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/KeyMapper.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/KeyMapper.cs index 75380a5..48c6add 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/KeyMapper.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/KeyMapper.cs @@ -5,53 +5,11 @@ using UnityEngine; namespace AxibugEmuOnline.Client.Input { - public class KeyMapper + public abstract class KeyMapper { - private Dictionary m_mapper = new Dictionary(); - private Dictionary m_mapperOpp = new Dictionary(); - private Dictionary m_keyIndexTable = new Dictionary(); - private EnumKeyKind[] m_focusKeys; - private bool[] m_keyStates; + public abstract void Init(); + public abstract void Update(); + public abstract bool IsPressing(EnumKeyKind keyKind); - public void SetKeyMapper(KeyCode inputKeycode, EnumKeyKind joyKey) - { - if (m_mapperOpp.TryGetValue(joyKey, out KeyCode keyCode))//如果该映射已设置过,移除之前的映射 - { - m_mapperOpp.Remove(joyKey); - m_mapper.Remove(keyCode); - } - m_mapper[inputKeycode] = joyKey; - m_mapperOpp[joyKey] = inputKeycode; - } - - public void SetComplete() - { - m_focusKeys = m_mapperOpp.Keys.ToArray(); - m_keyStates = new bool[m_focusKeys.Length]; - - m_keyIndexTable.Clear(); - for (int i = 0; i < m_focusKeys.Length; i++) - { - m_keyIndexTable[m_focusKeys[i]] = i; - } - } - - public void Update() - { - if (m_focusKeys == null) return; - - for (int i = 0; i < m_focusKeys.Length; i++) - { - var keyCode = m_mapperOpp[m_focusKeys[i]]; - m_keyStates[i] = UnityEngine.Input.GetKey(keyCode); - } - } - - public bool IsPressing(EnumKeyKind keyKind) - { - if (!m_keyIndexTable.TryGetValue(keyKind, out int index)) return false;//没有设置映射,直接false - - return m_keyStates[index]; - } } } diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs new file mode 100644 index 0000000..2a61db2 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs @@ -0,0 +1,72 @@ +using MyNes.Core; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace AxibugEmuOnline.Client.Input +{ + public class LocalKeyMapper : KeyMapper + { + private Dictionary m_mapper = new Dictionary(); + private Dictionary m_mapperOpp = new Dictionary(); + private Dictionary m_keyIndexTable = new Dictionary(); + private EnumKeyKind[] m_focusKeys; + private bool[] m_keyStates; + + public override void Init() + { + SetKeyMapper(KeyCode.W, EnumKeyKind.Up); + SetKeyMapper(KeyCode.S, EnumKeyKind.Down); + SetKeyMapper(KeyCode.A, EnumKeyKind.Left); + SetKeyMapper(KeyCode.D, EnumKeyKind.Right); + SetKeyMapper(KeyCode.V, EnumKeyKind.Select); + SetKeyMapper(KeyCode.B, EnumKeyKind.Start); + SetKeyMapper(KeyCode.J, EnumKeyKind.B); + SetKeyMapper(KeyCode.K, EnumKeyKind.A); + SetKeyMapper(KeyCode.U, EnumKeyKind.TurboB); + SetKeyMapper(KeyCode.I, EnumKeyKind.TurboA); + SetComplete(); + } + + void SetKeyMapper(KeyCode inputKeycode, EnumKeyKind joyKey) + { + if (m_mapperOpp.TryGetValue(joyKey, out KeyCode keyCode))//如果该映射已设置过,移除之前的映射 + { + m_mapperOpp.Remove(joyKey); + m_mapper.Remove(keyCode); + } + m_mapper[inputKeycode] = joyKey; + m_mapperOpp[joyKey] = inputKeycode; + } + + void SetComplete() + { + m_focusKeys = m_mapperOpp.Keys.ToArray(); + m_keyStates = new bool[m_focusKeys.Length]; + + m_keyIndexTable.Clear(); + for (int i = 0; i < m_focusKeys.Length; i++) + { + m_keyIndexTable[m_focusKeys[i]] = i; + } + } + + public override void Update() + { + if (m_focusKeys == null) return; + + for (int i = 0; i < m_focusKeys.Length; i++) + { + var keyCode = m_mapperOpp[m_focusKeys[i]]; + m_keyStates[i] = UnityEngine.Input.GetKey(keyCode); + } + } + + public override bool IsPressing(EnumKeyKind keyKind) + { + if (!m_keyIndexTable.TryGetValue(keyKind, out int index)) return false;//没有设置映射,直接false + + return m_keyStates[index]; + } + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs.meta b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs.meta new file mode 100644 index 0000000..550160a --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/LocalKeyMapper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f5ed9df7bb0a5ed4096219829b4e2f6e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NesJoyController.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NesJoyController.cs index 7f0084c..fa1c17a 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NesJoyController.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NesJoyController.cs @@ -6,7 +6,7 @@ namespace AxibugEmuOnline.Client public class NesJoyController : IJoypadConnecter { private EnumJoyIndex m_joyIndex; - + private bool turbo; public NesJoyController(EnumJoyIndex joyIndex) { @@ -14,6 +14,7 @@ namespace AxibugEmuOnline.Client } public override void Update() { + turbo = !turbo; DATA = 0; var state = MyNesMain.Supporter; if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.A)) @@ -24,6 +25,14 @@ namespace AxibugEmuOnline.Client { DATA |= 2; } + if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.TurboA) && turbo) + { + DATA |= 1; + } + if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.TurboB) && turbo) + { + DATA |= 2; + } if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Select)) { DATA |= 4; diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs new file mode 100644 index 0000000..66a65a1 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs @@ -0,0 +1,24 @@ +using AxibugEmuOnline.Client.Input; +using MyNes.Core; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace AxibugEmuOnline.Client +{ + public class NetKeyMapper : KeyMapper + { + public override void Init() + { + } + + public override void Update() + { + } + + public override bool IsPressing(EnumKeyKind keyKind) + { + return false; + } + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs.meta b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs.meta new file mode 100644 index 0000000..a721ba3 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/Emu/InputManager/NetKeyMapper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 266dac4486104b64cb089b6898b46cfc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: