feat: 增加player从广场到房间互相跳转的动作逻辑

This commit is contained in:
2026-01-10 00:44:01 +08:00
parent 4bfe49d5b1
commit 6a3823ad16
7 changed files with 98 additions and 16 deletions

View File

@@ -36,6 +36,7 @@ signal scene_change_started(scene_name: String)
# 场景状态
var current_scene_name: String = "" # 当前场景名称
var is_changing_scene: bool = false # 是否正在切换场景
var _next_scene_position: Variant = null # 下一个场景的初始位置 (Vector2 or null)
# 场景路径映射表
# 将场景名称映射到实际的文件路径
@@ -47,7 +48,9 @@ var scene_paths: Dictionary = {
"battle": "res://scenes/maps/battle_scene.tscn", # 战斗场景 - 战斗系统
"inventory": "res://scenes/ui/InventoryWindow.tscn", # 背包界面
"shop": "res://scenes/ui/ShopWindow.tscn", # 商店界面
"settings": "res://scenes/ui/SettingsWindow.tscn" # 设置界面
"settings": "res://scenes/ui/SettingsWindow.tscn", # 设置界面
"room": "res://scenes/Maps/room.tscn", # 房间场景
"square": "res://scenes/Maps/square.tscn" # 广场场景
}
# ============ 生命周期方法 ============
@@ -134,6 +137,17 @@ func change_scene(scene_name: String, use_transition: bool = true):
func get_current_scene_name() -> String:
return current_scene_name
# 设置下一个场景的初始位置
func set_next_scene_position(pos: Vector2) -> void:
_next_scene_position = pos
# 获取并清除下一个场景的初始位置
func get_next_scene_position() -> Variant:
var pos = _next_scene_position
_next_scene_position = null
return pos
# ============ 场景注册方法 ============
# 注册新场景

View File

@@ -0,0 +1,32 @@
extends Area2D
# 场景名称
@export var target_scene_name: String = "room"
@export var target_position: Vector2 = Vector2.ZERO # 目标场景的生成位置 (Vector2.ZERO 表示使用默认位置/不设置)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# 连接 body_entered 信号
body_entered.connect(_on_body_entered)
func _on_body_entered(body: Node2D) -> void:
# 检查进入的物体是否为玩家
# 我们可以通过多种方式检查:
# 1. 检查是否在 "player" 组中 (推荐,但需要设置)
# 2. 检查是否有特定方法 (如 _handle_movement)
# 3. 检查类名 (如果 scripts/characters/player.gd 有 class_name)
# 这里使用方法检查作为一种鲁棒的方式,或者检查是否为 CharacterBody2D 且父节点层级符合预期
# 最简单直接的方式是检查是否有 _handle_movement 方法,这是 player.gd 特有的
if body.has_method("_handle_movement"):
print("玩家进入传送门,正在切换到场景: ", target_scene_name)
_teleport_player()
func _teleport_player() -> void:
# 如果设置了目标位置,则传递给 SceneManager
if target_position != Vector2.ZERO:
SceneManager.set_next_scene_position(target_position)
# 使用 SceneManager 切换场景
# 确保 SceneManager 已经注册了相关场景路径
SceneManager.change_scene(target_scene_name)

View File

