Making server run custom map

This commit is contained in:
leca 2024-02-21 04:37:40 +03:00
parent 1f3864d53c
commit 38c518e65a
2 changed files with 68 additions and 23 deletions

View File

@ -8,21 +8,51 @@ var peer = ENetMultiplayerPeer.new()
var settings
var clients = {}
var last_client_id = 1
var map_path
var map_root_name
func parse_arguments():
var arguments = {}
for argument in OS.get_cmdline_args():
if argument.find("=") > -1:
var key_value = argument.split("=")
arguments[key_value[0].lstrip("--")] = key_value[1]
else:
# Options without an argument will be present in the dictionary,
# with the value set to an empty string.
arguments[argument.lstrip("--")] = ""
return arguments
func _ready():
var arguments = parse_arguments()
if "--server" in OS.get_cmdline_args():
await get_tree().create_timer(0.01).timeout #костыль пиздец но мне похую как-то
get_tree().change_scene_to_file("res://scenes/test_map.tscn")
await get_tree().create_timer(0.05).timeout #костыль
server_map = get_tree().root.get_node("test_map")
StartServer()
var path
if (arguments.has("map")):
map_root_name = str(arguments["map"])
path = "res://scenes/Map/" + str(arguments["map"]) + ".tscn"
else:
map_root_name = "OffisMi"
path = "res://scenes/Map/OffisMi.tscn"
StartServer(path)
map_path = path
#######################################SERVER####################################
func find_cb3d_by_internal_id(internal_id):
return server_map.get_node("player" + str(internal_id)).find_child("CharacterBody3D")
func StartServer():
func StartServer(map_name):
await get_tree().create_timer(0.01).timeout #костыль пиздец но мне похую как-то
if (map_name != null):
get_tree().change_scene_to_file(map_name)
await get_tree().create_timer(0.1).timeout #костыль пиздец но мне похую как-то
var root_node_name = (map_name.split("/")[-1]).split(".")[0]
print(str(root_node_name))
print(str(get_tree().root.get_children()))
server_map = get_tree().root.get_node(root_node_name)
await get_tree().create_timer(0.5).timeout #костыль
settings = GameData.server_settings
var port = int(settings["port"])
var maxclients = int(settings["maxclients"])
@ -221,6 +251,10 @@ func reloading_complete(client_id, reloading_time):
else:
current_client_weapon["magazine"] += current_client_weapon["ammo"]
current_client_weapon["ammo"] = 0
@rpc("reliable", "any_peer", "call_remote")
func get_map(client_id):
rpc_id(client_id, "receive_map", map_path)
##########################################CLIENT#######################
var player
@ -247,6 +281,10 @@ func despawn_puppet(internal_id):
var puppet = current_map_instance.get_node("player" + str(internal_id))
current_map_instance.remove_child(puppet)
@rpc("authority", "reliable", "call_remote")
func receive_map(p):
map_path = p
map_root_name = (map_path.split("/")[-1]).split(".")[0]
func ConnectToServer(ip, port):
peer.create_client(ip, port)
multiplayer.multiplayer_peer = peer
@ -269,10 +307,12 @@ func _Connection_Succseeded():
var nickname = GameData.client_settings["nickname"]
rpc_id(1, "set_nickname", multiplayer.get_unique_id(), nickname)
rpc_id(1, "get_character_properties", multiplayer.get_unique_id())
rpc_id(1, "get_map", multiplayer.get_unique_id())
await get_tree().create_timer(0.2).timeout # костыль
get_tree().change_scene_to_file("res://scenes/test_map.tscn")
get_tree().change_scene_to_file(map_path)
await get_tree().create_timer(1).timeout # костыль
current_map_instance = get_tree().root.get_node("test_map")
current_map_instance = get_tree().root.get_node(map_root_name)
rpc_id(1, "get_client_list", multiplayer.get_unique_id())
await get_tree().create_timer(0.05).timeout # костыль # tp

View File

