2021-12-29 15:08:26 -05:00
|
|
|
|
#define MULTITHREADED
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
using Godot;
|
2021-12-29 15:08:26 -05:00
|
|
|
|
#if MULTITHREADED
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
#endif
|
2021-12-27 23:29:02 -05:00
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
namespace Particles.ParticleSimulation
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
|
|
|
|
public class ParticleSimulation
|
|
|
|
|
{
|
2021-12-29 15:08:26 -05:00
|
|
|
|
// size of simulation space
|
2021-12-28 14:32:11 -05:00
|
|
|
|
public Vector2 SpaceSize;
|
2021-12-27 23:29:02 -05:00
|
|
|
|
|
2021-12-29 15:08:26 -05:00
|
|
|
|
// dictionary of particles with particle Id being the key
|
2021-12-28 14:32:11 -05:00
|
|
|
|
private readonly Dictionary<int, Particle> _particles = new Dictionary<int, Particle>();
|
2021-12-27 23:29:02 -05:00
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
|
2021-12-29 15:08:26 -05:00
|
|
|
|
|
|
|
|
|
// task list if multi-threaded
|
|
|
|
|
#if MULTITHREADED
|
|
|
|
|
private readonly List<Task> _tasks = new List<Task>();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// updated on every simulation update
|
2021-12-27 23:29:02 -05:00
|
|
|
|
public List<int> LastParticlesAdded { get; private set; } = new List<int>();
|
|
|
|
|
public List<int> LastParticlesRemoved { get; private set; } = new List<int>();
|
|
|
|
|
|
2021-12-29 15:08:26 -05:00
|
|
|
|
// counts up for each particle added
|
2021-12-28 14:32:11 -05:00
|
|
|
|
private int _idCount;
|
2021-12-27 23:29:02 -05:00
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
private const int MaxParticles = 1100;
|
2021-12-29 15:08:26 -05:00
|
|
|
|
private const int MaxParticleTypes = 10;
|
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
private const float HealthDelta = 0.005f;
|
|
|
|
|
private const float NegativeHealthMultiplier = 2f;
|
|
|
|
|
private const float PositiveHealthMultiplier = 4f;
|
2021-12-28 01:55:28 -05:00
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
private const float ParticleCollisionRadius = 20f;
|
2021-12-28 01:55:28 -05:00
|
|
|
|
|
2021-12-27 23:29:02 -05:00
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < MaxParticleTypes; i++)
|
|
|
|
|
CreateRandomParticleType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
LastParticlesRemoved.Clear();
|
|
|
|
|
LastParticlesAdded.Clear();
|
2021-12-29 15:08:26 -05:00
|
|
|
|
|
|
|
|
|
// update all particles
|
|
|
|
|
#if MULTITHREADED
|
|
|
|
|
_tasks.Clear();
|
|
|
|
|
foreach (var id in _particles.Keys)
|
|
|
|
|
_tasks.Add(Task.Factory.StartNew(UpdateParticle, id));
|
|
|
|
|
Task.WaitAll(_tasks.ToArray());
|
|
|
|
|
#else
|
2021-12-27 23:29:02 -05:00
|
|
|
|
foreach (var id in _particles.Keys)
|
2021-12-29 15:08:26 -05:00
|
|
|
|
UpdateParticle(id);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// used to ensure only one particle is moved per update
|
|
|
|
|
var movedParticle = false;
|
|
|
|
|
|
|
|
|
|
foreach (var particle in _particles.Select(p => p.Value))
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.WasTeleportedLast = false;
|
|
|
|
|
if (movedParticle == false && particle.Health == 0f)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
2021-12-29 15:08:26 -05:00
|
|
|
|
if (GD.Randf() < 0.1f)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
2021-12-28 14:32:11 -05:00
|
|
|
|
particle.Position = GetRandomParticlePosition();
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.WasTeleportedLast = true;
|
|
|
|
|
particle.Health = 1f;
|
|
|
|
|
movedParticle = true;
|
2021-12-27 23:29:02 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var position = particle.Position;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
particle.Velocity = particle.Velocity.Clamped(5f);
|
2021-12-27 23:29:02 -05:00
|
|
|
|
position += particle.Velocity;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
particle.Velocity *= 0.855f; // friction
|
2021-12-28 14:32:11 -05:00
|
|
|
|
if (position.x > SpaceSize.x)
|
|
|
|
|
{
|
|
|
|
|
position.x -= SpaceSize.x;
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.WasTeleportedLast = true;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
}
|
2021-12-27 23:29:02 -05:00
|
|
|
|
else if (position.x < 0)
|
2021-12-28 14:32:11 -05:00
|
|
|
|
{
|
|
|
|
|
position.x += SpaceSize.x;
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.WasTeleportedLast = true;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (position.y > SpaceSize.y)
|
|
|
|
|
{
|
|
|
|
|
position.y -= SpaceSize.y;
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.WasTeleportedLast = true;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
}
|
2021-12-27 23:29:02 -05:00
|
|
|
|
else if (position.y < 0)
|
2021-12-28 14:32:11 -05:00
|
|
|
|
{
|
|
|
|
|
position.y += SpaceSize.y;
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.WasTeleportedLast = true;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
}
|
2021-12-28 14:32:11 -05:00
|
|
|
|
/*
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.AddAverageSpeedValue(particle.Velocity.Length());
|
|
|
|
|
|
|
|
|
|
if (particle.AverageSpeed < 0.5f)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
particle.Health -= HealthDelta * NegativeHealthMultiplier;
|
|
|
|
|
else
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.Health += HealthDelta * PositiveHealthMultiplier;
|
|
|
|
|
|
|
|
|
|
if (movedParticle == false && particle.Health == 0f)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
2021-12-29 15:08:26 -05:00
|
|
|
|
if (GD.Randf() < 0.1f)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
2021-12-28 14:32:11 -05:00
|
|
|
|
particle.Position = GetRandomParticlePosition();
|
2021-12-29 15:08:26 -05:00
|
|
|
|
particle.ResetAverageSpeed();
|
|
|
|
|
particle.WasTeleportedLast = true;
|
|
|
|
|
particle.Health = 1f;
|
|
|
|
|
movedParticle = true;
|
2021-12-27 23:29:02 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-29 15:08:26 -05:00
|
|
|
|
*/
|
2021-12-27 23:29:02 -05:00
|
|
|
|
particle.Position = position;
|
|
|
|
|
}
|
2021-12-29 15:08:26 -05:00
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
// ReSharper disable once InvertIf
|
2021-12-27 23:29:02 -05:00
|
|
|
|
if (_particles.Count < MaxParticles)
|
|
|
|
|
{
|
2021-12-28 14:32:11 -05:00
|
|
|
|
for (var i = 0; i < 3 && _particles.Count < MaxParticles; i++)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
CreateRandomParticle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
// ReSharper disable once UnusedMember.Local
|
2021-12-27 23:29:02 -05:00
|
|
|
|
private void RemoveParticleType(ParticleType type)
|
|
|
|
|
{
|
|
|
|
|
_particleTypes.Remove(type);
|
|
|
|
|
foreach (var t in _particleTypes)
|
|
|
|
|
{
|
|
|
|
|
t.RemoveRelationship(type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 15:08:26 -05:00
|
|
|
|
// ReSharper disable once UnusedMember.Local
|
|
|
|
|
private void RemoveParticle(int id)
|
|
|
|
|
{
|
|
|
|
|
_particles.Remove(id);
|
|
|
|
|
LastParticlesRemoved.Add(id);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 23:29:02 -05:00
|
|
|
|
private void CreateRandomParticleType()
|
|
|
|
|
{
|
|
|
|
|
var type = new ParticleType()
|
|
|
|
|
{
|
|
|
|
|
Hue = (float) GD.RandRange(0, 1)
|
|
|
|
|
};
|
|
|
|
|
_particleTypes.Add(type);
|
|
|
|
|
foreach (var type1 in _particleTypes)
|
2021-12-28 14:32:11 -05:00
|
|
|
|
foreach (var type2 in _particleTypes)
|
|
|
|
|
type1.AddRelationship(type2,
|
|
|
|
|
new ParticleRelationshipProps(ParticleCollisionRadius, (float) GD.RandRange(25, 55),
|
|
|
|
|
(float)GD.RandRange(-0.675, 0.7)));
|
2021-12-27 23:29:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateRandomParticle()
|
|
|
|
|
{
|
|
|
|
|
var randomIndex = (int) (GD.Randi() % _particleTypes.Count);
|
|
|
|
|
var type = _particleTypes[randomIndex];
|
2021-12-28 14:32:11 -05:00
|
|
|
|
CreateParticle(type, (GetRandomParticlePosition() / 50f) + (SpaceSize / 2f));
|
2021-12-27 23:29:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
private void CreateParticle(ParticleType type, Vector2 position)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
|
|
|
|
var particle = new Particle(_idCount, type)
|
|
|
|
|
{
|
2021-12-28 14:32:11 -05:00
|
|
|
|
Position = position,
|
2021-12-27 23:29:02 -05:00
|
|
|
|
Health = 1f
|
|
|
|
|
};
|
|
|
|
|
LastParticlesAdded.Add(_idCount);
|
|
|
|
|
_particles.Add(_idCount, particle);
|
|
|
|
|
_idCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector2 GetRandomParticlePosition()
|
|
|
|
|
{
|
|
|
|
|
var position = new Vector2(
|
2021-12-28 14:32:11 -05:00
|
|
|
|
(float) GD.RandRange(0, SpaceSize.x),
|
|
|
|
|
(float) GD.RandRange(0, SpaceSize.y));
|
2021-12-27 23:29:02 -05:00
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector2 GetScreenWrapPosition(Vector2 p1, Vector2 p2)
|
|
|
|
|
{
|
|
|
|
|
var newPosition = p2;
|
2021-12-28 14:32:11 -05:00
|
|
|
|
if (p2.x > (p1.x + (SpaceSize.x / 2f)))
|
|
|
|
|
newPosition.x = p2.x - SpaceSize.x;
|
|
|
|
|
else if (p2.x < (p1.x - (SpaceSize.x / 2f)))
|
|
|
|
|
newPosition.x = p2.x + SpaceSize.x;
|
|
|
|
|
if (p2.y > (p1.y + (SpaceSize.y / 2f)))
|
|
|
|
|
newPosition.y = p2.y - SpaceSize.y;
|
|
|
|
|
else if (p2.y < (p1.y - (SpaceSize.y / 2f)))
|
|
|
|
|
newPosition.y = p2.y + SpaceSize.y;
|
2021-12-27 23:29:02 -05:00
|
|
|
|
return newPosition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Particle GetParticle(int id)
|
|
|
|
|
{
|
|
|
|
|
return _particles[id];
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 15:08:26 -05:00
|
|
|
|
private void UpdateParticle(object i)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
|
|
|
|
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);
|
2021-12-28 14:32:11 -05:00
|
|
|
|
if (distanceSquared > (55f * 55f))
|
2021-12-27 23:29:02 -05:00
|
|
|
|
continue;
|
|
|
|
|
|
2021-12-28 14:32:11 -05:00
|
|
|
|
if (distanceSquared < (35f * 35f))
|
2021-12-27 23:29:02 -05:00
|
|
|
|
closeCount++;
|
|
|
|
|
|
|
|
|
|
// collision force
|
|
|
|
|
float distance;
|
2021-12-29 15:08:26 -05:00
|
|
|
|
Vector2 direction;
|
|
|
|
|
|
2021-12-28 01:55:28 -05:00
|
|
|
|
if (distanceSquared < (ParticleCollisionRadius * ParticleCollisionRadius))
|
2021-12-27 23:29:02 -05:00
|
|
|
|
{
|
2021-12-29 15:08:26 -05:00
|
|
|
|
direction = particle1.Position.DirectionTo(position);
|
2021-12-27 23:29:02 -05:00
|
|
|
|
distance = particle1.Position.DistanceTo(position);
|
2021-12-28 14:32:11 -05:00
|
|
|
|
var collisionForce = 1f / (0.35f + Mathf.Pow(Mathf.E, -1.15f * (distance - 12f))) - 1f / 0.35f;
|
2021-12-27 23:29:02 -05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-12-29 15:08:26 -05:00
|
|
|
|
direction = particle1.Position.DirectionTo(position);
|
2021-12-27 23:29:02 -05:00
|
|
|
|
distance = particle1.Position.DistanceTo(position);
|
2021-12-28 01:55:28 -05:00
|
|
|
|
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)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-27 23:29:02 -05:00
|
|
|
|
particle1.Velocity += direction * particleForce;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 15:08:26 -05:00
|
|
|
|
if (closeCount > 70)
|
2021-12-27 23:29:02 -05:00
|
|
|
|
particle1.Health -= HealthDelta * NegativeHealthMultiplier;
|
|
|
|
|
else
|
2021-12-28 14:32:11 -05:00
|
|
|
|
particle1.Health += HealthDelta * PositiveHealthMultiplier;
|
2021-12-27 23:29:02 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|