Replace M* table-wrapper classes with typed Resources; add type hints
Two related cleanups from the best-practice audit: Task 3 — typed Resources instead of MBase / MScene / MLevel / MSetting The old model classes wrapped the godot_db_manager Table API: each row read went through table.get_data_at_row_idx(int), Cell.get_data(), and 'as int' / 'as String' casts that don't actually parse anything in Godot 4. m_value, m_lock, m_label, ... members shadowed the cell indirection. Setters round-tripped through table.edit_data() + Global.database.save_db(). That's a lot of plumbing for what is, in the end, three flat tables of static strings. Introduce three @export-typed Resources: db/scene_entry.gd class_name SceneEntry db/level_entry.gd class_name LevelEntry db/settings_data.gd class_name SettingsData Rewrite scripts/Database.gd so Database.DB holds: settings: SettingsData levels: Array[LevelEntry] scenes: Array[SceneEntry] Build them once at startup from ahog.json, and serialise back to the same JSON shape on save() so existing progress files keep working. LevelEntry carries its own object_to_find / object_finding / reset methods (talking to Global.database for cross-table lookups), and SceneEntry carries its own mesh_path / audio_sound. Per-scene dissolve state (value, tick_reference, dissolved) lives on SceneEntry as non-exported runtime fields. Delete db/MBase.gd / db/MScene.gd / db/MLevel.gd / db/MSetting.gd. Update consumers: - scripts/Setting.gd: read/write Global.database.settings directly, call Global.database.save() after each setter. - scenes/levels/Levels.gd: iterate Global.database.scenes_for_level( current_scene_int) instead of mscene.new(i) for every row; scene state reads (scene.lock, scene.mesh, scene.counter, ...) replace scene.lock() / scene.mesh() / scene.counter() method calls; runtime dissolve state lives on the SceneEntry instance instead of mutable m_value / m_tick_reference members on MScene; 'dissolved' flag replaces set_mesh(null) signalling. - scenes/UI/choose_scenes/ChooseScene.gd: iterate Global.database .levels; level.name / level.thumb property access in place of level.name() / level.thumbnail(). configure_reset() loses its redundant index argument (LevelEntry knows its own index). - scripts/event.gd: _on_reset_level signature now takes LevelEntry, reset path drops index forwarding. Task 2 — type hints across the remaining scripts scripts/Global.gd, scenes/Main.gd, scenes/UI/ending/Ending.gd, scenes/UI/loading/Loading.gd, scenes/UI/settings/Settings.gd: add typed parameters and -> return annotations. current_scene_int is now 'int = -1' (sentinel) so callers don't fall into Variant comparisons; event.gd:_on_reset_level resets it to -1 instead of null. Settings.gd no longer wraps button_pressed in int() before passing to the now-typed bool setters. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,68 +2,57 @@ extends Control
|
||||
|
||||
@export var template: PackedScene = load("res://scenes/UI/choose_scenes/parts/Template.tscn")
|
||||
|
||||
func _ready():
|
||||
_load_button_access_scenes()
|
||||
func _ready() -> void:
|
||||
for level in Global.database.levels:
|
||||
_apply_scene(level)
|
||||
|
||||
## PRIVATE
|
||||
func _load_button_access_scenes():
|
||||
for row_index in range(0, Global.database.get_table_by_name("levels").m_rows_count):
|
||||
_apply_scene(load("res://db/MLevel.gd").new(row_index), row_index)
|
||||
|
||||
func _apply_scene(level, index):
|
||||
$MarginContainer.add_child(_load_scene(level.name()))
|
||||
var node = _build_path(level.name())
|
||||
|
||||
func _apply_scene(level: LevelEntry) -> void:
|
||||
$MarginContainer.add_child(_load_scene(level.name))
|
||||
var node := _build_path(level.name)
|
||||
_configure_select(level, node)
|
||||
configure_reset(level, node, index, false)
|
||||
configure_reset(level, node, false)
|
||||
configure_counter(level, node)
|
||||
|
||||
func _load_scene(p_name):
|
||||
var template_instance = template.instantiate()
|
||||
func _load_scene(p_name: String) -> Node:
|
||||
var template_instance := template.instantiate()
|
||||
template_instance.set_name(p_name)
|
||||
|
||||
return template_instance
|
||||
|
||||
func _build_path(p_name):
|
||||
func _build_path(p_name: String) -> String:
|
||||
return "MarginContainer/" + p_name
|
||||
|
||||
func _load_texture(thumbnail):
|
||||
return load(thumbnail)
|
||||
|
||||
func _configure_select(level, node):
|
||||
var selector = get_node(node+"/MarginContainer/CenterAlign/MainButton")
|
||||
var thumbnail = get_node(node+"/MarginContainer/CenterAlign/MainButton/MarginStich/ThumbnailLevel")
|
||||
|
||||
thumbnail.set_texture(_load_texture(level.thumbnail()))
|
||||
var handler = Event.level_pressed(level.name())
|
||||
func _configure_select(level: LevelEntry, node: String) -> void:
|
||||
var selector := get_node(node + "/MarginContainer/CenterAlign/MainButton")
|
||||
var thumbnail: TextureRect = get_node(node + "/MarginContainer/CenterAlign/MainButton/MarginStich/ThumbnailLevel")
|
||||
thumbnail.texture = load(level.thumb)
|
||||
var handler := Event.level_pressed(level.name)
|
||||
if handler.is_valid():
|
||||
selector.pressed.connect(handler)
|
||||
|
||||
func configure_reset(level, node, index, animate):
|
||||
var reset = get_node(node+"/MarginContainer/CenterAlign/MainButton/TabAlign/ButtonReset")
|
||||
var animation = get_node(node+"/AnimationPlayer")
|
||||
func configure_reset(level: LevelEntry, node: String, animate: bool) -> void:
|
||||
var reset: BaseButton = get_node(node + "/MarginContainer/CenterAlign/MainButton/TabAlign/ButtonReset")
|
||||
var animation: AnimationPlayer = get_node(node + "/AnimationPlayer")
|
||||
if int(level.object_finding()) == 0:
|
||||
_configure_reset_disable(animation, reset, animate)
|
||||
else:
|
||||
_configure_reset_enable(animation, reset, level, node, index)
|
||||
_configure_reset_enable(animation, reset, level, node)
|
||||
|
||||
func _configure_reset_disable(animation, reset, animate = false):
|
||||
func _configure_reset_disable(animation: AnimationPlayer, reset: BaseButton, animate: bool = false) -> void:
|
||||
animation.play("SlideReset")
|
||||
if !animate:
|
||||
if not animate:
|
||||
animation.seek(1, false)
|
||||
|
||||
reset.set_disabled(true)
|
||||
reset.set_default_cursor_shape(CURSOR_ARROW)
|
||||
reset.mouse_default_cursor_shape = CURSOR_ARROW
|
||||
for c in reset.pressed.get_connections():
|
||||
reset.pressed.disconnect(c["callable"])
|
||||
|
||||
func _configure_reset_enable(animation, reset, level, node, index):
|
||||
func _configure_reset_enable(animation: AnimationPlayer, reset: BaseButton, level: LevelEntry, node: String) -> void:
|
||||
animation.play_backwards("SlideReset")
|
||||
reset.set_disabled(false)
|
||||
reset.set_default_cursor_shape(CURSOR_POINTING_HAND)
|
||||
reset.pressed.connect(Event._on_reset_level.bind(level, node, index, self))
|
||||
reset.mouse_default_cursor_shape = CURSOR_POINTING_HAND
|
||||
reset.pressed.connect(Event._on_reset_level.bind(level, node, level.index, self))
|
||||
|
||||
func configure_counter(level, node):
|
||||
var count = get_node(node+"/MarginContainer/CenterAlign/MainButton/TabAlign/ButtonCount/MarginBottom/Label")
|
||||
|
||||
count.set_text(level.object_finding()+" / "+level.object_to_find())
|
||||
func configure_counter(level: LevelEntry, node: String) -> void:
|
||||
var count: Label = get_node(node + "/MarginContainer/CenterAlign/MainButton/TabAlign/ButtonCount/MarginBottom/Label")
|
||||
count.text = level.object_finding() + " / " + level.object_to_find()
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
extends CenterContainer
|
||||
|
||||
func _ready():
|
||||
print("[ending_#ready]")
|
||||
func _ready() -> void:
|
||||
$AudioStreamPlayer.play()
|
||||
|
||||
func _on_Timer_timeout():
|
||||
print("[ending#_on_Timer_timeout]")
|
||||
func _on_Timer_timeout() -> void:
|
||||
Global.goto_scene("res://scenes/Main.tscn")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
extends Control
|
||||
|
||||
func _ready():
|
||||
func _ready() -> void:
|
||||
get_node("LabelLoading").text = tr("LOADING")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
extends Node
|
||||
|
||||
func _ready():
|
||||
func _ready() -> void:
|
||||
_apply_translation()
|
||||
_apply_settings_language()
|
||||
_apply_settings_gyroscope()
|
||||
@@ -9,50 +9,46 @@ func _ready():
|
||||
_apply_settings_fullscreen()
|
||||
|
||||
## PRIVATE
|
||||
func _apply_translation():
|
||||
func _apply_translation() -> void:
|
||||
$VBoxContainer/langue/VBoxContainer/Label.text = tr("SETTINGS_LABEL_LANGUE")
|
||||
$VBoxContainer/gyroscope/HBoxContainer/Label.text = tr("SETTINGS_LABEL_GYROSCOPE")
|
||||
|
||||
func _apply_settings_language():
|
||||
var data = $VBoxContainer/langue/VBoxContainer/data
|
||||
|
||||
func _apply_settings_language() -> void:
|
||||
var data: ItemList = $VBoxContainer/langue/VBoxContainer/data
|
||||
data.add_item("English", load("res://assets/ui/flags/english.png"), true) # id : 0
|
||||
data.add_item("Français", load("res://assets/ui/flags/french.png"), true) # id : 1
|
||||
|
||||
data.select(Setting.get_setting_language())
|
||||
|
||||
func _apply_settings_gyroscope():
|
||||
func _apply_settings_gyroscope() -> void:
|
||||
$VBoxContainer/gyroscope/HBoxContainer/data.button_pressed = Setting.get_setting_gyrosocpe()
|
||||
|
||||
func _apply_settings_sound_ambient():
|
||||
func _apply_settings_sound_ambient() -> void:
|
||||
$VBoxContainer/ambient_sound/HBoxContainer/data.button_pressed = Setting.get_setting_ambient_sound()
|
||||
|
||||
func _apply_settings_resolution():
|
||||
var data = $VBoxContainer/resolution/VBoxContainer/data
|
||||
|
||||
data.add_item("2560 x 1440", null, true) # id : 0
|
||||
data.add_item("1920 x 1080", null, true) # id : 1
|
||||
data.add_item("1280 x 720", null, true) # id : 2
|
||||
data.add_item("854 x 576", null, true) # id : 3
|
||||
|
||||
func _apply_settings_resolution() -> void:
|
||||
var data: ItemList = $VBoxContainer/resolution/VBoxContainer/data
|
||||
data.add_item("2560 x 1440", null, true)
|
||||
data.add_item("1920 x 1080", null, true)
|
||||
data.add_item("1280 x 720", null, true)
|
||||
data.add_item("854 x 576", null, true)
|
||||
for index in range(4):
|
||||
if data.get_item_text(index).split(' x ') == Setting.get_setting_resolution():
|
||||
if data.get_item_text(index).split(" x ") == Setting.get_setting_resolution():
|
||||
data.select(index)
|
||||
|
||||
func _apply_settings_fullscreen():
|
||||
func _apply_settings_fullscreen() -> void:
|
||||
$VBoxContainer/fullscreen/HBoxContainer/data.button_pressed = Setting.get_setting_fullscreen()
|
||||
|
||||
func _on_gyroscope_pressed():
|
||||
Setting.set_setting_gyroscope(int($VBoxContainer/gyroscope/HBoxContainer/data.button_pressed))
|
||||
func _on_gyroscope_pressed() -> void:
|
||||
Setting.set_setting_gyroscope($VBoxContainer/gyroscope/HBoxContainer/data.button_pressed)
|
||||
|
||||
func _on_ambient_sound_pressed():
|
||||
Setting.set_setting_ambient_sound(int($VBoxContainer/ambient_sound/HBoxContainer/data.button_pressed))
|
||||
func _on_ambient_sound_pressed() -> void:
|
||||
Setting.set_setting_ambient_sound($VBoxContainer/ambient_sound/HBoxContainer/data.button_pressed)
|
||||
|
||||
func _on_langue_item_selected(index):
|
||||
func _on_langue_item_selected(index: int) -> void:
|
||||
Setting.set_setting_language(index)
|
||||
|
||||
func _on_resolution_item_selected(index):
|
||||
func _on_resolution_item_selected(index: int) -> void:
|
||||
Setting.set_setting_resolution($VBoxContainer/resolution/VBoxContainer/data.get_item_text(index))
|
||||
|
||||
func _on_fullscreen_item_selected():
|
||||
Setting.set_setting_fullscreen(int($VBoxContainer/fullscreen/HBoxContainer/data.button_pressed))
|
||||
func _on_fullscreen_item_selected() -> void:
|
||||
Setting.set_setting_fullscreen($VBoxContainer/fullscreen/HBoxContainer/data.button_pressed)
|
||||
|
||||
Reference in New Issue
Block a user