-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerInputMapping.cs
118 lines (105 loc) · 3.63 KB
/
PlayerInputMapping.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
#region Key Press Type Enum
public enum KeyPressType
{
Continuous,
Single
}
#endregion
#region Player Actions
public enum PlayerAction
{
// Movement
Forward,
Backwards,
StrafeLeft,
StrafeRight,
// etc...
// Actions
Jump,
Crouch,
Sprint,
Interact,
// etc..
}
#endregion Player Actions
#region Input Mapping
public static class PlayerInputMapping
{
private static string filePath = Path.Combine(Application.persistentDataPath, "KeyBindings.json");
// Dictionary to hold the key bindings along with their press type
public static Dictionary<PlayerAction, (KeyCode key, KeyPressType pressType)> keyBindings = new Dictionary<PlayerAction, (KeyCode, KeyPressType)>();
public static List<SerializableKeyBinding> GetSerializableKeyBindings()
{
var bindings = new List<SerializableKeyBinding>();
foreach (var binding in keyBindings)
{
bindings.Add(new SerializableKeyBinding(binding.Key, binding.Value.key, binding.Value.pressType));
}
return bindings;
}
public static void SaveBindings()
{
var bindings = GetSerializableKeyBindings();
string json = JsonUtility.ToJson(new SerializableListWrapper<SerializableKeyBinding> { list = bindings });
File.WriteAllText(filePath, json); // Write the json string to a file
}
public static void LoadBindings()
{
if (File.Exists(filePath))
{
Debug.Log("Loading key bindings from " + filePath);
string json = File.ReadAllText(filePath); // Read the json string from the file
var bindings = JsonUtility.FromJson<SerializableListWrapper<SerializableKeyBinding>>(json).list;
keyBindings.Clear();
foreach (var binding in bindings)
{
keyBindings.Add(binding.action, (binding.key, binding.pressType));
}
}
else
{
Debug.Log("No key bindings found at " + filePath + ". Loading default key bindings.");
LoadDefaultKeybindings();
}
}
public static void LoadDefaultKeybindings()
{
// load default keybindings
keyBindings.Add(PlayerAction.Forward, (KeyCode.W, KeyPressType.Continuous));
keyBindings.Add(PlayerAction.StrafeLeft, (KeyCode.A, KeyPressType.Continuous));
keyBindings.Add(PlayerAction.Backwards, (KeyCode.S, KeyPressType.Continuous));
keyBindings.Add(PlayerAction.StrafeRight, (KeyCode.D, KeyPressType.Continuous));
keyBindings.Add(PlayerAction.Jump, (KeyCode.Space, KeyPressType.Single));
keyBindings.Add(PlayerAction.Crouch, (KeyCode.LeftControl, KeyPressType.Continuous));
keyBindings.Add(PlayerAction.Sprint, (KeyCode.LeftShift, KeyPressType.Continuous));
keyBindings.Add(PlayerAction.Interact, (KeyCode.E, KeyPressType.Single));
// Add other default bindings here...
SaveBindings();
LoadBindings();
}
}
#endregion Input Mapping
#region Helper Classes
[System.Serializable]
public class SerializableKeyBinding
{
public PlayerAction action;
public KeyCode key;
public KeyPressType pressType;
public SerializableKeyBinding(PlayerAction action, KeyCode key, KeyPressType pressType)
{
this.action = action;
this.key = key;
this.pressType = pressType;
}
}
[System.Serializable]
public class SerializableListWrapper<T>
{
public List<T> list;
}
#endregion Helper Classes