From a13eb426ef2039a9f3e78e8d4e74c14eb67a03fc Mon Sep 17 00:00:00 2001 From: Hamcha Date: Mon, 13 Jul 2020 11:20:51 +0200 Subject: [PATCH] Add async loading and start work on joining servers --- Scenes/Global/Multiplayer.gd | 18 ++++- Scenes/Global/SceneManager.gd | 29 +++++++ Scenes/Loader.gd | 7 ++ Scenes/Loader.tscn | 142 ++++++++++++++++++++++++++++++++++ Scenes/Menu.gd | 19 +++-- project.godot | 5 +- 6 files changed, 209 insertions(+), 11 deletions(-) create mode 100644 Scenes/Global/SceneManager.gd create mode 100644 Scenes/Loader.gd create mode 100644 Scenes/Loader.tscn diff --git a/Scenes/Global/Multiplayer.gd b/Scenes/Global/Multiplayer.gd index 0ef6f58..888aba5 100644 --- a/Scenes/Global/Multiplayer.gd +++ b/Scenes/Global/Multiplayer.gd @@ -22,6 +22,8 @@ export var player_name = "" var player_info = {} +onready var scene_manager = $"/root/SceneManager" + func _ready(): player_name = "tider-" + str(randi() % 1000) @@ -57,6 +59,12 @@ func discover_upnp(): return upnp.add_port_mapping(SERVER_PORT) func host(): + scene_manager.enter_loader() + scene_manager.loading_text = "Starting server" + + # Wait just a sec to draw + yield(get_tree().create_timer(0.3), "timeout") + print("Running UPNP magicks") if discover_upnp() == UPNP.UPNP_RESULT_SUCCESS: print("UPNP mapping added") @@ -79,12 +87,18 @@ func host(): server_name = player_name + "'s server" player_info[1] = { name=player_name } - get_tree().change_scene("res://Scenes/Game.tscn") + scene_manager.load_scene_with_args("res://Scenes/Game.tscn", []) # Add to master server create_ms_entry() func join(addr: String): + scene_manager.enter_loader() + scene_manager.loading_text = "Joining server " + str(addr) + + # Wait just a sec to draw + yield(get_tree().create_timer(0.3), "timeout") + bind_events() var peer = NetworkedMultiplayerENet.new() peer.create_client(addr, SERVER_PORT) @@ -103,6 +117,7 @@ func leave(): pass get_tree().network_peer = null emit_signal("left") + func _player_connected(id): rpc_id(id, "register_player", player_name) if get_tree().is_network_server(): @@ -115,6 +130,7 @@ func _player_disconnected(id): func _connected_ok(): print("Connected to server") + scene_manager.loading_text = "Connected to server" func _server_disconnected(): print("Disconnected from server") diff --git a/Scenes/Global/SceneManager.gd b/Scenes/Global/SceneManager.gd new file mode 100644 index 0000000..ea85dda --- /dev/null +++ b/Scenes/Global/SceneManager.gd @@ -0,0 +1,29 @@ +extends Node + +var queue = null +var target_scene = null +var loading_text = null + +var loader = preload("res://Scenes/Loader.tscn") + +func enter_loader(): + get_tree().change_scene_to(loader) + +func load_scene_with_args(scene_path: String, _args: Array): + queue = ResourceQueue.new() + queue.start() + target_scene = scene_path + queue.queue_resource(scene_path) + +func _physics_process(delta): + if queue != null and target_scene != null: + if queue.is_ready(target_scene): + get_tree().change_scene_to(queue.get_resource(target_scene)) + queue = null + target_scene = null + +func get_progress(): + if loading_text != null: + return loading_text + if queue != null and target_scene != null: + return "Loading scene (" + str(queue.get_progress(target_scene)) + ")" diff --git a/Scenes/Loader.gd b/Scenes/Loader.gd new file mode 100644 index 0000000..e330cda --- /dev/null +++ b/Scenes/Loader.gd @@ -0,0 +1,7 @@ +extends Control + +onready var text = $BottomRight/Label +onready var scene_manager = $"/root/SceneManager" + +func _physics_process(_delta): + text.text = str(scene_manager.get_progress()) diff --git a/Scenes/Loader.tscn b/Scenes/Loader.tscn new file mode 100644 index 0000000..308fa1c --- /dev/null +++ b/Scenes/Loader.tscn @@ -0,0 +1,142 @@ +[gd_scene load_steps=12 format=2] + +[ext_resource path="res://Graphics/UI/logo-temp-pixel.png" type="Texture" id=1] +[ext_resource path="res://Graphics/UI/iosevka-aile-regular.ttf" type="DynamicFontData" id=2] +[ext_resource path="res://Scenes/Loader.gd" type="Script" id=3] + +[sub_resource type="Shader" id=2] +code = "shader_type canvas_item; + +uniform vec2 tex_size; +uniform vec4 uv_rect; + +void fragment() { + vec2 uv_adjusted = (UV - (uv_rect.xy / tex_size)) * (tex_size / uv_rect.zw); + float dist = distance(uv_adjusted, vec2(0.5)); + if (dist < 0.26) { + COLOR = vec4(1); + } else { + COLOR = texture(TEXTURE, UV); + } +}" +custom_defines = "" + +[sub_resource type="ShaderMaterial" id=3] +shader = SubResource( 2 ) +shader_param/tex_size = Vector2( 240, 180 ) +shader_param/uv_rect = Plane( 126, 16, 82, 84 ) + +[sub_resource type="AtlasTexture" id=1] +atlas = ExtResource( 1 ) +region = Rect2( 126, 16, 82, 84 ) + +[sub_resource type="Shader" id=5] +code = "shader_type canvas_item; + +uniform vec2 tex_size; +uniform vec4 uv_rect; + +void fragment() { + vec4 tex = texture(TEXTURE, UV); + + vec2 uv_adjusted = (UV - (uv_rect.xy / tex_size)) * (tex_size / uv_rect.zw); + float dist = distance(uv_adjusted, vec2(0.5)); + if (dist > 0.56) { + discard; + } else { + COLOR = vec4(tex.aaa, 1.-tex.a); + } +}" +custom_defines = "" + +[sub_resource type="ShaderMaterial" id=6] +shader = SubResource( 5 ) +shader_param/tex_size = Vector2( 240, 180 ) +shader_param/uv_rect = Plane( 146, 39, 39, 38 ) + +[sub_resource type="AtlasTexture" id=4] +atlas = ExtResource( 1 ) +region = Rect2( 146, 39, 39, 38 ) + +[sub_resource type="DynamicFont" id=8] +size = 32 +font_data = ExtResource( 2 ) + +[sub_resource type="Animation" id=7] +resource_name = "spinner" +length = 7.0 +loop = true +tracks/0/type = "value" +tracks/0/path = NodePath("BottomRight/logo-temp-pixel2:rect_rotation") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0, 7 ), +"transitions": PoolRealArray( 1, 1 ), +"update": 0, +"values": [ 0.0, 360.0 ] +} + +[node name="Loading" type="Control"] +anchor_right = 0.5 +anchor_bottom = 0.5 +rect_scale = Vector2( 2, 2 ) +script = ExtResource( 3 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="BottomRight" type="Control" parent="."] +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = -114.5 +margin_top = -109.5 +margin_right = -2.5 +margin_bottom = -1.5 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="logo-temp-pixel2" type="TextureRect" parent="BottomRight"] +material = SubResource( 3 ) +margin_left = -7.11853 +margin_top = -6.38116 +margin_right = 92.8815 +margin_bottom = 93.6188 +rect_pivot_offset = Vector2( 50, 50 ) +texture = SubResource( 1 ) +stretch_mode = 4 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="logo-temp-pixel" type="TextureRect" parent="BottomRight"] +material = SubResource( 6 ) +margin_left = -1.5 +margin_top = -1.0 +margin_right = 88.5 +margin_bottom = 89.0 +rect_pivot_offset = Vector2( 79, 59 ) +texture = SubResource( 4 ) +stretch_mode = 4 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Label" type="Label" parent="BottomRight"] +margin_left = -220.0 +margin_top = 63.0 +margin_right = 174.0 +margin_bottom = 104.0 +rect_scale = Vector2( 0.5, 0.5 ) +custom_fonts/font = SubResource( 8 ) +custom_colors/font_color = Color( 1, 1, 1, 1 ) +align = 2 + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +autoplay = "spinner" +anims/spinner = SubResource( 7 ) diff --git a/Scenes/Menu.gd b/Scenes/Menu.gd index 80aad20..5fac0c6 100644 --- a/Scenes/Menu.gd +++ b/Scenes/Menu.gd @@ -1,11 +1,14 @@ extends Control export var scale = 4 setget set_scale -export var upThreshold = 1.0/Engine.iterations_per_second +export var upThreshold = 1.0/59.0 export var downThreshold = 1.0/30.0 export var delay = 1.0 +onready var viewport = $Viewport +onready var background = $Background + func _ready(): set_scale(scale) $"/root/Music/BGM".play() @@ -24,19 +27,23 @@ func _process(delta): func set_scale(val): scale = val - $Viewport.size = rect_size / scale - $Background.update() + if viewport: + viewport.size = rect_size / scale + background.update() func _on_resized(): - $Viewport.size = rect_size / scale - $Background.update() + if viewport: + viewport.size = rect_size / scale + background.update() func _host_pressed(): $"/root/Music/BGM".stop() $"/root/Multiplayer".host() func _join_pressed(): - pass + $"/root/Music/BGM".stop() + var addr = $CenterContainer/PanelContainer/HBoxContainer/ServerAddr + $"/root/Multiplayer".join(addr.text) func _server_addr_changed(new_text: String): $CenterContainer/PanelContainer/HBoxContainer/JoinButton.disabled = new_text.length() < 1 diff --git a/project.godot b/project.godot index 59bab19..13da8e6 100644 --- a/project.godot +++ b/project.godot @@ -144,10 +144,7 @@ config/icon="res://icon.png" Music="*res://Scenes/Global/Music.tscn" Multiplayer="*res://Scenes/Global/Multiplayer.gd" - -[debug] - -settings/stdout/print_fps=true +SceneManager="*res://Scenes/Global/SceneManager.gd" [display]