Jypeli  5
The simple game programming library
Lightning.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
5 
6 namespace Jypeli.Effects
7 {
8  internal class LightningLayer
9  {
10  public List<LightningNode> Nodes
11  {
12  get { return nodes; }
13  set { nodes = value; }
14  }
15 
16  private List<LightningNode> nodes;
17 
18  private double y;
19  public double Y { get { return y; } set { y = value; } }
20 
21  public LightningLayer()
22  {
23  nodes = new List<LightningNode>();
24  }
25 
26  public void Add(LightningNode n)
27  {
28  nodes.Add(n);
29  }
30  }
31 
32  internal class LightningNode
33  {
34  private LightningNode parentNode;
35  private List<LightningNode> childNodes;
36  public List<LightningNode> ChildNodes { get { return childNodes; } }
37 
38  private double splitChance = 0;
39  public double SplitChance { get { return splitChance; } }
40 
41  private Vector position;
42  public Vector Position { get { return position; } set { position = value; } }
43  public LightningNode()
44  {
45  childNodes = new List<LightningNode>();
46  }
47 
48  public LightningNode(Vector position)
49  {
50  this.position = position;
51  childNodes = new List<LightningNode>();
52  }
53 
54  public void AddChild(Vector position)
55  {
56  AddChild(new LightningNode(position));
57  }
58 
59  public void AddChild(LightningNode node)
60  {
61  node.parentNode = this;
62  this.childNodes.Add(node);
63  }
64  }
65 
66 
67  public class Lightning : ParticleSystem
68  {
69  private LightningLayer[] layers;
70  //public LightningLayer[] Layers { get { return layers; } }
71  private List<LightningNode> nodes;
72  private int layerAmount = 7;
73 
74  private int currentLayer;
75 
76  private double height;
77  private double width;
78 
79  private bool striking = false;
80  private double strikeSpeed = 0.0;
81 
82  private Color levelColor;
83 
84  private int particlesPerLayer;
85 
86  public Lightning(Image particleImage, int maxAmountOfParticles)
87  : base(particleImage, maxAmountOfParticles)
88  {
89  InitializeLayers();
90  particlesPerLayer = maxAmountOfParticles / layers.Length;
91  }
92 
93  protected override void InitializeParticle(Particle p, Vector position)
94  {
95  p.Initialize(position, 2.0, 0.0, 0.0, Vector.Zero, Vector.Zero, 0.4);
96  }
97 
98  public override void Update(Time time)
99  {
100  double t = time.SinceLastUpdate.TotalSeconds;
101  strikeSpeed += t;
102  if (striking)
103  {
104  if (strikeSpeed > 0.01)
105  {
106  foreach (LightningNode node in layers[currentLayer].Nodes)
107  {
108  foreach (LightningNode child in node.ChildNodes)
109  {
110  Vector v = child.Position - node.Position;
111  int particlesPerBranch = particlesPerLayer / node.ChildNodes.Count;
112  for (int i = 0; i < particlesPerBranch; i++)
113  {
114  base.AddEffect(node.Position + v*(i/(double)particlesPerBranch), 1);
115  }
116  }
117  //base.AddEffect(node.Position, 1);
118  }
119  currentLayer++;
120  strikeSpeed = 0.0;
121  if (currentLayer >= layers.Length)
122  {
123  striking = false;
124  currentLayer = 0;
125  }
126  }
127  }
128  base.Update(time);
129  }
130 
131  private void InitializeLayers()
132  {
133  layers = new LightningLayer[layerAmount];
134  for (int i = 0; i < layers.Length; i++)
135  {
136  layers[i] = new LightningLayer();
137  }
138  }
139 
140  public void Strike(Vector startPoint, double width, double height)
141  {
142  double branchHeight = height / layerAmount;
143  InitializeLayers();
144  LightningNode startNode = new LightningNode(startPoint);
145  layers[0].Add(startNode);
146  layers[0].Y = startNode.Position.Y;
147  //startNode.AddChild(startPoint);
148 
149  for (int i = 1; i < layers.Length - 1; i++)
150  {
151  for (int j = 0; j < layers[i - 1].Nodes.Count; j++)
152  {
153  LightningNode currentNode = layers[i - 1].Nodes[j];
154  layers[i].Y = layers[i - 1].Y - branchHeight;
155  if (RandomGen.NextDouble(0, 100) < layers[i - 1].Nodes[j].SplitChance)
156  {
157  Vector pLeft = new Vector(RandomGen.NextDouble(currentNode.Position.X - width / 6, currentNode.Position.X), layers[i].Y);
158  Vector pRight = new Vector(RandomGen.NextDouble(currentNode.Position.X, currentNode.Position.X + width / 6), layers[i].Y);
159  LightningNode nLeft = new LightningNode(pLeft);
160  LightningNode nRight = new LightningNode(pRight);
161  layers[i].Nodes.Add(nLeft);
162  currentNode.AddChild(nLeft);
163  layers[i].Nodes.Add(nRight);
164  currentNode.AddChild(nRight);
165 
166  }
167  Vector p = new Vector(RandomGen.NextDouble(startPoint.X - width / 4, startPoint.X + width / 4), layers[i].Y);
168  LightningNode n = new LightningNode(p);
169  layers[i].Nodes.Add(n);
170  currentNode.AddChild(n);
171  }
172  }
173  striking = true;
174  }
175  }
176 }
Järjestelmä partikkelien käsittelyyn
Lightning(Image particleImage, int maxAmountOfParticles)
Definition: Lightning.cs:86
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:78
TimeSpan SinceLastUpdate
Aika joka on kulunut viime päivityksestä.
Definition: Time.cs:24
static readonly Vector Zero
Nollavektori.
Definition: Vector.cs:61
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:13
override void InitializeParticle(Particle p, Vector position)
Alustaa yhden partikkelin
Definition: Lightning.cs:93
void Strike(Vector startPoint, double width, double height)
Definition: Lightning.cs:140
Kuva.
Definition: Image.cs:24
double X
Definition: Vector.cs:274
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:98
Väri.
Definition: Color.cs:13
void Initialize(Vector position, double scale, double rotation, double rotationSpeed, Vector velocity, Vector acceleration, double lifetime)
Alusta partikkeli
Definition: Particle.cs:113
2D-vektori.
Definition: Vector.cs:56