添加关卡管理器的新功能

This commit is contained in:
Reed 2026-01-07 18:09:57 +08:00
parent 6383432bbe
commit 5b95aee5d8
14 changed files with 112 additions and 63 deletions

View File

@ -1,24 +1,24 @@
[gd_scene load_steps=7 format=3 uid="uid://3vc8ojbiyy5w"]
[ext_resource type="Script" uid="uid://crgac4manhoud" path="res://_game/game.gd" id="1_5cwyw"]
[ext_resource type="PackedScene" uid="uid://cvqehvdjpoar4" path="res://_player/player_controller.tscn" id="2_4ake0"]
[ext_resource type="PackedScene" uid="uid://gwhff4qaouxy" path="res://_player/Avatar.tscn" id="3_vb6ml"]
[ext_resource type="PackedScene" uid="uid://cd88ydqhdo28" path="res://_scene/level1/l1_s1.tscn" id="4_vb6ml"]
[ext_resource type="PackedScene" uid="uid://djs1eg5y008cs" path="res://_scene/level1/l1_s2.tscn" id="5_a1vfv"]
[ext_resource type="PackedScene" uid="uid://dh43kt0l28qd5" path="res://_scene/level1/l1_s3.tscn" id="6_w6pya"]
[ext_resource type="Script" uid="uid://crgac4manhoud" path="res://_game/game.gd" id="1_yksyv"]
[ext_resource type="PackedScene" uid="uid://cvqehvdjpoar4" path="res://_player/player_controller.tscn" id="2_x2i0j"]
[ext_resource type="PackedScene" uid="uid://gwhff4qaouxy" path="res://_player/Avatar.tscn" id="3_4ifj7"]
[ext_resource type="PackedScene" uid="uid://cd88ydqhdo28" path="res://_scene/level1/l1_s1.tscn" id="4_m1t3p"]
[ext_resource type="PackedScene" uid="uid://djs1eg5y008cs" path="res://_scene/level1/l1_s2.tscn" id="5_5s0xe"]
[ext_resource type="PackedScene" uid="uid://dh43kt0l28qd5" path="res://_scene/level1/l1_s3.tscn" id="6_ktxjv"]
[node name="Game" type="Node2D"]
script = ExtResource("1_5cwyw")
script = ExtResource("1_yksyv")
[node name="PlayerController" parent="." node_paths=PackedStringArray("auto_controlled_avatar") instance=ExtResource("2_4ake0")]
[node name="PlayerController" parent="." node_paths=PackedStringArray("auto_controlled_avatar") instance=ExtResource("2_x2i0j")]
auto_controlled_avatar = NodePath("../Avatar")
[node name="Avatar" parent="." instance=ExtResource("3_vb6ml")]
[node name="Avatar" parent="." instance=ExtResource("3_4ifj7")]
position = Vector2(-330, 0)
collision_mask = 4
[node name="L1_S1" parent="." instance=ExtResource("4_vb6ml")]
[node name="L1_S1" parent="." instance=ExtResource("4_m1t3p")]
[node name="L1_S2" parent="." instance=ExtResource("5_a1vfv")]
[node name="L1_S2" parent="." instance=ExtResource("5_5s0xe")]
[node name="L1_S3" parent="." instance=ExtResource("6_w6pya")]
[node name="L1_S3" parent="." instance=ExtResource("6_ktxjv")]

View File

@ -1,6 +1 @@
extends SceneManager
func _on_player_trigger_volumn_player_entered(body: CharacterBody2D) -> void:
_act_manager.switch_act_with_id(1)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,11 @@
[gd_resource type="Resource" script_class="PropIDSceneTrigger" load_steps=3 format=3 uid="uid://cstjpy2eh8mck"]
[ext_resource type="Script" uid="uid://baamspwt4rm4r" path="res://addons/reedscene/scene/guard.gd" id="1_ugibh"]
[ext_resource type="Script" uid="uid://bprxx881tmhn8" path="res://addons/reedscene/scene/PropIDSceneTrigger.gd" id="2_501i5"]
[resource]
script = ExtResource("2_501i5")
prop_id = 1
target_act_id = 1
monitor_signal = &"player_entered"
metadata/_custom_type_script = "uid://bprxx881tmhn8"

View File

