-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClouds.cs
More file actions
82 lines (65 loc) · 2.01 KB
/
Clouds.cs
File metadata and controls
82 lines (65 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Godot;
using System;
using System.Collections.Generic;
public class Clouds : Node2D
{
[Export]
public List<Texture> cloudTextures;
[Export]
public int cloudCount = 10;
[Export]
public int maxY = 200;
private List<Cloud> clouds;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
public void Reset()
{
GD.Print("Reset clouds");
}
public void Init()
{
GD.Print("Initializing Clouds...");
clouds = GenerateClouds(cloudCount);
}
private List<Cloud> GenerateClouds(int count)
{
GD.Randomize();
var generatedClouds = new List<Cloud>(count);
for (int i = 0; i < count; i++)
{
var cloud = new Cloud();
cloud.Texture = cloudTextures[(int) (GD.RandRange(0, cloudTextures.Count - 1))];
cloud.RectGlobalPosition = new Vector2(GetViewportRect().Size.x * GD.Randf(), maxY * GD.Randf());
cloud.Visible = true;
cloud.MouseFilter = Control.MouseFilterEnum.Ignore;
cloud.Init();
generatedClouds.Add(cloud);
AddChild(cloud);
}
return generatedClouds;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
if (clouds == null) return;
foreach (var cloud in clouds)
{
cloud.Move(delta);
if (cloud.RectGlobalPosition.x > GetViewportRect().Size.x) {
Respawn(cloud);
}
}
}
private void Respawn(Cloud cloud)
{
//GD.Print("Respawn cloud");
var visible = GD.Randf() < 0.3;
cloud.Visible = visible;
if (!visible) return;
//cloud.Texture = cloudTextures[(int) (GD.RandRange(0, cloudTextures.Count - 1))];
cloud.Rerandomize();
cloud.RectGlobalPosition = new Vector2(0 - cloud.RectSize.x * cloud.RectScale.x, maxY * GD.Randf());
}
}