-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.cs
58 lines (45 loc) · 1.42 KB
/
World.cs
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
using System;
using System.Linq;
using Godot;
namespace Fish;
public partial class World : Node3D
{
private PackedScene _fishScene;
private Fish[] _fish;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
base._Ready();
_fishScene = GD.Load<PackedScene>("res://Fish.tscn");
// Spawn more fish!
_fish = Enumerable.Repeat(0, 100)
.Select(_ => CreateFish())
.ToArray();
foreach (var fish in _fish)
{
AddChild(fish);
}
}
private Fish CreateFish()
{
var fish = (Fish)_fishScene.Instantiate();
fish.WorldNode = this;
fish.Forward = RandomVector();
fish.DirectionDuration = NextSingle() * 10;
fish.GlobalPosition = RandomPosition();
return fish;
}
private static Vector3 RandomVector()
=> new Vector3(NegOneToOneSingle(), NegOneToOneSingle(), NegOneToOneSingle()).Normalized();
private Vector3 RandomPosition()
{
// Start location is based on world location
var pole = new Vector3(0, 51 + NextSingle() * 5, 0);
var randomPosition = pole.Rotated(RandomVector(), Mathf.Tau * NextSingle());
return randomPosition + GlobalPosition;
}
private static float NegOneToOneSingle()
=> (NextSingle() * 2) - 1;
private static float NextSingle()
=> Random.Shared.NextSingle();
}