Refactoring of FrogMinigame
This commit is contained in:
@@ -110,6 +110,10 @@ func _get_current_stimulus() -> Dictionary:
|
||||
return stimuli[current_progression % stimuli.size()]
|
||||
|
||||
|
||||
func _get_current_distractors() -> Array:
|
||||
return distractions[current_progression]
|
||||
|
||||
|
||||
# Get the current GP to find
|
||||
func _get_GP() -> Dictionary:
|
||||
var stimulus: = _get_current_stimulus()
|
||||
@@ -120,7 +124,7 @@ func _get_GP() -> Dictionary:
|
||||
|
||||
# Get a random distractor for the current GP
|
||||
func _get_distractor() -> Dictionary:
|
||||
return distractions[current_progression][current_word_progression].pick_random()
|
||||
return _get_current_distractors()[current_word_progression].pick_random()
|
||||
|
||||
|
||||
# Check if the provided GP is the expected answer
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
[ext_resource type="Script" path="res://sources/minigames/frog/frog.gd" id="1_68f6f"]
|
||||
[ext_resource type="Texture2D" uid="uid://begtyjietpcyo" path="res://assets/minigames/frog/graphics/frog.png" id="1_c6opx"]
|
||||
[ext_resource type="Material" uid="uid://bcoalg24yap4x" path="res://sources/minigames/frog/frog_material.tres" id="2_i6km5"]
|
||||
[ext_resource type="Material" uid="uid://cj241oskmxqgv" path="res://sources/minigames/frog/frog_material.tres" id="2_i6km5"]
|
||||
|
||||
[sub_resource type="Animation" id="Animation_42msv"]
|
||||
length = 0.001
|
||||
|
||||
@@ -5,6 +5,26 @@ class_name FrogMinigame
|
||||
const lilypad_track_scene: = preload("res://sources/minigames/frog/lilypad_track.tscn")
|
||||
|
||||
|
||||
class DifficultySettings:
|
||||
var targetsPerLane: = 1
|
||||
var padsSpeedDisabled: = 100.0
|
||||
var padsSpeed: = 200.0
|
||||
|
||||
func _init(p_targetsPerLane: int, p_padsSpeedDisabled: float, p_padsSpeed: float) -> void:
|
||||
targetsPerLane = p_targetsPerLane
|
||||
padsSpeedDisabled = p_padsSpeedDisabled
|
||||
padsSpeed = p_padsSpeed
|
||||
|
||||
|
||||
var difficulty_settings: Array[DifficultySettings] = [
|
||||
DifficultySettings.new(1, 100., 200.),
|
||||
DifficultySettings.new(2, 150., 250.),
|
||||
DifficultySettings.new(2, 200., 300.),
|
||||
DifficultySettings.new(3, 250., 350.),
|
||||
DifficultySettings.new(3, 300., 400.)
|
||||
]
|
||||
|
||||
|
||||
@onready var start: = %Start
|
||||
@onready var end: = %End
|
||||
|
||||
@@ -15,10 +35,7 @@ const lilypad_track_scene: = preload("res://sources/minigames/frog/lilypad_track
|
||||
@onready var frog: = %Frog
|
||||
|
||||
|
||||
var current_word: Dictionary
|
||||
var current_distractors: Array
|
||||
|
||||
|
||||
# TODO Remove
|
||||
func _old_find_stimuli_and_distractions() -> void:
|
||||
var words_list: = Database.get_words_for_lesson(lesson_nb)
|
||||
words_list.shuffle()
|
||||
@@ -41,42 +58,42 @@ func _old_find_stimuli_and_distractions() -> void:
|
||||
distractions.append(grapheme_distractions)
|
||||
|
||||
|
||||
func _start() -> void:
|
||||
_get_new_word()
|
||||
|
||||
|
||||
func _highlight() -> void:
|
||||
for track in lilypad_tracks_container.get_children():
|
||||
track.is_highlighting = true
|
||||
|
||||
|
||||
func _get_new_word() -> void:
|
||||
if audio_player.playing:
|
||||
await audio_player.finished
|
||||
|
||||
func _setup_word_progression() -> void:
|
||||
await _free_tracks()
|
||||
_update_current_word()
|
||||
super()
|
||||
_create_tracks()
|
||||
await _play_current_word()
|
||||
_start_tracks()
|
||||
|
||||
|
||||
func _highlight() -> void:
|
||||
for track: LilypadTrack in lilypad_tracks_container.get_children():
|
||||
track.is_highlighting = true
|
||||
|
||||
|
||||
func _reset_frog() -> void:
|
||||
frog.global_position = frog_spawn_point.global_position
|
||||
frog.jump_to(start.global_position)
|
||||
await frog.jumped
|
||||
|
||||
#region Tracks management
|
||||
|
||||
func _free_tracks() -> void:
|
||||
for track in lilypad_tracks_container.get_children():
|
||||
await track.delete()
|
||||
for track: LilypadTrack in lilypad_tracks_container.get_children():
|
||||
await track.reset()
|
||||
|
||||
for track in lilypad_tracks_container.get_children():
|
||||
for track: LilypadTrack in lilypad_tracks_container.get_children():
|
||||
track.queue_free()
|
||||
|
||||
|
||||
func _update_current_word() -> void:
|
||||
current_word = stimuli.pop_front()
|
||||
current_distractors = distractions.pop_front()
|
||||
# Waits for the track to be properly freed
|
||||
await track.tree_exited
|
||||
|
||||
|
||||
func _create_tracks() -> void:
|
||||
var current_word: = _get_current_stimulus()
|
||||
var current_distractors: = _get_current_distractors()
|
||||
|
||||
for i in range(current_word.GPs.size()):
|
||||
var track: = lilypad_track_scene.instantiate()
|
||||
var track: LilypadTrack = lilypad_track_scene.instantiate()
|
||||
track.name = str(i) + "_" + current_word.GPs[i].Grapheme
|
||||
lilypad_tracks_container.add_child(track)
|
||||
|
||||
track.top_to_bottom = i % 2
|
||||
@@ -96,27 +113,16 @@ func _create_tracks() -> void:
|
||||
|
||||
|
||||
func _start_tracks() -> void:
|
||||
for track in lilypad_tracks_container.get_children():
|
||||
for track: LilypadTrack in lilypad_tracks_container.get_children():
|
||||
await track.reset()
|
||||
track.start()
|
||||
|
||||
lilypad_tracks_container.get_child(0).is_enabled = true
|
||||
|
||||
#endregion
|
||||
|
||||
func _play_current_word() -> void:
|
||||
audio_player.stream = Database.get_audio_stream_for_word(current_word.Word)
|
||||
audio_player.play()
|
||||
if audio_player.playing:
|
||||
await audio_player.finished
|
||||
#region Connections
|
||||
|
||||
|
||||
func _reset_frog() -> void:
|
||||
frog.global_position = frog_spawn_point.global_position
|
||||
frog.jump_to(start.global_position)
|
||||
await frog.jumped
|
||||
|
||||
|
||||
func _on_track_lilypad_in_center(lilypad: Control, track: Control) -> void:
|
||||
func _on_track_lilypad_in_center(lilypad: Lilypad, track: LilypadTrack) -> void:
|
||||
frog.jump_to(lilypad.global_position)
|
||||
await frog.jumped
|
||||
|
||||
@@ -130,41 +136,41 @@ func _on_track_lilypad_in_center(lilypad: Control, track: Control) -> void:
|
||||
await frog.drowned
|
||||
|
||||
current_lives -= 1
|
||||
current_word_progression = 0
|
||||
|
||||
_start_tracks()
|
||||
await _reset_frog()
|
||||
else:
|
||||
track.stop()
|
||||
track.is_cleared = true
|
||||
current_word_progression += 1
|
||||
|
||||
var all_cleared: = true
|
||||
for other_track in lilypad_tracks_container.get_children():
|
||||
if not other_track.is_cleared:
|
||||
all_cleared = false
|
||||
|
||||
other_track.is_enabled = true
|
||||
func _on_current_word_progression_changed() -> void:
|
||||
# Enables the next track
|
||||
for track: LilypadTrack in lilypad_tracks_container.get_children():
|
||||
if not track.is_cleared:
|
||||
track.is_enabled = true
|
||||
break
|
||||
|
||||
if all_cleared:
|
||||
frog.jump_to(end.global_position)
|
||||
await frog.jumped
|
||||
|
||||
for other_track in lilypad_tracks_container.get_children():
|
||||
if other_track != track:
|
||||
other_track.right()
|
||||
await track.right()
|
||||
|
||||
frog.jump_to(frog_despawn_point.global_position)
|
||||
await frog.jumped
|
||||
|
||||
await _free_tracks()
|
||||
|
||||
current_progression += 1
|
||||
|
||||
|
||||
func _on_current_progression_changed() -> void:
|
||||
if stimuli.size() != 0:
|
||||
# Makes the frog jumps to the rock on the right
|
||||
frog.jump_to(end.global_position)
|
||||
await frog.jumped
|
||||
|
||||
# Play the animation on each pad
|
||||
for track: LilypadTrack in lilypad_tracks_container.get_children():
|
||||
track.right()
|
||||
|
||||
# Makes the frog jumps out of screen
|
||||
frog.jump_to(frog_despawn_point.global_position)
|
||||
await frog.jumped
|
||||
|
||||
# Resets the frog position
|
||||
await _reset_frog()
|
||||
await _get_new_word()
|
||||
else:
|
||||
_win()
|
||||
|
||||
# Setups the next word
|
||||
super()
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
extends Control
|
||||
class_name Lilypad
|
||||
|
||||
signal pressed()
|
||||
signal disappeared()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[gd_scene load_steps=11 format=3 uid="uid://bkswcqcmf7a0r"]
|
||||
|
||||
[ext_resource type="Material" uid="uid://cbs2x6nv6jl63" path="res://sources/minigames/frog/lilypad_material.tres" id="1_pbo2v"]
|
||||
[ext_resource type="Material" uid="uid://3n8kdc1u25om" path="res://sources/minigames/frog/lilypad_material.tres" id="1_pbo2v"]
|
||||
[ext_resource type="Script" path="res://sources/minigames/frog/lilypad.gd" id="2_pdqed"]
|
||||
[ext_resource type="Texture2D" uid="uid://bl4csvpvhsgqh" path="res://assets/minigames/frog/graphics/lilypad_enabled.png" id="3_1ueu4"]
|
||||
[ext_resource type="Texture2D" uid="uid://bafwfqkof2og7" path="res://assets/minigames/frog/graphics/lilypad_disabled.png" id="4_0e8ya"]
|
||||
@@ -58,6 +58,8 @@ libraries = {
|
||||
|
||||
[node name="HighlightFX" parent="." instance=ExtResource("5_1llgv")]
|
||||
|
||||
[node name="RightFX" parent="." instance=ExtResource("6_wslmk")]
|
||||
|
||||
[node name="TextureButton" type="TextureButton" parent="."]
|
||||
material = ExtResource("1_pbo2v")
|
||||
layout_mode = 1
|
||||
@@ -90,8 +92,6 @@ text = "A"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
|
||||
[node name="RightFX" parent="." instance=ExtResource("6_wslmk")]
|
||||
|
||||
[node name="WrongFX" parent="." instance=ExtResource("7_241b6")]
|
||||
|
||||
[connection signal="animation_finished" from="AnimationPlayer" to="." method="_on_animation_player_animation_finished"]
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
extends Control
|
||||
class_name LilypadTrack
|
||||
|
||||
signal lilypad_in_center(lilypad: Control)
|
||||
|
||||
signal lilypad_in_center(lilypad: Lilypad)
|
||||
|
||||
@onready var audio_player: = $AudioStreamPlayer2D
|
||||
@onready var spawn_timer: = $SpawnTimer
|
||||
@@ -18,8 +20,8 @@ var is_highlighting: = false:
|
||||
var ready_to_spawn: = false
|
||||
var top_to_bottom: = true
|
||||
|
||||
var lilypads: = []
|
||||
var tweens: = []
|
||||
var lilypads: Array[Lilypad] = []
|
||||
var tweens: Array[Tween] = []
|
||||
|
||||
var stimuli: = []:
|
||||
set = _set_stimuli
|
||||
@@ -29,10 +31,6 @@ var stimuli_queue: = []
|
||||
var stimuli_queue_size: = 0
|
||||
|
||||
|
||||
func delete() -> void:
|
||||
await reset()
|
||||
|
||||
|
||||
func reset() -> void:
|
||||
is_cleared = false
|
||||
ready_to_spawn = false
|
||||
@@ -41,7 +39,7 @@ func reset() -> void:
|
||||
for tween in tweens:
|
||||
tween.stop()
|
||||
|
||||
for lilypad in lilypads:
|
||||
for lilypad: Lilypad in lilypads:
|
||||
lilypad.disappear()
|
||||
|
||||
await get_tree().create_timer(0.5).timeout
|
||||
@@ -56,12 +54,12 @@ func stop() -> void:
|
||||
|
||||
|
||||
func right() -> void:
|
||||
for lilypad in lilypads:
|
||||
for lilypad: Lilypad in lilypads:
|
||||
await lilypad.right()
|
||||
|
||||
|
||||
func _spawn_lilypad() -> void:
|
||||
var lilypad: = lilypad_class.instantiate()
|
||||
var lilypad: Lilypad = lilypad_class.instantiate()
|
||||
lilypads.append(lilypad)
|
||||
|
||||
lilypad.pressed.connect(_on_lilypad_pressed.bind(lilypad))
|
||||
@@ -109,7 +107,7 @@ func _spawn_lilypad() -> void:
|
||||
tweens.append(tween)
|
||||
|
||||
|
||||
func _despawn_lilypad(lilypad: Control) -> void:
|
||||
func _despawn_lilypad(lilypad: Lilypad) -> void:
|
||||
lilypads.erase(lilypad)
|
||||
lilypad.queue_free()
|
||||
|
||||
@@ -135,7 +133,7 @@ func _set_stimuli(value: Array) -> void:
|
||||
stimuli_queue_size = max(0, stimuli.size() - 3)
|
||||
|
||||
|
||||
func _on_lilypad_pressed(lilypad: Control) -> void:
|
||||
func _on_lilypad_pressed(lilypad: Lilypad) -> void:
|
||||
audio_player.play()
|
||||
|
||||
ready_to_spawn = false
|
||||
@@ -156,18 +154,18 @@ func _on_lilypad_pressed(lilypad: Control) -> void:
|
||||
tweens.append(center_tween)
|
||||
|
||||
|
||||
func _on_lilypad_disappeared(lilypad: Control) -> void:
|
||||
func _on_lilypad_disappeared(lilypad: Lilypad) -> void:
|
||||
_despawn_lilypad(lilypad)
|
||||
|
||||
|
||||
func _on_tween_finished(lilypad: Control, tween: Tween) -> void:
|
||||
func _on_tween_finished(lilypad: Lilypad, tween: Tween) -> void:
|
||||
lilypad.disappear()
|
||||
|
||||
tweens.erase(tween)
|
||||
tween.kill()
|
||||
|
||||
|
||||
func _on_center_tween_finished(lilypad: Control, center_tween: Tween) -> void:
|
||||
func _on_center_tween_finished(lilypad: Lilypad, center_tween: Tween) -> void:
|
||||
lilypad_in_center.emit(lilypad)
|
||||
|
||||
center_tween.kill()
|
||||
|
||||
Reference in New Issue
Block a user