Skip to content

Commit

Permalink
feat(level_menu)!: 修改场景结构,添加选择章节功能
Browse files Browse the repository at this point in the history
LevelMenu 的场景结构发生变化:

- LevelMenu
  - LevelMenuCamera # 相机
    - SmoothMovement
  - LevelButtons # 选关按钮
  - UI
    - PreviousLevelButton
    - NextLevelButton
    - Title

初始化时,会生成所有的选关按钮。

按屏幕左右的按钮(PreviousLevelButton 或 NextLevelButton)可以移动
相机,从而实现切换章节的功能。

BREAKING CHANGE: LevelMenu 的场景结构发生变化。
  • Loading branch information
cutekibry committed Feb 18, 2024
1 parent 03871f2 commit b026348
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 66 deletions.
71 changes: 35 additions & 36 deletions levels/chapter_menu/level_menu/level_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,30 @@ const LevelButtonScn := preload("res://levels/chapter_menu/level_menu/level_butt
const BaseLevelScn := preload("res://levels/base_level/base_level.tscn")
const CreditsScn := preload("res://objects/credits/credits.tscn")

@export var chapter_id : int = 0
const WIDTH := 1920 / 4

var chapter_id : int = 0
const button_width : int = 50
const button_heigth : int = 50

func init(chap_id : int, lvl_num : int) -> void:
if chap_id == LevelData.get_chapter_count():
add_child(CreditsScn.instantiate())
$Title.set_text("")
return


if lvl_num == -1:
lvl_num = LevelData.get_chapter_level_count(chap_id)

$Title.set_text(LevelData.CHAP_NAMES[chap_id]["name-en"])

chapter_id = chap_id
for level_id in range(0, lvl_num):
#print(level_id)
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)
button.enter_level.connect(_on_button_enter_level)
add_child(button)
func init(chap_id: int) -> void:
self.chapter_id = chap_id
$UI/Title.text = LevelData.CHAP_NAMES[chapter_id]
$LevelMenuCamera.init_position(Vector2(WIDTH * chap_id, 0))

func _ready():
$BackButton/icon.play("return")
pass

for cid in range(LevelData.get_chapter_count()):
for lid in range(LevelData.get_chapter_level_count(cid)):
var button = LevelButtonScn.instantiate();
var x := WIDTH * cid + button_width * (lid % 7) + 60
var y := button_heigth * (lid / 7) + 100
button.init(cid, lid, Vector2(x, y), 1)
button.enter_level.connect(_on_button_enter_level)
$LevelButtons.add_child(button)

$UI/PreviousChapterButton.set_disabled(chapter_id == 0)
$UI/NextChapterButton.set_disabled(chapter_id == LevelData.get_chapter_count() - 1)


func _on_button_enter_level(chap_id: int, lvl_id: int) -> void:
var base_level := BaseLevelScn.instantiate()
Expand All @@ -46,15 +40,20 @@ func _on_button_enter_level(chap_id: int, lvl_id: int) -> void:
get_tree().root.add_child(base_level)
queue_free()

func _input(event: InputEvent):
if event is InputEventKey:
if event.keycode == KEY_ESCAPE and event.pressed:
_on_back_button_pressed()

func _on_back_button_pressed():
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)
queue_free()
func _on_previous_chapter_button_pressed():
self.chapter_id -= 1
$UI/Title.text = LevelData.CHAP_NAMES[chapter_id]
$UI/PreviousChapterButton.set_disabled(true)
$UI/NextChapterButton.set_disabled(true)


func _on_next_chapter_button_pressed():
self.chapter_id += 1
$UI/Title.text = LevelData.CHAP_NAMES[chapter_id]
$UI/PreviousChapterButton.set_disabled(true)
$UI/NextChapterButton.set_disabled(true)

func _on_smooth_movement_timeout():
$UI/PreviousChapterButton.set_disabled(chapter_id == 0)
$UI/NextChapterButton.set_disabled(chapter_id == LevelData.get_chapter_count() - 1)
59 changes: 29 additions & 30 deletions levels/chapter_menu/level_menu/level_menu.tscn
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
[gd_scene load_steps=8 format=3 uid="uid://doirpa1bwbjhr"]
[gd_scene load_steps=6 format=3 uid="uid://doirpa1bwbjhr"]

