Screen wrapping

This commit is contained in:
orosmatthew 2021-12-27 16:10:07 -05:00
parent 22ae297532
commit 8406578b80

62
Main.cs
View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,15 +11,16 @@ public class Main : Node2D
private readonly List<ParticleType> _particleTypes = new List<ParticleType>(); private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
private List<Particle> _particles; private List<Particle> _particles;
private const int MaxParticles = 700; private const int MaxParticles = 600;
private const float HealthDelta = 0.002f; private const float HealthDelta = 0.002f;
private const float PositiveHealthMultiplier = 2f; private const float PositiveHealthMultiplier = 2f;
private Task[] tasks = new Task[MaxParticles];
public override void _Ready() public override void _Ready()
{ {
GD.Randomize(); GD.Randomize();
InitializeParticleTypes(12); InitializeParticleTypes(10);
InitializeParticles(MaxParticles); InitializeParticles(MaxParticles);
} }
@ -83,7 +85,7 @@ public class Main : Node2D
private void CreateRandomParticle() private void CreateRandomParticle()
{ {
var randomIndex = (int)(GD.Randi() % _particleTypes.Count); var randomIndex = (int) (GD.Randi() % _particleTypes.Count);
var type = _particleTypes[randomIndex]; var type = _particleTypes[randomIndex];
CreateParticle(type); CreateParticle(type);
} }
@ -100,14 +102,17 @@ public class Main : Node2D
private void UpdateParticles() private void UpdateParticles()
{ {
_particles = GetNode<Node2D>("Particles").GetChildren().Cast<Particle>().ToList(); _particles = GetNode<Node2D>("Particles").GetChildren().Cast<Particle>().ToList();
var tasks = new Task[_particles.Count];
var viewportRect = GetViewportRect(); 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); Task.WaitAll(tasks);
var deletedParticle = false; var deletedParticle = false;
foreach (var p in _particles) foreach (var p in _particles)
{ {
if (deletedParticle == false && p.Health == 0f) if (deletedParticle == false && p.Health == 0f)
@ -119,12 +124,22 @@ public class Main : Node2D
continue; continue;
} }
} }
var screenSize = viewportRect.Size;
var position = p.Position; var position = p.Position;
p.Velocity = p.Velocity.Clamped(3f * 4f); p.Velocity = p.Velocity.Clamped(3f * 4f);
position += p.Velocity; position += p.Velocity;
p.Velocity *= 0.875f; // friction p.Velocity *= 0.875f; // friction
position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding); if (position.x > screenSize.x)
position.y = Mathf.Clamp(position.y, ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding); 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)); p.AddAverageSpeedValue(p.Position.DistanceTo(position));
if (p.AverageSpeed < 0.3f) if (p.AverageSpeed < 0.3f)
@ -141,9 +156,11 @@ public class Main : Node2D
continue; continue;
} }
} }
p.Position = position; p.Position = position;
} }
if (_particles.Count < MaxParticles) if (_particles.Count < MaxParticles)
if (GD.Randf() < 0.3f) if (GD.Randf() < 0.3f)
CreateRandomParticle(); CreateRandomParticle();
@ -158,28 +175,31 @@ public class Main : Node2D
var viewportRect = GetViewportRect(); var viewportRect = GetViewportRect();
if (p1 == p2) if (p1 == p2)
continue; continue;
var distance = p1.Position.DistanceTo(p2.Position); var position = GetScreenWrapPosition(viewportRect.Size, p1.Position, p2.Position);
if (distance > 60f) var distanceSquared = p1.Position.DistanceSquaredTo(position);
if (distanceSquared > (60f * 60f))
continue; continue;
var direction = p1.Position.DirectionTo(p2.Position); var direction = p1.Position.DirectionTo(position);
var distance = 0f;
if (distanceSquared < (70f * 70f))
if (distance < 70)
{ {
closeCount++; closeCount++;
} }
// collision force // 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; var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - 12f))) - 1f / 0.6f;
p1.Velocity += direction * collisionForce; p1.Velocity += direction * collisionForce;
} }
// particle relationship force // particle relationship force
var props = p1.Type.GetRelationship(p2.Type); 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; continue;
distance = p1.Position.DistanceTo(position);
var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f); var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f);
var particleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) + var particleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) +
props.Force; props.Force;
@ -192,4 +212,18 @@ public class Main : Node2D
p1.Health += HealthDelta; 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;
}
} }