-
Notifications
You must be signed in to change notification settings - Fork 9
/
demo.gd
278 lines (226 loc) · 6.63 KB
/
demo.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
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
extends Node
const CONNECT_ATTEMPTS = 20
var InterpolationBuffer = load("interpolationbuffer.gd")
var buffers_initialized = false
var interpolation = false
var timer = 0
var host = true
var ready = false
var start = null
var connect = null
var window = null
var network_fps = null
var port = null
var ip = null
var packet_peer = PacketPeerUDP.new()
# For server
var clients = []
# For client
var seq = -1
# Boxes in the scene
var boxes = null
# Kinematic buffers for each of the boxes
var buffers = {}
func _ready():
start = get_node("controls/start")
connect = get_node("controls/connect")
port = get_node("controls/port")
ip = get_node("controls/ip")
window = get_node("controls/window")
network_fps = get_node("controls/network_fps")
boxes = get_node("boxes").get_children()
set_packet_peer_boxes(packet_peer)
load_defaults()
for box in boxes:
buffers[box.get_name()] = InterpolationBuffer.new(window.get_value())
buffers_initialized = true
set_process(true)
for arg in OS.get_cmdline_args():
if (arg == "-server"):
start_server()
break
# Load default values
func load_defaults():
var config_file = ConfigFile.new()
config_file.load("res://defaults.cfg")
ip.set_text(config_file.get_value("defaults", "ip"))
port.set_value(config_file.get_value("defaults", "port"))
window.set_value(config_file.get_value("defaults", "window"))
network_fps.set_value(config_file.get_value("defaults", "network_fps"))
# Toggle starting/stoping a server
func _on_start_pressed():
if (not ready):
start_server()
else:
stop_server()
# Toggle connecting/disconnecting a client
func _on_connect_pressed():
if (not ready):
start_client()
else:
stop_client();
func _process(delta):
#Server update
if (ready and host):
# Handle incoming
while (packet_peer.get_available_packet_count() > 0):
var packet = packet_peer.get_var()
if (packet == null):
continue
var ip = packet_peer.get_packet_ip()
var port = packet_peer.get_packet_port()
if (packet[0] == "connect"):
if (not has_client(ip, port)):
print("Client connected from ", ip, ":", port)
clients.append({ ip = ip, port = port, seq = 0 })
packet_peer.set_send_address(ip, port)
packet_peer.put_var(["accepted"])
elif (packet[0] == "event"):
# Handle event locally
handle_event(packet)
# Broadcast event to clients
for client in clients:
if (client.ip != ip and client.port != port):
packet_peer.set_send_address(ip, port)
packet_peer.put_var(packet)
# Send outgoing
var duration = 1.0 / network_fps.get_value()
if (timer < duration):
timer += delta
else:
timer = 0
for client in clients:
var packet = ["update", client.seq]
client.seq += 1
for box in boxes:
packet.append([box.get_name(), box.get_pos(), box.get_rot(), box.get_linear_velocity()])
packet_peer.set_send_address(client.ip, client.port)
packet_peer.put_var(packet)
#Client update
if (ready and not host):
# Read snapshots from server and add it to a kinematic buffer (if interpolating),
# or immediately update the local state (if not interpolating)
while (packet_peer.get_available_packet_count() > 0):
var packet = packet_peer.get_var()
if (packet == null):
continue
if (packet[0] == "update"):
if (packet[1] > seq):
seq = packet[1]
for i in range(2, packet.size()):
var name = packet[i][0]
var pos = packet[i][1]
var rot = packet[i][2]
var vel = packet[i][3]
if (interpolation):
buffers[name].push_state(pos, rot, vel)
else:
var box = get_node("boxes/" + name)
box.set_pos(pos)
box.set_rot(rot)
# Update interpolation and local state
if (interpolation):
for box in boxes:
var buffer = buffers[box.get_name()]
buffer.update(delta)
box.set_pos(buffer.get_pos())
box.set_rot(buffer.get_rot())
# Toggle interpolation
func _on_lerp_toggled(pressed):
interpolation = pressed
if (pressed):
for box in boxes:
buffers[box.get_name()].reset()
# Change buffer window
func _on_window_value_changed(value):
if (buffers_initialized):
for box in boxes:
buffers[box.get_name()].window = value
buffers[box.get_name()].reset()
# Start/stop functions for client/server
func start_client():
# Select a port for the client
var client_port = port.get_val() + 1
while (packet_peer.listen(client_port) != OK):
client_port += 1
# Set server address
packet_peer.set_send_address(ip.get_text(), port.get_val())
# Try to connect to server
var attempts = 0
var connected = false
while (not connected and attempts < CONNECT_ATTEMPTS):
attempts += 1
packet_peer.put_var(["connect"])
OS.delay_msec(50)
while (packet_peer.get_available_packet_count() > 0):
var packet = packet_peer.get_var()
if (packet != null and packet[0] == "accepted"):
connected = true
break
if (not connected):
print("Error connecting to ", ip.get_text(), ":", port.get_val())
return
else:
print("Connected to ", ip.get_text(), ":", port.get_val())
connect.set_text("Disconnect")
start.set_disabled(true)
set_host_boxes(false)
toggle_kinematic_boxes(true)
host = false
ready = true
func stop_client():
ready = false
host = true
packet_peer.close()
toggle_kinematic_boxes(false)
set_host_boxes(true)
print("Disconnected from ", ip.get_text(), ":", port.get_val())
connect.set_text("Connect")
start.set_disabled(false)
func start_server():
if (packet_peer.listen(port.get_val()) != OK):
print("Error listening on port ", port.get_value())
return
else:
print("Listening on port ", port.get_value())
start.set_text("Stop Server")
connect.set_disabled(true)
set_host_boxes(true)
host = true
ready = true
func stop_server():
ready = false
packet_peer.close()
print("Stopped listening on ", port.get_value())
start.set_text("Start Server")
connect.set_disabled(false)
# Event handler
func handle_event(packet):
var type = packet[1]
var box = get_node("boxes/" + packet[2])
if (type == "drag"):
box.drag(packet[3])
elif (type == "stop_drag"):
box.stop_drag()
# Sets all boxes to host mode
func set_host_boxes(host):
for box in boxes:
box.host = host
# Set packet peer for boxes
func set_packet_peer_boxes(packet_peer):
for box in boxes:
box.packet_peer = packet_peer
# Sets toggles kinematic mode on boxes
func toggle_kinematic_boxes(enabled):
for box in boxes:
if (enabled):
box.set_mode(RigidBody2D.MODE_KINEMATIC)
else:
box.set_mode(RigidBody2D.MODE_RIGID)
box.set_sleeping(false)
# Check client is registered
func has_client(ip, port):
for client in clients:
if (client.ip == ip and client.port == port):
return true
return false