randomize seed for menu

This commit is contained in:
orosmatthew 2022-01-25 12:33:18 -05:00
parent 17017d631e
commit a3019beef1
4 changed files with 104 additions and 53 deletions

23
Main.cs
View File

@ -1,24 +1,37 @@
using Godot; using Godot;
using System; using System;
using Particles.ParticleSimulation;
public class Main : Node public class Main : Node
{ {
public void StartSimulation(int seed, int nParticles) public int Seed;
private int _nParticles;
public void StartSimulation(int nParticles)
{ {
_nParticles = nParticles;
var particleSimulationPackedScene = GD.Load<PackedScene>("res://ParticleSimulation/ParticleSimulationScene.tscn"); var particleSimulationPackedScene = GD.Load<PackedScene>("res://ParticleSimulation/ParticleSimulationScene.tscn");
var particleSimulationScene = particleSimulationPackedScene.Instance<ParticleSimulationScene>(); var particleSimulationScene = particleSimulationPackedScene.Instance<ParticleSimulationScene>();
particleSimulationScene.Name = "Simulation"; particleSimulationScene.Name = Seed.ToString();
AddChild(particleSimulationScene); AddChild(particleSimulationScene);
particleSimulationScene.Initialize(seed, nParticles); particleSimulationScene.Initialize(nParticles);
GetNode<Control>("MainMenu").Hide(); GetNode<Control>("MainMenu").Hide();
//OS.WindowResizable = false;
} }
public void ExitToMenu() public void ExitToMenu()
{ {
GetNode("Simulation").QueueFree(); GetNode(Seed.ToString()).QueueFree();
GetNode<Control>("MainMenu").Show(); GetNode<Control>("MainMenu").Show();
GetNode<MainMenu>("MainMenu").RefreshSeedText();
}
public void RestartSimulation()
{
GetNode<ParticleSimulationScene>(Seed.ToString()).Hide();
GetNode(Seed.ToString()).QueueFree();
Seed = (int) GD.Randi();
StartSimulation(_nParticles);
} }
} }

View File

