From e57103a4f99cdc0693471fe772dc7893ff65e855 Mon Sep 17 00:00:00 2001 From: Anson Bridges Date: Wed, 31 Aug 2022 17:19:49 -0700 Subject: added local servers --- scripts/ClientUI.gd | 35 +++++++++++++++++++++++++++---- scripts/GameBase.gd | 11 ++++++++++ scripts/Server.gd | 11 ++++++---- scripts/ServerUI.gd | 2 +- scripts/World.gd | 1 + scripts/ballistics/Cannonball.gd | 11 ++-------- scripts/ballistics/NetworkedProjectile.gd | 22 +++++++++++++++++++ scripts/machines/Cannon.gd | 10 +++++++++ 8 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 scripts/ballistics/NetworkedProjectile.gd (limited to 'scripts') diff --git a/scripts/ClientUI.gd b/scripts/ClientUI.gd index 6a3eb41..32e7269 100644 --- a/scripts/ClientUI.gd +++ b/scripts/ClientUI.gd @@ -2,26 +2,53 @@ extends Node2D var client : NetworkedMultiplayerENet var world +var local_server_tree = null +var is_local = false func _ready(): world = preload("res://scenes/GameBase.tscn").instance() - get_tree().connect("network_peer_connected", self, "_player_connected") - get_tree().connect("network_peer_disconnected", self, "_player_disconnected") + #get_tree().connect("network_peer_connected", self, "_player_connected") + #get_tree().connect("network_peer_disconnected", self, "_player_disconnected") get_tree().connect("connected_to_server", self, "_connection_successful") get_tree().connect("connection_failed", self, "_connected_fail") - get_tree().connect("server_disconnected", world, "_server_disconnected") + get_tree().connect("server_disconnected", world, "client_disconnect") + set_process(false) + set_physics_process(false) func _connect_btn(): client = NetworkedMultiplayerENet.new() client.create_client($IP.text, $Port.value) get_tree().set_network_peer(client) - func _connection_successful(): get_tree().get_root().add_child(world) world.client = client world.client_id = get_tree().get_network_unique_id() world.player_name = $Name.text + if is_local: + world.is_local = true + world.local_server_tree = local_server_tree print("Connected.") queue_free() + +func _run_local_server(): + local_server_tree = SceneTree.new() + local_server_tree.init() + local_server_tree.get_root().set_update_mode(Viewport.UPDATE_DISABLED) + var server_script = preload("res://scenes/Server.tscn").instance() + + #CHANGE LEVEL HERE + server_script.start_server("Local Server", "Local Singleplayer Server", 3, "res://maps/Main.tscn", "127.0.0.1", 25565, local_server_tree, null, "print") + set_process(true) + set_physics_process(true) + is_local = true + client = NetworkedMultiplayerENet.new() + client.create_client("127.0.0.1", 25565) + get_tree().set_network_peer(client) + +func _process(delta): + local_server_tree.idle(delta) + +func _physics_process(delta): + local_server_tree.iteration(delta) diff --git a/scripts/GameBase.gd b/scripts/GameBase.gd index ee26531..019aa14 100644 --- a/scripts/GameBase.gd +++ b/scripts/GameBase.gd @@ -2,6 +2,9 @@ extends Spatial var client var client_id +var is_local = false +var local_server_tree = null + var player_name : String var player_team : String var player_char = null @@ -12,6 +15,14 @@ var is_chatting = false var winddir = Vector3(1,0,0) +func _process(delta): + if is_local: + local_server_tree.idle(delta) + +func _physics_process(delta): + if is_local: + local_server_tree.iteration(delta) + remote func set_up_server_info(info): $HUD/ServerJoinMenu/MOTD.text = info["MOTD"] $HUD/ServerJoinMenu/ServerName.text = info["server_name"] diff --git a/scripts/Server.gd b/scripts/Server.gd index bd24911..531f30e 100644 --- a/scripts/Server.gd +++ b/scripts/Server.gd @@ -12,13 +12,16 @@ var output var output_func : String func print_line(line): + if output == null: + print("SERVER: " + line) + return output.call(output_func, line) func _ready(): get_tree().connect("network_peer_connected", self, "_client_connect") get_tree().connect("network_peer_disconnected", self, "_client_disconnect") -func start_server(_server_name: String, _motd: String, max_players: int, map_path: String, ip: String, port: int, root, output_obj, output_f): +func start_server(_server_name: String, _motd: String, max_players: int, map_path: String, ip: String, port: int, tree, output_obj, output_f): output = output_obj output_func = output_f server_name = _server_name @@ -33,12 +36,13 @@ func start_server(_server_name: String, _motd: String, max_players: int, map_pat return world.add_child(self) - root.add_child(world) + tree.get_root().add_child(world) + world.client_id = 1 server_enet = NetworkedMultiplayerENet.new() server_enet.create_server(port, max_players) - get_tree().set_network_peer(server_enet) + tree.set_network_peer(server_enet) print_line("Server started successfully.") @@ -96,7 +100,6 @@ func _client_request_change_character(arguments): old.rpc("set_owner", 1) func _character_death(arguments): - print(arguments) var victim_player = connected_players[arguments["victim_mp_id"]][0] if arguments["victim_mp_id"] != 1 else "" print_line(arguments["victim"] + " ("+victim_player+") killed by " + arguments["killer"] + " " + arguments["extra"] + ".") world.rpc("game_chat_msg", arguments["victim"] + " ("+victim_player+") killed by " + arguments["killer"] + " " + arguments["extra"] + ".") diff --git a/scripts/ServerUI.gd b/scripts/ServerUI.gd index 1acf9eb..8daf086 100644 --- a/scripts/ServerUI.gd +++ b/scripts/ServerUI.gd @@ -25,7 +25,7 @@ func start_server(): server = preload("res://scenes/Server.tscn").instance() $CmdPrompt.server_ref = server - server.start_server($ServerName.text, $MOTD.text, $PlayerCount.value, $MapPath.text, $IP.text, $Port.value, get_tree().get_root(), console, "add_line") + server.start_server($ServerName.text, $MOTD.text, $PlayerCount.value, $MapPath.text, $IP.text, $Port.value, get_tree(), console, "add_line") Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) hide_init_fields() diff --git a/scripts/World.gd b/scripts/World.gd index dd5b177..aaad39e 100644 --- a/scripts/World.gd +++ b/scripts/World.gd @@ -49,3 +49,4 @@ func draw_path(path_array): for x in path_array: im.add_vertex(x) im.end() + diff --git a/scripts/ballistics/Cannonball.gd b/scripts/ballistics/Cannonball.gd index e59ca6c..15f35a3 100644 --- a/scripts/ballistics/Cannonball.gd +++ b/scripts/ballistics/Cannonball.gd @@ -1,11 +1,9 @@ -extends RigidBody +extends "res://scripts/ballistics/NetworkedProjectile.gd" export var drag_constant = 0.3 var damage_exceptions = [] var oldvel -var shooter = "WORLD" - func _physics_process(_delta): oldvel = linear_velocity add_force(-1*linear_velocity*drag_constant, Vector3.ZERO) @@ -13,12 +11,7 @@ func _physics_process(_delta): func get_init_info(): return {"linear_velocity" : linear_velocity, "angular_velocity" : angular_velocity, "oldvel" : oldvel, "shooter" : shooter} -func mp_init(init_info): - for variable in init_info.keys(): - set(variable, init_info[variable]) - - func _on_collision(body): if linear_velocity.length() > 20 and !damage_exceptions.has(body) and body.has_method("damage"): - body.damage(oldvel.length(), "blunt") + body.rpc("damage", oldvel.length(), "blunt", shooter, "using 'cannon'") damage_exceptions.append(body) diff --git a/scripts/ballistics/NetworkedProjectile.gd b/scripts/ballistics/NetworkedProjectile.gd new file mode 100644 index 0000000..0626e9f --- /dev/null +++ b/scripts/ballistics/NetworkedProjectile.gd @@ -0,0 +1,22 @@ +extends RigidBody + + +var shooter = "WORLD" + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +func mp_init(init_info): + for variable in init_info.keys(): + set(variable, init_info[variable]) + +remote func update_phys_transform(t, lv, av): + transform = t + linear_velocity = lv + angular_velocity = av + +func _integrate_forces(state): + if is_network_master(): + rpc("update_phys_transform", transform, linear_velocity, angular_velocity) diff --git a/scripts/machines/Cannon.gd b/scripts/machines/Cannon.gd index d7c9a18..fcd842b 100644 --- a/scripts/machines/Cannon.gd +++ b/scripts/machines/Cannon.gd @@ -21,6 +21,11 @@ export var max_yaw = 15 onready var muzzle = get_node("YawJoint/PitchJoint/Muzzle") +remote func update_phys_transform(t, lv, av): + transform = t + linear_velocity = lv + angular_velocity = av + func get_init_info(): return {"pitch_rot" : $YawJoint/PitchJoint.rotation_degrees.z, "turn_rot" : $YawJoint.rotation_degrees.y, "in_use" : in_use} @@ -65,6 +70,8 @@ func _physics_process(delta): $YawJoint/PitchJoint.rotation_degrees.z = max_pitch elif $YawJoint/PitchJoint.rotation_degrees.z < min_pitch: $YawJoint/PitchJoint.rotation_degrees.z = min_pitch + if is_network_master() and mode == MODE_STATIC: + rpc("update_phys_transform", transform, linear_velocity, angular_velocity) func direction_input(fwd,bwd,left,right,_left,_right): pitch = fwd - bwd @@ -73,6 +80,9 @@ func direction_input(fwd,bwd,left,right,_left,_right): func attack1(): if cooldown > 0: return + rpc("fire") + +remotesync func fire(): $YawJoint/PitchJoint/Muzzle/explosion_sound.play() var expl = preload("res://particles/p_Explosion.tscn").instance() var cball = preload("res://scenes/ballistics/Cannonball.tscn").instance() -- cgit v1.2.3