Move all C# scripts from flat structure to organized folders: - Core/ for game logic (Game, GameState, GameSettings) - UI/Menus/ and UI/Dialogs/ for user interface - Systems/ for reusable systems (Save, Localization) - Data/ for data models and configuration - Added framework for future: Gameplay/, Story/, Modding/ Update all .tscn scene files to reference new script paths. Fix timing issue in AdvancedSaveDialog focus handling.
143 lines
4.4 KiB
C#
143 lines
4.4 KiB
C#
using Godot;
|
|
|
|
namespace TheGame
|
|
{
|
|
public partial class Game : Control
|
|
{
|
|
private Label _timerLabel;
|
|
private Control _pauseMenu;
|
|
private Label _gameContentLabel;
|
|
private float _gameTime = 0.0f;
|
|
private bool _isPaused = false;
|
|
private ExitConfirmationDialog _exitDialog;
|
|
|
|
public override void _Ready()
|
|
{
|
|
// Lägg till i localized_ui gruppen
|
|
AddToGroup("localized_ui");
|
|
|
|
_timerLabel = GetNode<Label>("UI/TimerLabel");
|
|
_pauseMenu = GetNode<Control>("PauseMenu");
|
|
_gameContentLabel = GetNode<Label>("GameContent");
|
|
|
|
// Sätt process mode så att pausmenyn fungerar när spelet är pausat
|
|
_pauseMenu.ProcessMode = ProcessModeEnum.WhenPaused;
|
|
|
|
// Kontrollera om vi laddar ett sparat spel eller startar nytt
|
|
if (GameState.LoadedGameTime >= 0)
|
|
{
|
|
// Laddar befintligt spel
|
|
_gameTime = GameState.LoadedGameTime;
|
|
GameState.LastSavedGameTime = _gameTime; // Sätt senaste sparade tid
|
|
GameState.LoadedGameTime = -1; // Reset efter användning
|
|
GD.Print($"Loaded existing game with seed: {GameState.CurrentGameSeed?.ToString() ?? "None"}");
|
|
}
|
|
else
|
|
{
|
|
// Nytt spel - skapa ny seed om ingen finns
|
|
if (GameState.CurrentGameSeed == null)
|
|
{
|
|
GameState.CurrentGameSeed = new GameSeed();
|
|
GD.Print($"Started new game with seed: {GameState.CurrentGameSeed.ToString()}");
|
|
}
|
|
GameState.LastSavedGameTime = 0;
|
|
}
|
|
|
|
// Skapa exit confirmation dialog
|
|
_exitDialog = new ExitConfirmationDialog();
|
|
AddChild(_exitDialog);
|
|
|
|
// Hantera fönsterstängning
|
|
GetTree().AutoAcceptQuit = false;
|
|
|
|
UpdateLocalization();
|
|
}
|
|
|
|
public void UpdateLocalization()
|
|
{
|
|
var loc = LocalizationManager.Instance;
|
|
_gameContentLabel.Text = loc.GetText("game_content");
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (!_isPaused)
|
|
{
|
|
_gameTime += (float)delta;
|
|
UpdateTimerDisplay();
|
|
|
|
// Kontrollera om det finns osparad progress
|
|
GameState.CheckUnsavedProgress(_gameTime);
|
|
}
|
|
}
|
|
|
|
public override void _Notification(int what)
|
|
{
|
|
if (what == NotificationWMCloseRequest)
|
|
{
|
|
// Hantera X-knappen på fönstret
|
|
HandleExitRequest(() => GetTree().Quit());
|
|
}
|
|
}
|
|
|
|
public void HandleExitRequest(System.Action exitAction)
|
|
{
|
|
if (GameState.HasUnsavedProgress)
|
|
{
|
|
// Pausa spelet och visa varning
|
|
if (!_isPaused)
|
|
{
|
|
TogglePause();
|
|
}
|
|
_exitDialog.ShowDialog(exitAction);
|
|
}
|
|
else
|
|
{
|
|
// Ingen osparad progress, avsluta direkt
|
|
exitAction?.Invoke();
|
|
}
|
|
}
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (@event.IsActionPressed("ui_cancel")) // ESC key
|
|
{
|
|
TogglePause();
|
|
}
|
|
}
|
|
|
|
private void UpdateTimerDisplay()
|
|
{
|
|
int hours = (int)(_gameTime / 3600);
|
|
int minutes = (int)((_gameTime % 3600) / 60);
|
|
int seconds = (int)(_gameTime % 60);
|
|
|
|
_timerLabel.Text = $"{hours:D2}:{minutes:D2}:{seconds:D2}";
|
|
}
|
|
|
|
public void TogglePause()
|
|
{
|
|
_isPaused = !_isPaused;
|
|
_pauseMenu.Visible = _isPaused;
|
|
|
|
if (_isPaused)
|
|
{
|
|
GetTree().Paused = true;
|
|
}
|
|
else
|
|
{
|
|
GetTree().Paused = false;
|
|
}
|
|
}
|
|
|
|
private void _on_pause_button_pressed()
|
|
{
|
|
TogglePause();
|
|
}
|
|
|
|
public float GetGameTime()
|
|
{
|
|
return _gameTime;
|
|
}
|
|
}
|
|
} |