-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameControl.js
More file actions
127 lines (117 loc) · 2.77 KB
/
Copy pathgameControl.js
File metadata and controls
127 lines (117 loc) · 2.77 KB
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
import ViGEmClient from "vigemclient";
import { UiohookKey } from "uiohook-napi";
const client = new ViGEmClient();
client.connect();
const controller = client.createX360Controller();
controller.connect();
// keyboard keycode -> controller action mapping
const gameKeyMap = {
[UiohookKey.W]: "moveUp",
[UiohookKey.A]: "moveLeft",
[UiohookKey.S]: "moveDown",
[UiohookKey.D]: "moveRight",
[UiohookKey.Shift]: "sprint",
[UiohookKey.Space]: "B", // dodge
[UiohookKey.Enter]: "start",
[UiohookKey.R]: "LB", // use gourd
[UiohookKey.F]: "L3", // transformation
[UiohookKey.Num1]: "A", // skill 1
[UiohookKey.Num2]: "RB", // skill 2
[UiohookKey.Num3]: "RT", // skill 3
[UiohookKey.Num4]: "dpadUp", // skill 4
};
const moveState = {
up: false,
down: false,
left: false,
right: false,
sprint: false,
};
function updateStick() {
let x = 0,
y = 0;
if (moveState.left) x -= 1;
if (moveState.right) x += 1;
if (moveState.up) y += 1;
if (moveState.down) y -= 1;
controller.axis.leftX.setValue(x);
controller.axis.leftY.setValue(y);
controller.axis.leftTrigger.setValue(moveState.sprint ? 1.0 : 0.0);
controller.update();
}
function pressButton(action, isDown) {
switch (action) {
case "B":
controller.button.B.setValue(isDown);
break;
case "A":
controller.button.A.setValue(isDown);
break;
case "X":
controller.button.X.setValue(isDown);
break;
case "Y":
controller.button.Y.setValue(isDown);
break;
case "LB":
controller.button.LB.setValue(isDown);
break;
case "RB":
controller.button.RB.setValue(isDown);
break;
case "RT":
controller.axis.rightTrigger.setValue(isDown ? 1.0 : 0.0);
break;
case "L3":
controller.button.LS.setValue(isDown);
break;
case "start":
controller.button.START.setValue(isDown);
break;
case "dpadUp":
controller.button.DPAD_UP.setValue(isDown);
break;
}
controller.update();
}
export function injectGameKey(object) {
const action = gameKeyMap[object.keyCode];
if (!action) {
console.error(`Unmapped game keycode: ${object.keyCode}`);
return;
}
const isDown = object.type === 4;
switch (action) {
case "moveUp":
moveState.up = isDown;
updateStick();
break;
case "moveDown":
moveState.down = isDown;
updateStick();
break;
case "moveLeft":
moveState.left = isDown;
updateStick();
break;
case "moveRight":
moveState.right = isDown;
updateStick();
break;
case "sprint":
moveState.sprint = isDown;
updateStick();
break;
default:
pressButton(action, isDown);
}
}
export function injectGameMouseButton(object) {
const isDown = object.type === 1;
if (object.button === 1) pressButton("X", isDown);
if (object.button === 2) pressButton("Y", isDown);
}
export function disconnectController() {
controller.disconnect();
client.disconnect();
}