Add async loading and start work on joining servers

This commit is contained in:
Hamcha 2020-07-13 11:20:51 +02:00
parent 1c328e3e2e
commit a13eb426ef
Signed by: hamcha
GPG key ID: 41467804B19A3315
6 changed files with 209 additions and 11 deletions

View file

@ -22,6 +22,8 @@ export var player_name = ""
var player_info = {} var player_info = {}
onready var scene_manager = $"/root/SceneManager"
func _ready(): func _ready():
player_name = "tider-" + str(randi() % 1000) player_name = "tider-" + str(randi() % 1000)
@ -57,6 +59,12 @@ func discover_upnp():
return upnp.add_port_mapping(SERVER_PORT) return upnp.add_port_mapping(SERVER_PORT)
func host(): 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") print("Running UPNP magicks")
if discover_upnp() == UPNP.UPNP_RESULT_SUCCESS: if discover_upnp() == UPNP.UPNP_RESULT_SUCCESS:
print("UPNP mapping added") print("UPNP mapping added")
@ -79,12 +87,18 @@ func host():
server_name = player_name + "'s server" server_name = player_name + "'s server"
player_info[1] = { name=player_name } 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 # Add to master server
create_ms_entry() create_ms_entry()
func join(addr: String): 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() bind_events()
var peer = NetworkedMultiplayerENet.new() var peer = NetworkedMultiplayerENet.new()
peer.create_client(addr, SERVER_PORT) peer.create_client(addr, SERVER_PORT)
@ -103,6 +117,7 @@ func leave():
pass pass
get_tree().network_peer = null get_tree().network_peer = null
emit_signal("left") emit_signal("left")
func _player_connected(id): func _player_connected(id):
rpc_id(id, "register_player", player_name) rpc_id(id, "register_player", player_name)
if get_tree().is_network_server(): if get_tree().is_network_server():
@ -115,6 +130,7 @@ func _player_disconnected(id):
func _connected_ok(): func _connected_ok():
print("Connected to server") print("Connected to server")
scene_manager.loading_text = "Connected to server"
func _server_disconnected(): func _server_disconnected():
print("Disconnected from server") print("Disconnected from server")

View file

@ -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)) + ")"

7
Scenes/Loader.gd Normal file
View file

@ -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())

142
Scenes/Loader.tscn Normal file
View file

@ -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 )

View file

@ -1,11 +1,14 @@
extends Control extends Control
export var scale = 4 setget set_scale 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 downThreshold = 1.0/30.0
export var delay = 1.0 export var delay = 1.0
onready var viewport = $Viewport
onready var background = $Background
func _ready(): func _ready():
set_scale(scale) set_scale(scale)
$"/root/Music/BGM".play() $"/root/Music/BGM".play()
@ -24,19 +27,23 @@ func _process(delta):
func set_scale(val): func set_scale(val):
scale = val scale = val
$Viewport.size = rect_size / scale if viewport:
$Background.update() viewport.size = rect_size / scale
background.update()
func _on_resized(): func _on_resized():
$Viewport.size = rect_size / scale if viewport:
$Background.update() viewport.size = rect_size / scale
background.update()
func _host_pressed(): func _host_pressed():
$"/root/Music/BGM".stop() $"/root/Music/BGM".stop()
$"/root/Multiplayer".host() $"/root/Multiplayer".host()
func _join_pressed(): 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): func _server_addr_changed(new_text: String):
$CenterContainer/PanelContainer/HBoxContainer/JoinButton.disabled = new_text.length() < 1 $CenterContainer/PanelContainer/HBoxContainer/JoinButton.disabled = new_text.length() < 1

View file

@ -144,10 +144,7 @@ config/icon="res://icon.png"
Music="*res://Scenes/Global/Music.tscn" Music="*res://Scenes/Global/Music.tscn"
Multiplayer="*res://Scenes/Global/Multiplayer.gd" Multiplayer="*res://Scenes/Global/Multiplayer.gd"
SceneManager="*res://Scenes/Global/SceneManager.gd"
[debug]
settings/stdout/print_fps=true
[display] [display]