-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.cs
103 lines (92 loc) · 3.36 KB
/
InputManager.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// Manages mapping and querying of input
/// in menus and during gameplay, respectively.
/// </summary>
public class InputManager : Singleton<InputManager>
{
public bool inMainMenu = false;
public CharacterInput[] characterInputs = new CharacterInput[4];
// Store all necessary inputs for 8 Xbox 360 gamepads
public string[] joyXAxis {get; private set;}
public KeyCode[] joyButton0 {get; private set;}
public KeyCode[] joyButton1 {get; private set;}
public KeyCode[] joyButtonStart {get; private set;}
public KeyCode[] allKeys {get; private set;}
void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
for (int i = 0; i < 4; i++)
characterInputs[i] = new CharacterInput(i);
joyXAxis = new string[8];
joyButton0 = new KeyCode[8];
joyButton1 = new KeyCode[8];
joyButtonStart = new KeyCode[8];
for (int i = 0; i < 8; i++)
{
joyXAxis[i] = "Joy"+(i+1)+"X";
// Reference for joystick buttons: http://wiki.unity3d.com/index.php?title=Xbox360Controller
joyButton0[i] = (KeyCode)System.Enum.Parse(typeof(KeyCode),
"Joystick"+(i+1)+"Button0");
joyButton1[i] = (KeyCode)System.Enum.Parse(typeof(KeyCode),
"Joystick"+(i+1)+"Button1");
joyButtonStart[i] = (KeyCode)System.Enum.Parse(typeof(KeyCode),
"Joystick"+(i+1)+"Button7");
}
allKeys = (KeyCode[]) System.Enum.GetValues(typeof(KeyCode)) ;
SceneManager.sceneLoaded += OnSceneChange;
}
/// <summary>Resets all characterinputs on menu load.</summary>
void OnSceneChange(Scene scene, LoadSceneMode mode)
{
if (inMainMenu)
{
for (int i = 0; i < 4; i++)
if (characterInputs != null)
characterInputs[i].UnmapPlayer();
}
}
/// <summary>Query input once per frame in-game.</summary>
void Update ()
{
if (!inMainMenu)
{
for (int i = 0; i < GameManager.Instance.NumPlayers; i++)
{
characterInputs[i].GetInput();
}
}
}
/// <summary>Checks if gamepad is mapped.</summary>
/// <param name="joyNumber">Gamepad number to check.</param>
/// <returns>true if it is already in use, else false.</returns>
public bool isJoyMapped(int joyNumber)
{
for (int i = 0; i < 4; i++)
{
if (characterInputs[i].padNumber == joyNumber)
return true;
}
return false;
}
/// <summary>checks if keycode is already mapped.</summary>
/// <param name="key">KeyCode to check</param>
/// <returns>true if it is already in use, else false.</returns>
public bool isButtonMapped(KeyCode key)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < characterInputs[i].buttons.Length; j++)
if (characterInputs[i].buttons[j] == key) return true;
}
return false;
}
}