diff --git a/main.tscn b/main.tscn index 7748f3f..8500076 100644 --- a/main.tscn +++ b/main.tscn @@ -1,13 +1,13 @@ [gd_scene load_steps=17 format=3 uid="uid://eiaw4xbs3suk"] +[ext_resource type="Script" uid="uid://btw2de6g358dt" path="res://server.gd" id="1_5vw27"] [ext_resource type="PackedScene" uid="uid://qb8cbljxgnub" path="res://world/killbrick.tscn" id="1_h2yge"] -[ext_resource type="PackedScene" uid="uid://cfceg80unq0pe" path="res://player/player.tscn" id="1_ig7tw"] [ext_resource type="PackedScene" uid="uid://bcmrj6qkemrll" path="res://world/radiohead_cube.tscn" id="2_0xm2m"] -[ext_resource type="Texture2D" uid="uid://dnt0vfav03ris" path="res://world/textures/grass.jpg" id="2_272bh"] +[ext_resource type="Texture2D" uid="uid://wdjmyv260he1" path="res://world/textures/grass.jpg" id="2_272bh"] [ext_resource type="PackedScene" uid="uid://of6tq8gpjxtu" path="res://gears/gear_pickup.tscn" id="3_lquwl"] [ext_resource type="PackedScene" uid="uid://c117buhmmkvkt" path="res://gears/ball.tscn" id="6_5vw27"] [ext_resource type="PackedScene" uid="uid://d3k7b6o56ue5k" path="res://gears/geep.tscn" id="7_kek77"] -[ext_resource type="Texture2D" uid="uid://bl4yvm44o6gcf" path="res://world/textures/sky.hdr" id="8_5vw27"] +[ext_resource type="Texture2D" uid="uid://2ku62tk8r74x" path="res://world/textures/sky.hdr" id="8_5vw27"] [sub_resource type="BoxShape3D" id="BoxShape3D_7dm0k"] size = Vector3(1000, 2, 1000) @@ -41,9 +41,7 @@ glow_hdr_luminance_cap = 0.0 [sub_resource type="Compositor" id="Compositor_5vw27"] [node name="Main" type="Node3D"] - -[node name="Player" parent="." instance=ExtResource("1_ig7tw")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0) +script = ExtResource("1_5vw27") [node name="Baseplate" type="StaticBody3D" parent="."] @@ -89,3 +87,54 @@ compositor = SubResource("Compositor_5vw27") transform = Transform3D(0.70710665, 0.5, -0.5, 0, 0.7071067, 0.7071067, 0.7071069, -0.49999988, 0.49999988, -500, 200, 500) light_color = Color(1, 0.87058824, 0.12941177, 1) shadow_enabled = true + +[node name="UI" type="CanvasLayer" parent="."] + +[node name="Menu" type="Panel" parent="UI"] +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="MarginContainer" type="MarginContainer" parent="UI/Menu"] +layout_mode = 0 +offset_right = 1149.0 +offset_bottom = 134.0 +theme_override_constants/margin_left = 15 +theme_override_constants/margin_top = 15 + +[node name="VBoxContainer" type="VBoxContainer" parent="UI/Menu/MarginContainer"] +layout_mode = 2 + +[node name="Label" type="Label" parent="UI/Menu/MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Menu +" +horizontal_alignment = 1 + +[node name="DisableUPNPButton" type="CheckButton" parent="UI/Menu/MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Disable UPNP +" + +[node name="ServerButton" type="Button" parent="UI/Menu/MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Host +" + +[node name="ClientButton" type="Button" parent="UI/Menu/MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Join +" + +[node name="LineEdit" type="LineEdit" parent="UI/Menu/MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Enter Address" + +[node name="MultiplayerSpawner" type="MultiplayerSpawner" parent="."] +_spawnable_scenes = PackedStringArray("uid://cfceg80unq0pe") +spawn_path = NodePath("..") + +[connection signal="pressed" from="UI/Menu/MarginContainer/VBoxContainer/ServerButton" to="." method="_on_server_button_pressed"] +[connection signal="pressed" from="UI/Menu/MarginContainer/VBoxContainer/ClientButton" to="." method="_on_client_button_pressed"] diff --git a/menu.tscn b/menu.tscn new file mode 100644 index 0000000..ede58aa --- /dev/null +++ b/menu.tscn @@ -0,0 +1,21 @@ +[gd_scene format=3 uid="uid://dbv3ty2ulxir4"] + +[node name="Menu" type="Control"] +layout_mode = 3 +anchors_preset = 0 + +[node name="client_button" type="Button" parent="."] +layout_mode = 0 +offset_right = 200.0 +offset_bottom = 100.0 +text = "Client +" + +[node name="server_button" type="Button" parent="."] +layout_mode = 0 +offset_top = 165.0 +offset_right = 200.0 +offset_bottom = 265.0 +text = "Server + +" diff --git a/player/player.gd b/player/player.gd index 6cf787c..bd018f7 100644 --- a/player/player.gd +++ b/player/player.gd @@ -18,6 +18,11 @@ var target_velocity = Vector3.ZERO var _health = 100 +# Multiplayer + +func _enter_tree(): + set_multiplayer_authority(str(name).to_int()) + # Player UI func resize_ui(): @@ -239,8 +244,18 @@ func _ready(): resize_ui() respawn() + if is_multiplayer_authority(): + $CameraGimbal/InnerGimbal/Camera3D.current = true + else: + $CameraGimbal/InnerGimbal/Camera3D.current = false + func _physics_process(delta): - if dead: + if dead or not is_multiplayer_authority(): + return + if health() < 1 or position.y <= -1000: + die() + + if suspended: return if health() < 1 or position.y <= -1000: diff --git a/player/player.tscn b/player/player.tscn index 5ab62db..a743889 100644 --- a/player/player.tscn +++ b/player/player.tscn @@ -1,7 +1,7 @@ -[gd_scene load_steps=8 format=4 uid="uid://cfceg80unq0pe"] +[gd_scene load_steps=9 format=4 uid="uid://cfceg80unq0pe"] -[ext_resource type="Script" uid="uid://djeyfi7vm2vw0" path="res://player/player.gd" id="1_onrkg"] -[ext_resource type="Script" uid="uid://oi6sint7jkc6" path="res://player/camera.gd" id="2_onrkg"] +[ext_resource type="Script" uid="uid://bfflnag3p4gen" path="res://player/player.gd" id="1_onrkg"] +[ext_resource type="Script" uid="uid://c63ed0a36rv04" path="res://player/camera.gd" id="2_onrkg"] [ext_resource type="Texture2D" uid="uid://cfb0gbwm57hm4" path="res://models/player_0.png" id="3_hqtel"] [sub_resource type="BoxShape3D" id="BoxShape3D_onrkg"] @@ -47,6 +47,35 @@ _surfaces = [{ }] shadow_mesh = SubResource("ArrayMesh_e57bw") +[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_g1dw6"] +properties/0/path = NodePath(".:position") +properties/0/spawn = true +properties/0/replication_mode = 1 +properties/1/path = NodePath("CameraGimbal:position") +properties/1/spawn = true +properties/1/replication_mode = 1 +properties/2/path = NodePath("CameraGimbal:rotation") +properties/2/spawn = true +properties/2/replication_mode = 1 +properties/3/path = NodePath("CameraGimbal/InnerGimbal:position") +properties/3/spawn = true +properties/3/replication_mode = 1 +properties/4/path = NodePath("CameraGimbal/InnerGimbal:rotation") +properties/4/spawn = true +properties/4/replication_mode = 1 +properties/5/path = NodePath("CameraGimbal/InnerGimbal/Camera3D:position") +properties/5/spawn = true +properties/5/replication_mode = 1 +properties/6/path = NodePath("CameraGimbal/InnerGimbal/Camera3D:rotation") +properties/6/spawn = true +properties/6/replication_mode = 1 +properties/7/path = NodePath("Pivot:position") +properties/7/spawn = true +properties/7/replication_mode = 1 +properties/8/path = NodePath("Pivot:rotation") +properties/8/spawn = true +properties/8/replication_mode = 1 + [node name="Player" type="CharacterBody3D"] collision_layer = 2 collision_mask = 3 @@ -102,6 +131,9 @@ text = "Pictures of you" horizontal_alignment = 2 vertical_alignment = 2 +[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] +replication_config = SubResource("SceneReplicationConfig_g1dw6") + [connection signal="child_entered_tree" from="Backpack" to="." method="_on_backpack_child_entered_tree"] [connection signal="child_exiting_tree" from="Backpack" to="." method="_on_backpack_child_exiting_tree"] [connection signal="timeout" from="RespawnTimer" to="." method="respawn"] diff --git a/server.gd b/server.gd new file mode 100644 index 0000000..b32ac21 --- /dev/null +++ b/server.gd @@ -0,0 +1,55 @@ +extends Node3D + +@onready var menu = $UI/Menu +@onready var address_entry = $UI/Menu/MarginContainer/VBoxContainer/LineEdit +@onready var disable_upnp = $UI/Menu/MarginContainer/VBoxContainer/DisableUPNPButton + +const port = 9999 +const player_scene = preload("res://player/player.tscn") +var peer = ENetMultiplayerPeer.new() + +func _on_server_button_pressed() -> void: + menu.hide() + peer.create_server(port) + multiplayer.multiplayer_peer = peer + multiplayer.peer_connected.connect(add_player) + multiplayer.peer_disconnected.connect(remove_player) + + add_player(multiplayer.get_unique_id()) + + if not disable_upnp.button_pressed: + upnp_setup() + +func _on_client_button_pressed() -> void: + var address = "localhost" + if not disable_upnp.button_pressed: + address = address_entry.text + menu.hide() + peer.create_client(address, port) + multiplayer.multiplayer_peer = peer + +func add_player(peer_id): + var player = player_scene.instantiate() + player.name = str(peer_id) + add_child(player) + +func remove_player(peer_id): + var player = get_node_or_null(str(peer_id)) + if player: + player.queue_free() + +func upnp_setup(): + var upnp = UPNP.new() + + if upnp.discover() != UPNP.UPNP_RESULT_SUCCESS: + return false + + var gateway = upnp.get_gateway() + if not gateway.is_valid_gateway(): + return false + + if upnp.add_port_mapping(port) != UPNP.UPNP_RESULT_SUCCESS: + return false + + print("Address: " + upnp.query_external_address()) + return true