class_name CPUVFXTemplate extends CPUParticles2D ## ========================= ## 播放模式 ## ========================= enum PlayMode { ##播放一次 ONLY_ONCE, ##永久播放 INFINITE, ##播放固定次数 FIXED_TIMES } ##可选择的播放模式 @export var play_mode: PlayMode = PlayMode.ONLY_ONCE ## 仅在 FIXED_TIMES 模式下生效 @export var fixed_times: int = 1 ## ========================= ## 内部状态 ## ========================= var _played_times: int = 0 var _life_timer: float = 0.0 var _playing: bool = false ## ========================= ## 生命周期 ## ========================= func _ready() -> void: _play() func _process(delta: float) -> void: if not _playing: return match play_mode: PlayMode.ONLY_ONCE: _update_only_once(delta) PlayMode.FIXED_TIMES: _update_fixed_times(delta) PlayMode.INFINITE: pass # 什么都不用做 ## ========================= ## 播放控制 ## ========================= func _play() -> void: emitting = true _playing = true _life_timer = 0.0 _played_times = 0 func stop_and_free() -> void: emitting = false queue_free() ## ========================= ## 模式逻辑 ## ========================= func _update_only_once(delta: float) -> void: _life_timer += delta if _life_timer >= lifetime: stop_and_free() func _update_fixed_times(delta: float) -> void: _life_timer += delta if _life_timer >= lifetime: _played_times += 1 _life_timer = 0.0 if _played_times >= fixed_times: stop_and_free()