restructured project
This commit is contained in:
38
ParticleSimulation/ParticleNode.cs
Normal file
38
ParticleSimulation/ParticleNode.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Godot;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public class ParticleNode : Node2D
|
||||
{
|
||||
private Label _labelNode;
|
||||
private Sprite _spriteNode;
|
||||
public Vector2 CurrentSimulationPosition = new Vector2();
|
||||
|
||||
public Vector2 LastSimulationPosition;
|
||||
public bool WasTeleportedLast = true;
|
||||
public int SimulationId;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_spriteNode = GetNode<Sprite>("Sprite");
|
||||
_labelNode = GetNode<Label>("Label");
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
Position = WasTeleportedLast == false
|
||||
? LastSimulationPosition.LinearInterpolate(CurrentSimulationPosition,
|
||||
GetParent<Node2D>().GetParent<ParticleSimulationScene>().PhysicsInterpolationFraction)
|
||||
: CurrentSimulationPosition;
|
||||
}
|
||||
|
||||
public void SetLabelText(string text)
|
||||
{
|
||||
_labelNode.Text = text;
|
||||
}
|
||||
|
||||
public void SetColor(float hue, float saturation, float opacity)
|
||||
{
|
||||
_spriteNode.Modulate = Color.FromHsv(hue, saturation, 1, opacity);
|
||||
}
|
||||
}
|
||||
23
ParticleSimulation/ParticleNode.tscn
Normal file
23
ParticleSimulation/ParticleNode.tscn
Normal file
@@ -0,0 +1,23 @@
|
||||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://ParticleSimulation/ParticleNode.cs" type="Script" id=1]
|
||||
[ext_resource path="res://textures/particle_noborder.png" type="Texture" id=2]
|
||||
|
||||
|
||||
[node name="ParticleNode" type="Node2D"]
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="Sprite" type="Sprite" parent="."]
|
||||
scale = Vector2( 0.25, 0.25 )
|
||||
texture = ExtResource( 2 )
|
||||
|
||||
[node name="Label" type="Label" parent="."]
|
||||
visible = false
|
||||
margin_left = -18.0
|
||||
margin_top = -23.0
|
||||
margin_right = 18.0
|
||||
margin_bottom = -9.0
|
||||
text = "0.05"
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
69
ParticleSimulation/ParticleSimulationScene.cs
Normal file
69
ParticleSimulation/ParticleSimulationScene.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Godot;
|
||||
using Particles.ParticleSimulation;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public class ParticleSimulationScene : Node2D
|
||||
{
|
||||
private Node2D _particleNodes;
|
||||
|
||||
private ParticleSimulation _particleSimulation;
|
||||
public float PhysicsInterpolationFraction;
|
||||
|
||||
public void Initialize(int nParticles)
|
||||
{
|
||||
_particleNodes = GetNode<Node2D>("ParticleNodes");
|
||||
GD.Randomize();
|
||||
ulong randomSeed = GD.Randi();
|
||||
GD.Seed(randomSeed);
|
||||
GD.Print("Last Seed: " + randomSeed);
|
||||
var viewSize = GetViewportRect().Size;
|
||||
var zoom = GetNode<Camera2D>("Camera2D").Zoom;
|
||||
var spaceSize = new Vector2(viewSize.x * zoom.x, viewSize.y * zoom.y);
|
||||
_particleSimulation = new ParticleSimulation
|
||||
{
|
||||
SpaceSize = spaceSize
|
||||
};
|
||||
_particleSimulation.Initialize(nParticles);
|
||||
foreach (var id in _particleSimulation.LastParticlesAdded) CreateParticleNode(id);
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
if (Input.IsActionJustPressed("quit")) GetTree().Quit();
|
||||
if (Input.IsActionJustPressed("reset")) GetTree().ReloadCurrentScene();
|
||||
PhysicsInterpolationFraction = Engine.GetPhysicsInterpolationFraction();
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(float delta)
|
||||
{
|
||||
_particleSimulation.Update();
|
||||
foreach (var id in _particleSimulation.LastParticlesRemoved)
|
||||
{
|
||||
var particleNode = _particleNodes.GetNode<ParticleNode>(id.ToString());
|
||||
particleNode.Free();
|
||||
}
|
||||
|
||||
foreach (var id in _particleSimulation.LastParticlesAdded) CreateParticleNode(id);
|
||||
|
||||
var particleNodes = _particleNodes.GetChildren();
|
||||
foreach (ParticleNode particleNode in particleNodes)
|
||||
{
|
||||
var simulationParticle = _particleSimulation.GetParticle(particleNode.SimulationId);
|
||||
particleNode.LastSimulationPosition = particleNode.Position;
|
||||
particleNode.CurrentSimulationPosition = simulationParticle.Position;
|
||||
particleNode.SetColor(simulationParticle.Type.Hue, simulationParticle.Health,
|
||||
Mathf.Clamp(simulationParticle.AverageSpeed / 1.5f, 1f, 1f));
|
||||
particleNode.WasTeleportedLast = simulationParticle.WasTeleportedLast;
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateParticleNode(int id)
|
||||
{
|
||||
var particleScene = GD.Load<PackedScene>("res://ParticleSimulation/ParticleNode.tscn");
|
||||
var particleNode = particleScene.Instance<ParticleNode>();
|
||||
particleNode.Name = id.ToString();
|
||||
particleNode.SimulationId = id;
|
||||
_particleNodes.AddChild(particleNode);
|
||||
}
|
||||
}
|
||||
14
ParticleSimulation/ParticleSimulationScene.tscn
Normal file
14
ParticleSimulation/ParticleSimulationScene.tscn
Normal file
@@ -0,0 +1,14 @@
|
||||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://ParticleSimulation/ParticleSimulationScene.cs" type="Script" id=1]
|
||||
|
||||
|
||||
[node name="ParticleSimulationScene" type="Node2D"]
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="ParticleNodes" type="Node2D" parent="."]
|
||||
|
||||
[node name="Camera2D" type="Camera2D" parent="."]
|
||||
anchor_mode = 0
|
||||
current = true
|
||||
zoom = Vector2( 1.35, 1.35 )
|
||||
Reference in New Issue
Block a user