Adds better folder structure, and adds simple dialogue system

This commit is contained in:
2024-10-06 15:57:47 +02:00
parent 2cf9884f9d
commit 0f6f75ea44
31 changed files with 178 additions and 53 deletions

112
Scenes/Button/button.gd Normal file
View File

@@ -0,0 +1,112 @@
extends Node2D
@export var input_name = ""
@export var color_mix:Color = Color(1,1,1)
const ENEMY = preload("res://Scenes/Worm/enemy.tscn")
const DISTANCE = 40
var red_button = false
var should_move = false
signal enemy_killed
var trans = false
func _unhandled_input(event: InputEvent) -> void:
if(event.is_action_pressed(input_name)):
print_debug(input_name)
$AnimatedSprite2D.play("pressed")
var children = get_children(false)
var was_valid_input = false
if(red_button):
get_parent().red_button_press()
for a in children:
if(a.has_method("button_pressed")):
a.button_pressed()
enemy_killed.emit()
was_valid_input = true
if(!was_valid_input):
if get_parent().has_method("bad_press"):
get_parent().bad_press()
func get_has_enemy():
return get_children().any(func (a:Node):return a.has_method("button_pressed"))
func set_red_button(is_red: bool):
red_button = is_red
if is_red:
$AnimatedSprite2D.play("red")
else:
$AnimatedSprite2D.play("default")
func red_in():
red_button = true
trans = true
$AnimatedSprite2D.play("red_in")
func red_out():
red_button = true
trans = true
$AnimatedSprite2D.play("red_out")
func _process(delta: float) -> void:
if(!trans):
$AnimatedSprite2D.modulate = color_mix
else:
$AnimatedSprite2D.modulate = Color(1,1,1)
func spawn_enemy():
if(trans):
return
if get_has_enemy():
return
if(randi_range(0,1)==1):
var enemy = ENEMY.instantiate()
print_debug(randf_range(0.0, 2.0) * PI)
add_child(enemy)
enemy.global_position = global_position
enemy.global_rotation=randf_range(0, 2 * PI)
enemy.look_at(global_position)
enemy.enemy_attacked.connect(enemy_attacked)
func enemy_attacked():
#TODO: check for collisions
if red_button:
get_parent().change_red_button()
return
#move_button()
get_parent().ate()
#func move_button():
#should_move = true
#global_position = get_random_position()
func get_random_position():
var area2d:CollisionShape2D = get_parent().get_node(^"SpawnArea/CollisionShape2D")
var xa = area2d.shape.get_rect().position.x
var ya = area2d.global_position.y
# collison shape position is in the center of the shape :(( it took me too long to figure that out
var x = randi_range(area2d.global_position.x + area2d.shape.get_rect().position.x, area2d.global_position.x + area2d.shape.get_rect().end.x)
var y = randi_range(area2d.global_position.y + area2d.shape.get_rect().position.y, area2d.global_position.y + area2d.shape.get_rect().end.y)
return Vector2(x,y)
var should_really_move = false
#func _on_area_2d_area_entered(area: Area2D) -> void:
#if should_really_move:
#move_button()
#if should_move:
#move_button()
func _on_area_2d_area_exited(area: Area2D) -> void:
if should_move:
should_really_move = true
should_move = false
func _on_animated_sprite_2d_animation_finished() -> void:
if(trans):
if($AnimatedSprite2D.animation == "red_out"):
red_button = false
trans = false

118
Scenes/Button/button.tscn Normal file
View File

