From 0e3803fe21f2c5a67fcf820540c34548f4761eb1 Mon Sep 17 00:00:00 2001 From: cutekibry Date: Sun, 11 Feb 2024 12:25:58 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E4=BF=AE=E6=AD=A3=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 给未添加 class_name 的 .gd 添加 class_name - 使用 Scn 作为存储场景的常量名 - 修正 chapter_menu 中使用重名变量(chapter_id)的错误 --- levels/chapter_menu/chapter_menu.gd | 32 ++++++++++---------- levels/chapter_menu/level_menu/level_menu.gd | 22 +++++++------- objects/card_base/card_base.gd | 4 +-- objects/main_menu/main_menu.gd | 10 +++--- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/levels/chapter_menu/chapter_menu.gd b/levels/chapter_menu/chapter_menu.gd index fa4c247..a9bb8af 100644 --- a/levels/chapter_menu/chapter_menu.gd +++ b/levels/chapter_menu/chapter_menu.gd @@ -1,10 +1,10 @@ extends Node2D +class_name ChapterMenu - -const BaseLevel := preload("res://levels/base_level/base_level.tscn") -const LevelButton := preload("res://levels/chapter_menu/level_menu/level_button/level_button.tscn") -const LevelMenu := preload("res://levels/chapter_menu/level_menu/level_menu.tscn") +const BaseLevelScn := preload("res://levels/base_level/base_level.tscn") +const LevelButtonScn := preload("res://levels/chapter_menu/level_menu/level_button/level_button.tscn") +const LevelMenuScn := preload("res://levels/chapter_menu/level_menu/level_menu.tscn") # Called when the node enters the scene tree for the first time. @@ -13,14 +13,14 @@ const button_width : int = 50 const button_heigth : int = 50 func init() -> void: - var chap_num : int = len(BaseLevel.instantiate().DATA) - for chapter_id in range(0, chap_num): - # print(chapter_id) - var level_num : int = len(BaseLevel.instantiate().DATA[chapter_id]) - var button = LevelButton.instantiate(); - var x : int = button_width * (chapter_id % 9) + 60 - var y : int = button_heigth * (chapter_id / 9) + 100 - button.init(chapter_id, level_num, Vector2(x, y), 0) + var chap_num : int = len(BaseLevelScn.instantiate().DATA) + for lvl_id in range(0, chap_num): + # print(lvl_id) + var level_num : int = len(BaseLevelScn.instantiate().DATA[lvl_id]) + var button = LevelButtonScn.instantiate(); + var x : int = button_width * (lvl_id % 9) + 60 + var y : int = button_heigth * (lvl_id / 9) + 100 + button.init(lvl_id, level_num, Vector2(x, y), 0) button.enter_chapter.connect(_is_choose_chapter) add_child(button) @@ -28,13 +28,13 @@ func init() -> void: func _ready(): # print("ready") init() - #print(BaseLevel.instantiate().DATA[chapter_id]) + #print(BaseLevelScn.instantiate().DATA[chapter_id]) # print(chapter_num) -func _is_choose_chapter(chapter_id : int, level_num : int): - var level_menu = LevelMenu.instantiate() - level_menu.init(chapter_id, level_num) +func _is_choose_chapter(chap_id : int, level_num : int): + var level_menu = LevelMenuScn.instantiate() + level_menu.init(chap_id, level_num) # print("here ready to change scene to ", chapter_id) get_tree().root.add_child(level_menu) queue_free() diff --git a/levels/chapter_menu/level_menu/level_menu.gd b/levels/chapter_menu/level_menu/level_menu.gd index f050b96..c6c9bbc 100644 --- a/levels/chapter_menu/level_menu/level_menu.gd +++ b/levels/chapter_menu/level_menu/level_menu.gd @@ -1,14 +1,14 @@ extends Node2D - +class_name LevelMenu const CHAP_NAMES = ["=P", "Add and Multiply", "()", "Equal?", "<>"] const I_NUMBER = ["I","II","III","VI","V"] -const LevelButton := preload("res://levels/chapter_menu/level_menu/level_button/level_button.tscn") -const BaseLevel := preload("res://levels/base_level/base_level.tscn") -const Credits := preload("res://objects/credits/credits.tscn") +const LevelButtonScn := preload("res://levels/chapter_menu/level_menu/level_button/level_button.tscn") +const BaseLevelScn := preload("res://levels/base_level/base_level.tscn") +const CreditsScn := preload("res://objects/credits/credits.tscn") # Called when the node enters the scene tree for the first time. @@ -19,21 +19,21 @@ const button_heigth : int = 50 func init(chap_id : int, lvl_num : int) -> void: #print("init?") - if chap_id == len(BaseLevel.instantiate().DATA): - add_child(Credits.instantiate()) + if chap_id == len(BaseLevelScn.instantiate().DATA): + add_child(CreditsScn.instantiate()) $Title.set_text("") return if lvl_num == -1: - lvl_num = len(BaseLevel.instantiate().DATA[chap_id]) + lvl_num = len(BaseLevelScn.instantiate().DATA[chap_id]) $Title.set_text("Ch." + I_NUMBER[chap_id] + " " + CHAP_NAMES[chap_id]) chapter_id = chap_id for level_id in range(0, lvl_num): #print(level_id) - var button = LevelButton.instantiate(); + var button = LevelButtonScn.instantiate(); var x : int = button_width * (level_id % 7) + 60 var y : int = button_heigth * (level_id / 7) + 100 button.init(chapter_id, level_id, Vector2(x, y), 1) @@ -50,7 +50,7 @@ func _ready(): func _on_button_enter_level(chap_id: int, lvl_id: int) -> void: - var base_level := BaseLevel.instantiate() + var base_level := BaseLevelScn.instantiate() # print(chap_id, lvl_id) @@ -64,8 +64,8 @@ func _input(event: InputEvent): _on_back_button_pressed() func _on_back_button_pressed(): - var ChapterMenu = load("res://levels/chapter_menu/chapter_menu.tscn") - var chapter_menu = ChapterMenu.instantiate() + var ChapterMenuScn = load("res://levels/chapter_menu/chapter_menu.tscn") + var chapter_menu = ChapterMenuScn.instantiate() chapter_menu.init() get_tree().root.add_child(chapter_menu) diff --git a/objects/card_base/card_base.gd b/objects/card_base/card_base.gd index 5cf3b4e..24b8580 100644 --- a/objects/card_base/card_base.gd +++ b/objects/card_base/card_base.gd @@ -2,7 +2,7 @@ extends Area2D class_name CardBase -const Card := preload("res://objects/card/card.tscn") +const CardScn := preload("res://objects/card/card.tscn") const FADE_MOVE_AMOUNT := 70 signal card_put @@ -42,7 +42,7 @@ func draw_card(): set_card_count(card_count - 1) $SFXPickUp.play() Input.set_default_cursor_shape(Input.CURSOR_DRAG) - new_card_node = Card.instantiate() + new_card_node = CardScn.instantiate() new_card_node.set_word($Word.get_word()) new_card_node.set_position(position) $Cards.add_child(new_card_node) diff --git a/objects/main_menu/main_menu.gd b/objects/main_menu/main_menu.gd index d88730c..3102de5 100644 --- a/objects/main_menu/main_menu.gd +++ b/objects/main_menu/main_menu.gd @@ -1,7 +1,9 @@ extends Node2D -const ChapterMenu = preload("res://levels/chapter_menu/chapter_menu.tscn") -const BaseLevel := preload("res://levels/base_level/base_level.tscn") +class_name MainMenu + +const ChapterMenuScn := preload("res://levels/chapter_menu/chapter_menu.tscn") +const BaseLevelScn := preload("res://levels/base_level/base_level.tscn") signal enter_level() @@ -10,7 +12,7 @@ func _ready(): func _on_start_button_pressed(): - var base_level := BaseLevel.instantiate() + var base_level := BaseLevelScn.instantiate() # print(chap_id, lvl_id) @@ -18,7 +20,7 @@ func _on_start_button_pressed(): get_tree().root.add_child(base_level) enter_level.emit() queue_free() - #get_tree().root.add_child(ChapterMenu.instantiate()) + #get_tree().root.add_child(ChapterMenuScn.instantiate()) #queue_free()