particles/Main.cs

125 lines
4.2 KiB
C#
Raw Normal View History

2021-12-26 16:45:02 -05:00
using System.Collections.Generic;
2021-12-26 21:16:28 -05:00
using System.Linq;
2021-12-26 23:08:40 -05:00
using System.Threading.Tasks;
2021-12-26 14:58:58 -05:00
using Godot;
public class Main : Node2D
{
2021-12-26 21:16:28 -05:00
private const int ParticleScreenPadding = 16;
2021-12-26 16:45:02 -05:00
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
2021-12-26 21:16:28 -05:00
private List<Particle> _particles;
2021-12-26 16:45:02 -05:00
2021-12-26 14:58:58 -05:00
public override void _Ready()
{
2021-12-26 15:56:57 -05:00
GD.Randomize();
2021-12-26 21:16:28 -05:00
InitializeParticleTypes(10);
2021-12-26 23:08:40 -05:00
InitializeParticles(700);
2021-12-26 21:16:28 -05:00
_particles = GetNode<Node2D>("Particles").GetChildren().Cast<Particle>().ToList();
2021-12-26 14:58:58 -05:00
}
public override void _Process(float delta)
{
2021-12-26 15:56:57 -05:00
if (Input.IsActionJustPressed("quit")) GetTree().Quit();
if (Input.IsActionJustPressed("reset")) GetTree().ReloadCurrentScene();
}
2021-12-26 21:16:28 -05:00
public override void _PhysicsProcess(float delta)
{
UpdateParticles();
}
2021-12-26 16:45:02 -05:00
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);
}
2021-12-26 21:16:28 -05:00
foreach (var type1 in _particleTypes)
foreach (var type2 in _particleTypes)
2021-12-26 23:08:40 -05:00
type1.AddRelationship(type2, 20, (float) GD.RandRange(25, 60), (float) GD.RandRange(-0.33 * 2f, 0.35 * 2f));
2021-12-26 16:45:02 -05:00
}
private void InitializeParticles(int nParticles)
2021-12-26 15:56:57 -05:00
{
2021-12-26 16:45:02 -05:00
var typeCount = 0;
2021-12-26 15:56:57 -05:00
var particleScene = GD.Load<PackedScene>("res://Particle.tscn");
2021-12-26 16:45:02 -05:00
for (var i = 0; i < nParticles; i++)
2021-12-26 14:58:58 -05:00
{
2021-12-26 15:56:57 -05:00
var particle = particleScene.Instance<Particle>();
GetNode<Node2D>("Particles").AddChild(particle);
particle.Position = GetRandomParticlePosition();
2021-12-26 16:45:02 -05:00
particle.Type = _particleTypes[typeCount];
2021-12-26 21:16:28 -05:00
2021-12-26 16:45:02 -05:00
if (typeCount < _particleTypes.Count - 1)
typeCount++;
else
typeCount = 0;
2021-12-26 14:58:58 -05:00
}
}
2021-12-26 15:56:57 -05:00
private Vector2 GetRandomParticlePosition()
{
var viewportRect = GetViewportRect();
2021-12-26 21:16:28 -05:00
var position = new Vector2(
(float) GD.RandRange(ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding),
(float) GD.RandRange(ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding));
2021-12-26 15:56:57 -05:00
return position;
}
2021-12-26 21:16:28 -05:00
private void UpdateParticles()
{
2021-12-26 23:08:40 -05:00
var tasks = new Task[_particles.Count];
2021-12-26 21:16:28 -05:00
var viewportRect = GetViewportRect();
2021-12-26 23:08:40 -05:00
for (var i = 0; i < _particles.Count; i++) tasks[i] = Task.Factory.StartNew(UpdateParticle, _particles[i]);
Task.WaitAll(tasks);
foreach (var p in _particles)
{
var position = p.Position;
p.Velocity = p.Velocity.Clamped(3f * 2f);
position += p.Velocity;
p.Velocity *= 0.85f;
position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding);
position.y = Mathf.Clamp(position.y, ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding);
p.Position = position;
}
}
private void UpdateParticle(object particle)
{
var p1 = (Particle) particle;
foreach (var p2 in _particles)
2021-12-26 21:16:28 -05:00
{
2021-12-26 23:08:40 -05:00
var viewportRect = GetViewportRect();
if (p1 == p2)
continue;
var distance = p1.Position.DistanceTo(p2.Position);
if (distance > 60f)
continue;
var direction = p1.Position.DirectionTo(p2.Position);
// collision force
if (distance < 20)
2021-12-26 21:16:28 -05:00
{
2021-12-26 23:08:40 -05:00
var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - 12f))) - 1f / 0.6f;
p1.Velocity += direction * collisionForce;
2021-12-26 21:16:28 -05:00
}
2021-12-26 23:08:40 -05:00
// particle relationship force
2021-12-26 21:16:28 -05:00
2021-12-26 23:08:40 -05:00
var props = p1.Type.GetRelationship(p2.Type);
if (props.Force == 0f || !(distance >= props.MinRadius) || !(distance <= props.MaxRadius))
continue;
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;
2021-12-26 21:16:28 -05:00
}
}
2021-12-26 15:56:57 -05:00
}