Added serving of the customers, npc exits, main menu, settings, saving and loading.

This commit is contained in:
Patrick 2024-12-20 19:51:46 +01:00
parent 3d1e6caa29
commit 2b6d1faadb
19 changed files with 360 additions and 39 deletions

View file

@ -15,14 +15,16 @@ var ip: String = "127.0.0.1"
var port: int = 25262
var max_clients: int = 1024
var roomname: String = "sample"
var username: String = "sample"
var roomname: String = ""
var username: String = ""
var mapname: String = "Testmap"
var launchmode: int = 0
func _ready() -> void:
Log.info("Running on: %s %s (%s)" % [OS.get_distribution_name(), OS.get_version(), OS.get_name()])
randomize()
load_settings()
var args: PackedStringArray = OS.get_cmdline_args()
var skip: bool = false
for i: int in range(args.size()):
@ -55,8 +57,8 @@ func _ready() -> void:
match launchmode:
0:
# Menu
if get_tree().change_scene_to_file("res://scenes/ui/create_room.tscn") != OK:
Log.error("Couldn't change to the create room scene! Closing application.", "Couldn't change to the create room scene!")
if get_tree().change_scene_to_file("res://scenes/ui/main_menu.tscn") != OK:
Log.error("Couldn't change to the main menu scene! Closing application.", "Couldn't change to the main menu scene!")
1:
# Client with direct join
Networking.join_room()
@ -64,3 +66,31 @@ func _ready() -> void:
# Server
if Networking.start_server() != OK:
Log.error("Failed to start server! Closing application.", "Failed to start server!")
func save_settings() -> void:
var to_save: Dictionary = {
"ip": ip,
"roomname": roomname,
"username": username,
}
var to_save_string: String = JSON.stringify(to_save)
var save_file: FileAccess = FileAccess.open("user://save.json", FileAccess.WRITE)
if save_file == null:
Log.warning("Couldn't save the json.")
return
save_file.store_string(to_save_string)
func load_settings() -> void:
var save_file: FileAccess = FileAccess.open("user://save.json", FileAccess.READ)
if save_file == null:
Log.warning("Couldn't open the json save.")
return
var json_string: String = save_file.get_as_text()
var json: JSON = JSON.new()
if json.parse(json_string) != OK:
Log.warning("Couldn't parse the json save.")
return
var data: Dictionary = json.data
ip = data["ip"]
roomname = data["roomname"]
username = data["username"]

View file

@ -7,6 +7,9 @@
# The peers are untrusted. They may cheat, or try something stupid. If at all possible, Direct peer to peer communication should be prevented.
# Rather, such a thing should be done through the game manager if possible.
#TODO: All instances, where any peer can send something, we have to extensively check it, else it may crash somebodys game! Especially NodePaths!
#TODO: Non affiliated peer log messages!
extends Node
signal playerlist_changed()
@ -328,19 +331,22 @@ func object_sync(position: Vector3, rotation: Vector3, node_name: String) -> voi
else:
Log.warning("Non-manager peer tried to send a manager only request: object_sync")
func npc_text_sync_call(text: String, target: Objective, npc: NPC) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "npc_text_sync", text, target.get_path(), npc.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the text above the npc.
@rpc("any_peer", "call_local", "reliable")
func npc_text_sync(text: String, npc: NodePath) -> void:
func npc_text_sync(text: String, target: NodePath, npc: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id():
(get_node(npc) as NPC).set_text(text)
if text == "":
(get_node(npc) as NPC).set_text_and_target(text, null)
else:
(get_node(npc) as NPC).set_text_and_target(text, get_node(target) as Objective)
else:
Log.warning("Non-manager peer tried to send a manager only request: npc_text_sync")
func npc_text_sync_call(text: String, npc: NPC) -> void:
for peer: int in Networking.get_ids():
if rpc_id(peer, "npc_text_sync", text, npc.get_path()) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void:
# We need to get the path before we send out sync calls. This is because the value changes depending on whether or not it already ran locally.
var carryable_path: NodePath = carryable.get_path()
@ -407,7 +413,10 @@ func guncase_sync(status: bool, player_path: NodePath) -> void:
#TODO: Anti-Cheat -> Check if the gun is even available!
if verify_id(multiplayer.get_remote_sender_id()):
(get_node("/root/%s/Guncase" % Game.mapname) as Guncase).held = status
(get_node(player_path) as Player).show_gun()
if status == true:
(get_node(player_path) as Player).show_gun()
else:
(get_node(player_path) as Player).hide_gun()
func bullet_sync_call(position: Vector3, rotation: Vector3) -> void:
for peer: int in Networking.get_ids():
@ -423,3 +432,32 @@ func bullet_sync(position: Vector3, rotation: Vector3) -> void:
bullet_instance.position = position
bullet_instance.rotation = rotation
get_node("/root/"+Game.mapname).add_child(bullet_instance)
func npc_free_sync_call(npc: NPC) -> void:
var npc_path: NodePath = npc.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "npc_free_sync", npc_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the npc removal.
@rpc("any_peer", "call_local", "reliable")
func npc_free_sync(npc_path: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id():
get_node(npc_path).queue_free()
else:
Log.warning("Non-manager peer tried to send a manager only request: npc_free_sync")
func npc_served_sync_call(npc: NPC) -> void:
var npc_path: NodePath = npc.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "npc_served_sync", npc_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes when the npc was served.
@rpc("any_peer", "call_local", "reliable")
func npc_served_sync(npc_path: NodePath) -> void:
#TODO: Anti-Cheat -> Prevent unauthorized serving!
if verify_id(multiplayer.get_remote_sender_id()):
(get_node(npc_path) as NPC).served()
else:
Log.warning("Non-manager peer tried to send a manager only request: npc_served_sync")