@@ -0,0 +1 @@
uid://3wghcufucve5

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=40 format=4 uid="uid://5cc0c6cpnhe8"]
[gd_scene load_steps=42 format=4 uid="uid://5cc0c6cpnhe8"]
[ext_resource type="Texture2D" uid="uid://baa5wkuyqouh6" path="res://assets/sprites/environment/standard_brick_128_128.jpg" id="1_rb5kq"]
[ext_resource type="Texture2D" uid="uid://dwlnclqw6lsa7" path="res://assets/sprites/environment/grass_256_256.png" id="2_dly5q"]
@@ -17,6 +17,7 @@
[ext_resource type="Texture2D" uid="uid://d3w3fncsm32oi" path="res://assets/sprites/environment/deck_384_167.png" id="13_tct6u"]
[ext_resource type="Texture2D" uid="uid://j0twhfkpj15i" path="res://assets/sprites/environment/deck_512_164.png" id="14_m4als"]
[ext_resource type="Texture2D" uid="uid://blre1srim52hs" path="res://assets/sprites/environment/deck_512_282.png" id="15_2rqka"]
[ext_resource type="Script" uid="uid://3wghcufucve5" path="res://scenes/Maps/door_teleport.gd" id="18_0xqio"]
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_7nixu"]
texture = ExtResource("1_rb5kq")
@@ -1380,6 +1381,9 @@ texture_region_size = Vector2i(32, 32)
tile_size = Vector2i(32, 32)
sources/0 = SubResource("TileSetAtlasSource_rixdf")
[sub_resource type="RectangleShape2D" id="RectangleShape2D_edt5w"]
size = Vector2(66, 56)
[node name="square" type="Node2D"]
[node name="ground" type="TileMapLayer" parent="."]
@@ -1414,3 +1418,10 @@ tile_map_data = PackedByteArray("AAAOAO//AAAAAAAAAAA=")
tile_set = SubResource("TileSet_u1t8b")
[node name="Player" parent="." instance=ExtResource("10_ho5ay")]
[node name="DoorArea" type="Area2D" parent="."]
script = ExtResource("18_0xqio")
[node name="CollisionShape2D" type="CollisionShape2D" parent="DoorArea"]
position = Vector2(647, -52)
shape = SubResource("RectangleShape2D_edt5w")

View File

@@ -13,10 +13,18 @@ const MOVE_SPEED = 200.0
var last_direction := "down"
func _ready() -> void:
# 检查是否有初始位置设置
call_deferred("_check_spawn_position")
# 播放初始动画
if animation_player.has_animation("idle"):
animation_player.play("idle")
func _check_spawn_position() -> void:
var spawn_pos = SceneManager.get_next_scene_position()
if spawn_pos != null:
global_position = spawn_pos
func _physics_process(delta: float) -> void:
_handle_movement(delta)

View File

@@ -1,10 +1,10 @@
[gd_scene load_steps=13 format=3 uid="uid://b2f8e24plwqgj"]
[ext_resource type="Script" uid="uid://btka26hrcvgen" path="res://Scenes/characters/player.gd" id="1_script"]
[ext_resource type="Texture2D" uid="uid://dpym0k5vurobw" path="res://assets/characters/player_spritesheet.png" id="2_texture"]
[ext_resource type="Texture2D" uid="uid://cghab1hkx5lg5" path="res://assets/characters/player_spritesheet.png" id="2_texture"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_1"]
radius = 16.0
radius = 21.0
height = 48.0
[sub_resource type="Animation" id="Animation_idle_down"]
@@ -145,14 +145,14 @@ tracks/0/keys = {
[sub_resource type="AnimationLibrary" id="AnimationLibrary_1"]
_data = {
"idle_down": SubResource("Animation_idle_down"),
"idle_left": SubResource("Animation_idle_left"),
"idle_right": SubResource("Animation_idle_right"),
"idle_up": SubResource("Animation_idle_up"),
"walk_down": SubResource("Animation_walk_down"),
"walk_left": SubResource("Animation_walk_left"),
"walk_right": SubResource("Animation_walk_right"),
"walk_up": SubResource("Animation_walk_up")
&"idle_down": SubResource("Animation_idle_down"),
&"idle_left": SubResource("Animation_idle_left"),
&"idle_right": SubResource("Animation_idle_right"),
&"idle_up": SubResource("Animation_idle_up"),
&"walk_down": SubResource("Animation_walk_down"),
&"walk_left": SubResource("Animation_walk_left"),
&"walk_right": SubResource("Animation_walk_right"),
&"walk_up": SubResource("Animation_walk_up")
}
[node name="Player" type="CharacterBody2D"]
@@ -160,18 +160,17 @@ script = ExtResource("1_script")
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(1.5000005, -24.5)
scale = Vector2(1, 1)
texture = ExtResource("2_texture")
hframes = 4
vframes = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, -24)
position = Vector2(2, -24)
shape = SubResource("CapsuleShape2D_1")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_1")
&"": SubResource("AnimationLibrary_1")
}
[node name="Camera2D" type="Camera2D" parent="."]