added ParticleType

This commit is contained in:
orosmatthew 2021-12-26 16:45:02 -05:00
parent 49d664e472
commit f0a8e7baeb
5 changed files with 74 additions and 12 deletions

30
Main.cs
View File

@ -1,11 +1,15 @@
using System.Collections.Generic;
using Godot; using Godot;
public class Main : Node2D public class Main : Node2D
{ {
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
public override void _Ready() public override void _Ready()
{ {
GD.Randomize(); GD.Randomize();
InitializeParticles(); InitializeParticleTypes(5);
InitializeParticles(50);
} }
public override void _Process(float delta) public override void _Process(float delta)
@ -14,15 +18,33 @@ public class Main : Node2D
if (Input.IsActionJustPressed("reset")) GetTree().ReloadCurrentScene(); 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<PackedScene>("res://Particle.tscn"); var particleScene = GD.Load<PackedScene>("res://Particle.tscn");
for (var i = 0; i < 50; i++) for (var i = 0; i < nParticles; i++)
{ {
var particle = particleScene.Instance<Particle>(); var particle = particleScene.Instance<Particle>();
GetNode<Node2D>("Particles").AddChild(particle); GetNode<Node2D>("Particles").AddChild(particle);
particle.Position = GetRandomParticlePosition(); particle.Position = GetRandomParticlePosition();
particle.Hue = (float) GD.RandRange(0, 1); particle.Type = _particleTypes[typeCount];
if (typeCount < _particleTypes.Count - 1)
typeCount++;
else
typeCount = 0;
} }
} }

View File

@ -2,16 +2,16 @@ using Godot;
public class Particle : Node2D public class Particle : Node2D
{ {
private float _spriteHue;
private Sprite _spriteNode; private Sprite _spriteNode;
private ParticleType _type;
public float Hue public ParticleType Type
{ {
get => _spriteHue; get => _type;
set set
{ {
_spriteHue = Mathf.Clamp(value, 0, 1); _type = value;
_spriteNode.Modulate = Color.FromHsv(_spriteHue, 1, 1); _spriteNode.Modulate = Color.FromHsv(_type.Hue, 1, 1);
} }
} }

38
ParticleType.cs Normal file
View File

@ -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<ParticleType, ParticleRelationshipProps> _particleRelationships =
new Dictionary<ParticleType, ParticleRelationshipProps>();
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;
}
}

View File

@ -1,5 +1,5 @@
<Project Sdk="Godot.NET.Sdk/3.3.0"> <Project Sdk="Godot.NET.Sdk/3.3.0">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net472</TargetFramework> <TargetFramework>net472</TargetFramework>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String></wpf:ResourceDictionary>