@@ -0,0 +1,118 @@
[gd_scene load_steps=10 format=3 uid="uid://bhvihrt8dipll"]
[ext_resource type="Script" path="res://Scenes/Button/button.gd" id="1_fkyus"]
[ext_resource type="Texture2D" uid="uid://q85jhjrqsrxk" path="res://Assets/Buttons/Idle.png" id="2_01nlj"]
[ext_resource type="Texture2D" uid="uid://blatwufdrwqml" path="res://Assets/Buttons/PressedIn1.png" id="3_180hi"]
[ext_resource type="Texture2D" uid="uid://4c7qtuk0exng" path="res://Assets/Buttons/FullPressed.png" id="4_5m7c4"]
[ext_resource type="Texture2D" uid="uid://ncd65m8iq7l0" path="res://Assets/Buttons/PressedOut.png" id="5_3xqo8"]
[ext_resource type="Texture2D" uid="uid://chckt1ppwsjq" path="res://Assets/Buttons/FORBIDDEN.png" id="6_p5821"]
[ext_resource type="Texture2D" uid="uid://cw6je7hakttbq" path="res://Assets/Buttons/ERROR.png" id="7_3w6j8"]
[sub_resource type="SpriteFrames" id="SpriteFrames_leuex"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}],
"loop": false,
"name": &"default",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("3_180hi")
}, {
"duration": 3.0,
"texture": ExtResource("4_5m7c4")
}, {
"duration": 1.0,
"texture": ExtResource("5_3xqo8")
}, {
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}],
"loop": false,
"name": &"pressed",
"speed": 15.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("6_p5821")
}],
"loop": true,
"name": &"red",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("7_3w6j8")
}, {
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}, {
"duration": 1.0,
"texture": ExtResource("7_3w6j8")
}, {
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}, {
"duration": 1.0,
"texture": ExtResource("7_3w6j8")
}, {
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}, {
"duration": 1.0,
"texture": ExtResource("6_p5821")
}],
"loop": false,
"name": &"red_in",
"speed": 3.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("7_3w6j8")
}, {
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}, {
"duration": 1.0,
"texture": ExtResource("7_3w6j8")
}, {
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}, {
"duration": 1.0,
"texture": ExtResource("7_3w6j8")
}, {
"duration": 1.0,
"texture": ExtResource("2_01nlj")
}],
"loop": false,
"name": &"red_out",
"speed": 3.0
}]
[sub_resource type="CircleShape2D" id="CircleShape2D_l0l4p"]
radius = 10.4089
[node name="Button" type="Node2D"]
script = ExtResource("1_fkyus")
metadata/_edit_group_ = true
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
scale = Vector2(1.38, 1.316)
sprite_frames = SubResource("SpriteFrames_leuex")
[node name="Area2D" type="Area2D" parent="."]
scale = Vector2(1.21306, 1.21306)
collision_layer = 2
collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
scale = Vector2(4.74125, 4.84)
shape = SubResource("CircleShape2D_l0l4p")
[connection signal="animation_finished" from="AnimatedSprite2D" to="." method="_on_animated_sprite_2d_animation_finished"]
[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"]
[connection signal="area_exited" from="Area2D" to="." method="_on_area_2d_area_exited"]

21
Scenes/Worm/enemy.gd Normal file
View File

@@ -0,0 +1,21 @@
extends Node2D
signal enemy_attacked
func button_pressed():
queue_free()
func _on_first_timer_timeout() -> void:
scale = Vector2(1.2,1.2)
func _on_second_timer_timeout() -> void:
scale = Vector2(1.4,1.4)
func _ready() -> void:
$AnimatedSprite2D.play("new_animation")
func _on_animated_sprite_2d_animation_finished() -> void:
enemy_attacked.emit()
queue_free()

60
Scenes/Worm/enemy.tscn Normal file
View File

@@ -0,0 +1,60 @@
[gd_scene load_steps=8 format=3 uid="uid://cuvoq65ec58jk"]
[ext_resource type="Script" path="res://Scenes/Worm/enemy.gd" id="1_t2xh1"]
[ext_resource type="Texture2D" uid="uid://7a1smbkaiutn" path="res://icon.svg" id="2_qu5u7"]
[ext_resource type="Texture2D" uid="uid://bjb6konwfdwma" path="res://Assets/Worm/1.png" id="3_6av35"]
[ext_resource type="Texture2D" uid="uid://cowon23pejcb6" path="res://Assets/Worm/2.png" id="4_x02e0"]
[ext_resource type="Texture2D" uid="uid://dl0wp6buga4d" path="res://Assets/Worm/3.png" id="5_eqhaj"]
[ext_resource type="Texture2D" uid="uid://c6cj6t3538iox" path="res://Assets/Worm/4.png" id="6_v8bp8"]
[sub_resource type="SpriteFrames" id="SpriteFrames_5w8q8"]
animations = [{
"frames": [],
"loop": true,
"name": &"default",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("3_6av35")
}, {
"duration": 3.0,
"texture": ExtResource("4_x02e0")
}, {
"duration": 1.0,
"texture": ExtResource("5_eqhaj")
}, {
"duration": 1.0,
"texture": ExtResource("6_v8bp8")
}],
"loop": false,
"name": &"new_animation",
"speed": 5.0
}]
[node name="Enemy" type="Node2D" groups=["enemy_group"]]
position = Vector2(10, 9)
script = ExtResource("1_t2xh1")
[node name="Icon" type="Sprite2D" parent="."]
visible = false
scale = Vector2(0.4, 0.4)
texture = ExtResource("2_qu5u7")
[node name="FirstTimer" type="Timer" parent="."]
one_shot = true
autostart = true
[node name="SecondTimer" type="Timer" parent="."]
wait_time = 2.0
one_shot = true
autostart = true
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
position = Vector2(-22, -55)
sprite_frames = SubResource("SpriteFrames_5w8q8")
animation = &"new_animation"
[connection signal="timeout" from="FirstTimer" to="." method="_on_first_timer_timeout"]
[connection signal="timeout" from="SecondTimer" to="." method="_on_second_timer_timeout"]
[connection signal="animation_finished" from="AnimatedSprite2D" to="." method="_on_animated_sprite_2d_animation_finished"]

