2 using System.Collections.Generic;
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;
11 namespace SuperPolarity
16 public static Color BlueColor;
17 public static Color RedColor;
19 ParticleEngine particleEngine;
21 protected bool Shooting;
22 protected int ShotCooldown;
24 protected float CurrentImmortalTime;
25 protected float MaxImmortalTime;
26 protected bool Flashing;
28 protected SoundEffect PolarityChange;
29 protected SoundEffect ShootSound;
30 protected SoundEffect Hit;
32 public MainShip(SuperPolarity newGame) : base(newGame) {}
36 particleEngine = null;
39 public override void Initialize(Texture2D texture, Vector2 position)
41 base.Initialize(texture, position);
43 MainShip.BlueColor = new Color(0, 184, 229);
44 MainShip.RedColor = new Color(201, 0, 68);
46 PolarityChange = game.Content.Load<SoundEffect>("Sound\\polaritychange");
47 ShootSound = game.Content.Load<SoundEffect>("Sound\\bullet");
48 Hit = game.Content.Load<SoundEffect>("Sound\\hit");
51 SetPolarity(Polarity.Positive);
56 MaxImmortalTime = 1500;
67 void InitParticleEngine()
69 particleEngine = ParticleEffectFactory.CreatePolarCircle(Position);
74 InputController.Bind("moveX", HandleHorizontalMovement);
75 InputController.Bind("moveY", HandleVerticalMovement);
76 InputController.Bind("changePolarity", HandleChangePolarity);
77 InputController.Bind("shoot", HandleShot);
80 protected void HandleShot(float value)
84 Timer t = new Timer(new TimerCallback(UnlockShot));
85 t.Change(ShotCooldown, Timeout.Infinite);
87 if (Children.Count > 10)
92 var bullet = ActorFactory.CreateBullet(Position, Angle);
100 protected void UnlockShot(object state)
102 InputController.Unlock("shoot");
106 protected void HandleChangePolarity(float value)
111 public void HandleHorizontalMovement(float value)
113 if (value >= -0.5 && value <= 0.5)
118 Velocity.X = value * MaxVelocity;
121 public void HandleVerticalMovement(float value)
123 if (value >= -0.5 && value <= 0.5)
128 Velocity.Y = value * MaxVelocity;
131 public override void SwitchPolarity()
133 base.SwitchPolarity();
134 SwitchParticleEngine(CurrentPolarity);
135 PolarityChange.Play();
136 game.Player.ResetMultiplier();
139 public override void SetPolarity(Polarity newPolarity)
141 base.SetPolarity(newPolarity);
142 SwitchParticleEngine(newPolarity);
145 protected void SwitchParticleEngine(Polarity polarity)
147 if (polarity == Polarity.Positive)
149 particleEngine.Color = MainShip.RedColor;
151 else if (polarity == Polarity.Negative)
153 particleEngine.Color = MainShip.BlueColor;
157 particleEngine.Color = Color.Gray;
161 public override void Update(GameTime gameTime)
163 base.Update(gameTime);
164 particleEngine.EmitterLocation = Position;
165 particleEngine.Update();
167 UpdateImmortality(gameTime);
170 public void UpdateImmortality(GameTime gameTime)
174 CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;
178 Color = new Color(255, 255, 255, 128);
185 Flashing = !Flashing;
187 if (CurrentImmortalTime > MaxImmortalTime)
195 public override void Move(GameTime gameTime)
197 var VelocityLimit = MaxVelocity;
198 var SavedVelocity = new Vector2(Velocity.X, Velocity.Y);
202 VelocityLimit = ActVelocity;
205 if (SavedVelocity.X > VelocityLimit)
207 SavedVelocity.X = VelocityLimit;
210 if (SavedVelocity.X < -VelocityLimit)
212 SavedVelocity.X = -VelocityLimit;
215 if (SavedVelocity.Y > VelocityLimit)
217 SavedVelocity.Y = VelocityLimit;
220 if (SavedVelocity.Y < -VelocityLimit)
222 SavedVelocity.Y = -VelocityLimit;
225 Position.X = Position.X + SavedVelocity.X;
226 Position.Y = Position.Y + SavedVelocity.Y;
229 public override void Magnetize(Ship ship, float distance, float angle)
233 protected void ConstrainToEdges()
244 if (Position.X > game.GraphicsDevice.Viewport.Width)
246 Position.X = game.GraphicsDevice.Viewport.Width;
262 if (Position.Y > game.GraphicsDevice.Viewport.Height)
264 Position.Y = game.GraphicsDevice.Viewport.Height;
273 public override void Draw(SpriteBatch spriteBatch)
275 particleEngine.Draw(spriteBatch);
276 base.Draw(spriteBatch);
279 public override void Collide(Actor other, Rectangle collision)
281 if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
288 protected override void Die()
290 game.Player.Lives = game.Player.Lives - 1;
291 game.Player.ResetMultiplier();
292 if (game.Player.Lives < 0)
300 CurrentImmortalTime = 0;
304 public override void CleanUp()
307 particleEngine = null;
308 InputController.Unbind("moveX", HandleHorizontalMovement);
309 InputController.Unbind("moveY", HandleVerticalMovement);
310 InputController.Unbind("changePolarity", HandleChangePolarity);
311 InputController.Unbind("shoot", HandleShot);