diff --git a/Main.cs b/Main.cs index a816118..2d3d0b3 100644 --- a/Main.cs +++ b/Main.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -10,17 +11,18 @@ public class Main : Node2D private readonly List _particleTypes = new List(); private List _particles; - private const int MaxParticles = 700; + private const int MaxParticles = 600; private const float HealthDelta = 0.002f; private const float PositiveHealthMultiplier = 2f; + private Task[] tasks = new Task[MaxParticles]; public override void _Ready() { GD.Randomize(); - InitializeParticleTypes(12); + InitializeParticleTypes(10); InitializeParticles(MaxParticles); - + } public override void _Process(float delta) @@ -47,8 +49,8 @@ public class Main : Node2D foreach (var type1 in _particleTypes) foreach (var type2 in _particleTypes) - type1.AddRelationship(type2, - new ParticleRelationshipProps(20, (float) GD.RandRange(25, 60), + type1.AddRelationship(type2, + new ParticleRelationshipProps(20, (float) GD.RandRange(25, 60), (float) GD.RandRange(-0.33 * 2f, 0.35 * 2f))); } @@ -83,11 +85,11 @@ public class Main : Node2D private void CreateRandomParticle() { - var randomIndex = (int)(GD.Randi() % _particleTypes.Count); + var randomIndex = (int) (GD.Randi() % _particleTypes.Count); var type = _particleTypes[randomIndex]; CreateParticle(type); } - + private Vector2 GetRandomParticlePosition() { var viewportRect = GetViewportRect(); @@ -100,14 +102,17 @@ public class Main : Node2D private void UpdateParticles() { _particles = GetNode("Particles").GetChildren().Cast().ToList(); - var tasks = new Task[_particles.Count]; + var viewportRect = GetViewportRect(); - for (var i = 0; i < _particles.Count; i++) tasks[i] = Task.Factory.StartNew(UpdateParticle, _particles[i]); + Array.Resize(ref tasks, _particles.Count); + for (int i = 0; i < _particles.Count; i++) + tasks[i] = Task.Factory.StartNew(UpdateParticle, _particles[i]); Task.WaitAll(tasks); var deletedParticle = false; + foreach (var p in _particles) { if (deletedParticle == false && p.Health == 0f) @@ -119,19 +124,29 @@ public class Main : Node2D continue; } } + + var screenSize = viewportRect.Size; var position = p.Position; p.Velocity = p.Velocity.Clamped(3f * 4f); position += p.Velocity; - p.Velocity *= 0.875f; // friction - position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding); - position.y = Mathf.Clamp(position.y, ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding); + 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); p.AddAverageSpeedValue(p.Position.DistanceTo(position)); - + if (p.AverageSpeed < 0.3f) p.Health -= HealthDelta * 1.5f; else p.Health += HealthDelta; - + if (deletedParticle == false && p.Health == 0f) { if (GD.Randf() < 0.2f) @@ -141,8 +156,10 @@ public class Main : Node2D continue; } } + p.Position = position; } + if (_particles.Count < MaxParticles) if (GD.Randf() < 0.3f) @@ -158,38 +175,55 @@ public class Main : Node2D var viewportRect = GetViewportRect(); if (p1 == p2) continue; - var distance = p1.Position.DistanceTo(p2.Position); - if (distance > 60f) + var position = GetScreenWrapPosition(viewportRect.Size, p1.Position, p2.Position); + var distanceSquared = p1.Position.DistanceSquaredTo(position); + if (distanceSquared > (60f * 60f)) continue; - var direction = p1.Position.DirectionTo(p2.Position); + var direction = p1.Position.DirectionTo(position); + var distance = 0f; - - if (distance < 70) + if (distanceSquared < (70f * 70f)) { closeCount++; } - + // collision force - if (distance < 20) + if (distanceSquared < (20f * 20f)) { + distance = p1.Position.DistanceTo(position); var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - 12f))) - 1f / 0.6f; p1.Velocity += direction * collisionForce; } // particle relationship force - + var props = p1.Type.GetRelationship(p2.Type); - if (props.Force == 0f || !(distance >= props.MinRadius) || !(distance <= props.MaxRadius)) + if (props.Force == 0f || !(distanceSquared >= props.MinRadius * props.MinRadius) || !(distanceSquared <= props.MaxRadius * props.MaxRadius)) continue; + distance = p1.Position.DistanceTo(position); 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; } - + if (closeCount < 3 || closeCount > 35) p1.Health -= HealthDelta * 1.5f; else p1.Health += HealthDelta; - + + } + + 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; } } \ No newline at end of file