From 6220fbdbf981664255928df225757c331ea9d2d1 Mon Sep 17 00:00:00 2001 From: Waldemar Tomme Date: Sat, 27 Jan 2024 13:35:14 +0100 Subject: [PATCH] Set sprites based on jokes --- godot/scenes/Joke Button.gd | 20 ++++++++++++++++---- godot/scenes/joke_button.tscn | 14 ++++++++------ godot/scenes/stage.tscn | 9 +++++++-- godot/types/joke.gd | 2 +- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/godot/scenes/Joke Button.gd b/godot/scenes/Joke Button.gd index 51d8837..9beb8cd 100644 --- a/godot/scenes/Joke Button.gd +++ b/godot/scenes/Joke Button.gd @@ -3,17 +3,29 @@ extends Node2D signal button_pressed(joke) @export_enum("joke_button_1", "joke_button_2", "joke_button_3") var action: String +@export var sprite: Sprite2D + +@export var type_sprites: Array[Texture2D] var current_joke: Joke -func _init(): - current_joke = Joke.new(Joke.JokeType.Joke1) +func _map_action_to_joke(): + match action: + "joke_button_1": + return Joke.new(Joke.JokeType.Joke1) + "joke_button_2": + return Joke.new(Joke.JokeType.Joke2) + "joke_button_3": + return Joke.new(Joke.JokeType.Joke3) -# Called when the node enters the scene tree for the first time. func _ready(): - pass # Replace with function body. + set_current_joke(_map_action_to_joke()) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): if Input.is_action_just_pressed(action): button_pressed.emit(current_joke) + +func set_current_joke(joke: Joke): + current_joke = joke + sprite.texture = type_sprites[joke.type] diff --git a/godot/scenes/joke_button.tscn b/godot/scenes/joke_button.tscn index 6741408..e361e95 100644 --- a/godot/scenes/joke_button.tscn +++ b/godot/scenes/joke_button.tscn @@ -1,13 +1,15 @@ -[gd_scene load_steps=3 format=3 uid="uid://r1i7ln2hpwq5"] +[gd_scene load_steps=5 format=3 uid="uid://r1i7ln2hpwq5"] [ext_resource type="Script" path="res://scenes/Joke Button.gd" id="1_lofpb"] +[ext_resource type="Texture2D" uid="uid://br2j6yu0f7x37" path="res://sprites/room/bubble_blue.svg" id="2_ivsb5"] +[ext_resource type="Texture2D" uid="uid://b2vtyqcfr5ukt" path="res://sprites/room/bubble_green.svg" id="3_26ki8"] +[ext_resource type="Texture2D" uid="uid://03ch1nqrnm6c" path="res://sprites/room/bubble_red.svg" id="4_o1r21"] -[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_k2q2d"] -size = Vector2(150, 50) - -[node name="Joke Button 1" type="Node2D"] +[node name="Joke Button" type="Node2D" node_paths=PackedStringArray("sprite")] script = ExtResource("1_lofpb") action = "joke_button_1" +sprite = NodePath("Sprite2D") +type_sprites = Array[Texture2D]([ExtResource("2_ivsb5"), ExtResource("3_26ki8"), ExtResource("4_o1r21")]) [node name="Sprite2D" type="Sprite2D" parent="."] -texture = SubResource("PlaceholderTexture2D_k2q2d") +texture = ExtResource("2_ivsb5") diff --git a/godot/scenes/stage.tscn b/godot/scenes/stage.tscn index 790994f..9912fbc 100644 --- a/godot/scenes/stage.tscn +++ b/godot/scenes/stage.tscn @@ -34,13 +34,14 @@ offset = Vector2(0, 50) position = Vector2(0, -25) [node name="Joke Button 1" parent="Tim/Joke Buttons" instance=ExtResource("3_0t41i")] +position = Vector2(0, -30) [node name="Joke Button 2" parent="Tim/Joke Buttons" instance=ExtResource("3_0t41i")] -position = Vector2(-150, -50) +position = Vector2(-100, -50) action = "joke_button_2" [node name="Joke Button 3" parent="Tim/Joke Buttons" instance=ExtResource("3_0t41i")] -position = Vector2(150, -50) +position = Vector2(100, -50) action = "joke_button_3" [node name="Joke Transmitter" type="Node2D" parent="Tim"] @@ -52,3 +53,7 @@ shape = SubResource("RectangleShape2D_wmfel") [node name="Boundary" type="Node2D" parent="."] script = ExtResource("2_8p6ir") + +[connection signal="button_pressed" from="Tim/Joke Buttons/Joke Button 1" to="Tim" method="_on_joke_button_button_pressed"] +[connection signal="button_pressed" from="Tim/Joke Buttons/Joke Button 2" to="Tim" method="_on_joke_button_button_pressed"] +[connection signal="button_pressed" from="Tim/Joke Buttons/Joke Button 3" to="Tim" method="_on_joke_button_button_pressed"] diff --git a/godot/types/joke.gd b/godot/types/joke.gd index a31b854..f7652fd 100644 --- a/godot/types/joke.gd +++ b/godot/types/joke.gd @@ -1,7 +1,7 @@ class_name Joke enum JokeType { - Joke1, + Joke1 = 0, Joke2, Joke3 }