-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractiveController.cs
175 lines (114 loc) · 3.49 KB
/
InteractiveController.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace nyxeka
{
public class InteractiveController : MonoBehaviour
{
public InputBehaviour[] input;
public string ControllerID = "";
public int defaultInputIndex = 0;
private int currentIB = 0;
private UnitCharacter unit;
public UnitCharacter defaultUnit;
private bool keepIBRunning = false;
private bool busy = false;
// Use this for initialization
void Start()
{
if (defaultUnit != null)
{
unit = defaultUnit;
}
//run through and make sure they are all turned off.
for(int i = 0; i < input.Length; i++)
{
if (input[i]) // null check
input[i].enabled = false;
}
currentIB = defaultInputIndex;
RunIB();
}
void RunIB()
{
RunIB(currentIB);
}
public void ResetCurrentIB()
{
RunIB(currentIB);
}
void RunIB(string IBName)
{
for(int i = 0;i<input.Length;i++)
{
if (input[i].ID == IBName)
{
RunIB(i);
return;
}
}
}
void RunIB(int IBindex)
{
InitIB(IBindex);
}
void SwitchIB(int newIBindex)
{
Debug.Log("IB killed. Running cleanup methods.");
input[currentIB].ExitCamera();
input[currentIB].ExitIB();
input[currentIB].enabled = false;
RunIB(newIBindex);
}
void InitIB(int IBindex)
{
if (input.Length == 0)
{
Debug.LogError("Warning! No input handlers detected!");
StopAllCoroutines();
}
//yield return null;
int index = (IBindex < input.Length ? IBindex : 0);
if (input[index] == null)
{
if (input[index] = gameObject.GetComponent<InputBehaviour>())
{
Debug.Log("Resetting to first input method found: " + input[index].ID);
}
else
{
Debug.LogError("Input at index is null. Warning! No input handlers detected!");
}
}
input[index].enabled = true;
Debug.Log("Input Behaviour set to: " + input[index].ID + ", IB Index: " + index.ToString());
currentIB = index;
//sinces the input behaviour is a component, this turns into a sort of pointer
InputBehaviour IB = input[index];
//init the IB.
//run some inits.
IB.InitCamera();
IB.Init(unit);
keepIBRunning = true;
}
void HandleCurrentIB()
{
input[currentIB].RunBehaviour();
input[currentIB].RunCamera();
}
// Update is called once per frame
void Update()
{
// quick hack for now.
if (Input.GetKeyDown(KeyCode.Tab))
{
Debug.Log("Switching Input Behaviours.");
SwitchIB((currentIB + 1) % input.Length);
}
}
private void FixedUpdate()
{
HandleCurrentIB();
}
}
}