35
Scenes/dialog.gd Normal file
View File

@@ -0,0 +1,35 @@
extends CanvasLayer
@export_file("*.json") var d_file
var dialogue = []
var dialog_step = 0
var is_dialog_active = false
signal dialogue_over
func start():
visible = true
is_dialog_active = true
dialogue = load_dialogue()
handle_dialog_change()
func _unhandled_input(event: InputEvent) -> void:
if !is_dialog_active:
return
if event.is_action_pressed("A"):
if(dialog_step >= dialogue.size()-1):
visible = false
is_dialog_active = false
dialogue_over.emit()
return
dialog_step +=1
handle_dialog_change()
func handle_dialog_change():
$NinePatchRect/Name.text = dialogue[dialog_step].name
$NinePatchRect/Text.text = dialogue[dialog_step].text
func load_dialogue():
var file = FileAccess.open(d_file, FileAccess.READ)
var content = JSON.parse_string(file.get_as_text())
return content

39
Scenes/dialog.tscn Normal file
View File

@@ -0,0 +1,39 @@
[gd_scene load_steps=3 format=3 uid="uid://du2jgrreaylhr"]
[ext_resource type="Script" path="res://Scenes/dialog.gd" id="1_lru6p"]
[ext_resource type="Texture2D" uid="uid://61did6cf2bnh" path="res://Assets/dialogue_box.png" id="2_cuk8x"]
[node name="Dialog" type="CanvasLayer"]
script = ExtResource("1_lru6p")
d_file = "res://Assets/dialogs.json"
[node name="NinePatchRect" type="NinePatchRect" parent="."]
offset_right = 101.0
offset_bottom = 35.0
scale = Vector2(5.08, 5.08)
texture = ExtResource("2_cuk8x")
region_rect = Rect2(0.0332336, 0.310051, 31.9668, 31.6899)
patch_margin_left = 7
patch_margin_top = 9
patch_margin_right = 7
patch_margin_bottom = 10
[node name="Name" type="RichTextLabel" parent="NinePatchRect"]
layout_mode = 0
offset_left = 4.33071
offset_top = 0.787402
offset_right = 227.331
offset_bottom = 26.7874
scale = Vector2(0.450723, 0.450723)
theme_override_colors/default_color = Color(0, 0, 0, 1)
text = "Name"
[node name="Text" type="RichTextLabel" parent="NinePatchRect"]
layout_mode = 0
offset_left = 6.29921
offset_top = 7.87402
offset_right = 214.299
offset_bottom = 59.874
scale = Vector2(0.450723, 0.450723)
theme_override_colors/default_color = Color(0, 0, 0, 1)
text = "Text"