-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputController.cs
executable file
·352 lines (322 loc) · 14.2 KB
/
InputController.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Input Controller.cs - applies Kinect data to game
*
*
*
* Based on KinectModelController.cs - Moves every 'bone' given to match
* the position of the corresponding bone given by
* the kinect. Useful for viewing the point tracking
* in 3D.
*
* Developed by Peter Kinney -- 6/30/2011
* - Developed By Nick Reynolds
*/
using UnityEngine;
using System;
using System.Collections;
public class InputController : MonoBehaviour
{
public KinectWrapper KinectBridge;
public GameObject Hip_Center;
public GameObject Spine;
public GameObject Shoulder_Center;
public GameObject Head;
public GameObject Shoulder_Left;
public GameObject Elbow_Left;
public GameObject Wrist_Left;
public GameObject Hand_Left;
public GameObject Shoulder_Right;
public GameObject Elbow_Right;
public GameObject Wrist_Right;
public GameObject Hand_Right;
public GameObject Hip_Left;
public GameObject Knee_Left;
public GameObject Ankle_Left;
public GameObject Foot_Left;
public GameObject Hip_Right;
public GameObject Knee_Right;
public GameObject Ankle_Right;
public GameObject Foot_Right;
private GameObject[] _bones; //internal handle for the bones of the model
//private Vector4[] _bonePos; //internal handle for the bone positions from the kinect
public int player;
public BoneMask Mask;
public float MIN_TRANSLATION_RANGE = .08f;
public float MAX_TRANSLATION_RANGE = .30f;
public float MIN_AIM_RANGE = .075f;
public GameObject Player;
public PlayerScript script_Player;
public int timer = 300;
public float translation_distance = 0.0f;
public float aiming_distance = 0.0f;
public Vector3 aiming_nominalPointRightHandFromHips;
public Vector3 aiming_nominalPointLeftHandFromHips;
public Vector2 aim;
public Vector2 translation;
public Vector2 nominalMouseAim;
public Vector2 mouseAim;
public Boolean rightHanded = true;
public int controllerType = 1;
public Boolean isDebug = true;
Vector3 normalMousePosition;
Vector3 currentMousePosition;
public int secondaryFireTimer = -1;
public int primaryFireTimer = -1;
public int raisePRISMSTimer = -1;
public int lowerPRISMSTimer = -1;
Boolean pressedRight, pressedLeft, pressedUp, pressedDown, fireSecondary, firePrimary, pressed1, pressedJump, pressed2, pressed3;
// Use this for initialization
void Start()
{
//store bones in a list for easier access
_bones = new GameObject[(int)BoneIndex.Num_Bones] {Hip_Center, Spine, Shoulder_Center, Head,
Shoulder_Left, Elbow_Left, Wrist_Left, Hand_Left,
Shoulder_Right, Elbow_Right, Wrist_Right, Hand_Right,
Hip_Left, Knee_Left, Ankle_Left, Foot_Left,
Hip_Right, Knee_Right, Ankle_Right, Foot_Right};
//_bonePos = new Vector4[(int)BoneIndex.Num_Bones];
Player = GameObject.FindGameObjectWithTag("Player");
script_Player = Player.GetComponent(typeof(PlayerScript)) as PlayerScript;
pressedRight = pressedLeft = pressedUp = pressedDown = false;
fireSecondary = false;
firePrimary = false;
pressed1 = false;
pressedJump = false;
pressed2 = false;
pressed3 = false;
}
// Update is called once per frame
void Update()
{
//update all of the bones positions
//KinectBridge.pollKinect();
//for (int ii = 0; ii < (int)BoneIndex.Num_Bones; ii++)
//{
// //_bonePos[ii] = KinectBridge.getBonePos(ii);
// if (((uint)Mask & (uint)(1 << ii)) > 0)
// {
// _bones[ii].transform.localPosition = KinectBridge.BonePos[player, ii];
// }
//}
if (Input.GetKey("a"))
{
pressedLeft = true;
}
if (Input.GetKey("d"))
{
pressedRight = true;
}
if (Input.GetKey("w"))
{
pressedUp = true;
}
if (Input.GetKey("s"))
{
pressedDown = true;
}
if (Input.GetMouseButton(1) && secondaryFireTimer < 0)
{
fireSecondary = true;
}
if (Input.GetMouseButton(0) && primaryFireTimer < 0)
{
firePrimary = true;
}
if (Input.GetKey("1") && raisePRISMSTimer < 0)
{
pressed1 = true;
}
if (Input.GetKey("2") && lowerPRISMSTimer < 0)
{
pressed2 = true;
}
if (Input.GetKey("3"))
{
pressed3 = true;
}
bool grounded = false;
if (Physics.Raycast(Player.transform.position, new Vector3(0.0f,-1.0f,0.0f), 2.0f))
{
grounded = true;
}
if (Input.GetKey("space"))
{
Debug.Log("pressed space bar");
if (grounded)
{
Debug.Log("grounded");
pressedJump = true;
}
}
}
void FixedUpdate()
{
//keep track of when to do start-of-game-events
timer++;
if (controllerType == 1)
{
//make all positions local
if (timer == 500)
{
//nominalMouseAim = new Vector2(Input.mousePosition[0], Input.mousePosition[1]);
if (rightHanded)
{
aiming_nominalPointRightHandFromHips = Hand_Right.transform.localPosition - Hip_Center.transform.localPosition;
}
else
{
aiming_nominalPointLeftHandFromHips = Hip_Left.transform.localPosition - Hip_Center.transform.localPosition;
}
}
else if (timer > 550)
{
//mouseAim = new Vector2(Input.mousePosition[0], Input.mousePosition[1]) - nominalMouseAim;
//find translation
Vector3 translationDifference = Shoulder_Center.transform.localPosition - Hip_Center.transform.localPosition;
Vector3 translationEffectiveDifference = translationDifference;
translationEffectiveDifference[1] = 0.0f;
translation_distance = translationEffectiveDifference.magnitude;
translation = new Vector2(translationEffectiveDifference[0], translationEffectiveDifference[2]);
if (translation_distance < MIN_TRANSLATION_RANGE)
{
//movement too small, apply no translation
Vector3 empty = new Vector3(0.0f, 0.0f, 0.0f);
script_Player.applyTranslation(empty);
}
else if (translation_distance < MAX_TRANSLATION_RANGE)
{
//movement in range, apply scalar multiple of it in XZ plane
Vector3 translationPointOhEight = translationEffectiveDifference.normalized * .08f;
translationEffectiveDifference -= translationPointOhEight;
script_Player.applyTranslation(3.0f*translationEffectiveDifference);
}
else
{
//movement out of range, apply max translation in XZ plane
translationEffectiveDifference.Normalize();
translationEffectiveDifference *= .30f;
Vector3 translationPointOhEight = translationEffectiveDifference.normalized * .08f;
translationEffectiveDifference -= translationPointOhEight;
script_Player.applyTranslation(3.0f*translationEffectiveDifference);
}
//aiming
Vector3 deltaAim = new Vector3(0.0f, 0.0f, 0.0f);
if (rightHanded)
{
Vector3 relativeHandPosition = Hand_Right.transform.localPosition - Hip_Center.transform.localPosition;
deltaAim = relativeHandPosition - aiming_nominalPointRightHandFromHips;
aiming_distance = deltaAim.magnitude;
}
else
{
Vector3 relativeHandPosition = Hand_Left.transform.localPosition - Hip_Center.transform.localPosition;
deltaAim = relativeHandPosition - aiming_nominalPointLeftHandFromHips;
aiming_distance = deltaAim.magnitude;
}
Vector2 dAim = new Vector3(deltaAim[0], deltaAim[1]);
aim = dAim;
if (aiming_distance > MIN_AIM_RANGE)
{
script_Player.applyAim(dAim);
}
}
}
else if (controllerType == 2)
{
if (timer == 500)
{
//Screen.lockCursor = true;
//normalMousePosition = Input.mousePosition;
}
else if (timer > 500)
{
//currentMousePosition = Input.mousePosition;
//Vector3 deltaAim = currentMousePosition - normalMousePosition;
//Vector2 dAim = new Vector2(deltaAim[0], deltaAim[1]);
//script_Player.applyAim(.001f*dAim);
//Input.mousePosition.
Vector3 translate = new Vector3(0.0f, 0.0f, 0.0f);
if (pressedRight)
{
translate += new Vector3(0.30f, 0.0f, 0.0f);
}
if (pressedLeft)
{
translate += new Vector3(-0.30f, 0.0f, 0.0f);
}
if (pressedUp)
{
translate += new Vector3(0.0f, 0.0f, 0.3f);
}
if (pressedDown)
{
translate += new Vector3(0.0f, 0.0f, -0.3f);
}
secondaryFireTimer--;
if (secondaryFireTimer < -1000)
secondaryFireTimer = -5;
if (fireSecondary)
{
secondaryFireTimer = 50;
Debug.Log("Firing secondary");
script_Player.fireSecondary();
}
primaryFireTimer--;
if (primaryFireTimer < -1000)
primaryFireTimer = -5;
if (firePrimary)
{
primaryFireTimer = 50;
script_Player.firePrimary();
}
pressedRight = pressedLeft = pressedUp = pressedDown = false;
firePrimary = fireSecondary = false;
script_Player.applyTranslation(translate);
script_Player.applyAim();
raisePRISMSTimer--;
if (raisePRISMSTimer < -1000)
raisePRISMSTimer = -5;
if (pressed1)
{
raisePRISMSTimer = 100;
script_Player.raisePrisms(5.0f);
}
pressed1 = false;
lowerPRISMSTimer--;
if (lowerPRISMSTimer < -1000)
lowerPRISMSTimer = -5;
if (pressed2)
{
Debug.Log("attempting to lower prisms");
lowerPRISMSTimer = 100;
script_Player.raisePrisms(-5.0f);
}
pressed2 = false;
if (pressed3)
{
script_Player.thisView.RPC("disownAllPrisms", RPCMode.AllBuffered, 4);
}
pressed3 = false;
if (pressedJump)
{
Debug.Log("apply jump force");
Player.rigidbody.AddForce(new Vector3(0.0f, 100.0f, 0.0f));
}
pressedJump = false;
}
}
}
void OnGUI()
{
if (controllerType == 1 && isDebug)
{
GUI.Label(new Rect(400, 100, 300, 30), "KINECT AIMING - X: " + aim[0] + " - Y: " + aim[1]);
GUI.Label(new Rect(400, 130, 300, 30), "KINECT TRANSL - Z: " + translation[0] + " - Z: " + translation[1]);
GUI.Label(new Rect(400, 160, 300, 30), "KINECT TRANSL LENGTH: " + translation_distance);
}
else if (controllerType == 2 && isDebug)
{
GUI.Label(new Rect(400, 190, 300, 30), "MOUSE AIMING - X: " + mouseAim[0] + " - Y: " + mouseAim[1]);
}
}
}