@ -1,5 +1,4 @@
using Godot; using Godot;
using System;
public class MainMenu : Control public class MainMenu : Control
{ {
@ -10,6 +9,7 @@ public class MainMenu : Control
private Button _toggleFullScreenButton; private Button _toggleFullScreenButton;
private TextEdit _seedText; private TextEdit _seedText;
private TextEdit _particleCountText; private TextEdit _particleCountText;
private Button _randomizeButton;
public override void _Ready() public override void _Ready()
{ {
@ -17,25 +17,33 @@ public class MainMenu : Control
_main = GetTree().Root.GetNode<Main>("Main"); _main = GetTree().Root.GetNode<Main>("Main");
_simulateButton = GetNode("MenuButtons").GetNode<Button>("SimulateButton"); _simulateButton = GetNode("MenuButtons").GetNode<Button>("SimulateButton");
_toggleFullScreenButton = GetNode("MenuButtons").GetNode<Button>("ToggleFullscreenButton"); _toggleFullScreenButton = GetNode("MenuButtons").GetNode<Button>("ToggleFullscreenButton");
_seedText = GetNode("MenuButtons").GetNode<TextEdit>("SeedText"); _seedText = GetNode("MenuButtons").GetNode("Inputs").GetNode("Seed").GetNode<TextEdit>("SeedText");
_particleCountText = GetNode("MenuButtons").GetNode<TextEdit>("ParticleCountText"); _particleCountText = GetNode("MenuButtons").GetNode("Inputs").GetNode("ParticleCount").GetNode<TextEdit>("ParticleCountText");
_randomizeButton = GetNode("MenuButtons").GetNode("Inputs").GetNode("Seed").GetNode<Button>("RandomizeButton");
// Connect signals // Connect signals
_simulateButton.Connect("pressed", this, nameof(_OnSimulatePressed)); _simulateButton.Connect("pressed", this, nameof(_OnSimulatePressed));
_toggleFullScreenButton.Connect("pressed", this, nameof(_OnToggleFullscreenPressed)); _toggleFullScreenButton.Connect("pressed", this, nameof(_OnToggleFullscreenPressed));
_randomizeButton.Connect("pressed", this, nameof(_OnRandomizePressed));
// Random seed // Random seed
GD.Randomize(); GD.Randomize();
var randomSeed = (int)GD.Randi(); var randomSeed = (int)GD.Randi();
_seedText.Text = randomSeed.ToString(); _main.Seed = randomSeed;
RefreshSeedText();
}
public void RefreshSeedText()
{
_seedText.Text = _main.Seed.ToString();
} }
public void _OnSimulatePressed() public void _OnSimulatePressed()
{ {
// Start simulation with seed and number of particles // Start simulation with seed and number of particles
var nParticles = _particleCountText.Text.ToInt(); var nParticles = _particleCountText.Text.ToInt();
var seed = _seedText.Text.ToInt(); _main.Seed = _seedText.Text.ToInt();
_main.StartSimulation(seed, nParticles); _main.StartSimulation(nParticles);
} }
public void _OnToggleFullscreenPressed() public void _OnToggleFullscreenPressed()
@ -43,6 +51,13 @@ public class MainMenu : Control
OS.WindowFullscreen = !OS.WindowFullscreen; OS.WindowFullscreen = !OS.WindowFullscreen;
} }
public void _OnRandomizePressed()
{
var randomSeed = (int) GD.Randi();
_main.Seed = randomSeed;
RefreshSeedText();
}
public override void _Input(InputEvent @event) public override void _Input(InputEvent @event)
{ {
// Quit only if menu is visible // Quit only if menu is visible

View File

@ -23,76 +23,99 @@ anchor_left = 0.5
anchor_top = 0.5 anchor_top = 0.5
anchor_right = 0.5 anchor_right = 0.5
anchor_bottom = 0.5 anchor_bottom = 0.5
margin_left = -150.0 margin_left = -259.5
margin_top = -132.0 margin_top = -114.0
margin_right = 150.0 margin_right = 259.5
margin_bottom = 337.685 margin_bottom = 172.5
custom_constants/separation = 12 custom_constants/separation = 12
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="ParticleCountLabel" type="Label" parent="MenuButtons"] [node name="Inputs" type="HBoxContainer" parent="MenuButtons"]
margin_right = 300.0 margin_right = 519.0
margin_bottom = 150.0
alignment = 1
[node name="Seed" type="VBoxContainer" parent="MenuButtons/Inputs"]
margin_right = 250.946
margin_bottom = 150.0
rect_min_size = Vector2( 250.946, 0 )
[node name="SeedLabel" type="Label" parent="MenuButtons/Inputs/Seed"]
margin_right = 250.0
margin_bottom = 27.0 margin_bottom = 27.0
text = "Number of Particles" text = "Seed Number"
align = 1
[node name="ParticleCountText" type="TextEdit" parent="MenuButtons"]
margin_top = 39.0
margin_right = 300.0
margin_bottom = 103.769
rect_min_size = Vector2( 0, 64.769 )
text = "1000"
[node name="SeedLabel" type="Label" parent="MenuButtons"]
margin_top = 115.0
margin_right = 300.0
margin_bottom = 142.0
text = "Optional Seed Number"
align = 1 align = 1
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="SeedText" type="TextEdit" parent="MenuButtons"] [node name="SeedText" type="TextEdit" parent="MenuButtons/Inputs/Seed"]
margin_top = 154.0 margin_top = 31.0
margin_right = 300.0 margin_right = 250.0
margin_bottom = 218.769 margin_bottom = 95.769
rect_min_size = Vector2( 0, 64.769 ) rect_min_size = Vector2( 0, 64.769 )
text = "12345" text = "12345"
[node name="RandomizeButton" type="Button" parent="MenuButtons/Inputs/Seed"]
margin_top = 99.0
margin_right = 250.0
margin_bottom = 150.251
text = "Randomize!"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ParticleCount" type="VBoxContainer" parent="MenuButtons/Inputs"]
margin_left = 254.0
margin_right = 519.0
margin_bottom = 150.0
[node name="ParticleCountLabel" type="Label" parent="MenuButtons/Inputs/ParticleCount"]
margin_right = 265.0
margin_bottom = 27.0
text = "Number of Particles"
align = 1
[node name="ParticleCountText" type="TextEdit" parent="MenuButtons/Inputs/ParticleCount"]
margin_top = 31.0
margin_right = 265.0
margin_bottom = 95.769
rect_min_size = Vector2( 0, 64.769 )
text = "1000"
[node name="SimulateButton" type="Button" parent="MenuButtons"] [node name="SimulateButton" type="Button" parent="MenuButtons"]
margin_top = 230.0 margin_top = 162.0
margin_right = 300.0 margin_right = 519.0
margin_bottom = 281.251 margin_bottom = 213.251
text = "Simulate!" text = "Simulate!"
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="Warning" type="Label" parent="MenuButtons"] [node name="Warning" type="Label" parent="MenuButtons"]
margin_top = 293.0 margin_top = 225.0
margin_right = 300.0 margin_right = 519.0
margin_bottom = 338.0 margin_bottom = 270.0
custom_fonts/font = SubResource( 1 ) custom_fonts/font = SubResource( 1 )
text = "Simulation cannot be resized text = "Simulation cannot be resized
once simulation starts!" once simulation starts!"
align = 1 align = 1
[node name="HBoxContainer" type="HBoxContainer" parent="MenuButtons"] [node name="HBoxContainer" type="HBoxContainer" parent="MenuButtons"]
margin_top = 350.0 margin_top = 282.0
margin_right = 300.0 margin_right = 519.0
margin_bottom = 350.0 margin_bottom = 282.0
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="ToggleFullscreenButton" type="Button" parent="MenuButtons"] [node name="ToggleFullscreenButton" type="Button" parent="MenuButtons"]
margin_top = 362.0 margin_top = 294.0
margin_right = 300.0 margin_right = 519.0
margin_bottom = 413.251 margin_bottom = 345.251
text = "Toggle Fullscreen" text = "Toggle Fullscreen!"
[node name="Logo" type="TextureRect" parent="."] [node name="Logo" type="TextureRect" parent="."]
show_behind_parent = true show_behind_parent = true
@ -110,5 +133,3 @@ texture = ExtResource( 2 )
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[connection signal="pressed" from="MenuButtons/ToggleFullscreenButton" to="." method="_on_ToggleFullscreen_pressed"]

