/
ulupapi
/
Race_game
Обзор
Документация
Войти
/
ulupapi
/
Race_game
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
scripts/RaceManager.gd
135 строк
4 KB
ulupapi
Game
26 дек 2025, 19:34
26 дек 2025, 19:34
7574b6d
Код
Авторство
О чём код?
# res://scripts/race/RaceManager.gd extends Node class_name RaceManager @export var lap_timer_path: NodePath @export var player_car_path: NodePath @export var total_laps: int = 3 @export var pause_on_finish: bool = true @export var show_finish_overlay: bool = true var _timer: Node = null var _player: Node = null var _race_over: bool = false func _ready() -> void: # Найти таймер и машину if lap_timer_path != NodePath(""): _timer = get_node_or_null(lap_timer_path) if player_car_path != NodePath(""): _player = get_node_or_null(player_car_path) if _timer == null or _player == null: push_error("[RaceManager] Укажи lap_timer_path и player_car_path в инспекторе.") return # Протолкнуть целевое число кругов в таймер (если есть сеттер) if total_laps > 0: if _timer.has_method("set_laps_total"): _timer.call("set_laps_total", total_laps) elif "laps_total" in _timer: _timer.laps_total = total_laps # Подписаться на события таймера if _timer.has_signal("lap_completed"): _timer.connect("lap_completed", Callable(self, "_on_lap_completed")) if _timer.has_signal("lap_started"): _timer.connect("lap_started", Callable(self, "_on_lap_started")) print("[RaceManager] READY | total_laps=", total_laps) func _on_lap_started(lap_index: int) -> void: # здесь можно обновлять HUD или делать логику старта круга pass func _on_lap_completed(lap_index: int, lap_time_s: float) -> void: if _race_over: return if total_laps <= 0: return # Игрок завершил нужное число кругов → финиш if lap_index >= total_laps: _race_over = true _show_finish(1) # Пока считаем, что ты финишировал P1. Позже добавим соперников. if pause_on_finish: get_tree().paused = true # останавливаем игру # -------------------- ОВЕРЛЕЙ ФИНИША -------------------- func _show_finish(place: int) -> void: if not show_finish_overlay: return var root: Window = get_tree().root var layer: CanvasLayer = CanvasLayer.new() layer.name = "FinishOverlay" layer.layer = 99 # Чтобы показываться даже когда paused = true layer.process_mode = Node.PROCESS_MODE_ALWAYS root.add_child(layer) var dim: ColorRect = ColorRect.new() dim.color = Color(0, 0, 0, 0.6) dim.anchor_left = 0.0 dim.anchor_top = 0.0 dim.anchor_right = 1.0 dim.anchor_bottom = 1.0 dim.offset_left = 0.0 dim.offset_top = 0.0 dim.offset_right = 0.0 dim.offset_bottom = 0.0 layer.add_child(dim) var panel: PanelContainer = PanelContainer.new() panel.anchor_left = 0.5 panel.anchor_top = 0.5 panel.anchor_right = 0.5 panel.anchor_bottom = 0.5 panel.offset_left = -220.0 panel.offset_right = 220.0 panel.offset_top = -110.0 panel.offset_bottom = 110.0 dim.add_child(panel) var pad: MarginContainer = MarginContainer.new() pad.add_theme_constant_override("margin_left", 16) pad.add_theme_constant_override("margin_right", 16) pad.add_theme_constant_override("margin_top", 16) pad.add_theme_constant_override("margin_bottom", 16) panel.add_child(pad) var v: VBoxContainer = VBoxContainer.new() v.alignment = BoxContainer.ALIGNMENT_CENTER v.size_flags_horizontal = Control.SIZE_EXPAND_FILL v.size_flags_vertical = Control.SIZE_EXPAND_FILL pad.add_child(v) var lbl_title: Label = Label.new() lbl_title.text = "FINISH" lbl_title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER v.add_child(lbl_title) var lbl_place: Label = Label.new() lbl_place.text = "Place: " + str(place) lbl_place.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER v.add_child(lbl_place) if total_laps > 0: var lbl_laps: Label = Label.new() lbl_laps.text = "Laps: " + str(total_laps) lbl_laps.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER v.add_child(lbl_laps) var btn: Button = Button.new() btn.text = "OK" v.add_child(btn) btn.pressed.connect(Callable(self, "_on_finish_ok_pressed").bind(layer)) func _on_finish_ok_pressed(layer: CanvasLayer) -> void: if get_tree().paused: get_tree().paused = false if is_instance_valid(layer): layer.queue_free()