Skip to content

Commit 2bd0ee9

Browse files
committed
feat: New Input system
Created the new input system that works with both controller and keyboard and with a dynamic interface to create new inputs. - Added a parameter to create cache for the BDebug.Log - Added the test and editor convention - Created the input sample - Started the new resource controller
1 parent b4e48ac commit 2bd0ee9

20 files changed

+1017
-22
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Bigasdev.BigasTools.Editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:a2f30b5245f5a1c44b59126beb8fa271"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

Editor/Bigasdev.BigasTools.Editor.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ViewCreatorEditor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace BigasTools{
88
public class CreateViewEditor : EditorWindow
99
{
10-
[MenuItem("BigasTools/Create View")]
10+
[MenuItem("Window/BigasTools/Create View")]
1111
private static void ShowWindow() {
1212
var window = GetWindow<CreateViewEditor>();
1313
window.titleContent = new GUIContent("View creator");
@@ -17,9 +17,9 @@ private static void ShowWindow() {
1717

1818
void OnGUI()
1919
{
20-
EditorGUILayout.LabelField("MODAL DOS ANOES", EditorStyles.wordWrappedLabel);
20+
EditorGUILayout.LabelField("Test view", EditorStyles.wordWrappedLabel);
2121
GUILayout.Space(70);
22-
if (GUILayout.Button("CAPITAO DE AREIA")) this.Close();
22+
if (GUILayout.Button("View tester")) this.Close();
2323
}
2424
}
2525
}

Runtime/Bigasdev.BigasTools.asmdef

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
{
2-
"name": "Bigasdev.BigasTools"
3-
}
2+
"name": "Bigasdev.BigasTools",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:75469ad4d38634e559750d17036d5f7c"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

Runtime/Controllers/AudioController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static AudioController Instance{
2222
/// </summary>
2323
/// <param name="name"></param>
2424
public void PlaySound(string name){
25-
var audio = ResourceController.GetAudio(name);
25+
var audio = ResourceController.Instance.GetObject<AudioClip>(name);
2626
if(audio == null)return;
2727
sfxSource.PlayOneShot(audio);
2828
}

Runtime/Controllers/ResourceController.cs

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,71 @@
33
using UnityEngine;
44
using System.Linq;
55
namespace BigasTools{
6-
public class ResourceController
6+
public class ResourceController: MonoBehaviour
77
{
8-
public const string audiosDirectory = "Audios/Sounds";
9-
private static AudioClip[] soundClips;
10-
public static void StartSets(){
11-
soundClips = Resources.LoadAll<AudioClip>(audiosDirectory);
8+
private static ResourceController instance;
9+
public static ResourceController Instance{
10+
get{
11+
if(instance == null)instance = FindObjectOfType<ResourceController>();
12+
return instance;
13+
}
14+
}
15+
[SerializeField] ResourceReference[] refs;
16+
ResourceReference GetRef(string name){
17+
try
18+
{
19+
return refs.Where(x => x.referenceName == name).FirstOrDefault();
20+
}
21+
catch (System.Exception)
22+
{
23+
BDebug.Log($"No '{name}' reference found", "Resource Controller", Color.red);
24+
return null;
25+
}
26+
}
27+
public T GetObject<T>(string name, string path = "Default") where T:class{
28+
var p = GetRef(path);
29+
if(p == null)return null;
30+
try
31+
{
32+
var fullPath = p.directory + "/" +name;
33+
BDebug.Log($"Trying to load '{fullPath}'", "Resource Controller", Color.red);
34+
var o = Resources.Load(fullPath) as T;
35+
Debug.Log(o);
36+
return o as T;
37+
}
38+
catch (System.Exception)
39+
{
40+
BDebug.Log($"Are you sure '{name}' has this component?", "Resource Controller", Color.red);
41+
throw;
42+
}
1243
}
13-
public static AudioClip GetAudio(string name){
14-
foreach(var a in soundClips){
15-
if(a.name == name){
16-
return a;
17-
}
44+
public T GetObjectComponent<T>(string name, string path = "Default") where T:Object{
45+
var p = GetRef(path);
46+
if(p == null)return null;
47+
try
48+
{
49+
var fullPath = p.directory + "/" +name;
50+
BDebug.Log($"Trying to load '{fullPath}'", "Resource Controller", Color.red);
51+
var o = Resources.Load(fullPath) as T;
52+
Debug.Log(o);
53+
return o as T;
54+
}
55+
catch (System.Exception)
56+
{
57+
BDebug.Log($"Are you sure '{name}' has this component?", "Resource Controller", Color.red);
58+
throw;
1859
}
19-
Debug.Log("ERROR 001 : NO AUDIO FOUND WITH THIS NAME!");
20-
return null;
60+
}
61+
}
62+
[System.Serializable]
63+
public class ResourceReference{
64+
public string referenceName = "Default";
65+
public string directory = "";
66+
67+
public ResourceReference(string referenceName, string directory)
68+
{
69+
this.referenceName = referenceName;
70+
this.directory = directory;
2171
}
2272
}
2373
}

Runtime/Controllers/StateController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,32 @@ public static StateController Instance{
1515
}
1616
}
1717
public States currentState;
18+
States lastState;
1819

20+
//Change the current state and save the last one.
1921
public void ChangeState(States state){
2022
if(state == currentState){
2123
return;
2224
}
25+
lastState = currentState;
2326
currentState = state;
2427
}
28+
//Return to the update state;
2529
public void ReturnToInitialState(){
2630
currentState = States.GAME_UPDATE;
2731
}
32+
//Return to the last registered state and save the new one
33+
public void ReturnToLastState(){
34+
ChangeState(lastState);
35+
}
2836

2937
}
3038
public enum States{
3139
GAME_UPDATE,
40+
GAME_MENU,
41+
GAME_DEATH,
42+
GAME_LISTENING,
43+
GAME_INTERACTING,
3244
GAME_IDLE
3345
}
3446
}

Runtime/Input.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Input/BGameInput.cs

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.InputSystem;
5+
using System.Linq;
6+
7+
namespace BigasTools.InputSystem{
8+
public class BGameInput : MonoBehaviour
9+
{
10+
private static BGameInput instance;
11+
public static BGameInput Instance{
12+
get{
13+
if(instance == null)instance = FindObjectOfType<BGameInput>();
14+
return instance;
15+
}
16+
}
17+
public InputData inputData;
18+
public Gamepad currentGamePad{
19+
get{
20+
return Gamepad.current;
21+
}
22+
}
23+
public bool GetKeyPress(string name){
24+
var i = inputData.profile.Where(x=>x.inputName == name).FirstOrDefault();
25+
if(i == null){
26+
BDebug.Log($"You haven't assigned the profile for key {name}", "GameInput", Color.blue, true);
27+
return false;
28+
}
29+
if(Input.GetKeyDown(i.inputKey) || GetJoysticKeyPress(i.joystickKey)){
30+
BDebug.Log($"Key Pressed: {name}", "GameInput", Color.blue, true);
31+
return true;
32+
}
33+
return false;
34+
}
35+
public bool GetKey(string name){
36+
var i = inputData.profile.Where(x=>x.inputName == name).FirstOrDefault();
37+
if(i == null){
38+
BDebug.Log($"You haven't assigned the profile for key {name}", "GameInput", Color.blue, true);
39+
return false;
40+
}
41+
if(Input.GetKey(i.inputKey) || GetJoysticKey(i.joystickKey)){
42+
BDebug.Log($"Key Hold: {name}", "GameInput", Color.blue, true);
43+
return true;
44+
}
45+
return false;
46+
}
47+
public Vector2 GetAxis(){
48+
Vector2 axis = Vector2.zero;
49+
if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)){
50+
axis = new Vector2(1, axis.y);
51+
}
52+
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)){
53+
axis = new Vector2(-1, axis.y);
54+
}
55+
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){
56+
axis = new Vector2(axis.x, 1);
57+
}
58+
if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)){
59+
axis = new Vector2(axis.x, -1);
60+
}
61+
if(currentGamePad != null){
62+
var jax = currentGamePad.leftStick.ReadUnprocessedValue();
63+
var check = currentGamePad.leftStick.ReadValue();
64+
if(check != Vector2.zero){
65+
return jax;
66+
}
67+
}
68+
return axis;
69+
}
70+
bool GetJoysticKeyPress(InputKeys key){
71+
if(currentGamePad == null)return false;
72+
switch(key){
73+
case InputKeys.A:
74+
return currentGamePad.aButton.wasPressedThisFrame;
75+
case InputKeys.B:
76+
return currentGamePad.bButton.wasPressedThisFrame;
77+
case InputKeys.X:
78+
return currentGamePad.xButton.wasPressedThisFrame;
79+
case InputKeys.Y:
80+
return currentGamePad.yButton.wasPressedThisFrame;
81+
case InputKeys.PAUSE:
82+
return currentGamePad.startButton.wasPressedThisFrame;
83+
case InputKeys.SELECT:
84+
return currentGamePad.aButton.wasPressedThisFrame;
85+
case InputKeys.LEFT_STICK:
86+
return currentGamePad.bButton.wasPressedThisFrame;
87+
case InputKeys.RIGHT_STICK:
88+
return currentGamePad.xButton.wasPressedThisFrame;
89+
case InputKeys.LEFT_SHOULDER:
90+
return currentGamePad.yButton.wasPressedThisFrame;
91+
case InputKeys.LEFT_TRIGGER:
92+
return currentGamePad.startButton.wasPressedThisFrame;
93+
case InputKeys.RIGHT_SHOULDER:
94+
return currentGamePad.yButton.wasPressedThisFrame;
95+
case InputKeys.RIGHT_TRIGGER:
96+
return currentGamePad.startButton.wasPressedThisFrame;
97+
}
98+
return false;
99+
}
100+
bool GetJoysticKey(InputKeys key){
101+
if(currentGamePad == null)return false;
102+
switch(key){
103+
case InputKeys.A:
104+
return currentGamePad.aButton.isPressed;
105+
case InputKeys.B:
106+
return currentGamePad.bButton.isPressed;
107+
case InputKeys.X:
108+
return currentGamePad.xButton.isPressed;
109+
case InputKeys.Y:
110+
return currentGamePad.yButton.isPressed;
111+
case InputKeys.PAUSE:
112+
return currentGamePad.startButton.isPressed;
113+
case InputKeys.SELECT:
114+
return currentGamePad.aButton.isPressed;
115+
case InputKeys.LEFT_STICK:
116+
return currentGamePad.bButton.isPressed;
117+
case InputKeys.RIGHT_STICK:
118+
return currentGamePad.xButton.isPressed;
119+
case InputKeys.LEFT_SHOULDER:
120+
return currentGamePad.yButton.isPressed;
121+
case InputKeys.LEFT_TRIGGER:
122+
return currentGamePad.startButton.isPressed;
123+
case InputKeys.RIGHT_SHOULDER:
124+
return currentGamePad.yButton.isPressed;
125+
case InputKeys.RIGHT_TRIGGER:
126+
return currentGamePad.startButton.isPressed;
127+
128+
}
129+
return false;
130+
}
131+
}
132+
[System.Serializable]
133+
public class InputData{
134+
public List<InputProfile> profile;
135+
public InputData(){
136+
var first = new InputProfile("Interaction", KeyCode.E, InputKeys.A);
137+
var second = new InputProfile("Pause", KeyCode.Escape, InputKeys.PAUSE);
138+
var third = new InputProfile("Info", KeyCode.CapsLock, InputKeys.SELECT);
139+
140+
profile = new List<InputProfile>();
141+
profile.Add(first);
142+
profile.Add(second);
143+
profile.Add(third);
144+
}
145+
}
146+
[System.Serializable]
147+
public class InputProfile{
148+
public string inputName;
149+
public KeyCode inputKey;
150+
public InputKeys joystickKey;
151+
152+
public InputProfile(string inputName, KeyCode inputKey, InputKeys joystickKey)
153+
{
154+
this.inputName = inputName;
155+
this.inputKey = inputKey;
156+
this.joystickKey = joystickKey;
157+
}
158+
}
159+
public enum InputKeys{
160+
A,
161+
B,
162+
X,
163+
Y,
164+
PAUSE,
165+
SELECT,
166+
LEFT_STICK,
167+
RIGHT_STICK,
168+
RIGHT_TRIGGER,
169+
RIGHT_SHOULDER,
170+
LEFT_TRIGGER,
171+
LEFT_SHOULDER
172+
173+
}
174+
}

Runtime/Input/BGameInput.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)