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 protected bool Shooting;
20 protected int ShotCooldown;
22 protected float CurrentImmortalTime;
23 protected float MaxImmortalTime;
24 protected bool Flashing;
26 protected SoundEffect PolarityChange;
27 protected SoundEffect ShootSound;
28 protected SoundEffect Hit;
31 protected Texture2D AuraPoint;
32 protected double AuraRadius;
33 protected double AuraAmplitude;
34 protected double AuraFrequency;
35 protected double AuraSubAmplitude;
36 protected double AuraWaveSize;
37 protected double AuraSubPhase;
38 protected double AuraSubFrequency;
39 public double[] EnemyModifications;
40 public double MaxEnemyModification;
41 public double EnemyModificationSpread;
43 public MainShip(SuperPolarity newGame) : base(newGame) {}
49 public override void Initialize(Texture2D texture, Vector2 position)
51 base.Initialize(texture, position);
53 MainShip.BlueColor = new Color(71, 182, 226);
54 MainShip.RedColor = new Color(235, 160, 185);
56 AuraPoint = CreateCircle (2);
64 AuraSubFrequency = 9 * Math.PI / 180;
65 MaxEnemyModification = 20;
66 EnemyModificationSpread = 10;
67 EnemyModifications = new double[360];
68 for ( int i = 0; i < EnemyModifications.Length; i++ ) {
69 EnemyModifications[i] = 0;
72 PolarityChange = game.Content.Load<SoundEffect>("Sound\\polaritychange");
73 ShootSound = game.Content.Load<SoundEffect>("Sound\\bullet");
74 Hit = game.Content.Load<SoundEffect>("Sound\\hit");
76 SetPolarity(Polarity.Positive);
81 MaxImmortalTime = 1500;
94 InputController.Bind("moveX", HandleHorizontalMovement);
95 InputController.Bind("moveY", HandleVerticalMovement);
96 InputController.Bind("changePolarity", HandleChangePolarity);
97 InputController.Bind("shoot", HandleShot);
100 protected void HandleShot(float value)
104 Timer t = new Timer(new TimerCallback(UnlockShot));
105 t.Change(ShotCooldown, Timeout.Infinite);
107 if (Children.Count > 10)
112 var bullet = ActorFactory.CreateBullet(Position, Angle);
114 Children.Add(bullet);
115 bullet.Parent = this;
120 protected void UnlockShot(object state)
122 InputController.Unlock("shoot");
126 protected void HandleChangePolarity(float value)
131 public void HandleHorizontalMovement(float value)
133 if (value >= -0.5 && value <= 0.5)
138 Velocity.X = value * MaxVelocity;
141 public void HandleVerticalMovement(float value)
143 if (value >= -0.5 && value <= 0.5)
148 Velocity.Y = value * MaxVelocity;
151 public override void SwitchPolarity()
153 base.SwitchPolarity();
154 PolarityChange.Play();
155 game.Player.ResetMultiplier();
158 public override void SetPolarity(Polarity newPolarity)
160 base.SetPolarity(newPolarity);;
163 public override void Update(GameTime gameTime)
165 base.Update(gameTime);
167 UpdateImmortality(gameTime);
170 public void ResetEnemyModification() {
171 for ( int i = 0; i < EnemyModifications.Length; i++ ) {
172 EnemyModifications[i] = 0;
176 public void UpdateImmortality(GameTime gameTime)
180 CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;
184 Color = new Color(255, 255, 255, 128);
191 Flashing = !Flashing;
193 if (CurrentImmortalTime > MaxImmortalTime)
201 public override void Move(GameTime gameTime)
203 var VelocityLimit = MaxVelocity;
204 var SavedVelocity = new Vector2(Velocity.X, Velocity.Y);
208 VelocityLimit = ActVelocity;
211 if (SavedVelocity.X > VelocityLimit)
213 SavedVelocity.X = VelocityLimit;
216 if (SavedVelocity.X < -VelocityLimit)
218 SavedVelocity.X = -VelocityLimit;
221 if (SavedVelocity.Y > VelocityLimit)
223 SavedVelocity.Y = VelocityLimit;
226 if (SavedVelocity.Y < -VelocityLimit)
228 SavedVelocity.Y = -VelocityLimit;
231 Position.X = Position.X + SavedVelocity.X;
232 Position.Y = Position.Y + SavedVelocity.Y;
235 public override void Magnetize(Ship ship, float distance, float angle)
239 protected void ConstrainToEdges()
250 if (Position.X > game.GraphicsDevice.Viewport.Width)
252 Position.X = game.GraphicsDevice.Viewport.Width;
268 if (Position.Y > game.GraphicsDevice.Viewport.Height)
270 Position.Y = game.GraphicsDevice.Viewport.Height;
279 public void UpdateEnemyModification(Ship enemy) {
281 var dx = enemy.Position.X - Position.X;
282 var dy = enemy.Position.Y - Position.Y;
283 var angleInDegrees = ((360 + Math.Round (Math.Atan2 (dy, dx) * 180 / Math.PI)) % 360);
285 for (var i = -EnemyModificationSpread; i < EnemyModificationSpread; i++) {
286 var strength = MaxEnemyModification - Math.Abs (i * MaxEnemyModification / EnemyModificationSpread);
288 var index = (int)((360 + angleInDegrees + i) % 360);
290 if (enemy.CurrentPolarity == CurrentPolarity) {
291 EnemyModifications[index] -= strength;
293 EnemyModifications[index] += strength;
296 if (EnemyModifications[index] > MaxEnemyModification) {
297 EnemyModifications[index] = MaxEnemyModification;
300 if (EnemyModifications[index] < -MaxEnemyModification) {
301 EnemyModifications[index] = -MaxEnemyModification;
306 public override void Draw(SpriteBatch spriteBatch)
308 DrawCircle (spriteBatch);
309 base.Draw(spriteBatch);
310 //spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
313 protected void DrawCircle(SpriteBatch spriteBatch) {
315 var j = AuraWaveSize;
316 var subWaveRadius = 0.0;
317 var randomPosition = 0.0;
319 for (var i = 0; i < 360; i++) {
320 var angle = i * Math.PI / 180;
322 if (j == AuraWaveSize) {
323 subWaveRadius = AuraSubAmplitude * Math.Sin (randomPosition + AuraSubPhase);
324 randomPosition -= (2 * Math.PI / 8);
329 var radius = EnemyModifications[i] + subWaveRadius + AuraRadius + AuraAmplitude * Math.Sin (i / AuraFrequency);
330 var x = Position.X + radius * Math.Cos (angle);
331 var y = Position.Y + radius * Math.Sin (angle);
335 var pointPosition = new Vector2 ((float)x, (float)y);
337 if (CurrentPolarity == Polarity.Positive) {
338 spriteBatch.Draw (AuraPoint, pointPosition, RedColor);
340 spriteBatch.Draw (AuraPoint, pointPosition, BlueColor);
344 AuraSubPhase += AuraSubFrequency;
349 var randomFactor = 5;
350 var randomAreaSize = 4;
352 var j = AuraWaveSize;
354 var randomPosition = 0;
358 for (var i = 0; i < 360; i++) {
359 var rad = Math.radians(i);
360 if (j == AuraWaveSize) {
361 //rand = Math.random() * 2 * AuraSubAmplitude - AuraSubAmplitude;
362 rand = AuraSubAmplitude * Math.sin(AuraSubPhase + offset);
363 AuraSubPhase -= (2 * Math.PI / 8);
367 var radius = polarityModifications[i] + rand + AuraRadius + AuraAmplitude * Math.sin(i / AuraFrequency);
368 var x = Position.x + radius * Math.cos(rad);
369 var y = Position.y + radius * Math.sin(rad);
374 //console.log(i, rad, x, y, dataStart);
377 ctx.putImageData(imgData, 0, 0);*/
380 protected Texture2D CreateCircle(int radius)
382 int outerRadius = radius * 2 + 2;
383 Texture2D texture = new Texture2D (game.GraphicsDevice, outerRadius, outerRadius);
385 Color[] data = new Color[outerRadius * outerRadius];
387 for (int i = 0; i < data.Length; i++) {
388 data [i] = Color.Transparent;
391 double angleStep = 1f / radius;
393 for (double angle = 0; angle < Math.PI * 2; angle += angleStep)
395 int x = (int)Math.Round (radius + radius * Math.Cos (angle));
396 int y = (int)Math.Round (radius + radius + Math.Sin (angle));
398 data [y * outerRadius + x + 1] = Color.White;
401 texture.SetData (data);
405 public override void Collide(Actor other, Rectangle collision)
407 if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
414 protected override void Die()
416 game.Player.Lives = game.Player.Lives - 1;
417 game.Player.ResetMultiplier();
418 if (game.Player.Lives < 0)
426 CurrentImmortalTime = 0;
430 public override void CleanUp()
433 InputController.Unbind("moveX", HandleHorizontalMovement);
434 InputController.Unbind("moveY", HandleVerticalMovement);
435 InputController.Unbind("changePolarity", HandleChangePolarity);
436 InputController.Unbind("shoot", HandleShot);