Set sprites based on jokes

This commit is contained in:
Waldemar Tomme 2024-01-27 13:35:14 +01:00
parent 9edf0e85b6
commit 6220fbdbf9
4 changed files with 32 additions and 13 deletions

View File

@ -3,17 +3,29 @@ extends Node2D
signal button_pressed(joke) signal button_pressed(joke)
@export_enum("joke_button_1", "joke_button_2", "joke_button_3") var action: String @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 var current_joke: Joke
func _init(): func _map_action_to_joke():
current_joke = Joke.new(Joke.JokeType.Joke1) 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(): 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. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(delta):
if Input.is_action_just_pressed(action): if Input.is_action_just_pressed(action):
button_pressed.emit(current_joke) button_pressed.emit(current_joke)
func set_current_joke(joke: Joke):
current_joke = joke
sprite.texture = type_sprites[joke.type]

View File

@ -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="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"] [node name="Joke Button" type="Node2D" node_paths=PackedStringArray("sprite")]
size = Vector2(150, 50)
[node name="Joke Button 1" type="Node2D"]
script = ExtResource("1_lofpb") script = ExtResource("1_lofpb")
action = "joke_button_1" 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="."] [node name="Sprite2D" type="Sprite2D" parent="."]
texture = SubResource("PlaceholderTexture2D_k2q2d") texture = ExtResource("2_ivsb5")

View File

@ -34,13 +34,14 @@ offset = Vector2(0, 50)
position = Vector2(0, -25) position = Vector2(0, -25)
[node name="Joke Button 1" parent="Tim/Joke Buttons" instance=ExtResource("3_0t41i")] [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")] [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" action = "joke_button_2"
[node name="Joke Button 3" parent="Tim/Joke Buttons" instance=ExtResource("3_0t41i")] [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" action = "joke_button_3"
[node name="Joke Transmitter" type="Node2D" parent="Tim"] [node name="Joke Transmitter" type="Node2D" parent="Tim"]
@ -52,3 +53,7 @@ shape = SubResource("RectangleShape2D_wmfel")
[node name="Boundary" type="Node2D" parent="."] [node name="Boundary" type="Node2D" parent="."]
script = ExtResource("2_8p6ir") 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"]

View File

@ -1,7 +1,7 @@
class_name Joke class_name Joke
enum JokeType { enum JokeType {
Joke1, Joke1 = 0,
Joke2, Joke2,
Joke3 Joke3
} }