AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesControllerMapper.cs

132 lines
4.3 KiB
C#
Raw Permalink Normal View History

2024-08-22 17:25:00 +08:00
using AxibugEmuOnline.Client.ClientCore;
using System;
2024-08-06 18:52:11 +08:00
using System.IO;
2024-08-06 18:09:32 +08:00
using UnityEngine;
using VirtualNes.Core;
namespace AxibugEmuOnline.Client
{
2024-08-06 18:52:11 +08:00
public class NesControllerMapper
2024-08-06 18:09:32 +08:00
{
2024-09-11 18:31:25 +08:00
private static readonly string ConfigFilePath = $"{App.PersistentDataPath}/NES/ControllerMapper.json";
2024-08-06 18:52:11 +08:00
2024-08-06 18:09:32 +08:00
public MapperSetter Player1 = new MapperSetter();
public MapperSetter Player2 = new MapperSetter();
public MapperSetter Player3 = new MapperSetter();
public MapperSetter Player4 = new MapperSetter();
2024-08-06 18:52:11 +08:00
public NesControllerMapper()
{
Player1.UP.keyCode = KeyCode.W;
Player1.DOWN.keyCode = KeyCode.S;
Player1.LEFT.keyCode = KeyCode.A;
Player1.RIGHT.keyCode = KeyCode.D;
Player1.B.keyCode = KeyCode.J;
Player1.A.keyCode = KeyCode.K;
Player1.SELECT.keyCode = KeyCode.V;
Player1.START.keyCode = KeyCode.B;
2024-11-21 17:18:15 +08:00
//PC XBOX
//Player1.B.keyCode = Common.PC_XBOXKEY.A;
//Player1.A.keyCode = Common.PC_XBOXKEY.B;
//Player1.SELECT.keyCode = Common.PC_XBOXKEY.ViewBtn;
//Player1.START.keyCode = Common.PC_XBOXKEY.MenuBtn;
2024-08-06 18:52:11 +08:00
}
public void Save()
{
var jsonStr = JsonUtility.ToJson(this);
File.WriteAllText(ConfigFilePath, jsonStr);
}
2024-08-06 18:09:32 +08:00
public ControllerState CreateState()
{
var state1 = Player1.GetButtons();
var state2 = Player2.GetButtons();
var state3 = Player3.GetButtons();
var state4 = Player4.GetButtons();
return new ControllerState(state1, state2, state3, state4);
}
2024-08-06 18:52:11 +08:00
private static NesControllerMapper s_setting;
public static NesControllerMapper Get()
{
if (s_setting == null)
{
try
{
2024-09-11 18:31:25 +08:00
var json = File.ReadAllText($"{App.PersistentDataPath}/Nes/ControllerMapper.json");
2024-08-06 18:52:11 +08:00
s_setting = JsonUtility.FromJson<NesControllerMapper>(json);
}
catch
{
s_setting = new NesControllerMapper();
}
}
return s_setting;
}
2024-08-06 18:09:32 +08:00
[Serializable]
public class Mapper
{
public EnumButtonType buttonType;
public KeyCode keyCode;
public Mapper(EnumButtonType buttonType)
{
this.buttonType = buttonType;
}
}
[Serializable]
public class MapperSetter
{
public Mapper UP = new Mapper(EnumButtonType.UP);
public Mapper DOWN = new Mapper(EnumButtonType.DOWN);
public Mapper LEFT = new Mapper(EnumButtonType.LEFT);
public Mapper RIGHT = new Mapper(EnumButtonType.RIGHT);
public Mapper A = new Mapper(EnumButtonType.A);
public Mapper B = new Mapper(EnumButtonType.B);
public Mapper SELECT = new Mapper(EnumButtonType.SELECT);
public Mapper START = new Mapper(EnumButtonType.START);
public Mapper MIC = new Mapper(EnumButtonType.MIC);
public EnumButtonType GetButtons()
{
EnumButtonType res = 0;
if (Input.GetKey(UP.keyCode))
res |= EnumButtonType.UP;
if (Input.GetKey(DOWN.keyCode))
res |= EnumButtonType.DOWN;
if (Input.GetKey(LEFT.keyCode))
res |= EnumButtonType.LEFT;
if (Input.GetKey(RIGHT.keyCode))
res |= EnumButtonType.RIGHT;
if (Input.GetKey(A.keyCode))
res |= EnumButtonType.A;
if (Input.GetKey(B.keyCode))
res |= EnumButtonType.B;
if (Input.GetKey(SELECT.keyCode))
res |= EnumButtonType.SELECT;
if (Input.GetKey(START.keyCode))
res |= EnumButtonType.START;
if (Input.GetKey(MIC.keyCode))
res |= EnumButtonType.MIC;
return res;
}
}
}
}