From f0a8e7baebc691bbf8e627a35f833adee106bd96 Mon Sep 17 00:00:00 2001 From: orosmatthew Date: Sun, 26 Dec 2021 16:45:02 -0500 Subject: [PATCH] added ParticleType --- Main.cs | 30 +++++++++++++++++++++++---- Particle.cs | 10 ++++----- ParticleType.cs | 38 ++++++++++++++++++++++++++++++++++ Particles.csproj | 6 +++--- Particles.sln.DotSettings.user | 2 ++ 5 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 ParticleType.cs create mode 100644 Particles.sln.DotSettings.user diff --git a/Main.cs b/Main.cs index 82c81ce..033ae29 100644 --- a/Main.cs +++ b/Main.cs @@ -1,11 +1,15 @@ +using System.Collections.Generic; using Godot; public class Main : Node2D { + private readonly List _particleTypes = new List(); + public override void _Ready() { GD.Randomize(); - InitializeParticles(); + InitializeParticleTypes(5); + InitializeParticles(50); } public override void _Process(float delta) @@ -14,15 +18,33 @@ public class Main : Node2D if (Input.IsActionJustPressed("reset")) GetTree().ReloadCurrentScene(); } - private void InitializeParticles() + private void InitializeParticleTypes(int nTypes) { + for (var i = 0; i < nTypes; i++) + { + var type = new ParticleType + { + Hue = (float) GD.RandRange(0, 1) + }; + _particleTypes.Add(type); + } + } + + private void InitializeParticles(int nParticles) + { + var typeCount = 0; var particleScene = GD.Load("res://Particle.tscn"); - for (var i = 0; i < 50; i++) + for (var i = 0; i < nParticles; i++) { var particle = particleScene.Instance(); GetNode("Particles").AddChild(particle); particle.Position = GetRandomParticlePosition(); - particle.Hue = (float) GD.RandRange(0, 1); + particle.Type = _particleTypes[typeCount]; + + if (typeCount < _particleTypes.Count - 1) + typeCount++; + else + typeCount = 0; } } diff --git a/Particle.cs b/Particle.cs index df553fe..e111f38 100644 --- a/Particle.cs +++ b/Particle.cs @@ -2,16 +2,16 @@ using Godot; public class Particle : Node2D { - private float _spriteHue; private Sprite _spriteNode; + private ParticleType _type; - public float Hue + public ParticleType Type { - get => _spriteHue; + get => _type; set { - _spriteHue = Mathf.Clamp(value, 0, 1); - _spriteNode.Modulate = Color.FromHsv(_spriteHue, 1, 1); + _type = value; + _spriteNode.Modulate = Color.FromHsv(_type.Hue, 1, 1); } } diff --git a/ParticleType.cs b/ParticleType.cs new file mode 100644 index 0000000..b18cdfb --- /dev/null +++ b/ParticleType.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using Godot; + +public struct ParticleRelationshipProps +{ + public ParticleRelationshipProps(float minRadius, float maxRadius, float force) + { + MinRadius = minRadius; + MaxRadius = maxRadius; + Force = force; + } + + public float MinRadius { get; } + public float MaxRadius { get; } + public float Force { get; } +} + +public class ParticleType +{ + private float _hue; + + private readonly Dictionary _particleRelationships = + new Dictionary(); + + public float Hue + { + get => _hue; + set => _hue = Mathf.Clamp(value, 0, 1); + } + + public void AddRelationship(ParticleType type, float minRadius, float maxRadius, float force) + { + if (_particleRelationships.ContainsKey(type)) + return; + var props = new ParticleRelationshipProps(minRadius, maxRadius, force); + _particleRelationships[type] = props; + } +} \ No newline at end of file diff --git a/Particles.csproj b/Particles.csproj index db88a8d..ceaf2bb 100644 --- a/Particles.csproj +++ b/Particles.csproj @@ -1,5 +1,5 @@ - - net472 - + + net472 + \ No newline at end of file diff --git a/Particles.sln.DotSettings.user b/Particles.sln.DotSettings.user new file mode 100644 index 0000000..f591ba1 --- /dev/null +++ b/Particles.sln.DotSettings.user @@ -0,0 +1,2 @@ + + INFO \ No newline at end of file