forked from simeonradivoev/Galaxia-Runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarFinder.cs
117 lines (101 loc) · 3.51 KB
/
StarFinder.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
namespace Galaxia
{
/// <summary>
/// The star Finder helper component.
/// Used to find particular stars.
/// </summary>
public class StarFinder : MonoBehaviour
{
#region Private
[SerializeField]
private int m_separation = 0;
[SerializeField]
private Distribution m_distribution;
private List<int> randomIndexes;
#endregion
public List<Particle> Find(Particles particles, int count)
{
return Find(particles, DefaultChooser, m_distribution, count);
}
public List<Particle> Find(Particles particles, Distribution distribution, int count)
{
return Find(particles, DefaultChooser, distribution, count);
}
public List<Particle> Find(Particles particles, Chooser chosser,Distribution distribution, int count)
{
if(count <= particles.ParticleList.Length )
{
List<Particle> chosenParticles = new List<Particle>();
SetUpIncrement(distribution, particles, count);
for (int i = 0; i < particles.ParticleList.Length; i += GetIncrement(distribution, particles, count))
{
int index = GetIndex(distribution,particles,count,i);
if (index < particles.ParticleList.Length && chosenParticles.Count < count)
{
if (chosser(particles.ParticleList[index], index))
{
chosenParticles.Add(particles.ParticleList[index]);
}
}
else
{
break;
}
}
return chosenParticles;
}
else
{
Debug.LogError("Not enough stars in galaxy");
return new List<Particle>();
}
}
void SetUpIncrement(Distribution distribution,Particles particles,int count)
{
switch (distribution)
{
case Distribution.Random:
if (particles.Prefab != null)
Random.seed = particles.Prefab.Seed;
randomIndexes = Enumerable.Range(0, particles.ParticleList.Length).OrderBy(r => Random.Next()).ToList();
break;
}
}
int GetIncrement(Distribution distribution,Particles particles,int count)
{
switch(distribution)
{
case Distribution.Linear:
return m_separation;
case Distribution.Spread:
return Mathf.CeilToInt((float)particles.ParticleList.Length / (float)count);
default:
return 1;
}
}
int GetIndex(Distribution distribution, Particles particles, int count,int index)
{
switch (distribution)
{
case Distribution.Random:
return randomIndexes[index];
default:
return index;
}
}
bool DefaultChooser(Particle particle, int index)
{
return true;
}
public delegate bool Chooser(Particle particle,int index);
public enum Distribution
{
Spread,
Linear,
Random
}
}
}