Skip to content

Commit 7f78181

Browse files
committed
synchronized moving_cube
1 parent 208c8dd commit 7f78181

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

client/assets/net/server_to_client.gd

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,27 @@ func free_player_on_client(id):
5858
func send_output_to_client_unreliable(states_udp_json_string: String):
5959
var states_udp: Dictionary = JSON.parse_string(states_udp_json_string)
6060
# print("udp_received: " + str(states_udp))
61-
for id in states_udp["player"]:
62-
var player = get_node("/root/main/players/" + id + "/" + id)
63-
if states_udp["player"][id].has("rotation"):
64-
player.rotation = Vector3(str_to_var("Vector3" + states_udp["player"][id]["rotation"]))
65-
if states_udp["player"][id].has("position"):
66-
player.position = Vector3(str_to_var("Vector3" + states_udp["player"][id]["position"]))
67-
if states_udp["player"][id].has("camera_arm_rotation"):
68-
player.camera_arm.rotation = Vector3(str_to_var("Vector3" + states_udp["player"][id]["camera_arm_rotation"]))
61+
62+
if states_udp.has("player"):
63+
for id in states_udp["player"]:
64+
var player = get_node("/root/main/players/" + id + "/" + id)
65+
if states_udp["player"][id].has("rotation"):
66+
player.rotation = Vector3(str_to_var("Vector3" + states_udp["player"][id]["rotation"]))
67+
if states_udp["player"][id].has("position"):
68+
player.position = Vector3(str_to_var("Vector3" + states_udp["player"][id]["position"]))
69+
if states_udp["player"][id].has("camera_arm_rotation"):
70+
player.camera_arm.rotation = Vector3(str_to_var("Vector3" + states_udp["player"][id]["camera_arm_rotation"]))
71+
if states_udp.has("moving_cube"):
72+
for id in states_udp["moving_cube"]:
73+
74+
if get_node("/root/main/maps/").get_child_count() > 0:
75+
print(get_node("/root/main/maps/").get_child(0).name)
76+
77+
var moving_cube = get_node("/root/main/maps/").get_child(0).get_node(id)
78+
if states_udp["moving_cube"][id].has("rotation"):
79+
moving_cube.rotation = Vector3(str_to_var("Vector3" + states_udp["moving_cube"][id]["rotation"]))
80+
if states_udp["moving_cube"][id].has("position"):
81+
moving_cube.position = Vector3(str_to_var("Vector3" + states_udp["moving_cube"][id]["position"]))
6982

7083
# var rotation = Vector3(str_to_var("Vector3" + output["rotation"]))
7184
# get_node("/root/main/players/" + str(id) + "/" + str(id)).rotation = rotation

server/assets/moving_cube/moving_cube.gd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var length = 0.8
66
var speed = 5
77
var accel = 0.1
88
var pos
9+
var old_position: Vector3
10+
var old_rotation: Vector3
911

1012
func _ready():
1113
pass
@@ -15,6 +17,22 @@ func _process(delta):
1517
pos = cos(vel*length)
1618
position.x += pos
1719

20+
#UDP: add player_state_udp to states_udp
21+
var moving_cube_state_udp: Dictionary = {}
22+
if position.distance_to(old_position) >0.00001: # only add if changed enough
23+
old_position=position
24+
moving_cube_state_udp["position"] = position
25+
if rotation != old_rotation: # only add if changed
26+
old_rotation=rotation
27+
moving_cube_state_udp["rotation"] = rotation
28+
if !moving_cube_state_udp.is_empty(): # only add if not empty
29+
#if states_udp doesnt have player category add it to states_udp
30+
if !get_node("/root/main/net").states_udp.has("moving_cube"):
31+
get_node("/root/main/net").states_udp["moving_cube"] = {}
32+
# add player_state to states_udp
33+
get_node("/root/main/net").states_udp["moving_cube"][name] = moving_cube_state_udp
34+
35+
1836
# moving_cube_state = [position]
1937
#
2038
# #if moving_cube doesnt exist add it to global_state

0 commit comments

Comments
 (0)