ported from perforce
This commit is contained in:
182
DonkeysAndDroids.Godot/MetaGameScreen.cs
Normal file
182
DonkeysAndDroids.Godot/MetaGameScreen.cs
Normal file
@@ -0,0 +1,182 @@
|
||||
using System;
|
||||
using DonkeysAndDroids;
|
||||
using Godot;
|
||||
using RobotAndDonkey.Game;
|
||||
using RobotAndDonkey.Game.Execution.Results;
|
||||
using RobotAndDonkey.Game.Utils;
|
||||
|
||||
public partial class MetaGameScreen : Control, IScreen
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
m_SeedLineEdit = GetNode<LineEdit>("MarginContainer/MarginContainer/MainVBox/SeedRow/SeedLineEdit");
|
||||
m_RandomizeButton = GetNode<Button>("MarginContainer/MarginContainer/MainVBox/SeedRow/RandomizeButton");
|
||||
m_DifficultyOptionButton = GetNode<OptionButton>("MarginContainer/MarginContainer/MainVBox/DifficultyRow/DifficultyOptionButton");
|
||||
m_StartButton = GetNode<Button>("%StartButton");
|
||||
m_OptionsButton = GetNode<Button>("%OptionsButton");
|
||||
|
||||
m_RobotContainer = GetNode<HBoxContainer>("MarginContainer/MarginContainer/MainVBox/RobotsSection/RobotsContainer");
|
||||
foreach (var robot in Enum.GetValues<ERobotType>())
|
||||
{
|
||||
var currentRobot = robot;
|
||||
var robotInstance = (Robot)RobotScene.Instantiate<Button>();
|
||||
robotInstance.Pressed += () => OnRobotButtonPressed(currentRobot);
|
||||
robotInstance.Name = robot.ToString();
|
||||
robotInstance.Texture = robot switch
|
||||
{
|
||||
ERobotType.Vintage => ResourceLoader.Load<Texture2D>("uid://b51acya8abb3p"),
|
||||
ERobotType.Courier => ResourceLoader.Load<Texture2D>("uid://c6h1eqa6n2ca8"),
|
||||
ERobotType.Analyst => ResourceLoader.Load<Texture2D>("uid://c6h1eqa6n2ca8"),
|
||||
ERobotType.Ranger => ResourceLoader.Load<Texture2D>("uid://c6h1eqa6n2ca8"),
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
|
||||
m_RobotContainer.AddChild(robotInstance);
|
||||
}
|
||||
|
||||
m_RobotButtons = new Button[m_RobotContainer.GetChildren().Count];
|
||||
for (var i = 0; i < m_RobotButtons.Length; i++)
|
||||
{
|
||||
m_RobotButtons[i] = (Button)m_RobotContainer.GetChildren()[i];
|
||||
// JAM
|
||||
|
||||
if (i > 0)
|
||||
m_RobotButtons[i].Disabled = true;
|
||||
}
|
||||
|
||||
m_DifficultyOptionButton.Clear();
|
||||
foreach (var difficulty in Enum.GetValues<EDifficulty>())
|
||||
m_DifficultyOptionButton.AddItem(difficulty.ToString(), (int)difficulty);
|
||||
m_DifficultyOptionButton.Selected = 0;
|
||||
|
||||
m_RandomizeButton.Pressed += OnRandomizePressed;
|
||||
m_StartButton.Pressed += OnStartPressed;
|
||||
m_OptionsButton.Pressed += OnOptionsPressed;
|
||||
m_SeedLineEdit.TextChanged += OnSeedTextChanged;
|
||||
|
||||
m_StartButton.Disabled = true;
|
||||
OnRandomizePressed();
|
||||
}
|
||||
|
||||
private void OnOptionsPressed()
|
||||
{
|
||||
Main.Instance.OptionsMenu.ShowMenu();
|
||||
}
|
||||
|
||||
public override void _UnhandledKeyInput(InputEvent @event)
|
||||
{
|
||||
if (@event.IsActionPressed("options") && !m_StartButton.Disabled)
|
||||
{
|
||||
Main.Instance.OptionsMenu.ShowMenu();
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRandomizePressed()
|
||||
{
|
||||
m_Seed = m_Random.Next(int.MaxValue);
|
||||
m_SeedLineEdit.Text = SeedString.ToString(m_Seed);
|
||||
}
|
||||
|
||||
private void OnSeedTextChanged(string newText)
|
||||
{
|
||||
if (!SeedString.TryParse(newText, out m_Seed, true))
|
||||
m_Seed = 0;
|
||||
|
||||
UpdateStartButtonState();
|
||||
}
|
||||
|
||||
private void OnRobotButtonPressed(ERobotType robot)
|
||||
{
|
||||
m_SelectedRobot = robot;
|
||||
UpdateRobotButtonsVisual();
|
||||
UpdateStartButtonState();
|
||||
}
|
||||
|
||||
private void UpdateRobotButtonsVisual()
|
||||
{
|
||||
var index = 0;
|
||||
foreach (var node in m_RobotContainer.GetChildren())
|
||||
{
|
||||
var robot = (ERobotType)index++;
|
||||
if (node is not Button button)
|
||||
continue;
|
||||
|
||||
button.ButtonPressed = robot == m_SelectedRobot;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateStartButtonState()
|
||||
{
|
||||
var hasSeed = m_Seed > 0;
|
||||
var hasRobot = m_SelectedRobot >= 0;
|
||||
|
||||
m_StartButton.Disabled = !(hasSeed && hasRobot);
|
||||
}
|
||||
|
||||
private void OnStartPressed()
|
||||
{
|
||||
if (m_StartButton.Disabled)
|
||||
return;
|
||||
|
||||
var seed = m_SeedLineEdit.Text.Trim();
|
||||
var difficulty = (EDifficulty)m_DifficultyOptionButton.Selected;
|
||||
|
||||
GD.Print($"Starting game with Seed={seed}, Difficulty={difficulty}, Robot={m_SelectedRobot}");
|
||||
Main.Instance.StartCoreLoop(m_Seed, difficulty, m_SelectedRobot);
|
||||
}
|
||||
|
||||
public void Deactivate()
|
||||
{
|
||||
}
|
||||
|
||||
public void EnableInputs()
|
||||
{
|
||||
m_DifficultyOptionButton.Disabled = false;
|
||||
m_RandomizeButton.Disabled = false;
|
||||
m_StartButton.Disabled = false;
|
||||
|
||||
// JAM
|
||||
m_RobotButtons[0].Disabled = false;
|
||||
//foreach (var button in m_RobotButtons)
|
||||
// button.Disabled = false;
|
||||
|
||||
m_SeedLineEdit.Editable = true;
|
||||
}
|
||||
|
||||
public void DisableInputs()
|
||||
{
|
||||
m_DifficultyOptionButton.Disabled = true;
|
||||
m_RandomizeButton.Disabled = true;
|
||||
m_StartButton.Disabled = true;
|
||||
foreach (var button in m_RobotButtons)
|
||||
button.Disabled = true;
|
||||
m_SeedLineEdit.Editable = false;
|
||||
}
|
||||
|
||||
public void Activate()
|
||||
{
|
||||
}
|
||||
|
||||
public bool HandleResult(Result result, Tween tween)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
[Export]
|
||||
public PackedScene RobotScene { get; set; }
|
||||
|
||||
public string StringSeed => SeedString.ToString(m_Seed);
|
||||
|
||||
private OptionButton m_DifficultyOptionButton;
|
||||
private Button m_RandomizeButton;
|
||||
private Button m_OptionsButton;
|
||||
private Button m_StartButton;
|
||||
private Button[] m_RobotButtons;
|
||||
private HBoxContainer m_RobotContainer;
|
||||
private LineEdit m_SeedLineEdit;
|
||||
|
||||
private SRandom m_Random = new((ulong)DateTime.UtcNow.ToFileTimeUtc());
|
||||
private int m_Seed;
|
||||
private ERobotType m_SelectedRobot = ERobotType.Vintage;
|
||||
}
|
||||
Reference in New Issue
Block a user