@ -32,16 +32,19 @@ func _ready() -> void:
pass
##通过ID切换act
func switch_act_with_id(act_id: int) -> void:
## 如果trans overwrite = 0则使用act上自己配置的use trans
## 如果trans overwrite = 1则全部禁用use trans
## 如果trans overwrite = 2则全部开启use trans注意如果没有trans则还是正常的切换
func switch_act_with_id(act_id: int,trans_overwrite: int = 0) -> void:
if not prop_state_map.has(act_id):
push_warning("[ActManager] Act id not found: %d" % act_id)
return
var act := prop_state_map[act_id]
_switch_act_internal(act, act_id)
_switch_act_internal(act, act_id,trans_overwrite)
##内部通过ID和Act来切换状态
func _switch_act_internal(act: Act, act_id: int) -> void:
func _switch_act_internal(act: Act, act_id: int, trans_overwrite: int = 0) -> void:
if act == null:
return
@ -59,7 +62,13 @@ func _switch_act_internal(act: Act, act_id: int) -> void:
if prop == null:
continue
prop.change_state(single.state_id, single.use_trans)
var use_trans: bool = false
match trans_overwrite:
0: use_trans = single.use_trans
1: use_trans = false
2: use_trans = true
prop.change_state(single.state_id, use_trans)
emit_signal("act_changed", from_id, act_id)

View File

@ -0,0 +1,3 @@
class_name PropIDSceneTrigger extends SceneTrigger
@export var prop_id: int

View File

@ -0,0 +1 @@
uid://bprxx881tmhn8

View File

@ -1,6 +1,7 @@
class_name SceneManager
extends Node
@export var quick_trigger: Array[SceneTrigger]
## ==============================
## References
## ==============================
@ -8,9 +9,6 @@ var _scene: ReedScene
var _act_manager: ActManager
var _props: Dictionary # prop_id -> PropComponent
## ==============================
## Lifecycle
## ==============================
func _ready() -> void:
await get_parent().ready
@ -20,11 +18,9 @@ func _ready() -> void:
return
_resolve_references()
_bind_signals()
## ==============================
## Resolve References
## ==============================
_bind_quick_trigger()
func _resolve_scene() -> void:
var p := get_parent()
if p is ReedScene:
@ -33,36 +29,39 @@ func _resolve_scene() -> void:
_scene = null
func _resolve_references() -> void:
# 从 ReedScene 拿“整理好的结果”
_act_manager = _scene._act_manager
_props = _scene._prop_map
func _bind_quick_trigger() -> void:
for qt in quick_trigger:
if qt is PropIDSceneTrigger:
_bind_prop_id_trigger(qt)
## ==============================
## Signal Binding
## ==============================
func _bind_prop_id_trigger(trigger: PropIDSceneTrigger) -> void:
var prop : Node = _props.get(trigger.prop_id).get_parent()
if prop == null:
return
func _bind_signals() -> void:
if _act_manager:
_act_manager.act_changed.connect(_on_act_changed)
var s: StringName = trigger.monitor_signal
if not prop.has_signal(s):
push_error("Prop has no signal: %s" % trigger.monitor_signal)
return
#for prop_id in _props:
#var prop : PropComponent = _props[prop_id]
#_bind_prop_signals(prop)
#
#func _bind_prop_signals(prop: PropComponent) -> void:
## 示例:监听 prop 的自定义信号
#if prop.has_signal("activated"):
#prop.activated.connect(func():
#_on_prop_activated(prop)
#)
prop.connect(
trigger.monitor_signal,
func(...args):
_on_trigger_fired(trigger, args)
)
func _on_trigger_fired(trigger: SceneTrigger, signal_args: Array) -> void:
if not trigger.can_trigger(signal_args):
return
## ==============================
## Callbacks
## ==============================
func _on_act_changed(from_act: int, to_act: int) -> void:
print("[SceneManager] Act changed:", from_act, "->", to_act)
var target_act_id: int = trigger.target_act_id
var trans_overwrite: int = trigger.trans_overwrite
if target_act_id < 0:
return
_act_manager.switch_act_with_id(target_act_id,trans_overwrite)
func _on_prop_activated(prop: PropComponent) -> void:
print("[SceneManager] Prop activated:", prop.prop_id)

View File

@ -0,0 +1,14 @@
@abstract
class_name SceneTrigger extends Resource
@export var target_act_id: int
@export var monitor_signal: StringName
@export var guards: Array[SceneGuard]
@export var trans_overwrite: int = 0
func can_trigger(args: Array) -> bool:
for g in guards:
if not g.check(args):
return false
return true

View File

@ -0,0 +1 @@
uid://ons77en82uls

View File

@ -0,0 +1,9 @@
'''
SceneTrigger的Resource
'''
@abstract
class_name SceneGuard
extends Resource
@abstract
func check(signal_args: Array) -> bool

View File

@ -0,0 +1 @@
uid://baamspwt4rm4r