From 6e5d40614c3418c4bf91e523258fcea1b1e9d38c Mon Sep 17 00:00:00 2001 From: orosmatthew Date: Mon, 27 Dec 2021 23:29:02 -0500 Subject: [PATCH] Decouple ParticleSimulation into separate class --- Main.cs | 229 +++--------------- Main.tscn | 2 +- Particle.cs | 49 ---- ParticleNode.cs | 25 ++ Particle.tscn => ParticleNode.tscn | 10 +- ParticleSimulation/Particle.cs | 33 +++ ParticleSimulation/ParticleSimulation.cs | 225 +++++++++++++++++ .../ParticleType.cs | 6 + 8 files changed, 327 insertions(+), 252 deletions(-) delete mode 100644 Particle.cs create mode 100644 ParticleNode.cs rename Particle.tscn => ParticleNode.tscn (69%) create mode 100644 ParticleSimulation/Particle.cs create mode 100644 ParticleSimulation/ParticleSimulation.cs rename ParticleType.cs => ParticleSimulation/ParticleType.cs (86%) diff --git a/Main.cs b/Main.cs index 2d3d0b3..28f86c0 100644 --- a/Main.cs +++ b/Main.cs @@ -3,26 +3,26 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Godot; +using Particles; public class Main : Node2D { - private const int ParticleScreenPadding = 16; - - private readonly List _particleTypes = new List(); - private List _particles; - - private const int MaxParticles = 600; - - private const float HealthDelta = 0.002f; - private const float PositiveHealthMultiplier = 2f; - private Task[] tasks = new Task[MaxParticles]; + private ParticleSimulation _particleSimulation; + private Node2D _particleNodes; public override void _Ready() { + _particleNodes = GetNode("ParticlesNodes"); GD.Randomize(); - InitializeParticleTypes(10); - InitializeParticles(MaxParticles); - + _particleSimulation = new ParticleSimulation + { + ScreenSize = GetViewportRect().Size + }; + _particleSimulation.Initialize(); + foreach (var id in _particleSimulation.LastParticlesAdded) + { + CreateParticleNode(id); + } } public override void _Process(float delta) @@ -33,197 +33,32 @@ public class Main : Node2D public override void _PhysicsProcess(float delta) { - UpdateParticles(); - } - - private void InitializeParticleTypes(int nTypes) - { - for (var i = 0; i < nTypes; i++) + _particleSimulation.Update(); + foreach (var id in _particleSimulation.LastParticlesRemoved) { - var type = new ParticleType - { - Hue = (float) GD.RandRange(0, 1) - }; - _particleTypes.Add(type); + var particleNode = _particleNodes.GetNode(id.ToString()); + particleNode.Free(); + } + foreach (var id in _particleSimulation.LastParticlesAdded) + { + CreateParticleNode(id); } - foreach (var type1 in _particleTypes) - foreach (var type2 in _particleTypes) - type1.AddRelationship(type2, - new ParticleRelationshipProps(20, (float) GD.RandRange(25, 60), - (float) GD.RandRange(-0.33 * 2f, 0.35 * 2f))); - } - - private void InitializeParticles(int nParticles) - { - var typeCount = 0; - var particleScene = GD.Load("res://Particle.tscn"); - for (var i = 0; i < nParticles; i++) + var particleNodes = _particleNodes.GetChildren(); + foreach (ParticleNode particleNode in particleNodes) { - var particle = particleScene.Instance(); - GetNode("Particles").AddChild(particle); - particle.Position = GetRandomParticlePosition(); - particle.Type = _particleTypes[typeCount]; - particle.Health = 1f; - - if (typeCount < _particleTypes.Count - 1) - typeCount++; - else - typeCount = 0; + var simulationParticle = _particleSimulation.GetParticle(particleNode.SimulationId); + particleNode.Position = simulationParticle.Position; + particleNode.SetColor(simulationParticle.Type.Hue, simulationParticle.Health); } } - private void CreateParticle(ParticleType type) + private void CreateParticleNode(int id) { - var particleScene = GD.Load("res://Particle.tscn"); - var particle = particleScene.Instance(); - GetNode("Particles").AddChild(particle); - particle.Position = GetRandomParticlePosition(); - particle.Type = type; - particle.Health = 1f; - } - - private void CreateRandomParticle() - { - var randomIndex = (int) (GD.Randi() % _particleTypes.Count); - var type = _particleTypes[randomIndex]; - CreateParticle(type); - } - - private Vector2 GetRandomParticlePosition() - { - var viewportRect = GetViewportRect(); - var position = new Vector2( - (float) GD.RandRange(ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding), - (float) GD.RandRange(ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding)); - return position; - } - - private void UpdateParticles() - { - _particles = GetNode("Particles").GetChildren().Cast().ToList(); - - var viewportRect = GetViewportRect(); - Array.Resize(ref tasks, _particles.Count); - for (int i = 0; i < _particles.Count; i++) - tasks[i] = Task.Factory.StartNew(UpdateParticle, _particles[i]); - - Task.WaitAll(tasks); - - var deletedParticle = false; - - - foreach (var p in _particles) - { - if (deletedParticle == false && p.Health == 0f) - { - if (GD.Randf() < 0.2f) - { - p.Free(); - deletedParticle = true; - continue; - } - } - - var screenSize = viewportRect.Size; - var position = p.Position; - p.Velocity = p.Velocity.Clamped(3f * 4f); - position += p.Velocity; - p.Velocity *= 0.875f; // friction - if (position.x > screenSize.x) - position.x -= screenSize.x; - else if (position.x < 0) - position.x += screenSize.x; - if (position.y > screenSize.y) - position.y -= screenSize.y; - else if (position.y < 0) - position.y += screenSize.y; - //position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding); - //position.y = Mathf.Clamp(position.y, ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding); - p.AddAverageSpeedValue(p.Position.DistanceTo(position)); - - if (p.AverageSpeed < 0.3f) - p.Health -= HealthDelta * 1.5f; - else - p.Health += HealthDelta; - - if (deletedParticle == false && p.Health == 0f) - { - if (GD.Randf() < 0.2f) - { - p.Free(); - deletedParticle = true; - continue; - } - } - - p.Position = position; - } - - - if (_particles.Count < MaxParticles) - if (GD.Randf() < 0.3f) - CreateRandomParticle(); - } - - private void UpdateParticle(object particle) - { - var p1 = (Particle) particle; - var closeCount = 0; - foreach (var p2 in _particles) - { - var viewportRect = GetViewportRect(); - if (p1 == p2) - continue; - var position = GetScreenWrapPosition(viewportRect.Size, p1.Position, p2.Position); - var distanceSquared = p1.Position.DistanceSquaredTo(position); - if (distanceSquared > (60f * 60f)) - continue; - var direction = p1.Position.DirectionTo(position); - var distance = 0f; - - if (distanceSquared < (70f * 70f)) - { - closeCount++; - } - - // collision force - if (distanceSquared < (20f * 20f)) - { - distance = p1.Position.DistanceTo(position); - var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - 12f))) - 1f / 0.6f; - p1.Velocity += direction * collisionForce; - } - // particle relationship force - - var props = p1.Type.GetRelationship(p2.Type); - if (props.Force == 0f || !(distanceSquared >= props.MinRadius * props.MinRadius) || !(distanceSquared <= props.MaxRadius * props.MaxRadius)) - continue; - distance = p1.Position.DistanceTo(position); - var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f); - var particleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) + - props.Force; - p1.Velocity += direction * particleForce; - } - - if (closeCount < 3 || closeCount > 35) - p1.Health -= HealthDelta * 1.5f; - else - p1.Health += HealthDelta; - - } - - private Vector2 GetScreenWrapPosition(Vector2 screenSize, Vector2 p1, Vector2 p2) - { - var newPosition = p2; - if (p2.x > (p1.x + (screenSize.x / 2f))) - newPosition.x = p2.x - screenSize.x; - else if (p2.x < (p1.x - (screenSize.x / 2f))) - newPosition.x = p2.x + screenSize.x; - if (p2.y > (p1.y + (screenSize.y / 2f))) - newPosition.y = p2.y - screenSize.y; - else if (p2.y < (p1.y - (screenSize.y / 2f))) - newPosition.y = p2.y + screenSize.y; - return newPosition; + var particleScene = GD.Load("res://ParticleNode.tscn"); + var particleNode = particleScene.Instance(); + particleNode.Name = id.ToString(); + particleNode.SimulationId = id; + _particleNodes.AddChild(particleNode); } } \ No newline at end of file diff --git a/Main.tscn b/Main.tscn index 1a5b5ef..4017b61 100644 --- a/Main.tscn +++ b/Main.tscn @@ -5,4 +5,4 @@ [node name="Main" type="Node2D"] script = ExtResource( 1 ) -[node name="Particles" type="Node2D" parent="."] +[node name="ParticlesNodes" type="Node2D" parent="."] diff --git a/Particle.cs b/Particle.cs deleted file mode 100644 index c49b32c..0000000 --- a/Particle.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Godot; - -public class Particle : Node2D -{ - private Sprite _spriteNode; - private Label _labelNode; - - private ParticleType _type; - private float _health = 0f; - private float _averageSpeed = 1f; - public Vector2 Velocity { get; set; } - public float AverageSpeed { get => _averageSpeed; } - - public float Health - { - get => _health; - set - { - _health = Mathf.Clamp(value, 0f, 1f); - _spriteNode.Modulate = Color.FromHsv(_type.Hue, Health, 1); - } - } - - public ParticleType Type - { - get => _type; - set - { - _type = value; - _spriteNode.Modulate = Color.FromHsv(_type.Hue, Health, 1); - } - } - - public override void _Ready() - { - _spriteNode = GetNode("Sprite"); - _labelNode = GetNode