particles/Main.cs

229 lines
7.6 KiB
C#
Raw Normal View History

2021-12-27 16:10:07 -05:00
using System;
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-27 16:10:07 -05:00
private const int MaxParticles = 600;
2021-12-27 14:58:33 -05:00
private const float HealthDelta = 0.002f;
private const float PositiveHealthMultiplier = 2f;
2021-12-27 16:10:07 -05:00
private Task[] tasks = new Task[MaxParticles];
2021-12-27 14:58:33 -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-27 16:10:07 -05:00
InitializeParticleTypes(10);
2021-12-27 14:58:33 -05:00
InitializeParticles(MaxParticles);
2021-12-27 16:10:07 -05:00
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)
2021-12-27 14:58:33 -05:00
foreach (var type2 in _particleTypes)
2021-12-27 16:10:07 -05:00
type1.AddRelationship(type2,
new ParticleRelationshipProps(20, (float) GD.RandRange(25, 60),
2021-12-27 14:58:33 -05:00
(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-27 14:58:33 -05:00
particle.Health = 1f;
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
2021-12-27 14:58:33 -05:00
private void CreateParticle(ParticleType type)
{
var particleScene = GD.Load<PackedScene>("res://Particle.tscn");
var particle = particleScene.Instance<Particle>();
GetNode<Node2D>("Particles").AddChild(particle);
particle.Position = GetRandomParticlePosition();
particle.Type = type;
particle.Health = 1f;
}
private void CreateRandomParticle()
{
2021-12-27 16:10:07 -05:00
var randomIndex = (int) (GD.Randi() % _particleTypes.Count);
2021-12-27 14:58:33 -05:00
var type = _particleTypes[randomIndex];
CreateParticle(type);
}
2021-12-27 16:10:07 -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-27 14:58:33 -05:00
_particles = GetNode<Node2D>("Particles").GetChildren().Cast<Particle>().ToList();
2021-12-27 16:10:07 -05:00
2021-12-26 21:16:28 -05:00
var viewportRect = GetViewportRect();
2021-12-27 16:10:07 -05:00
Array.Resize(ref tasks, _particles.Count);
for (int i = 0; i < _particles.Count; i++)
tasks[i] = Task.Factory.StartNew(UpdateParticle, _particles[i]);
2021-12-26 23:08:40 -05:00
Task.WaitAll(tasks);
2021-12-27 14:58:33 -05:00
var deletedParticle = false;
2021-12-27 16:10:07 -05:00
2021-12-26 23:08:40 -05:00
foreach (var p in _particles)
{
2021-12-27 14:58:33 -05:00
if (deletedParticle == false && p.Health == 0f)
{
if (GD.Randf() < 0.2f)
{
p.Free();
deletedParticle = true;
continue;
}
}
2021-12-27 16:10:07 -05:00
var screenSize = viewportRect.Size;
2021-12-26 23:08:40 -05:00
var position = p.Position;
2021-12-27 14:58:33 -05:00
p.Velocity = p.Velocity.Clamped(3f * 4f);
2021-12-26 23:08:40 -05:00
position += p.Velocity;
2021-12-27 16:10:07 -05:00
p.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;
//position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding);
//position.y = Mathf.Clamp(position.y, ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding);
2021-12-27 14:58:33 -05:00
p.AddAverageSpeedValue(p.Position.DistanceTo(position));
2021-12-27 16:10:07 -05:00
2021-12-27 14:58:33 -05:00
if (p.AverageSpeed < 0.3f)
p.Health -= HealthDelta * 1.5f;
else
p.Health += HealthDelta;
2021-12-27 16:10:07 -05:00
2021-12-27 14:58:33 -05:00
if (deletedParticle == false && p.Health == 0f)
{
if (GD.Randf() < 0.2f)
{
p.Free();
deletedParticle = true;
continue;
}
}
2021-12-27 16:10:07 -05:00
2021-12-26 23:08:40 -05:00
p.Position = position;
}
2021-12-27 16:10:07 -05:00
2021-12-27 14:58:33 -05:00
if (_particles.Count < MaxParticles)
if (GD.Randf() < 0.3f)
CreateRandomParticle();
2021-12-26 23:08:40 -05:00
}
private void UpdateParticle(object particle)
{
var p1 = (Particle) particle;
2021-12-27 14:58:33 -05:00
var closeCount = 0;
2021-12-26 23:08:40 -05:00
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;
2021-12-27 16:10:07 -05:00
var position = GetScreenWrapPosition(viewportRect.Size, p1.Position, p2.Position);
var distanceSquared = p1.Position.DistanceSquaredTo(position);
if (distanceSquared > (60f * 60f))
2021-12-26 23:08:40 -05:00
continue;
2021-12-27 16:10:07 -05:00
var direction = p1.Position.DirectionTo(position);
var distance = 0f;
2021-12-26 23:08:40 -05:00
2021-12-27 16:10:07 -05:00
if (distanceSquared < (70f * 70f))
2021-12-27 14:58:33 -05:00
{
closeCount++;
}
2021-12-27 16:10:07 -05:00
2021-12-26 23:08:40 -05:00
// collision force
2021-12-27 16:10:07 -05:00
if (distanceSquared < (20f * 20f))
2021-12-26 21:16:28 -05:00
{
2021-12-27 16:10:07 -05:00
distance = p1.Position.DistanceTo(position);
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-27 16:10:07 -05:00
2021-12-26 23:08:40 -05:00
var props = p1.Type.GetRelationship(p2.Type);
2021-12-27 16:10:07 -05:00
if (props.Force == 0f || !(distanceSquared >= props.MinRadius * props.MinRadius) || !(distanceSquared <= props.MaxRadius * props.MaxRadius))
2021-12-26 23:08:40 -05:00
continue;
2021-12-27 16:10:07 -05:00
distance = p1.Position.DistanceTo(position);
2021-12-26 23:08:40 -05:00
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-27 16:10:07 -05:00
2021-12-27 14:58:33 -05:00
if (closeCount < 3 || closeCount > 35)
p1.Health -= HealthDelta * 1.5f;
else
p1.Health += HealthDelta;
2021-12-27 16:10:07 -05:00
}
private Vector2 GetScreenWrapPosition(Vector2 screenSize, 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;
2021-12-26 21:16:28 -05:00
}
2021-12-26 15:56:57 -05:00
}