Jypeli 10
The simple game programming library
Lightning.cs
Siirry tämän tiedoston dokumentaatioon.
1using System.Collections.Generic;
2
3namespace Jypeli.Effects
4{
5 internal class LightningLayer
6 {
7 public List<LightningNode> Nodes
8 {
9 get { return nodes; }
10 set { nodes = value; }
11 }
12
13 private List<LightningNode> nodes;
14
15 private double y;
16 public double Y { get { return y; } set { y = value; } }
17
19 {
20 nodes = new List<LightningNode>();
21 }
22
23 public void Add(LightningNode n)
24 {
25 nodes.Add(n);
26 }
27 }
28
29 internal class LightningNode
30 {
32 private List<LightningNode> childNodes;
33 public List<LightningNode> ChildNodes { get { return childNodes; } }
34
35 private double splitChance = 0;
36 public double SplitChance { get { return splitChance; } }
37
39 public Vector Position { get { return position; } set { position = value; } }
41 {
42 childNodes = new List<LightningNode>();
43 }
44
46 {
47 this.position = position;
48 childNodes = new List<LightningNode>();
49 }
50
52 {
54 }
55
56 public void AddChild(LightningNode node)
57 {
58 node.parentNode = this;
59 this.childNodes.Add(node);
60 }
61 }
62
67 {
69 //public LightningLayer[] Layers { get { return layers; } }
70 //private List<LightningNode> nodes;
71 private int layerAmount = 7;
72
73 private int currentLayer;
74
75 //private double height;
76 //private double width;
77
78 private bool striking = false;
79 private double strikeSpeed = 0.0;
80
81 //private Color levelColor;
82
83 private int particlesPerLayer;
84
90 public Lightning(Image particleImage, int maxAmountOfParticles)
91 : base(particleImage, maxAmountOfParticles)
92 {
94 particlesPerLayer = maxAmountOfParticles / layers.Length;
95 }
96
98 protected override void InitializeParticle(Particle p, Vector position)
99 {
100 p.Initialize(position, 2.0, 0.0, 0.0, Vector.Zero, Vector.Zero, 0.4);
101 }
102
104 public override void Update(Time time)
105 {
106 double t = time.SinceLastUpdate.TotalSeconds;
107 strikeSpeed += t;
108 if (striking)
109 {
110 if (strikeSpeed > 0.01)
111 {
112 foreach (LightningNode node in layers[currentLayer].Nodes)
113 {
114 foreach (LightningNode child in node.ChildNodes)
115 {
116 Vector v = child.Position - node.Position;
117 int particlesPerBranch = particlesPerLayer / node.ChildNodes.Count;
118 for (int i = 0; i < particlesPerBranch; i++)
119 {
120 base.AddEffect(node.Position + v*(i/(double)particlesPerBranch), 1);
121 }
122 }
123 //base.AddEffect(node.Position, 1);
124 }
125 currentLayer++;
126 strikeSpeed = 0.0;
127 if (currentLayer >= layers.Length)
128 {
129 striking = false;
130 currentLayer = 0;
131 }
132 }
133 }
134 base.Update(time);
135 }
136
137 private void InitializeLayers()
138 {
140 for (int i = 0; i < layers.Length; i++)
141 {
142 layers[i] = new LightningLayer();
143 }
144 }
145
152 public void Strike(Vector startPoint, double width, double height)
153 {
154 double branchHeight = height / layerAmount;
156 LightningNode startNode = new LightningNode(startPoint);
157 layers[0].Add(startNode);
158 layers[0].Y = startNode.Position.Y;
159 //startNode.AddChild(startPoint);
160
161 for (int i = 1; i < layers.Length - 1; i++)
162 {
163 for (int j = 0; j < layers[i - 1].Nodes.Count; j++)
164 {
165 LightningNode currentNode = layers[i - 1].Nodes[j];
166 layers[i].Y = layers[i - 1].Y - branchHeight;
167 if (RandomGen.NextDouble(0, 100) < layers[i - 1].Nodes[j].SplitChance)
168 {
169 Vector pLeft = new Vector(RandomGen.NextDouble(currentNode.Position.X - width / 6, currentNode.Position.X), layers[i].Y);
170 Vector pRight = new Vector(RandomGen.NextDouble(currentNode.Position.X, currentNode.Position.X + width / 6), layers[i].Y);
171 LightningNode nLeft = new LightningNode(pLeft);
172 LightningNode nRight = new LightningNode(pRight);
173 layers[i].Nodes.Add(nLeft);
174 currentNode.AddChild(nLeft);
175 layers[i].Nodes.Add(nRight);
176 currentNode.AddChild(nRight);
177
178 }
179 Vector p = new Vector(RandomGen.NextDouble(startPoint.X - width / 4, startPoint.X + width / 4), layers[i].Y);
180 LightningNode n = new LightningNode(p);
181 layers[i].Nodes.Add(n);
182 currentNode.AddChild(n);
183 }
184 }
185 striking = true;
186 }
187 }
188}
void Strike(Vector startPoint, double width, double height)
Luo salamaniskun lähtien annetusta pisteestä
Definition: Lightning.cs:152
LightningLayer[] layers
Definition: Lightning.cs:68
override void Update(Time time)
Peliolion päivitys. Tätä kutsutaan, kun IsUpdated-ominaisuuden arvoksi on asetettu true ja olio on li...
Definition: Lightning.cs:104
Lightning(Image particleImage, int maxAmountOfParticles)
Salama
Definition: Lightning.cs:90
override void InitializeParticle(Particle p, Vector position)
Alustaa yhden partikkelin
Definition: Lightning.cs:98
void Add(LightningNode n)
Definition: Lightning.cs:23
List< LightningNode > nodes
Definition: Lightning.cs:13
List< LightningNode > Nodes
Definition: Lightning.cs:8
void AddChild(Vector position)
Definition: Lightning.cs:51
List< LightningNode > childNodes
Definition: Lightning.cs:32
List< LightningNode > ChildNodes
Definition: Lightning.cs:33
LightningNode parentNode
Definition: Lightning.cs:31
void AddChild(LightningNode node)
Definition: Lightning.cs:56
LightningNode(Vector position)
Definition: Lightning.cs:45
void Initialize(Vector position, double scale, double rotation, double rotationSpeed, Vector velocity, Vector acceleration, double lifetime)
Alusta partikkeli
Definition: Particle.cs:112
Järjestelmä partikkelien käsittelyyn
Kuva.
Definition: Image.cs:30
Satunnaisgeneraattori. Luo satunnaisia arvoja, mm. lukuja, vektoreita sekä kulmia.
Definition: RandomGen.cs:39
static double NextDouble(double min, double max)
Palauttaa satunnaisen liukuluvun parametrien
Definition: RandomGen.cs:70
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
TimeSpan SinceLastUpdate
Aika joka on kulunut viime päivityksestä.
Definition: Time.cs:27
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
static readonly Vector Zero
Nollavektori.
Definition: Vector.cs:71
double X
Vektorin X-komponentti.
Definition: Vector.cs:334