From 6934f2936b3b5ca41244efbc787d82239c1847b4 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 16 Jan 2024 23:01:16 +0100 Subject: [PATCH] Improved environment --- .gitattributes | 1 + .gitignore | 2 + network/Login.gd | 25 ++++--- network/Network.gd | 13 +--- network/Packet.gd | 7 ++ network/Ping.gd | 14 ++-- player/Player.gd | 38 ++++++++-- player/Player.tscn | 17 ++++- project.godot | 4 ++ server/login.go | 11 ++- server/ping.go | 2 - ui/PositionLabel.gd | 7 ++ world/Camera.gd | 20 ++++++ world/Game.tscn | 155 +++++++++++++++++++++++++++------------- world/Global.gd | 4 ++ world/Tree.blend.import | 50 +++++++++++++ 16 files changed, 274 insertions(+), 96 deletions(-) create mode 100644 network/Packet.gd create mode 100644 ui/PositionLabel.gd create mode 100644 world/Camera.gd create mode 100644 world/Global.gd create mode 100644 world/Tree.blend.import diff --git a/.gitattributes b/.gitattributes index 40b3c2d..c1079ab 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ * text=auto eol=lf +*.blend filter=lfs diff=lfs merge=lfs -text *.ttf filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore index 620245f..96394d0 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ !*.svg !*.tres !*.tscn +!*.blend +!*.ttf !.gitignore !.gitattributes !project.godot diff --git a/network/Login.gd b/network/Login.gd index 49b5fea..7639428 100644 --- a/network/Login.gd +++ b/network/Login.gd @@ -1,26 +1,33 @@ extends Node -const PLAYER = preload("res://player/Player.tscn") +@export var playerScene: PackedScene + +var logged_in := false func _ready(): - %Network.add_handler(%Network.Packet.LOGIN, self) + %Network.add_handler(Packet.LOGIN, self) send_login() func send_login(): + if logged_in: + return + var buffer := StreamPeerBuffer.new() - buffer.put_8(%Network.Packet.LOGIN) + buffer.put_8(Packet.LOGIN) buffer.put_data("password".to_utf8_buffer()) %Network.udp.put_packet(buffer.data_array) print("Connecting...") func handle_packet(data: PackedByteArray): - if data[1] != 0: + if data[0] != 0: print("Login failed.") return - print("Login succeeded.", data) - spawn_player() + print("Login succeeded.") + logged_in = true + Global.player = spawn_player() -func spawn_player(): - var player = PLAYER.instantiate() - add_child(player) +func spawn_player() -> Player: + var player = playerScene.instantiate() + %Players.add_child(player) + return player diff --git a/network/Network.gd b/network/Network.gd index 843a188..bc75d85 100644 --- a/network/Network.gd +++ b/network/Network.gd @@ -3,13 +3,6 @@ extends Node var udp := PacketPeerUDP.new() var handlers: Array[Node] = [] -enum Packet { - PING = 1, - LOGIN = 2, - LOGOUT = 3, - MOVE = 10, -} - func _init(): handlers.resize(256) udp.connect_to_host("127.0.0.1", 4242) @@ -19,15 +12,15 @@ func _process(_delta): return var packet := udp.get_packet() - var type := packet.decode_u8(0) as Packet + var type := packet.decode_u8(0) var handler := handlers[type] if handler == null: push_warning("Unknown packet type %d" % type) return - handler.handle_packet(packet) + handler.handle_packet(packet.slice(1)) -func add_handler(packet: Packet, node: Node): +func add_handler(packet: int, node: Node): assert(node.has_method("handle_packet")) handlers[packet] = node diff --git a/network/Packet.gd b/network/Packet.gd new file mode 100644 index 0000000..ca76b6e --- /dev/null +++ b/network/Packet.gd @@ -0,0 +1,7 @@ +class_name Packet +enum { + PING = 1, + LOGIN = 2, + LOGOUT = 3, + MOVE = 10, +} diff --git a/network/Ping.gd b/network/Ping.gd index e14ccca..86a705c 100644 --- a/network/Ping.gd +++ b/network/Ping.gd @@ -11,16 +11,11 @@ func _init(): history.resize(HISTORY_SIZE) func _ready(): - %Network.add_handler(%Network.Packet.PING, self) - var timer := Timer.new() - add_child(timer) - timer.wait_time = 1 - timer.timeout.connect(_ping) - timer.start() + %Network.add_handler(Packet.PING, self) -func _ping(): +func send_ping(): var buffer := StreamPeerBuffer.new() - buffer.put_8(%Network.Packet.PING) + buffer.put_8(Packet.PING) buffer.put_8(count) %Network.udp.put_packet(buffer.data_array) @@ -31,8 +26,7 @@ func _ping(): count = 0 func handle_packet(data: PackedByteArray): - print("Handled ping!", data) - var id := data.decode_u8(1) + var id := data.decode_u8(0) var ping := Time.get_unix_time_from_system() - history[id] changed.emit(ping) diff --git a/player/Player.gd b/player/Player.gd index dce3bc7..e552d20 100644 --- a/player/Player.gd +++ b/player/Player.gd @@ -1,7 +1,35 @@ -extends Node3D +class_name Player +extends CharacterBody3D -func _ready(): - pass +const MOVE_SPEED = 4.5 +const JUMP_VELOCITY = 4.5 +const DECELERATE = 0.75 -func _process(_delta): - pass +var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") +var move: Vector2 + +func _input(_event): + move = Input.get_vector("move_left", "move_right", "move_forward", "move_backward") + +func _physics_process(delta): + movement() + fall(delta) + move_and_slide() + +func movement(): + var direction = (Global.camera.transform.basis * Vector3(move.x, 0, move.y)).normalized() + # var direction = Vector3(move.x, 0, move.y).normalized() + + if direction: + velocity.x = direction.x * MOVE_SPEED + velocity.z = direction.z * MOVE_SPEED + else: + velocity.x *= DECELERATE + velocity.z *= DECELERATE + +func fall(delta): + if is_on_floor(): + if Input.is_action_just_pressed("jump"): + velocity.y = JUMP_VELOCITY + else: + velocity.y -= gravity * delta diff --git a/player/Player.tscn b/player/Player.tscn index 5ef2426..0498531 100644 --- a/player/Player.tscn +++ b/player/Player.tscn @@ -1,12 +1,23 @@ -[gd_scene load_steps=3 format=3 uid="uid://2lcnu3dy54lx"] +[gd_scene load_steps=5 format=3 uid="uid://2lcnu3dy54lx"] [ext_resource type="Script" path="res://player/Player.gd" id="1_8gebs"] -[sub_resource type="BoxMesh" id="BoxMesh_8125v"] +[sub_resource type="BoxShape3D" id="BoxShape3D_fx2di"] -[node name="Player" type="Node3D" groups=["Player"]] +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_5eict"] +diffuse_mode = 3 +albedo_color = Color(0.0588235, 0.0823529, 0.113725, 1) + +[sub_resource type="BoxMesh" id="BoxMesh_8125v"] +material = SubResource("StandardMaterial3D_5eict") + +[node name="Player" type="CharacterBody3D" groups=["Player"]] script = ExtResource("1_8gebs") +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) +shape = SubResource("BoxShape3D_fx2di") + [node name="MeshInstance3D" type="MeshInstance3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) mesh = SubResource("BoxMesh_8125v") diff --git a/project.godot b/project.godot index 343488a..f50ad50 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,10 @@ run/main_scene="res://world/Game.tscn" config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://ui/icon.svg" +[autoload] + +Global="*res://world/Global.gd" + [display] window/vsync/vsync_mode=0 diff --git a/server/login.go b/server/login.go index 247d551..0a2808c 100644 --- a/server/login.go +++ b/server/login.go @@ -9,13 +9,12 @@ import ( // login checks the account credentials and gives a network peer access to an account. func login(data []byte, client *core.Client) { - fmt.Println("2 - login!") - - if bytes.Equal(data, []byte("password")) { - fmt.Println("login success") - server.Send(packet.LOGIN, []byte{0}, client) - } else { + if !bytes.Equal(data, []byte("password")) { fmt.Println("login failure") server.Send(packet.LOGIN, []byte{1}, client) + return } + + fmt.Println("login success") + server.Send(packet.LOGIN, []byte{0}, client) } diff --git a/server/ping.go b/server/ping.go index 1f062e5..0363abc 100644 --- a/server/ping.go +++ b/server/ping.go @@ -1,13 +1,11 @@ package main import ( - "fmt" "server/core" "server/packet" ) // ping is used as a heartbeat and latency check. func ping(data []byte, client *core.Client) { - fmt.Println("1 - ping") server.Send(packet.PING, data, client) } diff --git a/ui/PositionLabel.gd b/ui/PositionLabel.gd new file mode 100644 index 0000000..f0d13c9 --- /dev/null +++ b/ui/PositionLabel.gd @@ -0,0 +1,7 @@ +extends Label + +func _process(_delta): + if Global.player == null: + return + + text = str(Global.player.global_position) diff --git a/world/Camera.gd b/world/Camera.gd new file mode 100644 index 0000000..ee92b0b --- /dev/null +++ b/world/Camera.gd @@ -0,0 +1,20 @@ +extends Camera3D + +@export var center: Node3D +@export var follow_speed: float +@export var shake_strength: float +var noise = FastNoiseLite.new() + +func _ready(): + Global.camera = self + +func _process(delta): + center.position = lerp(center.position, Global.player.position, follow_speed * delta) + +# func shake(time): +# var trauma_sq := 1.0 +# var h_offset := noise.get_noise_2d(time, 0) * trauma_sq * shake_strength +# var v_offset := noise.get_noise_2d(time, 1) * trauma_sq * shake_strength +# rotate_x(noise.get_noise_2d(time, 2) * trauma_sq * shake_strength) +# rotate_y(noise.get_noise_2d(time, 3) * trauma_sq * shake_strength) +# rotate_z(noise.get_noise_2d(time, 4) * trauma_sq * shake_strength) diff --git a/world/Game.tscn b/world/Game.tscn index f5d211c..348fe2c 100644 --- a/world/Game.tscn +++ b/world/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=20 format=3 uid="uid://b40y7iuskv1ar"] +[gd_scene load_steps=26 format=3 uid="uid://b40y7iuskv1ar"] [ext_resource type="Script" path="res://world/Game.gd" id="1_xmqq4"] [ext_resource type="Script" path="res://world/RotateY.gd" id="3_4gn6n"] @@ -7,33 +7,14 @@ [ext_resource type="Script" path="res://network/Network.gd" id="4_ao4cj"] [ext_resource type="Shader" path="res://world/shader/Outline.gdshader" id="4_gweie"] [ext_resource type="Script" path="res://network/Ping.gd" id="4_vx388"] +[ext_resource type="PackedScene" uid="uid://2lcnu3dy54lx" path="res://player/Player.tscn" id="5_6c2x8"] [ext_resource type="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="5_bll74"] [ext_resource type="Script" path="res://world/Sun.gd" id="5_pf5uw"] [ext_resource type="CameraAttributesPractical" uid="uid://b835orxyqq6w5" path="res://world/CameraAttributes.tres" id="6_8wfwf"] [ext_resource type="Script" path="res://network/Login.gd" id="6_augbg"] - -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_r8n03"] -diffuse_mode = 3 -albedo_color = Color(0.482353, 0.470588, 0.47451, 1) - -[sub_resource type="BoxMesh" id="BoxMesh_0bujj"] -material = SubResource("StandardMaterial3D_r8n03") -size = Vector3(20, 0.5, 20) - -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_4w7ln"] -diffuse_mode = 3 -albedo_color = Color(0.294118, 0.356863, 0.439216, 1) - -[sub_resource type="BoxMesh" id="BoxMesh_hf021"] -material = SubResource("StandardMaterial3D_4w7ln") - -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ahmjh"] -diffuse_mode = 3 -albedo_color = Color(0.14902, 0.517647, 1, 1) -roughness = 0.08 - -[sub_resource type="TorusMesh" id="TorusMesh_mu45b"] -material = SubResource("StandardMaterial3D_ahmjh") +[ext_resource type="Script" path="res://ui/PositionLabel.gd" id="8_jc5xy"] +[ext_resource type="Script" path="res://world/Camera.gd" id="9_qfhy4"] +[ext_resource type="PackedScene" uid="uid://hnn0n1xc2qt7" path="res://world/Tree.blend" id="15_csh38"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_qm56v"] render_priority = 0 @@ -50,26 +31,55 @@ material = SubResource("ShaderMaterial_qm56v") flip_faces = true size = Vector2(2, 2) +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_r8n03"] +diffuse_mode = 3 +albedo_color = Color(0.482353, 0.470588, 0.47451, 1) + +[sub_resource type="BoxMesh" id="BoxMesh_0bujj"] +material = SubResource("StandardMaterial3D_r8n03") +size = Vector3(20, 0.5, 20) + +[sub_resource type="BoxShape3D" id="BoxShape3D_58ws3"] +size = Vector3(20, 0.5, 20) + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_4w7ln"] +diffuse_mode = 3 +albedo_color = Color(0.294118, 0.356863, 0.439216, 1) + +[sub_resource type="BoxMesh" id="BoxMesh_hf021"] +material = SubResource("StandardMaterial3D_4w7ln") + +[sub_resource type="BoxShape3D" id="BoxShape3D_3lxo8"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ahmjh"] +diffuse_mode = 3 +albedo_color = Color(0.14902, 0.517647, 1, 1) +roughness = 0.08 + +[sub_resource type="TorusMesh" id="TorusMesh_mu45b"] +material = SubResource("StandardMaterial3D_ahmjh") + [node name="Game" type="Node"] script = ExtResource("1_xmqq4") -[node name="World" type="Node3D" parent="."] +[node name="Network" type="Node" parent="."] +unique_name_in_owner = true +script = ExtResource("4_ao4cj") -[node name="Ground" type="MeshInstance3D" parent="World"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.25, 0) -mesh = SubResource("BoxMesh_0bujj") -skeleton = NodePath("../..") +[node name="Ping" type="Node" parent="Network"] +unique_name_in_owner = true +script = ExtResource("4_vx388") -[node name="Box" type="MeshInstance3D" parent="World"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, -4) -mesh = SubResource("BoxMesh_hf021") -skeleton = NodePath("../../SubViewportContainer/SubViewport") +[node name="Timer" type="Timer" parent="Network/Ping"] +autostart = true -[node name="Torus" type="MeshInstance3D" parent="World"] -transform = Transform3D(0.7, 0, 0, 0, -3.0598e-08, -0.7, 0, 0.7, -3.0598e-08, 0, 1.79933, -4) -mesh = SubResource("TorusMesh_mu45b") -skeleton = NodePath("../../SubViewportContainer/SubViewport") -script = ExtResource("3_4gn6n") +[node name="Login" type="Node" parent="Network"] +script = ExtResource("6_augbg") +playerScene = ExtResource("5_6c2x8") + +[node name="Timer" type="Timer" parent="Network/Login"] +wait_time = 10.0 +autostart = true [node name="UI" type="Control" parent="."] layout_mode = 3 @@ -108,16 +118,17 @@ layout_mode = 2 text = "0" script = ExtResource("4_1a3hc") -[node name="Network" type="Node" parent="."] -unique_name_in_owner = true -script = ExtResource("4_ao4cj") +[node name="HBoxContainer3" type="HBoxContainer" parent="UI/CanvasLayer/MarginContainer/VBoxContainer"] +layout_mode = 2 -[node name="Ping" type="Node" parent="Network"] -unique_name_in_owner = true -script = ExtResource("4_vx388") +[node name="PositionLabel" type="Label" parent="UI/CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer3"] +layout_mode = 2 +text = "Position:" -[node name="Login" type="Node" parent="Network"] -script = ExtResource("6_augbg") +[node name="Position" type="Label" parent="UI/CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer3"] +layout_mode = 2 +text = "0" +script = ExtResource("8_jc5xy") [node name="SubViewportContainer" type="SubViewportContainer" parent="."] texture_filter = 1 @@ -134,15 +145,20 @@ handle_input_locally = false size = Vector2i(384, 216) render_target_update_mode = 4 -[node name="Camera" type="Camera3D" parent="SubViewportContainer/SubViewport"] -transform = Transform3D(0.707107, 0.331966, -0.624338, 0, 0.882947, 0.469472, 0.707107, -0.331966, 0.624338, -5, 5, 5) +[node name="CameraCenter" type="Node3D" parent="SubViewportContainer/SubViewport"] + +[node name="Camera" type="Camera3D" parent="SubViewportContainer/SubViewport/CameraCenter" node_paths=PackedStringArray("center")] +transform = Transform3D(0.707107, 0.408607, -0.577096, 0, 0.816138, 0.577858, 0.707107, -0.408607, 0.577096, -10, 10, 10) projection = 1 current = true fov = 90.0 -size = 9.6 -near = 0.001 +size = 10.0 +far = 100.0 +script = ExtResource("9_qfhy4") +center = NodePath("..") +follow_speed = 5.0 -[node name="PostProcessing" type="MeshInstance3D" parent="SubViewportContainer/SubViewport/Camera"] +[node name="PostProcessing" type="MeshInstance3D" parent="SubViewportContainer/SubViewport/CameraCenter/Camera"] unique_name_in_owner = true extra_cull_margin = 16384.0 mesh = SubResource("QuadMesh_7yiqd") @@ -156,3 +172,40 @@ script = ExtResource("5_pf5uw") [node name="Environment" type="WorldEnvironment" parent="SubViewportContainer/SubViewport"] environment = ExtResource("5_bll74") camera_attributes = ExtResource("6_8wfwf") + +[node name="World" type="Node3D" parent="SubViewportContainer/SubViewport"] + +[node name="Ground" type="MeshInstance3D" parent="SubViewportContainer/SubViewport/World"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.25, 0) +mesh = SubResource("BoxMesh_0bujj") +skeleton = NodePath("../../../..") + +[node name="StaticBody3D" type="StaticBody3D" parent="SubViewportContainer/SubViewport/World/Ground"] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="SubViewportContainer/SubViewport/World/Ground/StaticBody3D"] +shape = SubResource("BoxShape3D_58ws3") + +[node name="Box" type="MeshInstance3D" parent="SubViewportContainer/SubViewport/World"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, -4) +mesh = SubResource("BoxMesh_hf021") +skeleton = NodePath("../..") + +[node name="RigidBody3D" type="RigidBody3D" parent="SubViewportContainer/SubViewport/World/Box"] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="SubViewportContainer/SubViewport/World/Box/RigidBody3D"] +shape = SubResource("BoxShape3D_3lxo8") + +[node name="Torus" type="MeshInstance3D" parent="SubViewportContainer/SubViewport/World"] +transform = Transform3D(0.7, 0, 0, 0, -3.0598e-08, -0.7, 0, 0.7, -3.0598e-08, 0, 1.79933, -4) +mesh = SubResource("TorusMesh_mu45b") +skeleton = NodePath("../..") +script = ExtResource("3_4gn6n") + +[node name="Tree" parent="SubViewportContainer/SubViewport/World" instance=ExtResource("15_csh38")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5.11323, 0, -4.64839) + +[node name="Players" type="Node3D" parent="SubViewportContainer/SubViewport"] +unique_name_in_owner = true + +[connection signal="timeout" from="Network/Ping/Timer" to="Network/Ping" method="send_ping"] +[connection signal="timeout" from="Network/Login/Timer" to="Network/Login" method="send_login"] diff --git a/world/Global.gd b/world/Global.gd new file mode 100644 index 0000000..11e57bc --- /dev/null +++ b/world/Global.gd @@ -0,0 +1,4 @@ +extends Node + +var camera: Camera3D +var player: Player diff --git a/world/Tree.blend.import b/world/Tree.blend.import new file mode 100644 index 0000000..f773bdd --- /dev/null +++ b/world/Tree.blend.import @@ -0,0 +1,50 @@ +[remap] + +importer="scene" +importer_version=1 +type="PackedScene" +uid="uid://hnn0n1xc2qt7" +path="res://.godot/imported/Tree.blend-8edaf7c4bb18f0f1f1c8fa5b8380d9ea.scn" + +[deps] + +source_file="res://world/Tree.blend" +dest_files=["res://.godot/imported/Tree.blend-8edaf7c4bb18f0f1f1c8fa5b8380d9ea.scn"] + +[params] + +nodes/root_type="" +nodes/root_name="" +nodes/apply_root_scale=true +nodes/root_scale=1.0 +meshes/ensure_tangents=true +meshes/generate_lods=true +meshes/create_shadow_meshes=true +meshes/light_baking=1 +meshes/lightmap_texel_size=0.2 +meshes/force_disable_compression=false +skins/use_named_skins=true +animation/import=true +animation/fps=30 +animation/trimming=false +animation/remove_immutable_tracks=true +import_script/path="" +_subresources={} +gltf/naming_version=1 +gltf/embedded_image_handling=1 +blender/nodes/visible=0 +blender/nodes/punctual_lights=true +blender/nodes/cameras=true +blender/nodes/custom_properties=true +blender/nodes/modifiers=1 +blender/meshes/colors=false +blender/meshes/uvs=true +blender/meshes/normals=true +blender/meshes/tangents=true +blender/meshes/skins=2 +blender/meshes/export_bones_deforming_mesh_only=false +blender/materials/unpack_enabled=true +blender/materials/export_materials=1 +blender/animation/limit_playback=true +blender/animation/always_sample=true +blender/animation/group_tracks=true