particles/ParticleSimulation/ParticleSimulation.cs

255 lines
9.5 KiB
C#
Raw Normal View History

using System;
using Godot;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
namespace Particles
{
public class ParticleSimulation
{
public Vector2 ScreenSize;
private Dictionary<int, Particle> _particles = new Dictionary<int, Particle>();
private List<ParticleType> _particleTypes = new List<ParticleType>();
private Dictionary<ParticleType, int> _particleTypeCounts = new Dictionary<ParticleType, int>();
private List<Task> tasks = new List<Task>();
public List<int> LastParticlesAdded { get; private set; } = new List<int>();
public List<int> LastParticlesRemoved { get; private set; } = new List<int>();
private int _idCount = 0;
private const int MaxParticles = 1000;
private const int MaxParticleTypes = 10;
private const float HealthDelta = 0.002f;
private const float NegativeHealthMultiplier = 1.5f;
private const float ParticleCollisionRadius = 13f;
private const float ParticleCollisionStrength = 2.5f;
private const float ParticleCollisionResponse = 2f;
public void Initialize()
{
for (var i = 0; i < MaxParticleTypes; i++)
CreateRandomParticleType();
for (var i = 0; i < MaxParticles; i++)
CreateRandomParticle();
}
public void Update()
{
LastParticlesRemoved.Clear();
LastParticlesAdded.Clear();
tasks.Clear();
foreach (var id in _particles.Keys)
tasks.Add(Task.Factory.StartNew(UpdateParticle, id));
Task.WaitAll(tasks.ToArray());
var deletedParticle = false;
foreach (var p in _particles)
{
var particle = p.Value;
if (deletedParticle == false && particle.Health == 0f)
{
if (GD.Randf() < 0.2f)
{
LastParticlesRemoved.Add(p.Key);
deletedParticle = true;
continue;
}
}
var position = particle.Position;
particle.Velocity = particle.Velocity.Clamped(4f);
position += particle.Velocity;
particle.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;
particle.AddAverageSpeedValue(particle.Position.DistanceTo(position));
if (particle.AverageSpeed < 0.15f)
particle.Health -= HealthDelta * NegativeHealthMultiplier;
else
particle.Health += HealthDelta;
if (deletedParticle == false && particle.Health == 0f)
{
if (GD.Randf() < 0.2f)
{
LastParticlesRemoved.Add(p.Key);
deletedParticle = true;
continue;
}
}
particle.Position = position;
}
foreach (var id in LastParticlesRemoved)
{
_particles.Remove(id);
}
if (_particles.Count < MaxParticles)
{
if (GD.Randf() < 0.3f)
{
CreateRandomParticle();
}
}
}
private void RemoveParticleType(ParticleType type)
{
_particleTypes.Remove(type);
foreach (var t in _particleTypes)
{
t.RemoveRelationship(type);
}
}
private void CreateRandomParticleType()
{
var type = new ParticleType()
{
Hue = (float) GD.RandRange(0, 1)
};
_particleTypes.Add(type);
_particleTypeCounts[type] = 0;
foreach (var type1 in _particleTypes)
foreach(var type2 in _particleTypes)
type1.AddRelationship(type2,
new ParticleRelationshipProps(ParticleCollisionRadius, (float)GD.RandRange(22, 50),
(float)GD.RandRange(-0.3, 0.3)));
}
private void CreateRandomParticle()
{
var randomIndex = (int) (GD.Randi() % _particleTypes.Count);
var type = _particleTypes[randomIndex];
CreateParticle(type);
}
private void CreateParticle(ParticleType type)
{
var particle = new Particle(_idCount, type)
{
Position = GetRandomParticlePosition(),
Health = 1f
};
LastParticlesAdded.Add(_idCount);
_particles.Add(_idCount, particle);
_idCount++;
_particleTypeCounts[type]++;
}
private Vector2 GetRandomParticlePosition()
{
var position = new Vector2(
(float) GD.RandRange(0, ScreenSize.x),
(float) GD.RandRange(0, ScreenSize.y));
return position;
}
private Vector2 GetScreenWrapPosition(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;
}
public Particle GetParticle(int id)
{
return _particles[id];
}
private Task UpdateParticle(object i)
{
var id = (int)i;
var particle1 = _particles[id];
var closeCount = 0;
foreach (var p2 in _particles)
{
var particle2 = p2.Value;
if (particle1 == particle2)
continue;
var position = GetScreenWrapPosition(particle1.Position, particle2.Position);
var distanceSquared = particle1.Position.DistanceSquaredTo(position);
if (distanceSquared > (50f * 50f))
continue;
var direction = particle1.Position.DirectionTo(position);
if (distanceSquared < (50f * 50f))
closeCount++;
// collision force
float distance;
if (distanceSquared < (ParticleCollisionRadius * ParticleCollisionRadius))
{
distance = particle1.Position.DistanceTo(position);
var collisionForce = 1f / ((1f / ParticleCollisionStrength) + Mathf.Pow(Mathf.E, -ParticleCollisionResponse * (distance - ParticleCollisionRadius + 3f))) - 1f / (1f / ParticleCollisionStrength);
//var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - ParticleCollisionRadius - 2f))) - 1f / 0.6f;
particle1.Velocity += direction * collisionForce;
}
// particle relationship force
var props = particle1.Type.GetRelationship(particle2.Type);
if (props.Force != 0f && distanceSquared >= props.MinRadius * props.MinRadius &&
distanceSquared <= props.MaxRadius * props.MaxRadius)
{
distance = particle1.Position.DistanceTo(position);
var mid = (props.MinRadius + props.MaxRadius) / 2f;
float particleForce;
if (props.Force > 0)
{
if (distance <= mid)
{
particleForce = 1f / ((1f / props.Force) + Mathf.Pow(Mathf.E, -4 * (distance - props.MinRadius - 1f)));
}
else
{
particleForce = 1f / ((1f / props.Force) + Mathf.Pow(Mathf.E, 4 * (distance - props.MaxRadius + 1f)));
}
}
else
{
if (distance <= mid)
{
particleForce = -1f / ((-1f / props.Force) + Mathf.Pow(Mathf.E, -4 * (distance - props.MinRadius - 1f)));
}
else
{
particleForce = -1f / ((-1f / props.Force) + Mathf.Pow(Mathf.E, 4 * (distance - props.MaxRadius + 1f)));
}
}
particle1.Velocity += direction * particleForce;
}
}
if (closeCount < 4 || closeCount > 33)
particle1.Health -= HealthDelta * NegativeHealthMultiplier;
else
particle1.Health += HealthDelta;
return Task.CompletedTask;
}
}
}