Screen wrapping

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

84
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,17 +11,18 @@ 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);
} }
public override void _Process(float delta) public override void _Process(float delta)
@ -47,8 +49,8 @@ public class Main : Node2D
foreach (var type1 in _particleTypes) foreach (var type1 in _particleTypes)
foreach (var type2 in _particleTypes) foreach (var type2 in _particleTypes)
type1.AddRelationship(type2, type1.AddRelationship(type2,
new ParticleRelationshipProps(20, (float) GD.RandRange(25, 60), new ParticleRelationshipProps(20, (float) GD.RandRange(25, 60),
(float) GD.RandRange(-0.33 * 2f, 0.35 * 2f))); (float) GD.RandRange(-0.33 * 2f, 0.35 * 2f)));
} }
@ -83,11 +85,11 @@ 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);
} }
private Vector2 GetRandomParticlePosition() private Vector2 GetRandomParticlePosition()
{ {
var viewportRect = GetViewportRect(); var viewportRect = GetViewportRect();
@ -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,19 +124,29 @@ 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)
p.Health -= HealthDelta * 1.5f; p.Health -= HealthDelta * 1.5f;
else else
p.Health += HealthDelta; p.Health += HealthDelta;
if (deletedParticle == false && p.Health == 0f) if (deletedParticle == false && p.Health == 0f)
{ {
if (GD.Randf() < 0.2f) if (GD.Randf() < 0.2f)
@ -141,8 +156,10 @@ 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)
@ -158,38 +175,55 @@ 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;
p1.Velocity += direction * particleForce; p1.Velocity += direction * particleForce;
} }
if (closeCount < 3 || closeCount > 35) if (closeCount < 3 || closeCount > 35)
p1.Health -= HealthDelta * 1.5f; p1.Health -= HealthDelta * 1.5f;
else else
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;
} }
} }