View File

@ -10,9 +10,10 @@ public class ParticleSimulationScene : Node2D
private ParticleSimulation _particleSimulation; private ParticleSimulation _particleSimulation;
public float PhysicsInterpolationFraction; public float PhysicsInterpolationFraction;
public void Initialize(int seed, int nParticles) public void Initialize(int nParticles)
{ {
_particleNodes = GetNode<Node2D>("ParticleNodes"); _particleNodes = GetNode<Node2D>("ParticleNodes");
var seed = GetParent<Main>().Seed;
GD.Seed((ulong)seed); GD.Seed((ulong)seed);
GD.Print("Last Seed: " + seed); GD.Print("Last Seed: " + seed);
var viewSize = GetViewportRect().Size; var viewSize = GetViewportRect().Size;
@ -30,7 +31,8 @@ public class ParticleSimulationScene : Node2D
{ {
if (Input.IsActionJustPressed("quit")) if (Input.IsActionJustPressed("quit"))
GetParent<Main>().ExitToMenu(); GetParent<Main>().ExitToMenu();
if (Input.IsActionJustPressed("reset")) GetTree().ReloadCurrentScene(); if (Input.IsActionJustPressed("reset"))
GetParent<Main>().RestartSimulation();
PhysicsInterpolationFraction = Engine.GetPhysicsInterpolationFraction(); PhysicsInterpolationFraction = Engine.GetPhysicsInterpolationFraction();
} }