using Godot; using Particles.ParticleSimulation; // ReSharper disable once CheckNamespace // ReSharper disable once ClassNeverInstantiated.Global public class ParticleSimulationScene : Node2D { private Node2D _particleNodes; private ParticleSimulation _particleSimulation; public float PhysicsInterpolationFraction; public void Initialize(int seed, int nParticles) { _particleNodes = GetNode("ParticleNodes"); GD.Seed((ulong)seed); GD.Print("Last Seed: " + seed); var viewSize = GetViewportRect().Size; var zoom = GetNode("Camera2D").Zoom; var spaceSize = new Vector2(viewSize.x * zoom.x, viewSize.y * zoom.y); _particleSimulation = new ParticleSimulation { SpaceSize = spaceSize }; _particleSimulation.Initialize(nParticles); foreach (var id in _particleSimulation.LastParticlesAdded) CreateParticleNode(id); } public override void _Process(float delta) { if (Input.IsActionJustPressed("quit")) GetParent
().ExitToMenu(); if (Input.IsActionJustPressed("reset")) GetTree().ReloadCurrentScene(); PhysicsInterpolationFraction = Engine.GetPhysicsInterpolationFraction(); } public override void _PhysicsProcess(float delta) { _particleSimulation.Update(); foreach (var id in _particleSimulation.LastParticlesRemoved) { var particleNode = _particleNodes.GetNode(id.ToString()); particleNode.Free(); } foreach (var id in _particleSimulation.LastParticlesAdded) CreateParticleNode(id); var particleNodes = _particleNodes.GetChildren(); foreach (ParticleNode particleNode in particleNodes) { var simulationParticle = _particleSimulation.GetParticle(particleNode.SimulationId); particleNode.LastSimulationPosition = particleNode.Position; particleNode.CurrentSimulationPosition = simulationParticle.Position; particleNode.SetColor(simulationParticle.Type.Hue, simulationParticle.Health, Mathf.Clamp(simulationParticle.AverageSpeed / 1.5f, 1f, 1f)); particleNode.WasTeleportedLast = simulationParticle.WasTeleportedLast; } } private void CreateParticleNode(int id) { var particleScene = GD.Load("res://ParticleSimulation/ParticleNode.tscn"); var particleNode = particleScene.Instance(); particleNode.Name = id.ToString(); particleNode.SimulationId = id; _particleNodes.AddChild(particleNode); } }