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
|
extends Spatial
var client
var client_id: int
var is_local: bool = false
var is_host: bool = false
var local_server_tree = null
var player_name : String = "Player"
var player_team : int = -1 #-1 = spec
var player_char = null
onready var cam: ClippedCamera = $PLAYERCAM
onready var hud = $HUD
var players_info = {} #dictionary of id : name, team, ping, etc.
var teams_info = {-1: "SPEC"} #dictionary of teams for given server
var is_chatting: bool = false
var winddir = Vector3(1,0,0)
signal screen_shake(location, strength, time)
func _ready():
connect("screen_shake", self, "shake_cam")
if !is_local and !is_host: #if connecting to external server
NavigationServer.set_active(false)
elif !is_local and is_host: #dedicated server
remove_child($HUD)
remove_child($PLAYERCAM)
else: #local server
players_info[1] = ["Player", "SPEC"]
cam.attach($DEFAULTCAM, "STATIC", NodePath("."))
func shake_cam(loc, strength, time):
var dist: float = (cam.global_transform.origin - loc).length()
if dist < 30:
strength = strength*exp(-dist/10)
cam.shake(strength, time)
func _process(delta):
if is_local or !is_host:
$HUD/Health.text = str(player_char.health) if player_char != null else ""
func _physics_process(delta):
pass
remote func set_up_server_info(info):
$HUD/ServerJoinMenu/MOTD.text = info["MOTD"]
$HUD/ServerJoinMenu/ServerName.text = info["server_name"]
$DEFAULTCAM.transform = info["cam_pos"]
cam.attach($DEFAULTCAM, "STATIC", NodePath("."))
rpc_id(1, "_call_on_server", "_client_connection_confirmed", {"id" : client_id, "username" : player_name})
remote func load_map(geo_info):
for geo in geo_info:
var prop = load(geo["filename"]).instance()
prop.transform = geo["transform"]
$WORLDGEO.add_child(prop)
remote func load_entities(entity_info): #machines, players, and projectiles
for entity in entity_info:
var parent_section = get_node(entity["type"])
var ent = load(entity["filename"]).instance()
ent.name = entity["name"]
ent.set_network_master(entity["net_master"])
parent_section.add_child(ent, true)
ent.transform = entity["transform"]
ent.mp_init(entity["init_info"])
remotesync func _call_on_server(function, arguments):
if get_network_master() != 1: return
print('Remote server call: ' + function)
$Server.call(function, arguments)
remotesync func update_players_info(info):
players_info = info
if is_host and !is_local: return
$HUD/ServerJoinMenu/Team1Players.text = ""
$HUD/ServerJoinMenu/Team2Players.text = ""
$HUD/ServerJoinMenu/Spectators.text = ""
for player in players_info.keys():
var p_team = players_info[player][1]
var p_name = players_info[player][0]
if p_team == 0:
$HUD/ServerJoinMenu/Team1Players.text += p_name + ", "
elif p_team == 1:
$HUD/ServerJoinMenu/Team2Players.text += p_name + ", "
elif p_team == -1:
$HUD/ServerJoinMenu/Spectators.text += p_name + ", "
remotesync func game_update_chars():
if is_host and !is_local: return
$HUD.update_characters()
remotesync func game_chat_msg(msg):
if is_host and !is_local: return
$HUD.ui_chat_msg(msg)
remotesync func game_hitsound():
if is_host and !is_local: return
$HUD.ui_play_hitsound()
remotesync func game_killsound():
if is_host and !is_local: return
$HUD.ui_play_killsound()
func request_select_character(dest):
if player_char == null:
rpc_id(1, "_call_on_server", "_client_request_change_character", {"id" : client_id, "current_char_name" : "NULL", "char_name" : dest})
else:
rpc_id(1, "_call_on_server", "_client_request_change_character", {"id" : client_id, "current_char_name" : player_char.name, "char_name" : dest})
func client_disconnect():
if player_char != null:
player_char.deselect_character()
if is_local:
$Server.stop_server()
else:
client.close_connection()
back_to_main()
func _connection_lost():
if is_local:
local_server_tree.get_root().get_node("GAMEWORLD/Server").stop_server()
local_server_tree.free()
back_to_main()
func back_to_main():
var main_menu = load("res://ui/MainMenu.tscn").instance()
get_tree().get_root().add_child(main_menu)
get_tree().get_root().remove_child(self)
queue_free()
func join_team(team):
if player_char != null:
player_char.deselect_character()
cam.attach($DEFAULTCAM, "STATIC", NodePath("."))
player_team = team
rpc_id(1, "_call_on_server", "_client_change_teams", {"id" : client_id, "team" : team})
func get_client_id() -> int:
return client_id
func is_chatting_f() -> bool:
return is_chatting
func get_players_info(id: int, index: int) -> String:
return players_info[id][index]
func set_player_char(character) -> void:
player_char = character
func get_cam():
return cam
|