2021-12-26 16:45:02 -05:00
|
|
|
using System.Collections.Generic;
|
2021-12-26 14:58:58 -05:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
public class Main : Node2D
|
|
|
|
{
|
2021-12-26 16:45:02 -05:00
|
|
|
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
|
|
|
|
|
2021-12-26 14:58:58 -05:00
|
|
|
public override void _Ready()
|
|
|
|
{
|
2021-12-26 15:56:57 -05:00
|
|
|
GD.Randomize();
|
2021-12-26 16:45:02 -05:00
|
|
|
InitializeParticleTypes(5);
|
|
|
|
InitializeParticles(50);
|
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 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
private Vector2 GetRandomParticlePosition()
|
|
|
|
{
|
|
|
|
const int padding = 32;
|
|
|
|
var viewportRect = GetViewportRect();
|
|
|
|
var position = new Vector2((float) GD.RandRange(padding, viewportRect.Size.x - padding),
|
|
|
|
(float) GD.RandRange(padding, viewportRect.Size.y - padding));
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
}
|