From 10a1f377941d2a3eddd6a087127f48e94bc04116 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Sat, 1 Jun 2019 23:52:16 +0200 Subject: [PATCH] Make loading work --- Scenes/Components/Card.gd | 2 ++ Scenes/Global/Loader.gd | 50 ++++++++++++++++++++++++++++++ Scenes/Global/Loader.tscn | 10 ++++++ Scenes/Loading.tscn | 54 ++++++++++++++++++++++++++++++++- Scenes/Loading/LoadingScreen.gd | 21 ++++++++----- Scenes/Loading/ScatterSource.gd | 6 +++- Scenes/Scripts/Board.gd | 10 ++++++ Scenes/SplashScreen.gd | 5 +++ Scenes/SplashScreen.tscn | 8 +++++ project.godot | 3 +- 10 files changed, 158 insertions(+), 11 deletions(-) create mode 100644 Scenes/Global/Loader.gd create mode 100644 Scenes/Global/Loader.tscn create mode 100644 Scenes/SplashScreen.gd create mode 100644 Scenes/SplashScreen.tscn diff --git a/Scenes/Components/Card.gd b/Scenes/Components/Card.gd index d9653e0..757656b 100644 --- a/Scenes/Components/Card.gd +++ b/Scenes/Components/Card.gd @@ -10,6 +10,7 @@ signal card_dropped_anim() signal card_menu() signal card_moved(origin, direction, t) signal card_clicked() +signal loaded() var clicked := false var exhausted := false @@ -99,6 +100,7 @@ func _card_fetched(result: int, response_code: int, headers: PoolStringArray, bo texture.create_from_image(img) mat.albedo_texture = texture cardImage.set_surface_material(0, mat) + emit_signal("loaded") func tween_move_to(targetPos: Vector3, duration: float = 0.1): $Tween.interpolate_property(self, "translation", diff --git a/Scenes/Global/Loader.gd b/Scenes/Global/Loader.gd new file mode 100644 index 0000000..d93c8a5 --- /dev/null +++ b/Scenes/Global/Loader.gd @@ -0,0 +1,50 @@ +extends Node + +var loadingScreen = preload("res://Scenes/Loading.tscn") + +var loader: ResourceInteractiveLoader = null +var current_scene = null +var load_next: String = "" + +onready var root = get_tree().get_root() +onready var loading = $LoadingScreen + +func _ready(): + set_process(false) + +func load_scene(path: String): + current_scene = root.get_child(root.get_child_count() -1) + loading.fade_in() + load_next = path + +var delay := 0.1 +var cur_delay := 0.0 + +func set_new_scene(scene): + current_scene.queue_free() + var instance = scene.instance() + instance.connect("loaded", self, "_fade_out") + root.add_child(instance) + root.move_child(instance, 0) + +func _process(delta): + if loader == null: + # no need to process anymore + set_process(false) + return + + cur_delay += delta + if cur_delay > delay: + cur_delay = 0 + var err = loader.poll() + if err == ERR_FILE_EOF: # Finished loading. + var resource = loader.get_resource() + loader = null + set_new_scene(resource) + +func _fade_in(): + loader = ResourceLoader.load_interactive(load_next) + set_process(true) + +func _fade_out(): + loading.fade_out() \ No newline at end of file diff --git a/Scenes/Global/Loader.tscn b/Scenes/Global/Loader.tscn new file mode 100644 index 0000000..9720e84 --- /dev/null +++ b/Scenes/Global/Loader.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Scenes/Global/Loader.gd" type="Script" id=1] +[ext_resource path="res://Scenes/Loading.tscn" type="PackedScene" id=2] + +[node name="Node" type="Node"] +script = ExtResource( 1 ) + +[node name="LoadingScreen" parent="." instance=ExtResource( 2 )] +[connection signal="faded_in" from="LoadingScreen" to="." method="_fade_in"] diff --git a/Scenes/Loading.tscn b/Scenes/Loading.tscn index 6878066..268585e 100644 --- a/Scenes/Loading.tscn +++ b/Scenes/Loading.tscn @@ -33,6 +33,30 @@ tracks/1/keys = { "method": "scatter_in" } ] } +tracks/2/type = "value" +tracks/2/path = NodePath("TextureRect:visible") +tracks/2/interp = 1 +tracks/2/loop_wrap = true +tracks/2/imported = false +tracks/2/enabled = true +tracks/2/keys = { +"times": PoolRealArray( 0, 0.5 ), +"transitions": PoolRealArray( 1, 1 ), +"update": 1, +"values": [ false, true ] +} +tracks/3/type = "value" +tracks/3/path = NodePath(".:mouse_filter") +tracks/3/interp = 1 +tracks/3/loop_wrap = true +tracks/3/imported = false +tracks/3/enabled = true +tracks/3/keys = { +"times": PoolRealArray( 0, 0.2 ), +"transitions": PoolRealArray( 1, 1 ), +"update": 1, +"values": [ 2, 0 ] +} [sub_resource type="Animation" id=2] resource_name = "fade_out" @@ -63,10 +87,35 @@ tracks/1/keys = { "method": "scatter_out" } ] } +tracks/2/type = "value" +tracks/2/path = NodePath("TextureRect:visible") +tracks/2/interp = 1 +tracks/2/loop_wrap = true +tracks/2/imported = false +tracks/2/enabled = true +tracks/2/keys = { +"times": PoolRealArray( 0, 0.8 ), +"transitions": PoolRealArray( 1, 1 ), +"update": 1, +"values": [ true, false ] +} +tracks/3/type = "value" +tracks/3/path = NodePath(".:mouse_filter") +tracks/3/interp = 1 +tracks/3/loop_wrap = true +tracks/3/imported = false +tracks/3/enabled = true +tracks/3/keys = { +"times": PoolRealArray( 0, 0.8 ), +"transitions": PoolRealArray( 1, 1 ), +"update": 1, +"values": [ 0, 2 ] +} [node name="LoadingScreen" type="Control"] anchor_right = 1.0 anchor_bottom = 1.0 +mouse_filter = 2 script = ExtResource( 1 ) [node name="Cards" type="Node2D" parent="."] @@ -75,12 +124,15 @@ scale = Vector2( 0.5, 0.5 ) script = ExtResource( 2 ) [node name="TextureRect" type="TextureRect" parent="."] -modulate = Color( 1, 1, 1, 0.784314 ) +visible = false +modulate = Color( 1, 1, 1, 0 ) anchor_right = 1.0 anchor_bottom = 1.0 +mouse_filter = 2 texture = ExtResource( 3 ) expand = true [node name="AnimationPlayer" type="AnimationPlayer" parent="."] anims/fade_in = SubResource( 1 ) anims/fade_out = SubResource( 2 ) +[connection signal="animation_finished" from="AnimationPlayer" to="." method="_on_animation_finished"] diff --git a/Scenes/Loading/LoadingScreen.gd b/Scenes/Loading/LoadingScreen.gd index 7b72668..4c1701c 100644 --- a/Scenes/Loading/LoadingScreen.gd +++ b/Scenes/Loading/LoadingScreen.gd @@ -1,13 +1,18 @@ extends Control -var passed := 0.0 -var once := true +signal faded_in() +signal faded_out() -func _ready(): +func fade_in(): $AnimationPlayer.play("fade_in") -func _process(delta): - passed += delta - if passed > 5.0 and once: - $AnimationPlayer.play("fade_out") - once = false \ No newline at end of file +func fade_out(): + $AnimationPlayer.play("fade_out") + +func _on_animation_finished(anim_name): + match anim_name: + "fade_in": + emit_signal("faded_in") + "fade_out": + $Cards.cleanup() + emit_signal("faded_out") diff --git a/Scenes/Loading/ScatterSource.gd b/Scenes/Loading/ScatterSource.gd index 9b18ccf..2920dd5 100644 --- a/Scenes/Loading/ScatterSource.gd +++ b/Scenes/Loading/ScatterSource.gd @@ -77,4 +77,8 @@ func scatter_in(): func scatter_out(): for card in get_children(): - card.tweenTo(Vector2((card.position.x / 2)-3000, (card.position.y)-1800), PI) \ No newline at end of file + card.tweenTo(Vector2((card.position.x / 2)-3000, (card.position.y)-1800), PI) + +func cleanup(): + for card in get_children(): + card.queue_free() \ No newline at end of file diff --git a/Scenes/Scripts/Board.gd b/Scenes/Scripts/Board.gd index 3f87212..5fb5522 100644 --- a/Scenes/Scripts/Board.gd +++ b/Scenes/Scripts/Board.gd @@ -2,6 +2,8 @@ extends Spatial class_name Board +signal loaded() + onready var CardTemplate := preload("res://Scenes/Components/Card.tscn") onready var camera := $Camera @@ -18,6 +20,7 @@ var currentZone: Stack = null var mouseOrigin: Vector2 var lastCameraTransform: Transform +var cardsToLoad: int = 0 onready var zones: Dictionary = { "player1.deck": $Cards/P1Deck, @@ -39,6 +42,7 @@ func _ready(): "pr10","pr11","pr12","pr13","pr14","pr15","pr16","pr12","pr13","pr14", "pr10","pr11","pr12","pr13","pr14","pr15","pr16","pr12","pr13","pr14" ] + cardsToLoad = len(deck) # Fill deck with cards for card in deck: add_card(card, 0, "player1.deck") @@ -191,6 +195,11 @@ func _card_menu_action(id: int, card: Card): card.animation.queue("flip") card.flipped = true +func _card_loaded(): + cardsToLoad -= 1 + if cardsToLoad <= 0: + emit_signal("loaded") + func reparent(object: Node, from: Node, to: Node): from.remove_child(object) to.add_child(object) @@ -215,6 +224,7 @@ func add_card(cardID: String, playerID: int, zone: String): card.connect("card_moved", self, "_card_moved", [card]) card.connect("card_menu", ui, "show_card_menu", [card, self, "_card_menu_action"]) card.connect("card_clicked", self, "_card_clicked", [card]) + card.connect("loaded", self, "_card_loaded") if card.inHand: # TODO support for >2 players if playerID == 0: diff --git a/Scenes/SplashScreen.gd b/Scenes/SplashScreen.gd new file mode 100644 index 0000000..08fa934 --- /dev/null +++ b/Scenes/SplashScreen.gd @@ -0,0 +1,5 @@ +extends Control + +func _ready(): + #TODO Splash screen + Loader.load_scene("res://Scenes/Board.tscn") \ No newline at end of file diff --git a/Scenes/SplashScreen.tscn b/Scenes/SplashScreen.tscn new file mode 100644 index 0000000..6170625 --- /dev/null +++ b/Scenes/SplashScreen.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://Scenes/SplashScreen.gd" type="Script" id=1] + +[node name="Control" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) diff --git a/project.godot b/project.godot index a8985c7..a529ad7 100644 --- a/project.godot +++ b/project.godot @@ -45,13 +45,14 @@ _global_script_class_icons={ [application] config/name="MLP CARD GAME" -run/main_scene="res://Scenes/Board.tscn" +run/main_scene="res://Scenes/SplashScreen.tscn" config/icon="res://icon.png" [autoload] Remote="*res://Scenes/Global/Remote.tscn" Mods="*res://Scenes/Global/Mods.tscn" +Loader="*res://Scenes/Global/Loader.tscn" [display]