Random Particles

This commit is contained in:
orosmatthew 2021-12-26 15:56:57 -05:00
parent b84e5d4614
commit 49d664e472
9 changed files with 99 additions and 13 deletions

29
Main.cs
View File

@ -1,18 +1,37 @@
using Godot;
using System;
public class Main : Node2D
{
public override void _Ready()
{
GD.Randomize();
InitializeParticles();
}
public override void _Process(float delta)
{
if (Input.IsActionJustPressed("quit"))
if (Input.IsActionJustPressed("quit")) GetTree().Quit();
if (Input.IsActionJustPressed("reset")) GetTree().ReloadCurrentScene();
}
private void InitializeParticles()
{
var particleScene = GD.Load<PackedScene>("res://Particle.tscn");
for (var i = 0; i < 50; i++)
{
GetTree().Quit();
var particle = particleScene.Instance<Particle>();
GetNode<Node2D>("Particles").AddChild(particle);
particle.Position = GetRandomParticlePosition();
particle.Hue = (float) GD.RandRange(0, 1);
}
}
}
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;
}
}

View File

@ -4,3 +4,5 @@
[node name="Main" type="Node2D"]
script = ExtResource( 1 )
[node name="Particles" type="Node2D" parent="."]

22
Particle.cs Normal file
View File

@ -0,0 +1,22 @@
using Godot;
public class Particle : Node2D
{
private float _spriteHue;
private Sprite _spriteNode;
public float Hue
{
get => _spriteHue;
set
{
_spriteHue = Mathf.Clamp(value, 0, 1);
_spriteNode.Modulate = Color.FromHsv(_spriteHue, 1, 1);
}
}
public override void _Ready()
{
_spriteNode = GetNode<Sprite>("Sprite");
}
}

10
Particle.tscn Normal file
View File

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://textures/particle.png" type="Texture" id=1]
[ext_resource path="res://Particle.cs" type="Script" id=2]
[node name="Particle" type="Node2D"]
script = ExtResource( 2 )
[node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 1 )

View File

@ -1,7 +0,0 @@
[gd_resource type="Environment" load_steps=2 format=2]
[sub_resource type="ProceduralSky" id=1]
[resource]
background_mode = 2
background_sky = SubResource( 1 )

BIN
icon.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -28,6 +28,11 @@ quit={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":16777217,"unicode":0,"echo":false,"script":null)
]
}
reset={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":82,"unicode":0,"echo":false,"script":null)
]
}
[physics]
@ -38,4 +43,4 @@ common/enable_pause_aware_picking=true
quality/driver/driver_name="GLES2"
vram_compression/import_etc=true
vram_compression/import_etc2=false
environment/default_environment="res://default_env.tres"
environment/default_clear_color=Color( 0, 0, 0, 1 )

BIN
textures/particle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/particle.png-6bc2982443cc9a0ea7820fc4c0d2daf7.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://textures/particle.png"
dest_files=[ "res://.import/particle.png-6bc2982443cc9a0ea7820fc4c0d2daf7.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0