particles/Main.cs

64 lines
2.0 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;
using Particles;
2021-12-26 14:58:58 -05:00
public class Main : Node2D
{
2021-12-27 14:58:33 -05:00
private ParticleSimulation _particleSimulation;
private Node2D _particleNodes;
2021-12-26 14:58:58 -05:00
public override void _Ready()
{
_particleNodes = GetNode<Node2D>("ParticlesNodes");
2021-12-26 15:56:57 -05:00
GD.Randomize();
_particleSimulation = new ParticleSimulation
{
ScreenSize = GetViewportRect().Size
};
_particleSimulation.Initialize();
foreach (var id in _particleSimulation.LastParticlesAdded)
{
CreateParticleNode(id);
}
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)
{
_particleSimulation.Update();
foreach (var id in _particleSimulation.LastParticlesRemoved)
2021-12-26 16:45:02 -05:00
{
var particleNode = _particleNodes.GetNode<ParticleNode>(id.ToString());
particleNode.Free();
2021-12-26 16:45:02 -05:00
}
foreach (var id in _particleSimulation.LastParticlesAdded)
2021-12-26 14:58:58 -05:00
{
CreateParticleNode(id);
2021-12-26 14:58:58 -05:00
}
2021-12-27 16:10:07 -05:00
var particleNodes = _particleNodes.GetChildren();
foreach (ParticleNode particleNode in particleNodes)
2021-12-26 23:08:40 -05:00
{
var simulationParticle = _particleSimulation.GetParticle(particleNode.SimulationId);
particleNode.Position = simulationParticle.Position;
particleNode.SetColor(simulationParticle.Type.Hue, simulationParticle.Health);
2021-12-26 23:08:40 -05:00
}
2021-12-27 16:10:07 -05:00
}
private void CreateParticleNode(int id)
2021-12-27 16:10:07 -05:00
{
var particleScene = GD.Load<PackedScene>("res://ParticleNode.tscn");
var particleNode = particleScene.Instance<ParticleNode>();
particleNode.Name = id.ToString();
particleNode.SimulationId = id;
_particleNodes.AddChild(particleNode);
2021-12-26 21:16:28 -05:00
}
2021-12-26 15:56:57 -05:00
}