1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Microsoft.Xna.Framework; |
---|
5 | using Microsoft.Xna.Framework.Audio; |
---|
6 | using Microsoft.Xna.Framework.Content; |
---|
7 | using Microsoft.Xna.Framework.GamerServices; |
---|
8 | using Microsoft.Xna.Framework.Graphics; |
---|
9 | using Microsoft.Xna.Framework.Input; |
---|
10 | using Microsoft.Xna.Framework.Media; |
---|
11 | using Microsoft.Xna.Framework.Net; |
---|
12 | using Microsoft.Xna.Framework.Storage; |
---|
13 | |
---|
14 | namespace Effects |
---|
15 | { |
---|
16 | /// <summary> |
---|
17 | /// This is the main type for your game |
---|
18 | /// </summary> |
---|
19 | public class ParticleGame : Microsoft.Xna.Framework.Game |
---|
20 | { |
---|
21 | GraphicsDeviceManager graphics; |
---|
22 | SpriteBatch spriteBatch; |
---|
23 | public SpriteBatch SpriteBatch |
---|
24 | { |
---|
25 | get { return spriteBatch; } |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | Particle[] particles; |
---|
30 | Random random = new Random(); |
---|
31 | const int amountOfParticles = 15; |
---|
32 | |
---|
33 | KeyboardState previousKeyboardState = Keyboard.GetState(); |
---|
34 | |
---|
35 | public ParticleGame() |
---|
36 | { |
---|
37 | graphics = new GraphicsDeviceManager(this); |
---|
38 | Content.RootDirectory = "Content"; |
---|
39 | } |
---|
40 | |
---|
41 | /// <summary> |
---|
42 | /// Allows the game to perform any initialization it needs to before starting to run. |
---|
43 | /// This is where it can query for any required services and load any non-graphic |
---|
44 | /// related content. Calling base.Initialize will enumerate through any components |
---|
45 | /// and initialize them as well. |
---|
46 | /// </summary> |
---|
47 | protected override void Initialize() |
---|
48 | { |
---|
49 | // TODO: Add your initialization logic here |
---|
50 | |
---|
51 | base.Initialize(); |
---|
52 | } |
---|
53 | |
---|
54 | /// <summary> |
---|
55 | /// LoadContent will be called once per game and is the place to load |
---|
56 | /// all of your content. |
---|
57 | /// </summary> |
---|
58 | protected override void LoadContent() |
---|
59 | { |
---|
60 | particles = new Particle[amountOfParticles]; |
---|
61 | Texture2D glowTexture = Content.Load<Texture2D>("Glow"); |
---|
62 | Texture2D glowTexture2 = Content.Load<Texture2D>("Glow2"); |
---|
63 | // Create a new SpriteBatch, which can be used to draw textures. |
---|
64 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
65 | for (int i = 0; i < particles.Length; i++) |
---|
66 | { |
---|
67 | particles[i] = new Particle(this, glowTexture, glowTexture2, new Vector2( |
---|
68 | (float)random.NextDouble() * graphics.GraphicsDevice.Viewport.Width, |
---|
69 | (float)random.NextDouble() * graphics.GraphicsDevice.Viewport.Height), 2.0f); |
---|
70 | particles[i].Scale = (float)random.NextDouble();//0.2f + (float)random.NextDouble() * 0.8f; |
---|
71 | particles[i].Velocity = new Vector2( |
---|
72 | (float)random.NextDouble() * 5 - 2.5f, |
---|
73 | (float)random.NextDouble() * 5 - 2.5f); |
---|
74 | |
---|
75 | } |
---|
76 | // TODO: use this.Content to load your game content here |
---|
77 | } |
---|
78 | |
---|
79 | /// <summary> |
---|
80 | /// UnloadContent will be called once per game and is the place to unload |
---|
81 | /// all content. |
---|
82 | /// </summary> |
---|
83 | protected override void UnloadContent() |
---|
84 | { |
---|
85 | // TODO: Unload any non ContentManager content here |
---|
86 | } |
---|
87 | |
---|
88 | /// <summary> |
---|
89 | /// Allows the game to run logic such as updating the world, |
---|
90 | /// checking for collisions, gathering input, and playing audio. |
---|
91 | /// </summary> |
---|
92 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
93 | protected override void Update(GameTime gameTime) |
---|
94 | { |
---|
95 | float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; |
---|
96 | KeyboardState keyboardState = Keyboard.GetState(); |
---|
97 | MouseState mouseState = Mouse.GetState(); |
---|
98 | // Allows the game to exit |
---|
99 | if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) |
---|
100 | this.Exit(); |
---|
101 | for (int i = 0; i < particles.Length; i++) |
---|
102 | { |
---|
103 | if (particles[i].Alive) |
---|
104 | { |
---|
105 | particles[i].CurrentTime += dt; |
---|
106 | particles[i].Scale = 1.0f - (particles[i].Lifetime * particles[i].CurrentTime); |
---|
107 | particles[i].Position += particles[i].Velocity; |
---|
108 | if (particles[i].CurrentTime > particles[i].Lifetime) |
---|
109 | { |
---|
110 | particles[i].Alive = false; |
---|
111 | } |
---|
112 | } |
---|
113 | if (!particles[i].Alive) |
---|
114 | { |
---|
115 | particles[i].Alive = true; |
---|
116 | particles[i].Lifetime = 2.0f; |
---|
117 | particles[i].CurrentTime = 0.0f; |
---|
118 | particles[i].Scale = 1.0f; |
---|
119 | particles[i].Position = new Vector2( |
---|
120 | (float)mouseState.X, |
---|
121 | (float)mouseState.Y); |
---|
122 | particles[i].Velocity = new Vector2( |
---|
123 | (float)random.NextDouble() * 5 - 2.5f, |
---|
124 | (float)random.NextDouble() * 5 - 2.5f); |
---|
125 | //particles[i].Position = new Vector2( |
---|
126 | //(float)random.NextDouble() * graphics.GraphicsDevice.Viewport.Width, |
---|
127 | //(float)random.NextDouble() * graphics.GraphicsDevice.Viewport.Height); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | // TODO: Add your update logic here |
---|
132 | |
---|
133 | previousKeyboardState = keyboardState; |
---|
134 | base.Update(gameTime); |
---|
135 | } |
---|
136 | |
---|
137 | /// <summary> |
---|
138 | /// This is called when the game should draw itself. |
---|
139 | /// </summary> |
---|
140 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
141 | protected override void Draw(GameTime gameTime) |
---|
142 | { |
---|
143 | GraphicsDevice.Clear(Color.Black); |
---|
144 | // TODO: Add your drawing code here |
---|
145 | |
---|
146 | base.Draw(gameTime); |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|