]> git.r.bdr.sh - rbdr/super-polarity/blob - SuperPolarity/Actors/MainShip.cs
More spaces removed.
[rbdr/super-polarity] / SuperPolarity / Actors / MainShip.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.Graphics;
9 using Microsoft.Xna.Framework.Audio;
10
11 namespace SuperPolarity
12 {
13 class MainShip : Ship
14 {
15
16 public static Color BlueColor;
17 public static Color RedColor;
18
19 ParticleEngine particleEngine;
20
21 protected bool Shooting;
22 protected int ShotCooldown;
23
24 protected float CurrentImmortalTime;
25 protected float MaxImmortalTime;
26 protected bool Flashing;
27
28 protected SoundEffect PolarityChange;
29 protected SoundEffect ShootSound;
30 protected SoundEffect Hit;
31
32 public MainShip(SuperPolarity newGame) : base(newGame) {}
33
34 ~MainShip()
35 {
36 particleEngine = null;
37 }
38
39 public override void Initialize(Texture2D texture, Vector2 position)
40 {
41 base.Initialize(texture, position);
42
43 MainShip.BlueColor = new Color(211, 230, 234);
44 MainShip.RedColor = new Color(235, 160, 185);
45
46 PolarityChange = game.Content.Load<SoundEffect>("Sound\\polaritychange");
47 ShootSound = game.Content.Load<SoundEffect>("Sound\\bullet");
48 Hit = game.Content.Load<SoundEffect>("Sound\\hit");
49
50 InitParticleEngine();
51 SetPolarity(Polarity.Positive);
52
53 ActVelocity = 2.5f;
54
55 ShotCooldown = 50;
56 MaxImmortalTime = 1500;
57
58 BoxDimensions.X = 2;
59 BoxDimensions.Y = 2;
60 BoxDimensions.W = 2;
61 BoxDimensions.Z = 2;
62 InitBox();
63
64 BindInput();
65 }
66
67 void InitParticleEngine()
68 {
69 particleEngine = ParticleEffectFactory.CreatePolarCircle(Position);
70 }
71
72 void BindInput()
73 {
74 InputController.Bind("moveX", HandleHorizontalMovement);
75 InputController.Bind("moveY", HandleVerticalMovement);
76 InputController.Bind("changePolarity", HandleChangePolarity);
77 InputController.Bind("shoot", HandleShot);
78 }
79
80 protected void HandleShot(float value)
81 {
82
83 Shooting = true;
84 Timer t = new Timer(new TimerCallback(UnlockShot));
85 t.Change(ShotCooldown, Timeout.Infinite);
86
87 if (Children.Count > 10)
88 {
89 return;
90 }
91
92 var bullet = ActorFactory.CreateBullet(Position, Angle);
93
94 Children.Add(bullet);
95 bullet.Parent = this;
96
97 ShootSound.Play();
98 }
99
100 protected void UnlockShot(object state)
101 {
102 InputController.Unlock("shoot");
103 Shooting = false;
104 }
105
106 protected void HandleChangePolarity(float value)
107 {
108 SwitchPolarity();
109 }
110
111 public void HandleHorizontalMovement(float value)
112 {
113 if (value >= -0.5 && value <= 0.5)
114 {
115 value = 0;
116 }
117
118 Velocity.X = value * MaxVelocity;
119 }
120
121 public void HandleVerticalMovement(float value)
122 {
123 if (value >= -0.5 && value <= 0.5)
124 {
125 value = 0;
126 }
127
128 Velocity.Y = value * MaxVelocity;
129 }
130
131 public override void SwitchPolarity()
132 {
133 base.SwitchPolarity();
134 SwitchParticleEngine(CurrentPolarity);
135 PolarityChange.Play();
136 game.Player.ResetMultiplier();
137 }
138
139 public override void SetPolarity(Polarity newPolarity)
140 {
141 base.SetPolarity(newPolarity);
142 SwitchParticleEngine(newPolarity);
143 }
144
145 protected void SwitchParticleEngine(Polarity polarity)
146 {
147 if (polarity == Polarity.Positive)
148 {
149 particleEngine.Color = MainShip.RedColor;
150 }
151 else if (polarity == Polarity.Negative)
152 {
153 particleEngine.Color = MainShip.BlueColor;
154 }
155 else
156 {
157 particleEngine.Color = Color.Gray;
158 }
159 }
160
161 public override void Update(GameTime gameTime)
162 {
163 base.Update(gameTime);
164 particleEngine.EmitterLocation = Position;
165 particleEngine.Update();
166 ConstrainToEdges();
167 UpdateImmortality(gameTime);
168 }
169
170 public void UpdateImmortality(GameTime gameTime)
171 {
172 if (Immortal)
173 {
174 CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;
175
176 if (Flashing)
177 {
178 Color = new Color(255, 255, 255, 128);
179 }
180 else
181 {
182 Color = Color.White;
183 }
184
185 Flashing = !Flashing;
186
187 if (CurrentImmortalTime > MaxImmortalTime)
188 {
189 Immortal = false;
190 Color = Color.White;
191 }
192 }
193 }
194
195 public override void Move(GameTime gameTime)
196 {
197 var VelocityLimit = MaxVelocity;
198 var SavedVelocity = new Vector2(Velocity.X, Velocity.Y);
199
200 if (Shooting)
201 {
202 VelocityLimit = ActVelocity;
203 }
204
205 if (SavedVelocity.X > VelocityLimit)
206 {
207 SavedVelocity.X = VelocityLimit;
208 }
209
210 if (SavedVelocity.X < -VelocityLimit)
211 {
212 SavedVelocity.X = -VelocityLimit;
213 }
214
215 if (SavedVelocity.Y > VelocityLimit)
216 {
217 SavedVelocity.Y = VelocityLimit;
218 }
219
220 if (SavedVelocity.Y < -VelocityLimit)
221 {
222 SavedVelocity.Y = -VelocityLimit;
223 }
224
225 Position.X = Position.X + SavedVelocity.X;
226 Position.Y = Position.Y + SavedVelocity.Y;
227 }
228
229 public override void Magnetize(Ship ship, float distance, float angle)
230 {
231 }
232
233 protected void ConstrainToEdges()
234 {
235 if (Position.X < 0)
236 {
237 Position.X = 0;
238
239 if (Velocity.X < 0)
240 {
241 Velocity.X = 0;
242 }
243 }
244 if (Position.X > game.GraphicsDevice.Viewport.Width)
245 {
246 Position.X = game.GraphicsDevice.Viewport.Width;
247
248 if (Velocity.X > 0)
249 {
250 Velocity.X = 0;
251 }
252 }
253 if (Position.Y < 0)
254 {
255 Position.Y = 0;
256
257 if (Velocity.Y < 0)
258 {
259 Velocity.Y = 0;
260 }
261 }
262 if (Position.Y > game.GraphicsDevice.Viewport.Height)
263 {
264 Position.Y = game.GraphicsDevice.Viewport.Height;
265
266 if (Velocity.Y < 0)
267 {
268 Velocity.Y = 0;
269 }
270 }
271 }
272
273 public override void Draw(SpriteBatch spriteBatch)
274 {
275 particleEngine.Draw(spriteBatch);
276 base.Draw(spriteBatch);
277 }
278
279 public override void Collide(Actor other, Rectangle collision)
280 {
281 if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
282 !Immortal)
283 {
284 Die();
285 }
286 }
287
288 protected override void Die()
289 {
290 game.Player.Lives = game.Player.Lives - 1;
291 game.Player.ResetMultiplier();
292 if (game.Player.Lives < 0)
293 {
294 Dying = true;
295 game.GameOver();
296 }
297 else {
298 Hit.Play();
299 Immortal = true;
300 CurrentImmortalTime = 0;
301 }
302 }
303
304 public override void CleanUp()
305 {
306 base.CleanUp();
307 particleEngine = null;
308 InputController.Unbind("moveX", HandleHorizontalMovement);
309 InputController.Unbind("moveY", HandleVerticalMovement);
310 InputController.Unbind("changePolarity", HandleChangePolarity);
311 InputController.Unbind("shoot", HandleShot);
312 }
313 }
314 }