[ext_resource type="Script" path="res://levels/chapter_menu/level_menu/level_menu.gd" id="1_sd65g"]
[ext_resource type="PackedScene" uid="uid://bepyyenjtc0p3" path="res://levels/chapter_menu/level_menu/level_button/level_button.tscn" id="2_saprs"]
[ext_resource type="Texture2D" uid="uid://bxda1ilvqwc6f" path="res://objects/styled_button/return1.png" id="3_6x831"]
[ext_resource type="Script" path="res://levels/chapter_menu/level_menu/level_menu_camera.gd" id="2_4o5wo"]
[ext_resource type="PackedScene" uid="uid://drl1xe103umpi" path="res://objects/styled_button/styled_button.tscn" id="2_66j1c"]
[ext_resource type="PackedScene" uid="uid://fxcs5dmb21q2" path="res://scripts/smooth_movement/smooth_movement.tscn" id="3_1jj52"]
[ext_resource type="FontFile" uid="uid://1000owdyvwfg" path="res://fonts/unifont-15.1.04.otf" id="3_jlnpm"]
[ext_resource type="Texture2D" uid="uid://cyl7cqhs4xnux" path="res://objects/styled_button/return2.png" id="4_wd0qw"]
[ext_resource type="Texture2D" uid="uid://b48fn2ikxq33d" path="res://objects/styled_button/return3.png" id="5_eexmd"]

[sub_resource type="SpriteFrames" id="SpriteFrames_waq31"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": ExtResource("3_6x831")
}, {
"duration": 1.0,
"texture": ExtResource("4_wd0qw")
}, {
"duration": 1.0,
"texture": ExtResource("5_eexmd")
}],
"loop": true,
"name": &"return",
"speed": 5.0
}]

[node name="LevelMenu" type="Node2D"]
script = ExtResource("1_sd65g")

[node name="BackButton" parent="." instance=ExtResource("2_saprs")]
[node name="LevelMenuCamera" type="Camera2D" parent="."]
anchor_mode = 0
script = ExtResource("2_4o5wo")

[node name="icon" type="AnimatedSprite2D" parent="BackButton"]
position = Vector2(5, 5)
sprite_frames = SubResource("SpriteFrames_waq31")
animation = &"return"
centered = false
[node name="SmoothMovement" parent="LevelMenuCamera" instance=ExtResource("3_1jj52")]
smooth_type = 1

[node name="Title" type="Label" parent="."]
[node name="LevelButtons" type="Node" parent="."]

[node name="UI" type="CanvasLayer" parent="."]

[node name="PreviousChapterButton" parent="UI" instance=ExtResource("2_66j1c")]
offset_top = 118.0
offset_bottom = 152.0

[node name="NextChapterButton" parent="UI" instance=ExtResource("2_66j1c")]
offset_left = 446.0
offset_top = 118.0
offset_right = 480.0
offset_bottom = 152.0

[node name="Title" type="Label" parent="UI"]
offset_top = 8.0
offset_right = 480.0
offset_bottom = 31.0
Expand All @@ -44,4 +39,8 @@ text = "Title"
horizontal_alignment = 1
vertical_alignment = 1

[connection signal="pressed" from="BackButton" to="." method="_on_back_button_pressed"]
[connection signal="timeout" from="LevelMenuCamera/SmoothMovement" to="." method="_on_smooth_movement_timeout"]
[connection signal="pressed" from="UI/PreviousChapterButton" to="." method="_on_previous_chapter_button_pressed"]
[connection signal="pressed" from="UI/PreviousChapterButton" to="LevelMenuCamera" method="_on_previous_chapter_button_pressed"]
[connection signal="pressed" from="UI/NextChapterButton" to="." method="_on_next_chapter_button_pressed"]
[connection signal="pressed" from="UI/NextChapterButton" to="LevelMenuCamera" method="_on_next_chapter_button_pressed"]
22 changes: 22 additions & 0 deletions levels/chapter_menu/level_menu/level_menu_camera.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends Camera2D


const WIDTH := 1920 / 4
const HEIGHT := 1080 / 4
const MOVE_TIME := 0.3


func _process(_delta):
self.position = $SmoothMovement.get_position()
# print($SmoothMovement.get_position())

func init_position(pos: Vector2) -> void:
$SmoothMovement.set_position(pos)


func _on_next_chapter_button_pressed():
$SmoothMovement.move_to($SmoothMovement.get_position() + Vector2(WIDTH, 0), MOVE_TIME)


func _on_previous_chapter_button_pressed():
$SmoothMovement.move_to($SmoothMovement.get_position() - Vector2(WIDTH, 0), MOVE_TIME)

0 comments on commit b026348

Please sign in to comment.