-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdvancedInput.gd
200 lines (161 loc) · 6.03 KB
/
AdvancedInput.gd
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
extends Component
export(bool) var run_is_toggle : bool = true
export(bool) var crouch_is_toggle : bool = true
export(bool) var captured : bool = true # Does not let the mouse leave the screen
var can_jump = true
var jump_timer = null
var input_devices : Dictionary = {}
var local_input : Dictionary = {}
func _ready():
#Input.set_use_accumulated_input(false)
Input.connect("joy_connection_changed", self, "_on_joy_changed")
_component_name = "input"
actor.input["look_y"] = 0
actor.input["look_x"] = 0
actor.input["special"] = 0
actor.input["left"] = 0
actor.input["right"] = 0
actor.input["forward"] = 0
actor.input["back"] = 0
actor.input["jump"] = 0
actor.input["extra_jump"] = 0
actor.input["use"] = 0
actor.input["crouch"] = 0
actor.input["sprint"] = 0
actor.input["next_weapon"] = 0
actor.input["shoot"] = 0
actor.input["reload"] = 0
actor.input["zoom"] = 0
local_input["look_y"] = 0
local_input["look_x"] = 0
local_input["special"] = 0
local_input["left"] = 0
local_input["right"] = 0
local_input["forward"] = 0
local_input["back"] = 0
local_input["jump"] = 0
local_input["extra_jump"] = 0
local_input["use"] = 0
local_input["crouch"] = 0
local_input["sprint"] = 0
local_input["next_weapon"] = 0
local_input["shoot"] = 0
local_input["reload"] = 0
local_input["zoom"] = 0
get_tree().create_timer(0.01).connect("timeout", self, "functional_routine")
func _mouse_toggle() -> void:
# Function to lock or unlock the mouse in the center of the screen
if Input.is_action_just_pressed("KEY_ESCAPE"):
# Captured will receive the opposite of the value itself
captured = !captured
if captured:
# Locks the mouse in the center of the screen
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
# Unlocks the mouse from the center of the screen
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
func functional_routine():
if get_tree().has_network_peer():
if not is_network_master() or not enabled:
return
else:
get_input()
get_tree().create_timer(0.01).connect("timeout", self, "functional_routine")
else:
get_input()
get_tree().create_timer(0.01).connect("timeout", self, "functional_routine")
func get_input():
actor.input["left"] = local_input["left"]
actor.input["right"] = local_input["right"]
actor.input["forward"] = local_input["forward"]
actor.input["back"] = local_input["back"]
actor.input["next_weapon"] = local_input["next_weapon"]
if not crouch_is_toggle:
actor.input["crouch"] = local_input["crouch"]
if not run_is_toggle:
actor.input["sprint"] = local_input["sprint"]
actor.input["use"] = local_input["use"]
actor.input["shoot"] = local_input["shoot"]
actor.input["reload"] = local_input["reload"]
actor.input["zoom"] = local_input["zoom"]
actor.input["special"] = local_input["special"]
actor.input["extra_jump"] = local_input["extra_jump"]
actor.input["look_y"] = local_input["look_y"]
actor.input["look_x"] = local_input["look_x"]
sync_input()
func sync_input():
if get_tree().has_network_peer():
if is_network_master() and not get_tree().is_network_server():
actor.rset_unreliable_id(1, "input", actor.input)
Gamestate.set_in_all_clients(actor, "input", actor.input)
func mouse_move(event):
Input.get_last_mouse_speed()
if event is InputEventMouseMotion:
actor.input["look_y"] = event.relative.y
actor.input["look_x"] = event.relative.x
yield(get_tree().create_timer(0.001), "timeout") # Replace timer with a tenth of a frame quantum (From new singleton)
actor.input["look_y"] = 0
actor.input["look_x"] = 0
func _unhandled_input(event):
if get_tree().has_network_peer():
if not is_network_master() or not enabled:
return
else:
unhandled(event)
else:
unhandled(event)
func unhandled(event):
# Calls function to switch between locked and unlocked mouse
_mouse_toggle()
if int(Input.is_action_just_pressed("KEY_SPACE")):
actor.input["jump"] = true
mouse_move(event)
if run_is_toggle:
if Input.is_action_just_pressed("KEY_SHIFT"):
actor.input["sprint"] = int(not bool(actor.input["sprint"]))
if Input.is_action_pressed("KEY_CTRL") or actor.run_speed < 0.3 or Input.is_action_just_released("KEY_W"):
actor.input["sprint"] = 0
if crouch_is_toggle:
if Input.is_action_just_released("KEY_CTRL"):
actor.input["crouch"] = int(not bool(actor.input["crouch"]))
if Input.is_action_pressed("KEY_SHIFT") or Input.is_action_just_released("KEY_SPACE"):
actor.input["crouch"] = 0
# if get_tree().has_network_peer():
# if is_network_master() and not get_tree().is_network_server():
#Gamestate.set_in_all_clients(self,"input", actor.input)
# actor.rset_unreliable_id(1, "input", actor.input)
# if Input.is_action_just_released(("KEY_SPACE")) and Input.is_action_pressed("KEY_SPACE"):
# actor.input["jump_extra"] = 1
func _on_joy_changed(device : int):
input_devices[device]["player"]
# Deletes a subdictionary when the corresponding device is disconnected
func _on_joy_disconnected(device : int):
input_devices.erase(device)
func _input(event):
if input_devices.has(event.device):
pass
else:
input_devices[event.device] = {}
if event is InputEventJoypadButton or event is InputEventJoypadMotion:
if Input.get_joy_name(event.device) == "XInput Gamepad":
_XInput_scheme(event)
func _XInput_scheme(event):
if event is InputEventJoypadMotion:
if event.axis == JOY_AXIS_0:
if event.axis_value > 0:
input_devices[event.device]["right"] = clamp(event.axis_value, 0, 1)
else:
input_devices[event.device]["left"] = clamp(-event.axis_value, 0, 1)
if event.axis == JOY_AXIS_1:
if event.axis_value > 0:
input_devices[event.device]["back"] = clamp(event.axis_value, 0, 1)
else:
input_devices[event.device]["forward"] = clamp(-event.axis_value, 0, 1)
if event.axis == JOY_AXIS_2:
input_devices[event.device]["look_x"] = event.axis_value
if event.axis == JOY_AXIS_3:
input_devices[event.device]["look_y"] = event.axis_value
if event.axis == JOY_AXIS_6:
input_devices[event.device]["shoot"] = event.axis_value
if event.axis == JOY_AXIS_7:
input_devices[event.device]["zoom"] = event.axis_value