@ -30,14 +30,16 @@ var properties = {
current_weapon = null, #game_settings["weapons"]["knife"].duplicate(),
current_weapon_number = 0,
last_shot = 0,
reloading = false
reloading = false,
offline_mode = false
}
var speed = 0
var walk = 0
var run = 0
var jump = 0
var gravity = 0
var speed = 12
var walk = 12
var run = 24
var jump = 16
var gravity = 9.8
var game_settings
var client_settings
#var current_weapon = Weapons["knife"]
@ -90,10 +92,14 @@ func find_current_weapon_by_number(number):
return found_weapon
func _ready():
#if ($"..".name == "player"):
#properties["offline_mode"] = true
#properties["is_playable"] = true
#await Networking.StartServer("res://scenes/Map/OffisMi.tscn")
$"Head/Nickname".text = properties["nickname"]
if (!properties["is_playable"]): return
camera.make_current()
print("I am alive")
var hud = load("res://scenes/hud.tscn").instantiate()
add_child(hud)
HUD = $"HUD"
@ -117,7 +123,7 @@ func try_shoot():
time_since_last_shot = (Time.get_ticks_msec() - properties["last_shot"]) / 1000.
if (time_since_last_shot > current_weapon_settings["fireRate"]):
Networking.shot.rpc_id(1, multiplayer.get_unique_id())
if (!properties["offline_mode"]): Networking.shot.rpc_id(1, multiplayer.get_unique_id())
if (properties["current_weapon"]["magazine"] > 0):
properties["current_weapon"]["magazine"] -= 1
properties["last_shot"] = Time.get_ticks_msec()
@ -143,14 +149,13 @@ func _unhandled_input(event):
if Input.is_action_pressed("MWU"):
var weapon_number = wrap(properties["current_weapon"]["number"] + 1, 0, game_settings["weapons"].size())
#properties["current_weapon"] = find_weapon_by_number(weapon_number)
properties["current_weapon"] = find_current_weapon_by_number(weapon_number)
Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
if (!properties["offline_mode"]): Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
change_weapon(properties["current_weapon"]["number"])
if Input.is_action_pressed("MWD"):
var weapon_number = wrap(properties["current_weapon"]["number"] - 1, 0, game_settings["weapons"].size())
properties["current_weapon"] = find_current_weapon_by_number(weapon_number)
Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
if (!properties["offline_mode"]): Networking.change_weapon.rpc_id(1, multiplayer.get_unique_id(), properties["current_weapon"]["number"])
change_weapon(properties["current_weapon"]["number"])
@ -161,6 +166,7 @@ func _unhandled_input(event):
func _physics_process(delta):
if (!properties["is_playable"]): return
#if (game_settings == null and !properties["offline_mode"]): return
if (game_settings == null): return
if (multiplayer.multiplayer_peer == null):
properties["is_playable"] = false
@ -173,7 +179,7 @@ func _physics_process(delta):
if Input.is_action_just_pressed("reload"):
var current_weapon = game_settings["weapons"].find_key(find_weapon_by_number(properties["current_weapon"]["number"]))
Networking.client_reloading.rpc_id(1, multiplayer.get_unique_id())
if (!properties["offline_mode"]): Networking.client_reloading.rpc_id(1, multiplayer.get_unique_id())
properties["reloading"] = true
get_tree().create_timer(game_settings["weapons"][current_weapon]["reload"]).connect("timeout", reload)
@ -182,7 +188,6 @@ func _physics_process(delta):
if not is_on_floor():
velocity.y -= 4 * gravity * delta
if is_on_floor():
if Input.is_action_just_pressed("jump"):
velocity.y = jump
@ -201,7 +206,7 @@ func _physics_process(delta):
velocity.z = lerp(velocity.z, direction.z * speed, delta)
move_and_slide()
if (!multiplayer.is_server()):
Networking.sync_client.rpc_id(1, multiplayer.get_unique_id(), $".".position, {"y": head.rotation.y, "x": camera.rotation.x})
if (!properties["offline_mode"]): Networking.sync_client.rpc_id(1, multiplayer.get_unique_id(), $".".position, {"y": head.rotation.y, "x": camera.rotation.x})
#print("I am " + $"..".name +", belonging of " + str(multiplayer.get_unique_id()) + ", with internal ID: " + str(properties["internal_id"]))
@rpc ("authority", "call_remote", "unreliable")