diff --git a/Player.tscn b/Player.tscn deleted file mode 100644 index 6609112..0000000 --- a/Player.tscn +++ /dev/null @@ -1,40 +0,0 @@ -[gd_scene load_steps=6 format=3 uid="uid://cxxxguab8btnm"] - -[ext_resource type="Script" path="res://Player.gd" id="1_va1f4"] - -[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_jywl0"] - -[sub_resource type="CapsuleMesh" id="CapsuleMesh_7voq6"] - -[sub_resource type="PrismMesh" id="PrismMesh_v72wx"] -size = Vector3(0.66, 0.5, 0.5) - -[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_xlkje"] -properties/0/path = NodePath("..:position") -properties/0/spawn = true -properties/0/replication_mode = 1 - -[node name="CharacterBody3D" type="CharacterBody3D"] -input_ray_pickable = false -script = ExtResource("1_va1f4") - -[node name="CollisionShape3D" type="CollisionShape3D" parent="."] -shape = SubResource("CapsuleShape3D_jywl0") - -[node name="MeshInstance3D" type="MeshInstance3D" parent="CollisionShape3D"] -mesh = SubResource("CapsuleMesh_7voq6") - -[node name="MeshInstance3D2" type="MeshInstance3D" parent="CollisionShape3D/MeshInstance3D"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.690861, -0.370152) -mesh = SubResource("PrismMesh_v72wx") -skeleton = NodePath("../..") - -[node name="Camera3D" type="Camera3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.9682, 0.250177, 0, -0.250177, 0.9682, 0, 1.90604, 2.54868) -cull_mask = 1048573 - -[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] -root_path = NodePath("../CollisionShape3D") -replication_interval = 1.0 -delta_interval = 1.0 -replication_config = SubResource("SceneReplicationConfig_xlkje") diff --git a/assets/Player.tscn b/assets/Player.tscn new file mode 100644 index 0000000..01de0c4 --- /dev/null +++ b/assets/Player.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=4 format=3 uid="uid://bqml7625b3l1q"] + +[ext_resource type="Script" path="res://src/Player.gd" id="1_la1lv"] + +[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_5mrtf"] +points = PackedVector3Array(-0.2, -0.9, -0.2, -0.2, -0.9, 0.2, 0, 0.9, -0.2, 0.2, -0.9, -0.2, 0.2, -0.9, 0.2, 0, 0.9, 0.2) + +[sub_resource type="PrismMesh" id="PrismMesh_c7ovg"] +size = Vector3(0.4, 1.8, 0.4) + +[node name="Player" type="CharacterBody3D"] +script = ExtResource("1_la1lv") + +[node name="Camera3D" type="Camera3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.899966, 0) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.899966, 0) +shape = SubResource("ConvexPolygonShape3D_5mrtf") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="CollisionShape3D"] +mesh = SubResource("PrismMesh_c7ovg") +skeleton = NodePath("../..") diff --git a/globals.gd b/globals.gd new file mode 100644 index 0000000..f4f6135 --- /dev/null +++ b/globals.gd @@ -0,0 +1,4 @@ +extends Node + +var is_server = false +var server_ip = "" \ No newline at end of file diff --git a/icon.png.import b/icon.png.import new file mode 100644 index 0000000..93680d8 --- /dev/null +++ b/icon.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://ce54x86vpupge" +path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.png" +dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/levels/TestLevel.tscn b/levels/TestLevel.tscn index 4554218..a243b0c 100644 --- a/levels/TestLevel.tscn +++ b/levels/TestLevel.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=12 format=3 uid="uid://dh63aggy3a08x"] -[ext_resource type="Script" path="res://GameLogic.gd" id="1_0vmht"] +[ext_resource type="Script" path="res://src/GameLogic.gd" id="1_0vmht"] [ext_resource type="Environment" uid="uid://5s8lkk12bxyx" path="res://assets/default_environment.tres" id="3_8gl6a"] [ext_resource type="Environment" uid="uid://olhmjdg33u2i" path="res://assets/camera_environment.tres" id="3_evlwd"] diff --git a/src/GameLogic.gd b/src/GameLogic.gd new file mode 100644 index 0000000..985f8b8 --- /dev/null +++ b/src/GameLogic.gd @@ -0,0 +1,48 @@ +extends Node + +const PlayerScene = preload("res://assets/Player.tscn") +var rng = RandomNumberGenerator.new() +func _ready(): + $MultiplayerSpawner.add_spawnable_scene("res://assets/Player.tscn") + print($MultiplayerSpawner.get_spawnable_scene(0)) + # Start the server if Godot is passed the "--server" argument, + # and start a client otherwise. + if Globals.is_server: + start_network(true) + else: + start_network(false) + + +func start_network(server: bool): + var peer = ENetMultiplayerPeer.new() + if server: + multiplayer.peer_connected.connect(self.create_player) + multiplayer.peer_disconnected.connect(self.destroy_player) + + peer.create_server(4242) + print('server listening on localhost 4242') + else: + multiplayer.connected_to_server.connect(self.connected_to_server) + peer.create_client(Globals.server_ip, 4242) + + multiplayer.set_multiplayer_peer(peer) + +func create_player(id): + #pass + # Instantiate a new player for this client. + var p = PlayerScene.instantiate() + #var p = Node3D.new() + + # Set the name, so players can figure out their local authority + p.name = str(1) + print(str(id) + ' just connected') + + $NetworkedNodes.add_child(p) + +func destroy_player(id): + pass + # Delete this peer's node. + #$NetworkedNodes.get_node(str(id)).queue_free() + +func connected_to_server(): + $Pole/Camera3D.queue_free() diff --git a/src/Player.gd b/src/Player.gd new file mode 100644 index 0000000..592d576 --- /dev/null +++ b/src/Player.gd @@ -0,0 +1,63 @@ +extends CharacterBody3D + +const SPEED = 5.0 +const JUMP_VELOCITY = 4.5 + +var synced_position := Vector3.ZERO +var synced_rotation := Vector3.ZERO + +# Get the gravity from the project settings to be synced with RigidDynamicBody nodes. +var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") + +var has_control = false + +func _ready(): + has_control = name == str(multiplayer.get_unique_id()) + print( 'I am ' + str(multiplayer.get_unique_id())) + print( 'or ' + str(multiplayer.get_remote_sender_id())) + has_control = !Globals.is_server + if Globals.is_server == false && has_control: + print ( 'Taking control!') + get_node("Camera3D").current = true + +func _physics_process(delta): + if not has_control: + position = synced_position + rotation = synced_rotation + return + # Add the gravity. + if not is_on_floor(): + velocity.y -= gravity * delta + + # Handle Jump. + if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + velocity.y = JUMP_VELOCITY + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var input_dir = Input.get_vector("strafe_left", "strafe_right", "move_forward", "move_backward") + var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + velocity.x = direction.x * SPEED + velocity.z = direction.z * SPEED + else: + velocity.x = move_toward(velocity.x, 0, SPEED) + velocity.z = move_toward(velocity.z, 0, SPEED) + move_and_slide() + + if Input.is_action_pressed("look_left"): + rotation.y += 0.02 + if Input.is_action_pressed("look_right"): + rotation.y -= 0.02 + rpc_id(1, StringName('update_server'), position, rotation) + #update_server(position, rotation).rpc() + +@rpc("any_peer", "call_remote", "unreliable_ordered") +func update_server(pos: Vector3, rot: Vector3): + if not multiplayer.is_server(): + return + #if name != str(multiplayer.get_remote_sender_id()): + ##print ("foei?") + #return + synced_position = pos + synced_rotation = rot